Week 3 Linked Lists Advanced Topics
Week 3 Linked Lists Advanced Topics
2
Linked Lists Vs Arrays
3
Linked Lists Vs Arrays
4
Linked List
• Linked List is a linear data structure
• Elements are not stored at a contiguous location, rather they are linked
using pointers.
• Linked List forms a series of connected nodes, where each node stores
the data and the address of the next node.
5
Linked List
Node Structure: A node in a linked list typically consists of two
components:
• Data: It holds the actual value or data associated with the node.
• Next Pointer: It stores the memory address (reference) of the next node in the
sequence.
1.Single-linked list
2.Double linked list
3.Circular linked list
8
Singly linked list
• In a singly linked list, each node contains a reference to the next node
in the sequence. Traversing a singly linked list is done in a forward
direction
9
Doubly linked list
• In a doubly linked list, each node contains references to both the next
and previous nodes.
• This allows for traversal in both forward and backward directions, but
it requires additional memory for the backward reference
10
Circular linked list
• In a circular linked list, the last node points back to the head node,
creating a circular structure. It can be either singly or doubly linked.
11
Complexity Analysis of Linked List
12
Operations on Linked Lists
Insertion: Adding a new node to a linked list involves adjusting the
pointers of the existing nodes to maintain the proper sequence. Insertion
can be performed at the beginning, end, or any position within the list
Deletion: Removing a node from a linked list requires adjusting the
pointers of the neighboring nodes to bridge the gap left by the deleted
node. Deletion can be performed at the beginning, end, or any position
within the list.
Searching: Searching for a specific value in a linked list involves
traversing the list from the head node until the value is found or the end
of the list is reached.
13
Inserting nodes in the list
14
Inserting in a linked list:
• Consider the following Linked List,
17
Case 2: Insert in between:
• Assuming index starts from 0, we can insert an element at index i>0 as follows:
1.Bring a temporary pointer p pointing to the node before the element you want to insert
in the linked list.
2.Since we want to insert between 8 and 2, we bring pointer p to 8.
18
Case 3: Insert at the end:
• In order to insert an element at the end of the linked list, we bring a
temporary pointer to the last element
19
Case 4: Insert after a node:
• Similar to the other cases, ptr can be inserted after a node as follows:
• ptr->next = prevNode-> next;
• prevNode-> next = ptr;
20
//1. structure
struct Node{
int data;
Exampl
struct Node * next;
}; e
//7. transversing
//4. linking data 1st and second node
void linkedlistTransversal(struct Node* ptr) head->data = 7;
{ head->next = second;
while (ptr !=NULL){
printf("Element: %d\n ", ptr->data); //5. linking data 2nd and 3rd node
ptr = ptr->next; second->data = 11;
} second->next = third;
}
//6. Terminate the list at the third node
int main(){ third->data = 50;
//2. creating nodes third->next = NULL;
struct Node * head;
struct Node * second;
struct Node * third; linkedlistTransversal(head); //8. function call
// 3. LL in heap not in stack
head = (struct Node *) malloc(sizeof(struct Node)); return 0;
second = (struct Node *) malloc(sizeof(struct Node));
third = (struct Node *) malloc(sizeof(struct Node)); } 21
Inserting nodes at the start of a list
Pseudocode
Vertex vtx = new Vertex(v)
vtx.next = head
head = vtx
22
Inserting nodes at an index/value in a list
Pseudocode
Vertex pre = head
for (k = 0; k < i-1; k++)
pre = pre.next
Vertex aft = pre.next
Vertex vtx = new Vertex(v)
vtx.next = aft
pre.next = vtx
23
Inserting nodes at the end of a list
Pseudocode
Vertex vtx = new Vertex(v)
tail.next = vtx
tail = vtx
24
Deleting nodes from the list
25
Deleting the first node of a list
Pseudocode
if empty, do nothing
temp = head
head = head.next
delete temp
26
Deleting the last node of a list
Pseudocode
if empty, do nothing
Vertex pre = head
temp = head.next
while (temp.next != null)
pre = pre.next
temp = temp.next
pre.next = null
delete temp, tail = pre
27
Deleting node with index/value in the list
Pseudocode
if empty, do nothing
Vertex pre = head
for (k = 0; k < i-1; k++)
pre = pre.next
Vertex del = pre.next, aft = del.next
pre.next = aft // bypass del
delete del
28
Searching a linked list for a record
Pseudocode
if empty, return NOT_FOUND
index = 0, temp = head
while (temp.item != v)
index++, temp = temp.next
if temp == null
return NOT_FOUND
return index
29
Redefining our node structure
struct employee
{ Some advantages of this approach:
char name[50]; • Increased scalability
int age;
float bs; • Code is better organized
}; • Code is easier to understand and
debug
struct node
• Easy to port this to some other
{
struct employee data;
project
struct node * next;
};
30
31