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

DSA Module 3

DSA

Uploaded by

MAAZ KHAN
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

DSA Module 3

DSA

Uploaded by

MAAZ KHAN
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

~ASIF KHAN ~MAAZ KHAN

23EC29 23EC28
Data Structure & Algorithm
Module 3: LinkedList

➢ Linkedlist
• Linked List is Series of Nodes.
• Each node Consist of two Parts viz Data Part & Pointer Part.
• Pointer Part stores the address of the next node.

• It is a data Structure which consists of group of nodes that forms a sequence.


• Linked list comprise of group or list of nodes in which each node have link to
next node to form a chain.

➢ What is linked list? State its advantage, Explain its types [3M DEC-18]
✓ Advantages of linked list
• Linked List is Dynamic data Structure.
• Linked List can grow and shrink during run time.
• Insertion and Deletion Operations are Easier.
• Efficient Memory Utilization, i.e no need to pre-allocate memory Faster Access
time, can be expanded in constant time without memory overhead.
• Linear Data Structures such as Stack, Queue can be easily implemeted using
Linked list

✓ Disadvantages of Linked List


1. Wastage of Memory
• Pointer Requires extra memory for storage.
2. No Random Access
• In array we can access nth element easily just by using a[n].
• In Linked list no random access is given to user, we have to access each
node sequentially.
3. Time Complexity
• Array can be randomly accessed, while the Linked accessed Randomly.
4. Reverse Traversing is difficult
• In case if we are using singly linked list very difficult to traverse linked list
from end.
5. Heap Space Restriction
• If there is insufficient space heap then it won't create any memory.

Linked list as an ADT


Structure in "c" can be used define a node.
Address of the successor node can be stored in pointer variable.

Linked list is a self referential structure in "c", each node has variable to store data
and next field store the address of next field.

typedef struct node


{
int data;
struct node *next;
}node;

➢ Types of linkedlist
1. Singly Linked List
• In this type of Linked List two successive nodes are linked together in linear
fashion
• Each Node contain address of the next node to be followed.
• In Singly Linked List only Linear or Forward Sequential movement is
possible.
2. Doubly linked list
• In this type of linked list each node holds two pointer field.
• In doubly linked list address of next as well as preceding element are linked
with current node.

3. Circular linked list


• In a circular list the first and last element are adjacent.
• A linked list can be made circular by storing the address of first node in next
field of last node.
What is Single Linked List?
Simply a list is a sequence of data, and the linked list is a sequence of data linked
with each other.
The formal definition of a single linked list is as follows...
Single linked list is a sequence of elements in which every element has link to its
next element in the sequence.
In any single linked list, the individual element is called as "Node". Every "Node"
contains two fields, data field, and the next field. The data field is used to store actual
value of the node and next field is used to store the address of next node in the
sequence.
The graphical representation of a node in a single linked list is as follows...
Insertion
In a single linked list, the insertion operation can be performed in three ways. They are
as follows...
1. Inserting At Beginning of the list
2. Inserting At End of the list
3. Inserting At Specific location in the list

Inserting At Beginning of the list


We can use the following steps to insert a new node at beginning of the single linked
list...
Step 1 - Create a newNode with given value.
Step 2 - Check whether list is Empty (head == NULL)
Step 3 - If it is Empty then, set newNode->next = NULL and head = newNode. Step 4 - If
it is Not Empty then, set newNode->next = head and head = newNode.

Inserting At End of the list


We can use the following steps to insert a new node at end of the single linked list...
Step 1 - Create a newNode with given value and newNode → next as NULL.
Step 2-Check whether list is Empty (head == NULL).
Step 3 - If it is Empty then, set head = newNode.
Step 4-If it is Not Empty then, define a node pointer temp and initialize with head.
Step 5 - Keep moving the temp to its next node until it reaches to the last node in the
list (until temp→ next is equal to NULL).
Step 6-Set temp→ next = newNode.

Inserting At Specific location in the list (After a Node)


We can use the following steps to insert a new node after a node in the single linked list
Step 1 - Create a newNode with given value.
Step 2-Check whether list is Empty (head == NULL)
Step 3-If it is Empty then, set newNode→ next = NULL and head = newNode.
Step 4-If it is Not Empty then, define a node pointer temp and initialize with head.
Step 5 - Keep moving the temp to its next node until it reaches to the node after which
we want to insert the newNode (until temp1 → data is equal to location, here location
is the node value after which we want to insert the newNode).
Step 6 - Every time check whether temp is reached to last node or not. If it is reached
to last node then display 'Given node is not found in the list!!! Insertion not
possible!!!" and terminate the function. Otherwise move the temp to next node.
Step 7 - Finally, Set 'newNode → next = temp→ next' and 'temp next = newNode
Deletion
In a single linked list, the deletion operation can be performed in three ways. They are
as follows...
1. Deleting from Beginning of the list
2. Deleting from End of the list
3. Deleting a Specific Node

Deleting from Beginning of the list


We can use the following steps to delete a node from beginning of the single linked
list...
Step 1 - Check whether list is Empty (head == NULL)
Step 2 - If it is Empty then, display "List is Empty!!! Deletion is not possible' and
terminate the function.
Step 3 - If it is Not Empty then, define a Node pointer 'temp' and initialize with head.
Step 4 - Check whether list is having only one node (temp→ next == NULL)
Step 5 - If it is TRUE then set head = NULL and delete temp (Setting Empty list
conditions)
Step 6 - If it is FALSE then set head = temp→ next, and delete temp.

Deleting from End of the list


We can use the following steps to delete a node from end of the single linked list...
Step 1 - Check whether list is Empty (head == NULL)
Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and
terminate the function.
Step 3 - If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and
initialize 'temp1' with head.
Step 4 - Check whether list has only one Node (temp1 → next == NULL)
Step 5 - If it is TRUE. Then, set head = NULL and delete temp1. And terminate the
function. (Setting Empty list condition)
Step 6- If it is FALSE. Then, set 'temp2 = temp1' and move temp1 to its next node.
Repeat the same until it reaches to the last node in the list.
(until temp1 →next == NULL)
Step 7 - Finally, Set temp2→ next = NULL and delete temp1.
Deleting a Specific Node from the list
We can use the following steps to delete a specific node from the single linked list...
Step 1 - Check whether list is Empty (head == NULL)
Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and
terminate the function.
Step 3 - If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and
initialize 'temp1' with head.
Step 4 - Keep moving the temp1 until it reaches to the exact node to be deleted or to
the last node. And every time set 'temp2 = temp1' before moving the 'temp1' to its next
node.
Step 5 - If it is reached to the last node then display 'Given node not found in the list!
Deletion not possible!!!'. And terminate the function.
Step 6 - If it is reached to the exact node which we want to delete, then check whether
list is having only one node or not
Step 7 - If list has only one node and that is the node to be deleted, thenset head =
NULL and delete temp1 (free(temp1)).
Step 8 - If list contains multiple nodes, then check whether temp1 is the first node in
the list (temp1 == head).
Step 9 - If temp1 is the first node then move the head to the next node (head = head →
next) and delete temp1.
Step 10 - If temp1 is not first node then check whether it is last node in the list (temp1
next == NULL).
Step 11 - If temp1 is last node then set temp2 → next = NULL and delete temp1
(free(temp1)).
Step 12 - If temp1 is not first node and not last node then set temp2→ next = temp1 →
next and delete temp1 (free(temp1)).

Displaying a Single Linked List


We can use the following steps to display the elements of a single linked list...
Step 1 - Check whether list is Empty (head == NULL)
Step 2 - If it is Empty then, display 'List is Empty!!!' and terminate the function.
Step 3 - If it is Not Empty then, define a Node pointer 'temp' and initialize with head.
Step 4 - Keep displaying temp→ data with an arrow (--->) until temp reaches to the last
node
Step 5- Finally display temp → data with arrow pointing to NULL (temp → data --- NULL)
Applications of Linked List

Computer Science:
1. Dynamic memory allocation for efficient memory usage.
2. Implementation of data structures like stacks and queues.
3. Collision resolution in hash tables using chaining.
4. Adjacency list representation for graphs in algorithms.
5. Sparse matrix representation to save memory.
6. Undo functionality in text editors and applications.
7. Storing polynomials for arithmetic operations.
8. Managing free blocks in file systems.

Real World:
1. Music playlists for sequential or shuffled playback.
2. Navigation in image viewers with forward and backward options.
3. Dynamic job scheduling in operating systems.
4. Social media feeds updating dynamically in real-time.
5. Navigation in browser tabs for efficient management.
6. Real-time data handling in video games and simulations.
7. Maintaining train routes with dynamic stops.
8. Linked storage of items in inventory systems.
Applications of Circular Linked List
1. Round-robin scheduling for managing CPU processes.
2. Token passing in networks like token ring topology.
3. Implementing circular buffers for streaming data.
4. Cyclic navigation of music or video playlists.
5. Simulation of real-time traffic control systems.
6. Multiplayer games for cyclic player turns.
7. Recurring task scheduling in event systems.
8. Periodic log file rotations in servers.

Applications of Doubly Linked List


1. Backward and forward navigation in web browsers.
2. Undo/redo operations in text and image editors.
3. Implementation of double-ended queues (deque).
4. Least Recently Used (LRU) cache management.
5. Page replacement algorithms in virtual memory.
6. Bi-directional graph traversal for algorithms.
7. Media player navigation between tracks.
8. Efficient routing in GPS and navigation systems.
Code
Insert At Beginning
#include<stdio.h>
#include<stdlib.h>

struct node {
int data ;
struct node *link;
};

struct node* insert_begin(struct node *head,int d){

struct node *temp2=malloc(sizeof(struct node));


temp2->data=d;
temp2->link=NULL;

temp2->link=head;
head=temp2;

return head;
}

int main(){

struct node *head= malloc(sizeof(struct node));


head->data=38;
head->link=NULL;

struct node *current= malloc(sizeof(struct node));


current->data=67;
current->link=NULL;

head->link=current;

int data = 45;

head=insert_begin(head,data);
current=head;

while(current!=NULL)
{
printf("%d",current->data);
current=current->link;
}
return 0;

}
Insert At Between
#include<stdio.h>
#include<stdlib.h>

struct node {
int data ;
struct node *link;
};

void insert_between(struct node* head,int data,int pos){

struct node *ptr=head;

struct node *temp2=malloc(sizeof(struct node));


temp2->data=data;
temp2->link=NULL;

pos--;
while(pos!=1){
ptr=ptr->link;
pos--;
}
temp2->link=ptr->link;
ptr->link=temp2;
}

int main(){

struct node *head= malloc(sizeof(struct node));


head->data=38;
head->link=NULL;

struct node *current= malloc(sizeof(struct node));


current->data=67;
current->link=NULL;
head->link=current;

struct node *current2= malloc(sizeof(struct node));


current2->data=77;
current2->link=NULL;
head->link->link=current2;

int data = 45;


int position = 3;

insert_between(head,data,position);
struct node *temp=head;

while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->link;
}
return 0;

}
Insert At the End
#include<stdio.h>
#include<stdlib.h>

struct node {
int data;
struct node *link;
};

void print( struct node *head){

if(head==NULL)
printf("Linked list is empty");

struct node *temp = NULL;


temp=head;

while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->link;
}
}

void insert_at_end(struct node *head,int data){

struct node *temp,*temp1;


temp=head;

temp1=malloc(sizeof(struct node));

temp1->data=data;
temp1->link=NULL;

while(temp->link!=NULL){
temp=temp->link;
}
temp->link=temp1;

void print2( struct node *head){

if(head==NULL)
printf("Linked list is empty");

struct node *temp = NULL;


temp=head;

while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->link;
}
}

int main()
{
struct node *head = NULL;
head=malloc(sizeof(struct node));
head->data=66;
head->link=NULL;

struct node *current = malloc(sizeof(struct node));


current->data=33;
current->link=NULL;
head->link=current;

struct node *current2 = malloc(sizeof(struct node));


current2->data=53;
current2->link=NULL;
head->link->link=current2;

print(head);

insert_at_end(head,45);

printf("\n");
print2(head);

return 0;

}
Delete At The Beginning
#include<stdio.h>
#include<stdlib.h>

struct node {
int data;
struct node *link;
};

void print( struct node *head){

if(head==NULL)
printf("Linked list is empty");

struct node *temp = NULL;


temp=head;

while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->link;
}
}

struct node* delete_begin(struct node *head){

if(head==NULL)
printf("Empty linked list");
else{
struct node *temp;
temp=head;

head= head->link;
free(temp);
temp=NULL;
}
return head;
}

void print2( struct node *head){

if(head==NULL)
printf("Linked list is empty");

struct node *temp = NULL;


temp=head;

while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->link;
}
}

int main()
{
struct node *head = NULL;
head=malloc(sizeof(struct node));
head->data=66;
head->link=NULL;

struct node *current = malloc(sizeof(struct node));


current->data=33;
current->link=NULL;
head->link=current;

struct node *current2 = malloc(sizeof(struct node));


current2->data=53;
current2->link=NULL;
head->link->link=current2;

print(head);
struct node *head1;
head1=delete_begin(head);

printf("\n");
print2(head1);

return 0;

}
Delete Between
#include<stdio.h>
#include<stdlib.h>

struct node {
int data;
struct node *link;
};

void print( struct node *head){

if(head==NULL)
printf("Linked list is empty");

struct node *temp = NULL;


temp=head;

while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->link;
}
}

struct node* delete_between(struct node *head,int pos){

struct node *temp=head;


struct node *temp1=head;

if(head==NULL)
printf("Empty linked list");
else if (pos==1){
head=temp->link;
free(temp);
temp=NULL;
}
else{
while(pos!=1){
temp1=temp; /*here it is already is in this condition*/
temp=temp->link;
pos--;
}
temp1->link=temp->link;
free(temp);
temp=NULL;
}
}

void print2( struct node *head){

if(head==NULL)
printf("Linked list is empty");
struct node *temp = NULL;
temp=head;

while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->link;
}
}

int main()
{
struct node *head = NULL;
head=malloc(sizeof(struct node));
head->data=66;
head->link=NULL;

struct node *current = malloc(sizeof(struct node));


current->data=33;
current->link=NULL;
head->link=current;

struct node *current2 = malloc(sizeof(struct node));


current2->data=53;
current2->link=NULL;
head->link->link=current2;

print(head);

int position= 2;
struct node *head1;
head1= delete_between(head,position);

printf("\n");

print2(head1);

return 0;
}
Delete At the End
#include<stdio.h>
#include<stdlib.h>

struct node {
int data;
struct node *link;
};

void print( struct node *head){

if(head==NULL)
printf("Linked list is empty");

struct node *temp = NULL;


temp=head;

while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->link;
}
}

struct node* delete_last(struct node *head){

if(head==NULL)
printf("Empty linked list");
else if (head->link==NULL){
free(head);
head=NULL;
}
else{
struct node *temp=head;
struct node *temp1=head;

while(temp->link!=NULL){
temp1=temp;
temp=temp->link;
}
temp1->link=NULL;
free(temp);
temp=NULL;
}
return head;
}

void print2( struct node *head){

if(head==NULL)
printf("Linked list is empty");
struct node *temp = NULL;
temp=head;

while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->link;
}
}

int main()
{
struct node *head = NULL;
head=malloc(sizeof(struct node));
head->data=66;
head->link=NULL;

struct node *current = malloc(sizeof(struct node));


current->data=33;
current->link=NULL;
head->link=current;

struct node *current2 = malloc(sizeof(struct node));


current2->data=53;
current2->link=NULL;
head->link->link=current2;

print(head);
struct node *head1;
head1=delete_last(head);

printf("\n");

print2(head1);

return 0;

}
Delete The list
#include<stdio.h>
#include<stdlib.h>

struct node {
int data;
struct node *link;
};

void print( struct node *head){

if(head==NULL)
printf("Linked list is empty");

struct node *temp = NULL;


temp=head;

while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->link;
}
}

struct node* delete_list(struct node *head){

struct node *temp=head;

while(temp!=NULL){
temp=temp->link;
free(head);
head=temp;
}
return head;
}

void print2( struct node *head){

if(head==NULL)
printf("Linked list is empty");

struct node *temp = NULL;


temp=head;

while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->link;
}
}
int main()
{
struct node *head = NULL;
head=malloc(sizeof(struct node));
head->data=66;
head->link=NULL;

struct node *current = malloc(sizeof(struct node));


current->data=33;
current->link=NULL;
head->link=current;

struct node *current2 = malloc(sizeof(struct node));


current2->data=53;
current2->link=NULL;
head->link->link=current2;

print(head);

struct node *head1;


head1= delete_list(head);

printf("\n");

print2(head1);

return 0;

}
PrintSinglyList
#include<stdio.h>
#include<stdlib.h>

struct node {
int data;
struct node *link;
};

int main()
{
struct node *head = NULL;
head=malloc(sizeof(struct node));
head->data=66;
head->link=NULL;

head = malloc(sizeof(struct node));


head->data=23;
head->link=NULL;

struct node *current = malloc(sizeof(struct node));


current->data=33;
current->link=NULL;
head->link=current;

struct node *current2 = malloc(sizeof(struct node));


current2->data=53;
current2->link=NULL;
head->link->link=current2;

struct node *temp = NULL;


temp=head;

while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->link;
}
return 0;
}
PrintSinglylist by Function
#include<stdio.h>
#include<stdlib.h>

struct node {
int data;
struct node *link;
};

void print( struct node *head){

if(head==NULL)
printf("Linked list is empty");

struct node *temp = NULL;


temp=head;

while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->link;
}
}

int main()
{
struct node *head = NULL;
head=malloc(sizeof(struct node));
head->data=66;
head->link=NULL;

struct node *current = malloc(sizeof(struct node));


current->data=33;
current->link=NULL;
head->link=current;

struct node *current2 = malloc(sizeof(struct node));


current2->data=53;
current2->link=NULL;
head->link->link=current2;

print(head);
return 0;

}
Reverse List
#include<stdio.h>
#include<stdlib.h>

struct node {
int data;
struct node *link;
};

void print( struct node *head){

if(head==NULL)
printf("Linked list is empty");

struct node *temp = NULL;


temp=head;

while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->link;
}
}

struct node* reverse(struct node *head){

struct node *prev = NULL;


struct node *next = NULL;

while(head != NULL){
next=head->link;
head->link = prev;
prev = head;
head = next;
}
head=prev;

return head;
}

void print2( struct node *head){

if(head==NULL)
printf("Linked list is empty");

struct node *temp = NULL;


temp=head;

while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->link;
}
}

int main()
{
struct node *head = NULL;
head=malloc(sizeof(struct node));
head->data=66;
head->link=NULL;

struct node *current = malloc(sizeof(struct node));


current->data=33;
current->link=NULL;
head->link=current;

struct node *current2 = malloc(sizeof(struct node));


current2->data=53;
current2->link=NULL;
head->link->link=current2;

print(head);

struct node *head1;


head1= reverse(head);

printf("\n");

print2(head1);

return 0;

You might also like