You are on page 1of 38

Linked Lists in Data Structures

Sidra Malik
sidra.malik@ciitlahore.edu.pk
Linked Lists
• A linked list is a linear collection of homogenous data
elements, called nodes, where the linear order is
given by means of pointers.

• Each node is divided into two parts:


The first part contains the data of the element and
The second part contains the address of the next node
(link /next pointer field) in the list

Data Link Link to the next node

Data Structrues and Algorithms-Sidra Malik 2


Linked Lists…
Types
• Depends on how pointers are maintained
• Three major groups
– Single Linked List
– Circular Linked List
– Double Linked List

• Single Linked List


– Each node contains only one link
– points to subsequent node in the list

Data Structrues and Algorithms-Sidra Malik 3


Linked Lists…
Single Linked Lists
Link

59 Link 38 Link 64 Link

80 X

14 Link 72 Link

A Single Linked List with 6 Nodes

Data Structrues and Algorithms-Sidra Malik 4


Linked Lists…
Representation in Memory
• Memory Bank
– Collection of free memory spaces
• Memory Manager
– A program which allocates memory to nodes
• Garbage Collector
– Returns the unused space to memory again
• Execution:
– A request is made to memory manager
– Memory manager checks for requested block through Memory Bank
– If available then grants requested block to caller
– Garbage collector called whenever a node is given to memory bank
again
– Memory is Dynamically Represented

Data Structrues and Algorithms-Sidra Malik 5


Linked Lists…
Algorithm for Traversing
Visit every node from start to the end
Input: pointer to Header Node
Output: According to Process
Data Structure: Link List
Steps:
1. ptr=header.link
2. while(ptr!=NULL)
1. process()
2. ptr=ptr.link
3. Endwhile
4. Stop

Data Structrues and Algorithms-Sidra Malik 6


Linked Lists…
Implementation for Traversing
node *temp; //create a temporary
node temp = (node*)malloc(sizeof(node)); //allocate space for node

Creating a Temporary Node


Create node *temp1
Transfer the address of *head to *temp1
*temp1 is also pointed at the front of the linked list; Linked list has 3 nodes.
Traversing and Printing Data
while( temp1!=NULL )
{ cout<< temp1->data<<" "; // show the data in the linked list
temp1 = temp1->next; // transfer the address of 'temp->next' to 'temp' }

temp

head Link 59 Link 80 X

Data Structrues and Algorithms-Sidra Malik 7


Linked Lists…
Implementation for Traversing
node *temp; //create a temporary
node temp = (node*)malloc(sizeof(node)); //allocate space for node

Creating a Temporary Node


temp->data = info; // store data(first field)
temp->next=head; // store the address of the pointer head(second field)
head = temp; // transfer the address of 'temp' to 'head'
Traversing and Printing Data
while( temp1!=NULL )
{ cout<< temp1->data<<" "; // show the data in the linked list
temp1 = temp1->next; // transfer the address of 'temp->next' to 'temp' }

temp

head Link 59 Link 80 X

Data Structrues and Algorithms-Sidra Malik 8


Insertion at the Front
Linked Lists…
Algorithm for Insertion
Insert at Front

Step 1: Update the next link of a new node, to point to the current head node

Data Structrues and Algorithms-Sidra Malik 10


Linked Lists…
Algorithm for Insertion
Insert at Front
Step 2: Update head link to point to the new node.

Data Structrues and Algorithms-Sidra Malik 11


Linked Lists…
Implementation for Inserting at front
node *head = NULL; //empty linked list
node *temp; //create a temporary
node temp = (node*)malloc(sizeof(node)); //allocate space for node
Creating a Temporary Node
Then place info to temp->data.
first field of the node *temp is filled.
temp->next must copy the address of the *head

Insertion at front
temp->data = info; // store data(first field)
temp->next=head; // store the address of the pointer head(second field)
head = temp; // transfer the address of 'temp' to 'head'

Data Structrues and Algorithms-Sidra Malik 12


Insertion at the End
Linked Lists…
Algorithm for Insertion at the End
Insert at End

Step 1: Update the next link of the current tail node, to point to the new node.

Data Structrues and Algorithms-Sidra Malik 14


Linked Lists…
Algorithm for Insertion at the End
Insert at End
Step 2: Update tail link to point to the new node.

Data Structrues and Algorithms-Sidra Malik 15


Linked Lists…
Implementation for Inserting at End
node *temp1; // create a temporary node
temp1=(node*)malloc(sizeof(node)); // allocate space for node
temp1 = head; // transfer the address of 'head' to 'temp1'
while(temp1->next!=NULL) // go to the last node
temp1 = temp1->next; //tranfer the address of 'temp1->next' to
'temp1‘

Creating a Temporary Node


• Create a temporary node node *temp and allocate space for it.
• info to temp->data
• node *temp will be the last node of the linked list. temp->next will be NULL.
• To create a connection between linked list and the new node, the last node of the
existing linked
• list node *temp1`s second field temp1>next is pointed to node *temp.

Insertion in the End


node *temp; // create a temporary node
temp = (node*)malloc(sizeof(node)); // allocate space for node
temp->data = info; // store data(first field)
temp->next = NULL; // second field will be null(last node) t
Data Structrues and Algorithms-Sidra Malik 16
temp1->next = temp; // 'temp' node will be the last node
Linked Lists…
Implementation for Inserting at End

Data Structrues and Algorithms-Sidra Malik 17


Insertion in the Middle
Linked Lists…
Algorithm for Insertion in Middle
Insert Between Two Nodes

Step 1: Update link of the "previous" node, to point to the new node.

Data Structrues and Algorithms-Sidra Malik 19


Linked Lists…
Algorithm for Insertion in Middle
Insert between two
Step 2: Update tail link to point to the new node.

Data Structrues and Algorithms-Sidra Malik 20


Linked Lists…
Implementation for Inserting in Middle
Input node number from user at which he/she wants to insert a new node
node *temp1; // create a temporary node
temp1=(node*)malloc(sizeof(node)); // allocate space for node
temp1 = head; // transfer the address of 'head' to 'temp1'
Getting to the desired node
for( int i = 1 ; i < node_number ; i++ )
{ temp1 = temp1->next; // go to the next node
if( temp1 == NULL )
{ printf( “ %d node is not exist”,node_number }

Insertion in Middle
node *temp; // create a temporary node
temp = (node*)malloc(sizeof(node)); // allocate space for node
temp->data = info; // store data(first field)

temp->next = temp1->next; //transfer the address of temp1->next to temp->next


temp1->next = temp; //transfer the address of temp to temp1->next

Data Structrues and Algorithms-Sidra Malik 21


Linked Lists…
Insertion in Middle
.

Data Structrues and Algorithms-Sidra Malik 22


Deletion at Front
• we create node *temp.
• Transfer the address of *head to*temp.
• *temp is pointed at the front of the linked list.
• transfer the address of temp->next to head so that it now pointed to the
second node.
• Now free the space allocated for first node.

• node *temp; // create a temporary node


• temp = (node*)malloc(sizeof(node)); // allocate space for node
• temp = head; // transfer the address of 'head' to 'temp'
• head = temp->next; // transfer the address of 'temp->next' to 'head'
• free(temp);

Data Structrues and Algorithms-Sidra Malik 23


Deletion at Front
• node *temp; // create a temporary node
• temp = (node*)malloc(sizeof(node)); // allocate space for node
• temp = head; // transfer the address of 'head' to 'temp'
• head = temp->next; // transfer the address of 'temp->next' to 'head'
• free(temp);

Data Structrues and Algorithms-Sidra Malik 24


Deletion at End
• we create node *temp.
• Transfer the address of *head to*temp.
• *temp is pointed at the front of the linked list.
• transfer the address of temp->next to head so that it now pointed to the
second node.
• Now free the space allocated for first node.

// create a temporary node


node *temp1;
temp1 = (node*)malloc(sizeof(node)); // allocate space for node
temp1 = head; //transfer the address of head to temp1
node *old_temp; // create a temporary node
old_temp = (node*)malloc(sizeof(node)); // allocate space for node
while(temp1->next!=NULL) // go to the last node
{
old_temp = temp1; // transfer the address of 'temp1' to 'old_temp'
temp1 = temp1->next; // transfer the address of 'temp1->next' to temp1'
}
Data Structrues and Algorithms-Sidra Malik 25
Deletion at End
old_temp->next = NULL; // previous node of the last node is null
free(temp1);

Data Structrues and Algorithms-Sidra Malik 26


Deleting Specific Node
• find the specified node and previous node of the specified node
• Create temporary node * temp1, *old_temp and allocate space for it.
• Take the input from user to know the number of the node.

node *temp1; // create a temporary node


temp1 = (node*)malloc(sizeof(node)); // allocate space for node
temp1 = head; // transfer the address of 'head' to 'temp1‘
node *old_temp; // create a temporary node
old_temp = (node*)malloc(sizeof(node)); // allocate space for node
old_temp = temp1; // transfer the address of 'temp1' to 'old_temp’

for( int i = 1 ; i < node_number ; i++ )


{ old_temp = temp1; // store previous node
temp1 = temp1->next; // store current node
}
Data Structrues and Algorithms-Sidra Malik 27
Deleting Specific Node
old_temp->next = temp1->next;;
free(temp1);

Data Structrues and Algorithms-Sidra Malik 28


Double Linked Lists
Double Linked Lists
• Single
– Can move from header to any node in one directio
n only
– One way lists
• Double Linked lists
– Two-way
– traverse in either way… left to right, right to left

Data Structrues and Algorithms-Sidra Malik 30


Double Linked Lists…
Structure

• info: the user's data


• next, back: the address of the next
and previous node in the list

back
.back .info .next
Double Linked Lists
• Insertion at Front

Data Structrues and Algorithms-Sidra Malik 32


Double Linked Lists
• Insertion at End

Data Structrues and Algorithms-Sidra Malik 33


Double Linked Lists
• Insertion at Middle

Data Structrues and Algorithms-Sidra Malik 34


Double Linked Lists
• Insertion at Front, Middle, End ?
Exercise Algorithm

Data Structrues and Algorithms-Sidra Malik 35


Double Linked Lists
• Deletion at Front and End

Data Structrues and Algorithms-Sidra Malik 36


Double Linked Lists
• Deletion at Middle

Data Structrues and Algorithms-Sidra Malik 37


Double Linked Lists
• Deletion at Front, Middle, End ?
Exercise Algorithm

Data Structrues and Algorithms-Sidra Malik 38

You might also like