Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

CSE331 - Data Structures and Algorithms

The one with the l→i→n→k→e→d→” “→l→i→s→t→⏚


Section 3

● Linked list (Singly linked list)


- List elements are scattered in memory
- Each node holds the data value and address to the next node
- The linked list class contains a pointer to the head node
Section 3

● Node class
Section 3

● Linked list class


Section 3

● Linked list class


- Constructor:
Section 3

● Linked list class


- empty:
Section 3

● Linked list class


- length:
Section 3

● Linked list class


- Push front:
Section 3

● Linked list class


- Pop front:
Section 3

● Linked list class


- Push back:
Section 3

● Linked list class


- Pop back:
Section 3

● Linked list class


- print:
Section 3

● Linked list class


- Print reverse:
Section 3

● Linked list class


- contains:
Section 3

● Linked list class


- clear:
Section 3

● Linked list class


- Destructor:
Section 3

● Practice Problems

Delete node https://leetcode.com/problems/delete-node-in-a-linked-list/

Remove nth from the end https://leetcode.com/problems/remove-nth-node-from-end-of-list/

Reverse Linked list https://leetcode.com/problems/reverse-linked-list/

Cycle in linked list https://leetcode.com/problems/linked-list-cycle/

Intersection of 2 LL https://leetcode.com/problems/intersection-of-two-linked-lists/

Middle of a linked list https://leetcode.com/problems/middle-of-the-linked-list


Section 3

● Sheet 3
● Modify the selection sort to be used for a linked list.
Section 3

● Sheet 3
● Modify the selection sort
to be used for a linked list.
Section 3

● Sheet 3
● Bubble sort is based on swapping the consecutive arrays contents when they are
not in order, is this sort method suitable for a linked list version than selection sort?
Discuss
Section 3

● Sheet 3
● Bubble sort is based on swapping the
consecutive arrays contents when
they are not in order, is this sort
method suitable for a linked list
version than selection sort? Discuss
Section 3

● Sheet 3
● Bubble sort is based on swapping the
consecutive arrays contents when
they are not in order, is this sort
method suitable for a linked list
version than selection sort? Discuss
Section 3

● Sheet 3
● A linked list is used to hold coefficients of a polynomial; the most significant
coefficient is in front, The linked list P(x) = 4*x^3 + 2*x^2 -1 will look like,
P(5) = 4*125 + 2*25 -1 = 500+50-1=549
Create a C++ class to implement such an application and in your class show how to
perform the following tasks in OO fashion as much as possible:
a. Construct a polynomial linked list from an array values
b. Multiply two polynomials together and the result will be a new polynomial
c. Evaluate a polynomial with a value x in the best big O
d. Differentiate the polynomial with respect to x i.e. dP(x)/dx
Section 3

● Sheet 3
● You are given a large number of coins 1024, one of them is faked and lighter than
the rest, the shape of the coin is identical to the rest of the coins. Given a scale, plan
how to discover the faked coin in the minimum number of scale uses.
Write a function in c/java to simulate the process the function takes a parameter n
which is the size of the coins under current test.
Section 3

● Linked list (doubly linked list)


- List elements are scattered in memory
- Each node holds the data value and address to the next node and address to the
previous node
- The linked list class contains a pointer to the head node

You might also like