0% found this document useful (0 votes)
9 views24 pages

Linked List

Data Structures

Uploaded by

devhamnah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views24 pages

Linked List

Data Structures

Uploaded by

devhamnah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 24

Lecture 3

Linked List

1
Linked List
 A linked list is a linear data structure, in which
the elements are not stored at continuous
memory locations. The elements in a linked list
are linked using pointers as shown in the below
image:

 In simple words, a linked list consists of nodes


where each node contains a data field and a
reference(link) to the next node in the list
2
List Using Linked Memory
 Various cells of memory are not allocated
consecutively in memory.

3
List Using Linked Memory
 Various cells of memory are not allocated
consecutively in memory.
 Not only to store the elements of the list.

4
List Using Linked Memory
 Various cells of memory are not allocated
consecutively in memory.
 Not enough to store the elements of the list.
 With arrays, the second element was right next
to the first element.

5
List Using Linked Memory
 Various cells of memory are not allocated
consecutively in memory.
 Not enough to store the elements of the list.
 With arrays, the second element was right next
to the first element.
 Now the first element must explicitly tell us
where to look for the second element.

6
List Using Linked Memory
 Various cells of memory are not allocated
consecutively in memory.
 Not enough to store the elements of the list.
 With arrays, the second element was right next
to the first element.
 Now the first element must explicitly tell us
where to look for the second element.
 Do this by holding the memory address of the
second element

7
Linked List
 Create a structure called a Node.

object next

 The object field will hold the actual list element.


 The next field in the structure will hold the
starting location of the next node.
 Chain the nodes together to form a linked list.

8
Linked List
 Picture of our list (2, 6, 7, 8, 1) stored as a
linked list:

head

2 6 8 7 1 size=5

current

9
Linked List
Note some features of the list:
 Need a head to point to the first node of the list.
Otherwise we won’t know where the start of the
list is.

10
Linked List
Note some features of the list:
 Need a head to point to the first node of the list.
Otherwise we won’t know where the start of the
list is.
 The current here is a pointer, not an index.

11
Linked List
Note some features of the list:
 Need a head to point to the first node of the list.
Otherwise we won’t know where the start of the
list is.
 The current here is a pointer, not an index.
 The next field in the last node points to nothing.
We will place the memory address NULL which
is guaranteed to be inaccessible.

12
Linked List
 Actual picture in memory:
1051 6
1052 1063
current 1053 1063
1054 2
head 1055 1051
1056
2 6 8 7 1 1057 7
1058 1060
current 1059
1060 1
1061 0
head 1062 1054
1063 8
1064 1057
1065
13
Linked List Operations
 add(9): Create a new node in memory to hold ‘9’
Node* newNode = new Node(9); newNode 9

14
Linked List Operations
 add(9): Create a new node in memory to hold ‘9’
Node* newNode = new Node(9); newNode 9

 Link the new node into the list

head

2 6 8 7 1 size=5 6

current 2 1
3
9

newNode

15
C++ Code for Linked List

16
Building a Linked List
List list; headNode size=0

17
Building a Linked List
List list; headNode size=0

currentNode

list.add(2); headNode 2 size=1

lastcurrentNode

18
Building a Linked List
List list; headNode size=0

currentNode

list.add(2); headNode 2 size=1

lastcurrentNode

currentNode

list.add(6); headNode 2 6 size=2

lastcurrentNode

19
Building a Linked List

list.add(8); list.add(7); list.add(1);

currentNode

headNode 2 6 8 7 1 size=5

lastcurrentNode

20
C++ Code for Linked List

int get() {
if (currentNode != NULL)
return currentNode->get();
};

21
Analysis of Linked List
 add
• we simply insert the new node after the current
node. So add is a one-step operation.

22
Analysis of Linked List
 add
• we simply insert the new node after the current
node. So add is a one-step operation.
 remove
 remove is n step operation, as we need to traverse
the list from the start to keep the track of Last
current node.
 Or save the pointer of current node and last current
node then it would be one step operation.

23
Analysis of Linked List
 add
• we simply insert the new node after the current node.
So add is a one-step operation.
 remove
 remove is n step operation, as we need to traverse the
list from the start to keep the track of Last current
node.
 Or save the pointer of current node and last current
node then it would be one step operation.
 find
 worst-case: may have to search the entire list

24

You might also like