SlideShare ist ein Scribd-Unternehmen logo
1 von 52
UNIT-IIUNIT-II
Topics to be coveredTopics to be covered
 Singly linked listSingly linked list
 Circular linked listCircular linked list
 Doubly linked listDoubly linked list
 Representing Stack with linked listRepresenting Stack with linked list
 Representing Queues with linked listRepresenting Queues with linked list
 In array(or lists) are simple data structures used to hold sequence
of data.
 Array elements are stored in consecutive memory locations. To
occupy the adjacent space, block of memory that is required for
the array should be allocated before hand.
 Once memory allocated it cannot be extended any more. So that
array is called the static data structure.
 Wastage of memory is more in arrays.
int a[ ]= {50,42,85,71,99};
What’s wrong with Array and Why linked lists?
 Disadvantages of arrays as storage data structures:
– slow searching in unordered array
– insertion and deletion operations are slow. Because,
we have to shift subsequent elements
– Fixed size
– Wastage of memory
 Linked lists solve some of these problems
- Linked list is able to grow in size as needed
• Does not require the shifting of items during
insertions and deletions.
- No wastage of memory.
Linked ListsLinked Lists
Linked list
 Linked list is a linear data structure that supports the dynamic memory
allocation( the amount of memory could be varied during its use). It is
also dynamic data structure.
 Linked list is used to hold sequence of data values.
 Data values need not be stored in adjacent memory cells
 each data values has pointer which indicates where its next data value
in computer memory.
 An element in a linked list is known as a node. A node contains a data
part and one or two pointer part which contains the address of the
neighborhood nodes in the list.
Node structure-
• Types of linked list
• Depending on the requirements the pointers are
maintained, and accordingly linked list can be classified
into three groups
1. Singly linked lists
2. Circular linked lists
3. Doubly linked lists
1. Singly linked list
in singly linked list, each node has two parts one is data
part and other is address part.
- data part stores the data values.
- address part contains the address of its next node.
• Structure of singly linked list
10 2500
2000
20 2600
2500
30 2367 40 NULL
23672600
2000
Header
A NULL pointer used to mark the end of the linked list
The head or header always points to the first node in the
list.
Possible operations on singly linked
list
1. Insertion
2. Deletion
3. Traversedisplay
4. Search
5. reverse a linked list
6. Copying
7. Merging (combine two linked lists)
Insertion in linked list
• There are various positions where node
can be inserted.
1. Insert at front ( as a first element)
2. Insert at end ( as a last node)
3. Insert at middle ( any position)
Singly linked lists
Node Structure
struct node
{
int data;
struct node *link;
}*new, *ptr, *header, *ptr1;
Creating a node
new = malloc (sizeof(struct node));
new -> data = 10;
new -> link = NULL;
data link
2000
10
new
2000
NULL
1000
header
10 20 30
5
2000
2
1
1. Insert new node at front in linked list
Algorithm
step1- create a new node
Step2- new->link=header->link
Step3- new->data=item
Step4- header->link=new.
Step5-stop
5
New node
2000
1000 1500 2050
1500 2050
1000
2000
Insert new node at end of the
linked list
• Algorithm
• Step1- create a new node
• Step2- ptr=header
• Step3- while(ptr->link!=null)
• 3.1. ptr=ptr->link
• Step4- ptr->link=new
• Step5- new->data=item
• Step6- new->link=null
• Step7-stop
Insert new node at end of the linked list
2300
header
10 20 30
40
2500
ptr
1
2300 2400
2400
2450
2450
New
2500
Algorithm
Step1- create a new node
Step2- ptr=header
Step3- while(ptr->link!=null)
3.1. ptr=ptr->link
Step4- ptr->link=new
Step5- new->data=item
Step6- new->link=null
Step7-stop
Insert new node at any position in linked
list• Algorithm
1.Create new node
2. ptr=header
3. Enter the position
4. for(i=1;i<pos-1;i++)
4.1 ptr=ptr->link;
5. new->link=ptr->link;
6. ptr->link=new;
7. new->data=item
8.stop
10 2500
2000
20 2600
2500
30 2367 40 NULL
23672600
2000
Header
Inserted position is : 3
ptr
5
New
node
2600
1000
Deletion of a node from singly linked list
Like insertion, there are also various
cases of deletion:
1. Deletion at the front
2. Deletion at the end
3. Deletion at any position in the list
Deleting a node at the beginning
if (header = = NULL)
print “List is Empty”;
else
{
ptr = header;
header = header -> link;
free(ptr);
}
10 1800 20 30 1400 40 NULL
1500 1800 1200
1400
1200
1500
header
1500
ptr
1800
Deleting a node at the end
10 1800 20 1200 30 1400 40 NULL
1500 1800 1200
1400
1500
header
ptr = header;
while(ptr -> link != NULL)
{
ptr1=ptr;
ptr = ptr -> link;
}
ptr1 -> link = NULL;
free(ptr);
1500
ptr
18001200
NULL
ptr1 ptr1 ptr1
1400
Deleting a node at the given position
10 1800 20 1200 30 1400 40 NULL
1500
header
ptr = header ;
for(i=1;i<pos-1;i++)
ptr = ptr -> link;
ptr1 = ptr -> link;
ptr -> link = ptr1-> link;
free(ptr1);
1500
ptr
1500 1800 1200
1400
Delete position : 31800
ptr1
1200
1400
Traversing an elements of a list
10 1800 20 1200 30 1400 40 NULL
1500 1800 1200 1400
1500
header
if(header = = NULL)
print “List is empty”;
else
for (ptr = header ; ptr != NULL ; ptr = ptr -> link)
print “ptr->data”;
ptr
1500
SLL program
#include<stdio.h>
#include<malloc.h>
void search();
void traverse();
void deletion();
void insertion();
int choice,i,pos,item;
struct node
{
int data;
struct node *link;
}*header,*ptr,*ptr1,*new;
void main(){
header=NULL;
printf("****Menu****n");
printf("n1.insertionn 2.deletionn
3.traverse n4.search n5.exitn");
while(1)
{
printf("nenter ur choice");
scanf("%d",&choice);
switch(choice){
case 1: insertion();
break;
case 2: deletion();
break;
case 3: traverse();
break;
case 4:search();
break;
case 5:exit(0);
default:printf("nwrong choicen");
}//switch}//while}//main
//insertion function
void insertion()
{
new=malloc(sizeof(struct node));
printf("n enter the item to be insertedn");
scanf("%d",&item);
new->data=item;
if(header==NULL)
{
new->link=NULL;
header=new;
}//if
else
{
printf("nenter the place to insert the itemn");
printf("1.startn 2.middlen 3. endn");
scanf("%d",&choice);
if(choice==1)
{
new->link=header;
header=new;
}//if
if(choice==2)
{
ptr=header;
printf("enter the position to place
itemn");
scanf("%d",&pos);
for(i=0;i<pos-1;i++)
ptr=ptr->link;
new->link=ptr->link;
ptr->link=new;
}//if
if(choice==3)
{
ptr=header;
while(ptr->link!=NULL)
ptr=ptr->link;
new->link=NULL;
ptr->link=new;
}//if}//else}//insertion
//deletion function
void deletion()
{
ptr=header;
if(header==NULL)
{
printf("nthe list is empty");
}
else
{
printf("n1.start n2.middle n3.end");
printf("n enter the place to delete the
element from list");
scanf("%d",&choice);
if(choice==1)
{
printf("nthe deleted item from the list
is -> %d",ptr->data);
header=header->link;
}//if
if(choice==2){
printf("n enter the position to delete
the element from the list");
scanf("%d",&pos);
for(i=0;i<pos-1;i++)
{ptr1=ptr;
ptr=ptr->link;
}
printf("n the deleted element is ->
%d",ptr->data);
ptr1->link=ptr->link;
}//if
if(choice==3){
while(ptr->link!=NULL){
ptr1=ptr;
ptr=ptr->link;
}//while
printf("nthe deleted element from the
list is ->%d", ptr->data);
ptr1->link=NULL;
}}}
void search()
{
int loc=0;
ptr=header;
printf("n enter the element to
be searched in the list");
scanf("%d",&item);
while((ptr->data!=item)&&(ptr-
>link!=NULL))
{
ptr=ptr->link;
loc++;
}
If((ptr->link==NULL)&&(ptr-
>data!=item))
Printf(“n element not found”);
else
printf("n the element found
at location %d",loc);
}//search()
//traverse function
void traverse()
{
if(header==NULL)
printf("list is emptyn");
else
{
printf("n the elements in the list are");
for(ptr=header;ptr!=NULL;ptr=ptr->link)
printf(“ %d”, ptr->data);
}//else
}//traverse
 Disadvantage of using an array to implement a stack or queue
is the wastage of space.
 Implementing stacks as linked lists provides a feasibility on
the number of nodes by dynamically growing stacks, as a
linked list is a dynamic data structure.
 The stack can grow or shrink as the program demands it to.
 A variable top always points to top element of the stack.
 top = NULL specifies stack is empty.
Representing Stack with Linked List
 In this representation, first node in the list is last
inserted element hence top must points to the first
element on the stack
 Last node in the list is the first inserted element in the
stack.
 Thus, push operation always adds the new element at
front of the list
 And pop operation removes the element at front of
the list.
 Size of the stack not required.
 Test for overflow is not applicable in this case.
10 NULL
1500
1800
1200
1400
20 1400
30 1200
40 1800
50 1500 1100
top
Example:
The following list consists of five cells, each of which holds a data object
and a link to another cell.
A variable, top, holds the address of the first cell in the list.
/* write a c program to implement stack using linked list */
#include<stdio.h> #include<malloc.h> #include<stdlib.h>
int push(); int pop(); int display();
int choice,i,item;
struct node {
int data;
struct node *link;
}*top,*new,*ptr;
main() { top=NULL;
printf("n***Select Menu***n");
while(1) {
printf("n1.Push n2.Pop n3.Display n4.Exitn5.Count");
printf("nnEnter ur choice: ");
scanf("%d",&choice);
switch(choice) {
case 1: push(); break;
case 2: pop(); break;
case 3: display(); break;
case 4: exit(0);
case 5: count(); break;
default: printf("nWrong choice");
}/* end of switch */
}/* end of while */
}/* end of main */
int push()
{
new=malloc(sizeof(struct node));
printf("nEnter the item: ");
scanf("%d",&item);
new->data=item;
if(top==NULL)
{
new->link=NULL;
}
else
{
new->link=top;
}
top=new;
return;
}/* end of insertion */
int pop()
{
if(top = = NULL)
{
printf("nnStack is empty");
return;
}//if
else
{
printf("nnThe deleted element
is: %d",top->data);
top=top->link;
}
return;
}/* end of pop() */
int display()
{
ptr=top;
if(top= =NULL)
{
printf("nThe list is empty");
return;
}
printf("nThe elements in the stact are: ");
while(ptr!=NULL)
{
printf("n %d",ptr->data);
ptr=ptr->link;
}/* end of while */
return;
}/* end of display() */
int count()
{
int count=1;
ptr=top;
if(top = = NULL)
{
printf("nThe list is empty");
return;
}
while(ptr->link!=NULL)
{
++count;
ptr=ptr->link;
}
printf("nnThe number of elements in
the stack are: %d",count);
return;
}/* end of count */
 New items are added to the end of the list.
 Removing an item from the queue will be done from the front.
 A pictorial representation of a queue being implemented as a linked list
is given below.
 The variables front points to the first item in the queue and rear points
to the last item in the queue.
Representing Queue with Linked List
10 1800 20 1200 30 1400 40 NULL
1500 1800 1200 1400
front rear
10 1800 20 1200 30 1400 40 NULL
1500 1800 1200 1400
front rear
int enqueue()
{new=malloc(sizeof(struct node));
printf("nenter the item");
scanf("%d",&item);
new->data=item;
new->link=NULL;
if(front==NULL) {
front=new; }
else
{
rear->link=new;
}
rear=new;
return;
}/*end of enqueue */
/*write a c program to implement queue using linked list*/
#include<stdio.h> #include<malloc.h> #include<stdlib.h>
int choice,i,item;
struct node {
int data;
struct node *link;
}*front,*rear,*new,*ptr;
main() {
front=NULL;
rear=NULL;
printf("nn MENU");
printf("n1.Enqueue n2.Dequeue n3.Display n4.Exit");
while(1) {
printf("nEnter your choice: ");
scanf("%d",&choice);
switch(choice) {
case 1:enqueue(); break;
case 2:dequeue(); break;
case 3:display(); break;
case 4:exit(0);
default:printf("nwrong choice");
}/*end of switch */
}/*end of while */
}/*end of main */
int enqueue()
{
new=malloc(sizeof(struct node));
printf("nenter the item");
scanf("%d",&item);
new->data=item;
new->link=NULL;
if(front==NULL)
{
front=new;
}
else
{
rear->link=new;
}
rear=new;
return;
}/*end of enqueue */
display()
{
if(front==NULL)
printf("nThe list is
emtpy");
else
{
for(ptr=front;ptr!=NULL;ptr=ptr->link)
printf(" %d",ptr->data);
}
return;
}/* end of display */
dequeue()
{
if(front==NULL)
printf("nThe list is empty");
else
if(front==rear) /*list has single element*/
{
printf("nThe deleted element is: %d",front-
>data);
front=rear=NULL;
}
else
{
printf("nThe deleted element is: %d",front-
>data);
front=front->link;
}
return;
}/*end ofdequeue*/
Doubly linked list
 In a singly linked list one can move from the header node to any node in
one direction only (left-right).
 A doubly linked list is a two-way list because one can move in either
direction. That is, either from left to right or from right to left.
 It maintains two links or pointer. Hence it is called as doubly linked list.
 Where, DATA field - stores the element or data, PREV- contains the
address of its previous node, NEXT- contains the address of its next
node.
PREV DATA NEXT
Structure of the node
Operations on doubly linked list
• All the operations as mentioned for the singly linked can be
implemented on the doubly linked list more efficiently.
• Insertion
• Deletion
• Traverse
• Search.
Insertion on doubly linked list
• Insertion of a node at the front
• Insertion of a node at any position in the list
• Insertion of a node at the end
Deletion on doubly linked list
• Deletion at front
• Deletion at any position
• Deletion at end
if(header==NULL)
{
new->prev=NULL;
new->next=NULL;
header=new;
}
else
{
new->next=ptr;
ptr->prev=new;
new->prev=NULL;
header=new;
}
New 1200
1000
1050
2
1050
1050 1100 2000
1100 2000 1100
header
ptr
1
1200 10 20 30
5
Insertion of a node at the front
1200
Insertion of a node at the end
1. Create a new node
2. Read the item
3. new->data=item
4. ptr= header
5. while(ptr->next!=NULL)
5.1 ptr=ptr->next;
6. new->next=NULL;
7. ptr->next=new;
8. new->prev=ptr;
1050
1050
1050 1100 2000
1100 2000 1100
header
ptr
10 20 30
New 1200
40
1200
2000
Insertion of a node at any position in the list
1. create a node new
2. read item
3. new->data=item
4. ptr=header;
5. Read the position where the element is
to be inserted
6. for(i=1;i<pos-1;i++)
6.1 ptr=ptr->next;
7. 1 ptr1=ptr->next;
7.2 new->next=ptr1;
7.3 ptr1->prev=new;
7.4 new->prev=ptr;
7.5 ptr->next=new;
Algorithm
header
20 10001010 30 20002020 40 NULL100010 2020NULL
1010 2020 1000 2000
1010 ptr1010 ptr
50 NULLNULL
2200 new
Before inserting a node at position 3
header
20 22001010 30 20002200 40 NULL100010 2020NULL
1010 2020 1000 2000
1010
ptr2020 ptr
50 10002020
2200 new
ptr1000 ptr1
After inserting a node at position 3
Algorithm:
1.ptr=header
2.ptr1=ptr->next;
3.header=ptr1;
4.if(ptr1!=NULL)
1.ptr1->prev=NULL;
5. free(ptr);
header
20 10001010 30 20002020 40 NULL100010 2020NULL
1010 2020 1000 2000
1010
ptr1ptr
20 1000NULL 30 20002020 40 NULL100010 2020NULL
1010 2020 1000 2000
2020
1010
header
Before deleting a node at beginning
After deleting a node at beginning
header
20 10001010 30 20002020 40 NULL100010 2020NULL
1010 2020 1000 2000
1010
Algorithm:
1. ptr=header
2. while(ptr->next!=NULL)
1. ptr=ptr->next;
3. end while
4. ptr1=ptr->prev;
5. ptr1->next=NULL;
Before deleting a node at end
ptrpt1header
20 10001010 30 NULL2020 40 NULL100010 2020NULL
1010 2020 1000 2000
1010
20001000After deleting a node at end
Deletion at any position
Algorithm
1. ptr=header
1.for(i=0;i<pos-1;i++)
1. ptr=ptr->next;
2. ptr1=ptr->prev;
3. ptr2=ptr->next;
4. ptr1->next=ptr2;
5. ptr2->prev=ptr1;
6. free(ptr);
header
20 10001010 30 20002020 40 NULL100010 2020NULL
1010 2020 1000 2000
1010 ptr1010 ptr
Before deleting a node at position 3
After deleting a node at position 3
2000header
20 20001010 30 20002200 40 NULL202010 2020NULL
1010 2020 1000 2000
1010
ptr2020
ptr
1
ptr1000 ptr2
Displaying elements of a list
Algorithm:
1. ptr=header;
2. if(header = = NULL)
1. printf("The list is emptyn");
3. else
1. print “The elements in farword order: “
2. while(ptr!=NULL)
1. print “ptr->data”;
2. if(ptr->next = = NULL)
1. break;
3. ptr=ptr->next;
3. print “The elements in reverse order: “
4. while(ptr!=header)
4.1 print “ptr->data”;
4.2ptr=ptr->prev;
5. End while
6. print “ptr->data”;
7.end else
20 10001010 30 20002020 40 NULL100010 2020NULL
1010 2020 1000 2000
header
1010
ptr
1010
Forward Order : 10 20 30 40
Reverse Order : 40 30 20 10
Circular linked list
• In a single linked list the last node link is NULL, but a number of
advantages can be gained if we utilize this link field to store the pointer of
the header node.(address of first node).
• Definition- the linked list where the last node points the header node is
called circular linked list.
Structure of the circular linked list
Advantages of circular linked list
1. Accessibility of a member node in the list
2. No Null link problem
3. Merging and splitting operations implemented easily
4. Saves time when you want to go from last node to first node.
Disadvantage
1. Goes into infinite loop, if proper care is not taken
2. It is not easy to reverse the elements
3. Visiting previous node is also difficult
/* Write a c program to implement circular linked list*/
#include<stdio.h> #include<conio.h> #include<malloc.h>
#include<stdlib.h>
int choice,i,item;
struct node {
int data;
struct node *link;
}*front,*rear,*new,*ptr1,*ptr;
main() {
front=rear=NULL;
printf("n select menun");
while(1) {
printf("n1.Enqueue n2.Dequeue n3.Display n4.Exit");
printf("nEnter ur choice: ");
scanf("%d",&choice);
switch(choice) {
case 1: enqueue(); break;
case 2: dequeue(); break;
case 3: display(); break;
case 4: exit(0);
default: printf("nWrong choice.");
}/*end of switch*/
}/*end of while*/
}/*end of main*/
int enqueue()
{
new=malloc(sizeof(struct node));
printf("nEnter the item: ");
scanf("%d",&item);
new->data=item;
if(front==NULL)
front=new;
else
rear->link=new;
rear=new;
rear->link=front;
return;
}/*end of enqueue()*/
dequeue()
{
if(front==NULL)
printf("nThe circular list is empty.");
else
if(front==rear)// cll has single element
{
printf("nThe deleted element is: %d",front->data)
front=rear=NULL;
}
else
{
printf("nThe deleted element is: %d",front->data)
front=front->link;
rear->link=front;
}
return;
}/*end of dequeue*/
display()
{
ptr=front;
if(front==NULL)
printf("nThe circular list is empty.");
else
{
printf("nElements in the list are: ");
while(ptr!=rear)
{
printf(" %d",ptr->data);
ptr=ptr->link;
}/*end of while*/
printf(“ %d”, ptr->data);
return;
}/*end of else*/
}/*end of display*/

Weitere ähnliche Inhalte

Was ist angesagt?

Data Structure (Dynamic Array and Linked List)
Data Structure (Dynamic Array and Linked List)Data Structure (Dynamic Array and Linked List)
Data Structure (Dynamic Array and Linked List)Adam Mukharil Bachtiar
 
Circular linked list
Circular linked listCircular linked list
Circular linked listmaamir farooq
 
Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Muhammad Hammad Waseem
 
Circular link list.ppt
Circular link list.pptCircular link list.ppt
Circular link list.pptTirthika Bandi
 
header, circular and two way linked lists
header, circular and two way linked listsheader, circular and two way linked lists
header, circular and two way linked listsstudent
 
linked list using c
linked list using clinked list using c
linked list using cVenkat Reddy
 
Linked lists in Data Structure
Linked lists in Data StructureLinked lists in Data Structure
Linked lists in Data StructureMuhazzab Chouhadry
 
Array implementation and linked list as datat structure
Array implementation and linked list as datat structureArray implementation and linked list as datat structure
Array implementation and linked list as datat structureTushar Aneyrao
 
Linked List Static and Dynamic Memory Allocation
Linked List Static and Dynamic Memory AllocationLinked List Static and Dynamic Memory Allocation
Linked List Static and Dynamic Memory AllocationProf Ansari
 
Data Structure Lecture 6
Data Structure Lecture 6Data Structure Lecture 6
Data Structure Lecture 6Teksify
 

Was ist angesagt? (20)

Data Structure (Dynamic Array and Linked List)
Data Structure (Dynamic Array and Linked List)Data Structure (Dynamic Array and Linked List)
Data Structure (Dynamic Array and Linked List)
 
linked list
linked listlinked list
linked list
 
Singly link list
Singly link listSingly link list
Singly link list
 
Linked list
Linked listLinked list
Linked list
 
Doubly Link List
Doubly Link ListDoubly Link List
Doubly Link List
 
Linklist
LinklistLinklist
Linklist
 
Linked lists a
Linked lists aLinked lists a
Linked lists a
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
 
Link List
Link ListLink List
Link List
 
Linked lists 1
Linked lists 1Linked lists 1
Linked lists 1
 
Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]
 
Team 10
Team 10Team 10
Team 10
 
Circular link list.ppt
Circular link list.pptCircular link list.ppt
Circular link list.ppt
 
header, circular and two way linked lists
header, circular and two way linked listsheader, circular and two way linked lists
header, circular and two way linked lists
 
linked list using c
linked list using clinked list using c
linked list using c
 
Linked lists in Data Structure
Linked lists in Data StructureLinked lists in Data Structure
Linked lists in Data Structure
 
Algo>ADT list & linked list
Algo>ADT list & linked listAlgo>ADT list & linked list
Algo>ADT list & linked list
 
Array implementation and linked list as datat structure
Array implementation and linked list as datat structureArray implementation and linked list as datat structure
Array implementation and linked list as datat structure
 
Linked List Static and Dynamic Memory Allocation
Linked List Static and Dynamic Memory AllocationLinked List Static and Dynamic Memory Allocation
Linked List Static and Dynamic Memory Allocation
 
Data Structure Lecture 6
Data Structure Lecture 6Data Structure Lecture 6
Data Structure Lecture 6
 

Andere mochten auch

Andere mochten auch (8)

Applications of queue
Applications of queueApplications of queue
Applications of queue
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
 
Queue in Data Structure
Queue in Data StructureQueue in Data Structure
Queue in Data Structure
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
 
Ppt presentation of queues
Ppt presentation of queuesPpt presentation of queues
Ppt presentation of queues
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Queue as data_structure
Queue as data_structureQueue as data_structure
Queue as data_structure
 

Ähnlich wie UNIT-II Topics: Singly Linked Lists, Stacks, Queues

Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListManishPrajapati78
 
Revisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queueRevisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queuessuser7319f8
 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structuresDurgaDeviCbit
 
Data Structures_Linked List
Data Structures_Linked ListData Structures_Linked List
Data Structures_Linked ListThenmozhiK5
 
Linked list and its operations - Traversal
Linked list and its operations - TraversalLinked list and its operations - Traversal
Linked list and its operations - Traversalkasthurimukila
 
DS Module 03.pdf
DS Module 03.pdfDS Module 03.pdf
DS Module 03.pdfSonaPathak5
 
STACK, LINKED LIST ,AND QUEUE
STACK, LINKED LIST ,AND QUEUESTACK, LINKED LIST ,AND QUEUE
STACK, LINKED LIST ,AND QUEUEDev Chauhan
 
ds 4Linked lists.ppt
ds 4Linked lists.pptds 4Linked lists.ppt
ds 4Linked lists.pptAlliVinay1
 

Ähnlich wie UNIT-II Topics: Singly Linked Lists, Stacks, Queues (20)

DS Unit 2.ppt
DS Unit 2.pptDS Unit 2.ppt
DS Unit 2.ppt
 
Linked list
Linked listLinked list
Linked list
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
 
Revisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queueRevisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queue
 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structures
 
Data Structures_Linked List
Data Structures_Linked ListData Structures_Linked List
Data Structures_Linked List
 
Linked list and its operations - Traversal
Linked list and its operations - TraversalLinked list and its operations - Traversal
Linked list and its operations - Traversal
 
DS Module 03.pdf
DS Module 03.pdfDS Module 03.pdf
DS Module 03.pdf
 
Linked list
Linked list Linked list
Linked list
 
Linked List
Linked ListLinked List
Linked List
 
STACK, LINKED LIST ,AND QUEUE
STACK, LINKED LIST ,AND QUEUESTACK, LINKED LIST ,AND QUEUE
STACK, LINKED LIST ,AND QUEUE
 
Lec3-Linked list.pptx
Lec3-Linked list.pptxLec3-Linked list.pptx
Lec3-Linked list.pptx
 
ds 4Linked lists.ppt
ds 4Linked lists.pptds 4Linked lists.ppt
ds 4Linked lists.ppt
 
Unit 5 linked list
Unit   5 linked listUnit   5 linked list
Unit 5 linked list
 
Chapter 5 ds
Chapter 5 dsChapter 5 ds
Chapter 5 ds
 
Data Structure
Data StructureData Structure
Data Structure
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 

Mehr von Durga Devi

Mehr von Durga Devi (6)

Unit i
Unit iUnit i
Unit i
 
Unit v(dsc++)
Unit v(dsc++)Unit v(dsc++)
Unit v(dsc++)
 
Unit iv(dsc++)
Unit iv(dsc++)Unit iv(dsc++)
Unit iv(dsc++)
 
Unit iii(dsc++)
Unit iii(dsc++)Unit iii(dsc++)
Unit iii(dsc++)
 
Unit i(dsc++)
Unit i(dsc++)Unit i(dsc++)
Unit i(dsc++)
 
Unit vi(dsc++)
Unit vi(dsc++)Unit vi(dsc++)
Unit vi(dsc++)
 

Kürzlich hochgeladen

HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
FILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinoFILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinojohnmickonozaleda
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 

Kürzlich hochgeladen (20)

HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
FILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinoFILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipino
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 

UNIT-II Topics: Singly Linked Lists, Stacks, Queues

  • 1. UNIT-IIUNIT-II Topics to be coveredTopics to be covered  Singly linked listSingly linked list  Circular linked listCircular linked list  Doubly linked listDoubly linked list  Representing Stack with linked listRepresenting Stack with linked list  Representing Queues with linked listRepresenting Queues with linked list
  • 2.  In array(or lists) are simple data structures used to hold sequence of data.  Array elements are stored in consecutive memory locations. To occupy the adjacent space, block of memory that is required for the array should be allocated before hand.  Once memory allocated it cannot be extended any more. So that array is called the static data structure.  Wastage of memory is more in arrays. int a[ ]= {50,42,85,71,99};
  • 3. What’s wrong with Array and Why linked lists?  Disadvantages of arrays as storage data structures: – slow searching in unordered array – insertion and deletion operations are slow. Because, we have to shift subsequent elements – Fixed size – Wastage of memory  Linked lists solve some of these problems - Linked list is able to grow in size as needed • Does not require the shifting of items during insertions and deletions. - No wastage of memory.
  • 5. Linked list  Linked list is a linear data structure that supports the dynamic memory allocation( the amount of memory could be varied during its use). It is also dynamic data structure.  Linked list is used to hold sequence of data values.  Data values need not be stored in adjacent memory cells  each data values has pointer which indicates where its next data value in computer memory.  An element in a linked list is known as a node. A node contains a data part and one or two pointer part which contains the address of the neighborhood nodes in the list. Node structure-
  • 6.
  • 7. • Types of linked list • Depending on the requirements the pointers are maintained, and accordingly linked list can be classified into three groups 1. Singly linked lists 2. Circular linked lists 3. Doubly linked lists 1. Singly linked list in singly linked list, each node has two parts one is data part and other is address part. - data part stores the data values. - address part contains the address of its next node.
  • 8. • Structure of singly linked list 10 2500 2000 20 2600 2500 30 2367 40 NULL 23672600 2000 Header A NULL pointer used to mark the end of the linked list The head or header always points to the first node in the list.
  • 9. Possible operations on singly linked list 1. Insertion 2. Deletion 3. Traversedisplay 4. Search 5. reverse a linked list 6. Copying 7. Merging (combine two linked lists)
  • 10. Insertion in linked list • There are various positions where node can be inserted. 1. Insert at front ( as a first element) 2. Insert at end ( as a last node) 3. Insert at middle ( any position)
  • 11. Singly linked lists Node Structure struct node { int data; struct node *link; }*new, *ptr, *header, *ptr1; Creating a node new = malloc (sizeof(struct node)); new -> data = 10; new -> link = NULL; data link 2000 10 new 2000 NULL
  • 12. 1000 header 10 20 30 5 2000 2 1 1. Insert new node at front in linked list Algorithm step1- create a new node Step2- new->link=header->link Step3- new->data=item Step4- header->link=new. Step5-stop 5 New node 2000 1000 1500 2050 1500 2050 1000 2000
  • 13. Insert new node at end of the linked list • Algorithm • Step1- create a new node • Step2- ptr=header • Step3- while(ptr->link!=null) • 3.1. ptr=ptr->link • Step4- ptr->link=new • Step5- new->data=item • Step6- new->link=null • Step7-stop
  • 14. Insert new node at end of the linked list 2300 header 10 20 30 40 2500 ptr 1 2300 2400 2400 2450 2450 New 2500 Algorithm Step1- create a new node Step2- ptr=header Step3- while(ptr->link!=null) 3.1. ptr=ptr->link Step4- ptr->link=new Step5- new->data=item Step6- new->link=null Step7-stop
  • 15. Insert new node at any position in linked list• Algorithm 1.Create new node 2. ptr=header 3. Enter the position 4. for(i=1;i<pos-1;i++) 4.1 ptr=ptr->link; 5. new->link=ptr->link; 6. ptr->link=new; 7. new->data=item 8.stop 10 2500 2000 20 2600 2500 30 2367 40 NULL 23672600 2000 Header Inserted position is : 3 ptr 5 New node 2600 1000
  • 16. Deletion of a node from singly linked list Like insertion, there are also various cases of deletion: 1. Deletion at the front 2. Deletion at the end 3. Deletion at any position in the list
  • 17. Deleting a node at the beginning if (header = = NULL) print “List is Empty”; else { ptr = header; header = header -> link; free(ptr); } 10 1800 20 30 1400 40 NULL 1500 1800 1200 1400 1200 1500 header 1500 ptr 1800
  • 18. Deleting a node at the end 10 1800 20 1200 30 1400 40 NULL 1500 1800 1200 1400 1500 header ptr = header; while(ptr -> link != NULL) { ptr1=ptr; ptr = ptr -> link; } ptr1 -> link = NULL; free(ptr); 1500 ptr 18001200 NULL ptr1 ptr1 ptr1 1400
  • 19. Deleting a node at the given position 10 1800 20 1200 30 1400 40 NULL 1500 header ptr = header ; for(i=1;i<pos-1;i++) ptr = ptr -> link; ptr1 = ptr -> link; ptr -> link = ptr1-> link; free(ptr1); 1500 ptr 1500 1800 1200 1400 Delete position : 31800 ptr1 1200 1400
  • 20. Traversing an elements of a list 10 1800 20 1200 30 1400 40 NULL 1500 1800 1200 1400 1500 header if(header = = NULL) print “List is empty”; else for (ptr = header ; ptr != NULL ; ptr = ptr -> link) print “ptr->data”; ptr 1500
  • 21. SLL program #include<stdio.h> #include<malloc.h> void search(); void traverse(); void deletion(); void insertion(); int choice,i,pos,item; struct node { int data; struct node *link; }*header,*ptr,*ptr1,*new; void main(){ header=NULL; printf("****Menu****n"); printf("n1.insertionn 2.deletionn 3.traverse n4.search n5.exitn"); while(1) { printf("nenter ur choice"); scanf("%d",&choice); switch(choice){ case 1: insertion(); break; case 2: deletion(); break; case 3: traverse(); break; case 4:search(); break; case 5:exit(0); default:printf("nwrong choicen"); }//switch}//while}//main
  • 22. //insertion function void insertion() { new=malloc(sizeof(struct node)); printf("n enter the item to be insertedn"); scanf("%d",&item); new->data=item; if(header==NULL) { new->link=NULL; header=new; }//if else { printf("nenter the place to insert the itemn"); printf("1.startn 2.middlen 3. endn"); scanf("%d",&choice); if(choice==1) { new->link=header; header=new; }//if if(choice==2) { ptr=header; printf("enter the position to place itemn"); scanf("%d",&pos); for(i=0;i<pos-1;i++) ptr=ptr->link; new->link=ptr->link; ptr->link=new; }//if if(choice==3) { ptr=header; while(ptr->link!=NULL) ptr=ptr->link; new->link=NULL; ptr->link=new; }//if}//else}//insertion
  • 23. //deletion function void deletion() { ptr=header; if(header==NULL) { printf("nthe list is empty"); } else { printf("n1.start n2.middle n3.end"); printf("n enter the place to delete the element from list"); scanf("%d",&choice); if(choice==1) { printf("nthe deleted item from the list is -> %d",ptr->data); header=header->link; }//if if(choice==2){ printf("n enter the position to delete the element from the list"); scanf("%d",&pos); for(i=0;i<pos-1;i++) {ptr1=ptr; ptr=ptr->link; } printf("n the deleted element is -> %d",ptr->data); ptr1->link=ptr->link; }//if if(choice==3){ while(ptr->link!=NULL){ ptr1=ptr; ptr=ptr->link; }//while printf("nthe deleted element from the list is ->%d", ptr->data); ptr1->link=NULL; }}}
  • 24. void search() { int loc=0; ptr=header; printf("n enter the element to be searched in the list"); scanf("%d",&item); while((ptr->data!=item)&&(ptr- >link!=NULL)) { ptr=ptr->link; loc++; } If((ptr->link==NULL)&&(ptr- >data!=item)) Printf(“n element not found”); else printf("n the element found at location %d",loc); }//search() //traverse function void traverse() { if(header==NULL) printf("list is emptyn"); else { printf("n the elements in the list are"); for(ptr=header;ptr!=NULL;ptr=ptr->link) printf(“ %d”, ptr->data); }//else }//traverse
  • 25.  Disadvantage of using an array to implement a stack or queue is the wastage of space.  Implementing stacks as linked lists provides a feasibility on the number of nodes by dynamically growing stacks, as a linked list is a dynamic data structure.  The stack can grow or shrink as the program demands it to.  A variable top always points to top element of the stack.  top = NULL specifies stack is empty. Representing Stack with Linked List
  • 26.  In this representation, first node in the list is last inserted element hence top must points to the first element on the stack  Last node in the list is the first inserted element in the stack.  Thus, push operation always adds the new element at front of the list  And pop operation removes the element at front of the list.  Size of the stack not required.  Test for overflow is not applicable in this case.
  • 27. 10 NULL 1500 1800 1200 1400 20 1400 30 1200 40 1800 50 1500 1100 top Example: The following list consists of five cells, each of which holds a data object and a link to another cell. A variable, top, holds the address of the first cell in the list.
  • 28. /* write a c program to implement stack using linked list */ #include<stdio.h> #include<malloc.h> #include<stdlib.h> int push(); int pop(); int display(); int choice,i,item; struct node { int data; struct node *link; }*top,*new,*ptr; main() { top=NULL; printf("n***Select Menu***n"); while(1) { printf("n1.Push n2.Pop n3.Display n4.Exitn5.Count"); printf("nnEnter ur choice: "); scanf("%d",&choice); switch(choice) { case 1: push(); break; case 2: pop(); break; case 3: display(); break; case 4: exit(0); case 5: count(); break; default: printf("nWrong choice"); }/* end of switch */ }/* end of while */ }/* end of main */
  • 29. int push() { new=malloc(sizeof(struct node)); printf("nEnter the item: "); scanf("%d",&item); new->data=item; if(top==NULL) { new->link=NULL; } else { new->link=top; } top=new; return; }/* end of insertion */ int pop() { if(top = = NULL) { printf("nnStack is empty"); return; }//if else { printf("nnThe deleted element is: %d",top->data); top=top->link; } return; }/* end of pop() */
  • 30. int display() { ptr=top; if(top= =NULL) { printf("nThe list is empty"); return; } printf("nThe elements in the stact are: "); while(ptr!=NULL) { printf("n %d",ptr->data); ptr=ptr->link; }/* end of while */ return; }/* end of display() */ int count() { int count=1; ptr=top; if(top = = NULL) { printf("nThe list is empty"); return; } while(ptr->link!=NULL) { ++count; ptr=ptr->link; } printf("nnThe number of elements in the stack are: %d",count); return; }/* end of count */
  • 31.  New items are added to the end of the list.  Removing an item from the queue will be done from the front.  A pictorial representation of a queue being implemented as a linked list is given below.  The variables front points to the first item in the queue and rear points to the last item in the queue. Representing Queue with Linked List 10 1800 20 1200 30 1400 40 NULL 1500 1800 1200 1400 front rear
  • 32. 10 1800 20 1200 30 1400 40 NULL 1500 1800 1200 1400 front rear int enqueue() {new=malloc(sizeof(struct node)); printf("nenter the item"); scanf("%d",&item); new->data=item; new->link=NULL; if(front==NULL) { front=new; } else { rear->link=new; } rear=new; return; }/*end of enqueue */
  • 33. /*write a c program to implement queue using linked list*/ #include<stdio.h> #include<malloc.h> #include<stdlib.h> int choice,i,item; struct node { int data; struct node *link; }*front,*rear,*new,*ptr; main() { front=NULL; rear=NULL; printf("nn MENU"); printf("n1.Enqueue n2.Dequeue n3.Display n4.Exit"); while(1) { printf("nEnter your choice: "); scanf("%d",&choice); switch(choice) { case 1:enqueue(); break; case 2:dequeue(); break; case 3:display(); break; case 4:exit(0); default:printf("nwrong choice"); }/*end of switch */ }/*end of while */ }/*end of main */
  • 34. int enqueue() { new=malloc(sizeof(struct node)); printf("nenter the item"); scanf("%d",&item); new->data=item; new->link=NULL; if(front==NULL) { front=new; } else { rear->link=new; } rear=new; return; }/*end of enqueue */ display() { if(front==NULL) printf("nThe list is emtpy"); else { for(ptr=front;ptr!=NULL;ptr=ptr->link) printf(" %d",ptr->data); } return; }/* end of display */
  • 35. dequeue() { if(front==NULL) printf("nThe list is empty"); else if(front==rear) /*list has single element*/ { printf("nThe deleted element is: %d",front- >data); front=rear=NULL; } else { printf("nThe deleted element is: %d",front- >data); front=front->link; } return; }/*end ofdequeue*/
  • 36. Doubly linked list  In a singly linked list one can move from the header node to any node in one direction only (left-right).  A doubly linked list is a two-way list because one can move in either direction. That is, either from left to right or from right to left.  It maintains two links or pointer. Hence it is called as doubly linked list.  Where, DATA field - stores the element or data, PREV- contains the address of its previous node, NEXT- contains the address of its next node. PREV DATA NEXT Structure of the node
  • 37. Operations on doubly linked list • All the operations as mentioned for the singly linked can be implemented on the doubly linked list more efficiently. • Insertion • Deletion • Traverse • Search. Insertion on doubly linked list • Insertion of a node at the front • Insertion of a node at any position in the list • Insertion of a node at the end Deletion on doubly linked list • Deletion at front • Deletion at any position • Deletion at end
  • 39. Insertion of a node at the end 1. Create a new node 2. Read the item 3. new->data=item 4. ptr= header 5. while(ptr->next!=NULL) 5.1 ptr=ptr->next; 6. new->next=NULL; 7. ptr->next=new; 8. new->prev=ptr; 1050 1050 1050 1100 2000 1100 2000 1100 header ptr 10 20 30 New 1200 40 1200 2000
  • 40. Insertion of a node at any position in the list 1. create a node new 2. read item 3. new->data=item 4. ptr=header; 5. Read the position where the element is to be inserted 6. for(i=1;i<pos-1;i++) 6.1 ptr=ptr->next; 7. 1 ptr1=ptr->next; 7.2 new->next=ptr1; 7.3 ptr1->prev=new; 7.4 new->prev=ptr; 7.5 ptr->next=new; Algorithm
  • 41. header 20 10001010 30 20002020 40 NULL100010 2020NULL 1010 2020 1000 2000 1010 ptr1010 ptr 50 NULLNULL 2200 new Before inserting a node at position 3 header 20 22001010 30 20002200 40 NULL100010 2020NULL 1010 2020 1000 2000 1010 ptr2020 ptr 50 10002020 2200 new ptr1000 ptr1 After inserting a node at position 3
  • 42. Algorithm: 1.ptr=header 2.ptr1=ptr->next; 3.header=ptr1; 4.if(ptr1!=NULL) 1.ptr1->prev=NULL; 5. free(ptr); header 20 10001010 30 20002020 40 NULL100010 2020NULL 1010 2020 1000 2000 1010 ptr1ptr 20 1000NULL 30 20002020 40 NULL100010 2020NULL 1010 2020 1000 2000 2020 1010 header Before deleting a node at beginning After deleting a node at beginning
  • 43. header 20 10001010 30 20002020 40 NULL100010 2020NULL 1010 2020 1000 2000 1010 Algorithm: 1. ptr=header 2. while(ptr->next!=NULL) 1. ptr=ptr->next; 3. end while 4. ptr1=ptr->prev; 5. ptr1->next=NULL; Before deleting a node at end ptrpt1header 20 10001010 30 NULL2020 40 NULL100010 2020NULL 1010 2020 1000 2000 1010 20001000After deleting a node at end
  • 44. Deletion at any position Algorithm 1. ptr=header 1.for(i=0;i<pos-1;i++) 1. ptr=ptr->next; 2. ptr1=ptr->prev; 3. ptr2=ptr->next; 4. ptr1->next=ptr2; 5. ptr2->prev=ptr1; 6. free(ptr);
  • 45. header 20 10001010 30 20002020 40 NULL100010 2020NULL 1010 2020 1000 2000 1010 ptr1010 ptr Before deleting a node at position 3 After deleting a node at position 3 2000header 20 20001010 30 20002200 40 NULL202010 2020NULL 1010 2020 1000 2000 1010 ptr2020 ptr 1 ptr1000 ptr2
  • 46. Displaying elements of a list Algorithm: 1. ptr=header; 2. if(header = = NULL) 1. printf("The list is emptyn"); 3. else 1. print “The elements in farword order: “ 2. while(ptr!=NULL) 1. print “ptr->data”; 2. if(ptr->next = = NULL) 1. break; 3. ptr=ptr->next; 3. print “The elements in reverse order: “ 4. while(ptr!=header) 4.1 print “ptr->data”; 4.2ptr=ptr->prev; 5. End while 6. print “ptr->data”; 7.end else
  • 47. 20 10001010 30 20002020 40 NULL100010 2020NULL 1010 2020 1000 2000 header 1010 ptr 1010 Forward Order : 10 20 30 40 Reverse Order : 40 30 20 10
  • 48. Circular linked list • In a single linked list the last node link is NULL, but a number of advantages can be gained if we utilize this link field to store the pointer of the header node.(address of first node). • Definition- the linked list where the last node points the header node is called circular linked list. Structure of the circular linked list
  • 49. Advantages of circular linked list 1. Accessibility of a member node in the list 2. No Null link problem 3. Merging and splitting operations implemented easily 4. Saves time when you want to go from last node to first node. Disadvantage 1. Goes into infinite loop, if proper care is not taken 2. It is not easy to reverse the elements 3. Visiting previous node is also difficult
  • 50. /* Write a c program to implement circular linked list*/ #include<stdio.h> #include<conio.h> #include<malloc.h> #include<stdlib.h> int choice,i,item; struct node { int data; struct node *link; }*front,*rear,*new,*ptr1,*ptr; main() { front=rear=NULL; printf("n select menun"); while(1) { printf("n1.Enqueue n2.Dequeue n3.Display n4.Exit"); printf("nEnter ur choice: "); scanf("%d",&choice); switch(choice) { case 1: enqueue(); break; case 2: dequeue(); break; case 3: display(); break; case 4: exit(0); default: printf("nWrong choice."); }/*end of switch*/ }/*end of while*/ }/*end of main*/
  • 51. int enqueue() { new=malloc(sizeof(struct node)); printf("nEnter the item: "); scanf("%d",&item); new->data=item; if(front==NULL) front=new; else rear->link=new; rear=new; rear->link=front; return; }/*end of enqueue()*/ dequeue() { if(front==NULL) printf("nThe circular list is empty."); else if(front==rear)// cll has single element { printf("nThe deleted element is: %d",front->data) front=rear=NULL; } else { printf("nThe deleted element is: %d",front->data) front=front->link; rear->link=front; } return; }/*end of dequeue*/
  • 52. display() { ptr=front; if(front==NULL) printf("nThe circular list is empty."); else { printf("nElements in the list are: "); while(ptr!=rear) { printf(" %d",ptr->data); ptr=ptr->link; }/*end of while*/ printf(“ %d”, ptr->data); return; }/*end of else*/ }/*end of display*/