0% found this document useful (0 votes)
19 views

Week 3 Linked Lists Advanced Topics

Uploaded by

Muneeb Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Week 3 Linked Lists Advanced Topics

Uploaded by

Muneeb Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Operations on Singly Linked Lists

CSC-211 Data Structures and Algorithms


Lecture outline
• Linked Lists Vs Arrays
• Operations supported on Singly Linked Lists
• Inserting nodes in the list
• Deleting nodes from the list
• Searching for a record in the list
• Implementing these operations
• Redefining our node structure

2
Linked Lists Vs Arrays

3
Linked Lists Vs Arrays

Arrays Linked Lists


Linear collection of data Linear collection of data
Data stored in contiguous Data is not stored in contiguous
memory locations locations
Random access of data allowed Sequential access allowed
The size of an arrays is fixed Linked lists have variable sizes

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.

Head and Tail:


• The linked list is accessed through the head node, which points to the first
node in the list.
• The last node in the list points to NULL or nullptr, indicating the end of the
list. This node is known as the tail node.
6
Why linked list data structure needed?
• Dynamic Data structure: The size of memory can be allocated or de-
allocated at run time based on the operation insertion or deletion.
• Ease of Insertion/Deletion: The insertion and deletion of elements are
simpler than arrays since no elements need to be shifted after insertion
and deletion, Just the address needed to be updated.
• Efficient Memory Utilization: As we know Linked List is a dynamic
data structure the size increases or decreases as per the requirement so
this avoids the wastage of memory.
• Implementation: Various advanced data structures can be implemented
using a linked list like a stack, queue, graph, hash maps, etc.
7
Types of linked lists

There are mainly three types of linked lists:

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

• Time Complexity: O(n)

• Auxiliary Space: O(n)

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

Nodes can be inserted in a list in a variety of ways:


• Inserting node at the start of the list
• Inserting node at a certain index in the list
• Inserting node at the end of a list
• Inserting node before/after a certain value in the list

14
Inserting in a linked list:
• Consider the following Linked List,

Insertion in this list can be divided into the following categories:


Case 1: Insert at the beginning
Case 2: Insert in between
Case 3: Insert at the end
Case 4: Insert after the node
15
Inserting in a linked list:
• For insertion following any of the mentioned cases,
• we would first need to create that extra node.
• And then, we overwrite the current connection and make new
connections.
• And that is how we insert a new node at our desired place.
• Syntax for creating a node:

struct Node *ptr = (struct Node*) malloc (sizeof (struct Node))


16
Case 1: Insert at the beginning
• In order to insert the new node at the beginning, we would need to have
the head pointer pointing to this new node and the new node’s pointer to
the current head.

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

Like insertion, nodes can be deleted from a list in a variety of ways:


• Deleting the first node of a list
• Deleting the last node from a list
• Deleting node at a certain index in the list
• Deleting a node with a certain value in 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

You might also like