DS Unit 3
DS Unit 3
LinkedList
LinkedList
• LinkedListisaverycommonlyusedlineardata
structurewhichconsistsofgroupofnodesina
sequence.
• Eachnodeholdsitsowndataandtheaddressofthe next
node hence forming a chain like structure.
• LinkedListsareusedtocreatetreesandgraphs.
AdvantagesofLinkedLists
• Theyareadynamicinnaturewhichallocates
the memory when required.
• Insertionanddeletionoperationscanbe
easily implemented.
• Stacksandqueuescanbeeasilyexecuted.
DisadvantagesofLinkedLists
• Thememoryiswastedaspointersrequire
extra memory for storage.
• Noelementcanbeaccessedrandomly;ithas to
access each node sequentially.
• ReverseTraversingisdifficultinlinkedlist.
ApplicationsofLinkedLists
• Linkedlistsareusedtoimplementstacks,
queues, graphs, etc.
• Linkedlistsletyouinsertelementsatthe
beginning and end of the list.
• InLinkedListswedon'tneedtoknowthesize in
advance.
TypesofLinkedLists
• SinglyLinkedList
• DoublyLinkedList
• CircularLinkedList
SinglyLinkedList
• Singlylinkedlistscontainnodeswhichhave
adatapartaswellasanaddressparti.e.next,which points to
the next node in the sequence of nodes.
• Theoperationswecanperformonsinglylinkedlists are
insertion, deletion and traversal.
DoublyLinkedList
• Inadoublylinkedlist,eachnodecontains a
data part and two addresses, one for
thepreviousnodeandoneforthenextnode.
CircularLinkedList
• Incircularlinkedlistthelastnodeofthelist
holds the address of the first node hence
forming a circular chain.
DifferencebetweenArraysandLinkedList
SinglyLinkedList
• Whatisa Node?
• A Node in a linked list holds the data value
andthepointerwhichpointstothelocationof the
next node in the linked list.
Node Implementation
//Alinkedlistnode
struct Node
{
intdata;
structNode*next;
};
typedefstructBooks{
char title[50];
char author[50];
charsubject[100];
int book_id;
structBooks*add;
}Book;
Inserting a node
• A node can be added in three ways
1) At the front of the linked list
2) After a given node.
3) At the end of the linked list.
At the front of the linked list
/*Givenareference(pointertopointer)totheheadofalist and an int, inserts a new node on the front of the list. */ void push(struct Node**
/*3.Makenextofnewnodeashead*/
new_node->next=(*head_ref);
/*4.movetheheadtopointtothenewnode*/ (*head_ref)
= new_node;
}
Add a node at the end
Display the content of Linked List
Add a node at specified position
Singly Linked List: Deleting a node
• A node can be deleted in three ways
1) At the front of the linked list
2) After a given node /specified position
3) At the end of the linked list.
Delete a node at the front
voidPop()
{
structnode*ptr;
if(head==NULL)
{
printf("\nListisempty");
}
else
{
ptr=head;
head=ptr->next;
free(ptr);
printf("\nNodedeletedfromthebegining...");
}
}
Delete a node at the end
voidend_delete()
{
structnode*ptr,*ptr1;
if(head==NULL)
{
printf("\nlistisempty");
}
elseif(head->next== NULL)
{
free(head);
head=NULL;
printf("\nOnlynodeofthelistdeleted...");
}
else
{
ptr=head;
while(ptr->next!=NULL)
{
ptr1=ptr;
ptr=ptr->next;
}
ptr1->next=NULL;
free(ptr);
printf("\nDeletedNodefromthelast...");
}
}
Delete a node at specified position
voiddelete_specified()
{
structnode*ptr,*ptr1;
int loc,i;
scanf("%d",&loc);
ptr=head;
for(i=0;i<loc;i++)
{
ptr1=ptr;
ptr=ptr->next;
if(ptr== NULL)
{
printf("\nTherearelessthan%delements inthelist..\n",loc);
return;
}
}
ptr1->next=ptr->next;
free(ptr);
printf("\nDeleted%dnode",loc);
}
Singly Linked List Operations
• Search
• Count number of nodes
• Concatenation
• Merging
• Reversing
Search
Count number of nodes
voidCount_nodes()
{
/*temppointerpointstohead*/
structnode*temp= head;
/*Initializecountvariable*/ int
count=0;
/*Traversethelinkedlistandmaintainthecount*/ while(temp !=
NULL)
{
temp=temp->next;
/*Incrementcountvariable.*/ count+
+;
}
/*Printthetotalcount.*/
printf("\nTotalno.ofnodesis%d",count);
}
Concatenation
Merging
Reversing
Applications of Linked List
• Stack
• Queue implementation
Stack Implementation using Linked List
voidpush(structNode**head_ref,int new_data)
{
structNode*new_node=(structNode*)malloc(sizeof(structNode));
new_node->data = new_data;
new_node->next=(*head_ref);
(*head_ref) = new_node;
}
voidPop()
{
structnode*ptr;
if(head== NULL)
{
printf("\nListisempty");
}
else
{
ptr= head;
head=ptr->next; free(ptr);
printf("\nNodedeletedfromthebegining ...");
}
}
Queue implementation using Linked
List
void Enqueue(item)
{
structnode*ptr,*temp;
ptr=(structnode*)malloc(sizeof(structnode));
ptr->data=item;ptr-
>next=NULL;if(head == NULL)
{
head = ptr;printf("\
nNodeinserted");
}
else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next= ptr;printf("\
nNodeinserted");
}
}
VoidDequeue()
{
structnode*ptr;i
f(head==NULL)
{
printf("\nListisempty");
}
else
{
ptr= head;
head=ptr-
>next;free(ptr);
printf("\nNodedeletedfromthebegining...");
}
Circular Linked list
• InacircularSinglylinkedlist,thelastnodeofthelistcontainsapointertothefirst node of
the list. We can have circular singly linked list as well as circular doubly linked list.
• We traverse a circular singly linked list until we reach the same node where we
started.Thecircularsinglylikedlisthasnobeginningandnoending.Thereisno null
value present in the next part of any of the nodes.
Operationson Circular List
• Insertion at the beginning
• Insertion at the end
• Deletion at the beginning
• Deletion at the end
• Searching
• Traversing
Insertion at the beginning
voidbeg_insert(intitem)
{
structnode*ptr=(structnode*)malloc(sizeof(structnode));
structnode*temp;
if(ptr==NULL)
{
printf("\nOVERFLOW");
}
else
{
ptr->data=item;
if(head==NULL)
{
head=ptr;
ptr->next =head;
}
else
{
temp =head;
while(temp->next!=head)
temp = temp->next;
ptr->next = head;
temp->next=ptr;
head = ptr;
}
printf("\nNodeInserted\n");
}
Insertion at the end
voidlastinsert(structnode*ptr,structnode*temp,intitem)
{
ptr=(structnode*)malloc(sizeof(structnode));
if(ptr==NULL)
{
printf("\nOVERFLOW\n");
}
else
{
ptr->data= item;
if(head==NULL)
{
head=ptr;
ptr->next =head;
}
else
{
temp =head;
while(temp->next!=head)
{
temp =temp ->next;
}
temp->next=ptr; ptr-
>next =head;
}
}
Deletion at the beginning
voidbeg_delete()
{
structnode*ptr;
if(head==NULL)
{
printf("\nUNDERFLOW\n");
}
elseif(head->next==head)
{
head=NULL;
free(head);
printf("\nNodeDeleted\n");
}
else
{
ptr=head;
while(ptr->next!=head)
ptr = ptr -> next;
ptr->next=head->next;
free(head);
head = ptr->next; printf("\
nNode Deleted\n");
}
}
Deletion at the end
voidlast_delete()
{
structnode*ptr,*preptr;
if(head==NULL)
{
printf("\nUNDERFLOW\n");
}
elseif(head->next ==head)
{
head=NULL;
free(head);
printf("\nNodeDeleted\n");
}
else
{
ptr=head;
while(ptr->next!=head)
{
preptr=ptr;
ptr=ptr->next;
}
preptr->next=ptr->next;
free(ptr);
printf("\nNodeDeleted\n");
}
}
Searching
voidsearch()
{
struct node
*ptr;intitem,i=0,fla
g=1;ptr = head;
if(ptr==NULL)
{
printf("\nEmptyList\n");
}
else
{
printf("\nEnteritemwhichyouwanttosearch?\
n");scanf("%d",&item);
if(head->data==item)
{
printf("itemfoundatlocation
%d",i+1);flag=0;
return;
}
else
{
while(ptr->next!=head)
{
if(ptr->data==item)
{
printf("itemfoundatlocation%d",i+1);flag=0;
return;
}
else
{
flag=1;
}
i++;
ptr=ptr-> next;
}
}
if(flag!= 0)
{
printf("Itemnotfound\n");
return;
}
• }
Traversing
voidtraverse()
{
structnode*ptr;
ptr=head;
if(head==NULL)
{
printf("\nnothingtoprint");
}
else
{
printf("\nprintingvalues...\n");
while(ptr->next!=head)
{
printf("%d\n",ptr->data); ptr
= ptr -> next;
}
printf("%d\n",ptr->data);
}
}
Doubly Linked List
OperationsonDoublyLinkedList
• Insertion at beginning
• Insertion at end
• Insertion after specified node
• Deletion at beginning
• Deletion at end
• Deletion of the node given data
• Searching
• Traversing
Insertion at beginning
voidinsertbeginning(intitem)
{
structnode*ptr=(structnode*)malloc(sizeof(structnode));
if(head==NULL)
{
ptr->next=NULL;
ptr->prev=NULL;
ptr->data=item;
head=ptr;
}
else
{
ptr->data=item;
ptr->prev=NULL;
ptr->next=head;
head->prev=ptr;
head=ptr;
}
}
Insertion at end
voidinsertlast(intitem)
{
structnode*ptr=(structnode*)malloc(sizeof(structnode));
structnode*temp;
ptr->data=item;
if(head==NULL)
{
ptr->next=NULL;
ptr->prev=NULL;
head = ptr;
}
else
{
temp = head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=ptr;
ptr ->prev=temp;
ptr->next=NULL;
}
printf("\nNodeInserted\n");
}
Insert after specified node
voidinsert_specified(intitem)
{
structnode*ptr=(structnode*)malloc(sizeof(structnode)); struct
node *temp;
int i,loc;
printf("\nEnterthelocation\n");
scanf("%d",&loc);
temp=head;
for(i=0;i<loc;i++)
{
temp=temp->next;
if(temp==NULL)
{
printf("\ncan'tinsert\n");
return;
}
}
ptr->data=item;
ptr->next=temp->next;
ptr -> prev = temp;
temp->next = ptr;
temp->next->prev=ptr;
printf("Node Inserted\n");
}
}
Deletion at beginning
voidbeginning_delete()
{
structnode*ptr;
if(head ==NULL)
{
printf("\nUNDERFLOW\n");
}
elseif(head->next ==NULL)
{
head=NULL;
free(head);
printf("\nNodeDeleted\n");
}
else
{
ptr=head;
head =head ->next;
head->prev=NULL;
free(ptr);
printf("\nNodeDeleted\n");
}
}
Deletion at end
voidlast_delete()
{
structnode*ptr;
if(head==NULL)
{
printf("\nUNDERFLOW\n");
}
elseif(head->next==NULL)
{
head=NULL;
free(head);
printf("\nNodeDeleted\n");
}
else
{
ptr=head;
if(ptr->next!=NULL)
{
ptr= ptr->next;
}
ptr->prev->next=NULL;
free(ptr);
printf("\nNodeDeleted\n");
}
}
Deletion of a specified node
voiddelete_specified()
{
structnode*ptr, *temp;
int val;
printf("Enterthevalue");
scanf("%d",&val);
temp=head;
while(temp->data!=val)
temp = temp -> next;
if(temp -> next == NULL)
{
printf("\nCan'tdelete\n");
}
elseif(temp->next->next== NULL)
{
temp ->next = NULL; printf("\
nNodeDeleted\n");
}
else
{
ptr= temp->next;
temp->next=ptr->next;
ptr->next->prev=temp;
free(ptr);
printf("\nNodeDeleted\n");
}
}
Searching
voidsearch()
{
structnode*ptr;i
ntitem,i=0,flag;p
tr = head;
if(ptr==NULL)
{
printf("\nEmptyList\n");
}
else
{
printf("\nEnteritemwhichyouwanttosearch?\
n");scanf("%d",&item);
while(ptr!=NULL)
{
if(ptr->data==item)
{
printf("\nitemfoundatlocation%d",i+1);
flag=0;
break;
}
else
{
flag=1;
}
i++;
ptr=ptr-> next;
}
if(flag==1)
{
printf("\nItemnotfound\n");
}
}
}
Traversing
inttraverse()
{
structnode*ptr;
if(head==NULL)
{
printf("\nEmptyList\n");
}
else
{
ptr=head;
while(ptr!=NULL)
{
printf("%d\n",ptr->data);
ptr=ptr->next;
}
}