SlideShare ist ein Scribd-Unternehmen logo
1 von 64
Downloaden Sie, um offline zu lesen
UNIT-II
Topics to be covered
 Linked Lists: Introduction Linked lists
 Representation of linked list
 operations on linked list
 Comparison of Linked Lists with Arrays and
Dynamic Arrays
 Types of Linked Lists and operations-Circular
Single Linked List, Double Linked List,
Circular Double Linked List
S. Durga Devi , CSE, CBIT
 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};
S. Durga Devi , CSE, CBIT
What’s wrong with Array and Why linked lists?
 Disadvantages of arrays as storage data structures:
– slow searching in unorderedarray
– insertion and deletionoperationsare slow. Because, we have to
shift subsequentelements
– 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.
S. Durga Devi , CSE, CBIT
Linked Lists
S. Durga Devi , CSE, CBIT
Linked list
 Linked list is a linear data structure that supportsthe 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 sequenceof 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 nodecontains a data part and one
or two pointer part which contains the address of the neighborhoodnodes in the list.
Node structure-
S. Durga Devi , CSE, CBIT
S. Durga Devi , CSE, CBIT
• 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
4. Circular doubly linked list
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.
S. Durga Devi , CSE, CBIT
• 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.
S. Durga Devi , CSE, CBIT
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)
S. Durga Devi , CSE, CBIT
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)
S. Durga Devi , CSE, CBIT
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
S. Durga Devi , CSE, CBIT
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
S. Durga Devi , CSE, CBIT
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
S. Durga Devi , CSE, CBIT
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
S. Durga Devi , CSE, CBIT
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 positionis : 3
ptr
5
New node
1000
2600
1000
S. Durga Devi , CSE, CBIT
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
S. Durga Devi , CSE, CBIT
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
S. Durga Devi , CSE, CBIT
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
S. Durga Devi , CSE, CBIT
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
S. Durga Devi , CSE, CBIT
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
S. Durga Devi , CSE, CBIT
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;
S. Durga Devi , CSE, CBIT
SLL program
int 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
S. Durga Devi , CSE, CBIT
//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
S. Durga Devi , CSE, CBIT
//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;
}}}
S. Durga Devi , CSE, CBIT
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
S. Durga Devi , CSE, CBIT
Comparison of linked list and array
S. Durga Devi , CSE, CBIT
• Linked list and arrays are used to store collection of data.
Both purpose is same but differ in their usage.
• Arrays:
- 1. to access an array element, the address of an element
computed by using base address and multiply position
with element size then added to base address. Hence, it
takes constant time to access an array element.
- 2. the size of the array is fixed
- 3. random access is possible.
- 4. inserting new element is expensive as it takes
shifting operation.
- 5. takes less space to store elements
- 6. memory allocated during compile time.
- 7. wastage of memory is more.
Linked list
S. Durga Devi , CSE, CBIT
• Accessing linked list elements are slower as it takes time proportional
to i to find the i-th member of a linked list by skipping over the
first i−1 cells.
• Dynamic size.
• Insertion and deletion operationsare easy.
• No possibility of randomaccessing.
• Extra space is required as it points to the next element.
• Memory allocatedduring run time.
• No wastage of memory
Dynamic arrays
S. Durga Devi , CSE, CBIT
• Dynamic array is also called as resizable array, growable
array , mutable array or arraylist.
• Dynamic array is a random access and variable size list
data structure and enables to add or remove elements.
• The size of the array grows up automatically when we try
to insert new element when there is no space. Array size
will be doubled.
• Dynamic array starts with predefined size and as soon as
array become full, array size would get doubled of the
original size.
• Similarly array size reduced to half when elements in the
array are less then the half.
Dynamic array
S. Durga Devi , CSE, CBIT
Comparision of linked list with arrays and dynamic arrays
S. Durga Devi , CSE, CBIT
Refer: Data structures and algorithms by Narasimha karumunchi
Applications of Linked list
S. Durga Devi , CSE, CBIT
Implement stack and queues
Represents trees and graphs
Keep track of blocks of memory
allocated to a file
Web browser nagivation.
Playing next song in the music player
Applications of doubly linked list
S. Durga Devi , CSE, CBIT
• Represents a deck of cards in a game
• Undo and redo operations in text editors
• A music player which has next and
previous button uses doubly linked list.
• Circular dll and doubly linked list are used
to implement rewind and forward functions
in the playlist.
• web browser forward and back.
Applications of circular ll
S. Durga Devi , CSE, CBIT
• Used in operatingsystem. When multiple applicationsare runningin
PC operatingsystem to put the running applicationson a list and then
to cycle through them, giving each of them a slice of time to execute,
and then making them wait while the CPU is given to another
application.
• To repeat songs in the music player.
• Escalatorwhich will run circularlyuses the circular linked list.
Doubly linked list
 In a singly linked list one can move from the header nodeto any nodein
one direction only (left-right).
 A doublylinked 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 doublylinked list.
 Where, DATA field - stores the element or data, PREV- containsthe
address of its previous node, NEXT- contains the address of its next node.
PREV DATA NEXT
Structure of the node
S. Durga Devi , CSE, CBIT
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
S. Durga Devi , CSE, CBIT
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
S. Durga Devi , CSE, CBIT
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
S. Durga Devi , CSE, CBIT
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
S. Durga Devi , CSE, CBIT
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 insertinga node at position3
S. Durga Devi , CSE, CBIT
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
S. Durga Devi , CSE, CBIT
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
S. Durga Devi , CSE, CBIT
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);
S. Durga Devi , CSE, CBIT
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 position3
2000header
20 20001010 30 20002200 40 NULL202010 2020NULL
1010 2020 1000 2000
1010
ptr2020
ptr
1
ptr1000 ptr2
S. Durga Devi , CSE, CBIT
Displayingelements 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
S. Durga Devi , CSE, CBIT
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
S. Durga Devi , CSE, CBIT
S. Durga Devi , CSE, CBIT
Disadvantage of doubly linked list
Uses extra pointer as it keep track of
previous and next nodes, requires extra
space.
Insertion and deletion operations takes
more time(needs more pointer operations)
Applications of doubly linked list
S. Durga Devi , CSE, CBIT
• Represents a deck of cards in a game
• Undo and redo operations in text editors
• A music player which has next and
previous button uses doubly linked list.
• Circular dll and doubly linked list are used
to implement rewind and forward functions
in the playlist.
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 nodepoints the header nodeis
called circular linked list.
Structure of the circular linked list
S. Durga Devi , CSE, CBIT
S. Durga Devi , CSE, CBIT
Hyderabad ORR one of example for 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.
S. Durga Devi , CSE, CBIT
Applications of cll
S. Durga Devi , CSE, CBIT
• Used in operatingsystem. When multiple applicationsare runningin
PC operatingsystem to put the running applicationson a list and then
to cycle through them, giving each of them a slice of time to execute,
and then making them wait while the CPU is given to another
application.
• To repeat songs in the music player.
• Escalatorwhich will run circularlyuses the circular linked list.
/* 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*/ S. Durga Devi , CSE, CBIT
int enqueue(){
// insert new node at end
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()
{ // delete first node in cll
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*/
S. Durga Devi , CSE, CBIT
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*/
S. Durga Devi , CSE, CBIT
Circular LL
S. Durga Devi , CSE, CBIT
// insert new node as first element
void insert()
{
new=malloc(sizeof(struct node));
printf("nEnter the item: ");
scanf("%d",&item);
new->data=item;
if(first==NULL)
first=new;
else{
new->link=first;}
first=new;
last->link=new;
}
Circular LL
S. Durga Devi , CSE, CBIT
// delete last node
void deletion()
{
if(first==NULL)
print” list is empty”
else
if(first==last)
first=last=NULL
else
{ ptr=first->link;
while(ptr!=last)
{
ptr1=ptr;
ptr=ptr->lin;
}
ptr1->link=first;
last=ptr1;
free(ptr);}
}//deletion
Circular doubly linked list
S. Durga Devi , CSE, CBIT
Applications of circular doubly linked list
• Managing songs playlist in media player
applications
• Managing shopping cart in online shopping
Circular doubly linked list
S. Durga Devi , CSE, CBIT
- Circular doublylinked list last node next pointerpoints to first node address
and first node prev pointerpoints to last node address.
- There is no conceptof NULL pointer.
Applications
Managing songs playlist in media player applications
Managing shoppingcart in onlineshopping
S. Durga Devi , CSE, CBIT
/* Write a c program to implement circular doubly linked list*/
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<stdlib.h>
int choice,i,item;
struct node
{ int data;
struct node*next;
struct node*prev;
}*head,*tail,*new,*ptr1,*ptr;
S. Durga Devi , CSE, CBIT
main()
{ head=tail=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*/
S. Durga Devi , CSE, CBIT
// insertion
int enqueue()
{ // insert new node at last
new=malloc(sizeof(structnode));
printf("nEnterthe item: ");
scanf("%d",&item);
new->data=item;
if(head==NULL)
{new->next=new;
new->prev=new;
head=new;
tail=new;}
else{ new->prev=tail;
new->next=head;
tail->next=new;
head->prev=new;
tail=new;
}
S. Durga Devi , CSE, CBIT
// deletion
dequeue()
{ // delete a first node from list
if(head==NULL)
printf("nThe circular doubly list is empty.");
else
if(head==tail)
{
printf("nThe deleted element is: %d",head->data);
head=tail=NULL;
}
else
{ printf("nThe deleted element is: %d",head->data);
tail->next=head->next;
head->next->prev=tail;
head=head->next;
}
return;
}/*end of dequeue*/
S. Durga Devi , CSE, CBIT
// display
display()
{
ptr=head;
ptr1=NULL;
if(head==NULL)
printf("n circular doubly list emptynn");
else
{
printf("nElements in the list are: ");
while(ptr!=tail)
{
printf(" %d",ptr->data);
ptr=ptr->next;
}/*end of while*/
printf(" %d",ptr->data);
}//else
return;
}/*end of display*/
S. Durga Devi , CSE, CBIT

Weitere ähnliche Inhalte

Was ist angesagt?

Linked list
Linked listLinked list
Linked list
eShikshak
 
Introduction to stack
Introduction to stackIntroduction to stack
Introduction to stack
vaibhav2910
 
Data structure and its types
Data structure and its typesData structure and its types
Data structure and its types
Navtar Sidhu Brar
 

Was ist angesagt? (20)

Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
 
Linked list
Linked listLinked list
Linked list
 
Introduction to data structure ppt
Introduction to data structure pptIntroduction to data structure ppt
Introduction to data structure ppt
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
 
Different Sorting tecniques in Data Structure
Different Sorting tecniques in Data StructureDifferent Sorting tecniques in Data Structure
Different Sorting tecniques in Data Structure
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
single linked list
single linked listsingle linked list
single linked list
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
Introduction to stack
Introduction to stackIntroduction to stack
Introduction to stack
 
Circular link list.ppt
Circular link list.pptCircular link list.ppt
Circular link list.ppt
 
Queue ppt
Queue pptQueue ppt
Queue ppt
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
Stack
StackStack
Stack
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Data Structures- Part7 linked lists
Data Structures- Part7 linked listsData Structures- Part7 linked lists
Data Structures- Part7 linked lists
 
Data structure and its types
Data structure and its typesData structure and its types
Data structure and its types
 
List Data Structure
List Data StructureList Data Structure
List Data Structure
 
Binary search tree operations
Binary search tree operationsBinary search tree operations
Binary search tree operations
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Queue implementation
Queue implementationQueue implementation
Queue implementation
 

Ähnlich wie linked lists in data structures

Notes of bca Question paper for exams and tests
Notes of bca Question paper for exams and testsNotes of bca Question paper for exams and tests
Notes of bca Question paper for exams and tests
priyanshukumar97908
 

Ähnlich wie linked lists in data structures (20)

Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
 
module 3-.pptx
module 3-.pptxmodule 3-.pptx
module 3-.pptx
 
Unit 1_SLL and DLL.pdf
Unit 1_SLL and DLL.pdfUnit 1_SLL and DLL.pdf
Unit 1_SLL and DLL.pdf
 
Linked list (1).pptx
Linked list (1).pptxLinked list (1).pptx
Linked list (1).pptx
 
DS Unit 2.ppt
DS Unit 2.pptDS Unit 2.ppt
DS Unit 2.ppt
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 
Linkedlists
LinkedlistsLinkedlists
Linkedlists
 
Linked List
Linked ListLinked List
Linked List
 
ds-lecture-4-171012041008 (1).pdf
ds-lecture-4-171012041008 (1).pdfds-lecture-4-171012041008 (1).pdf
ds-lecture-4-171012041008 (1).pdf
 
Data Structures_Linked List
Data Structures_Linked ListData Structures_Linked List
Data Structures_Linked List
 
DS Module 1.pptx
DS Module 1.pptxDS Module 1.pptx
DS Module 1.pptx
 
Link list
Link listLink list
Link list
 
CS8391-DATA-STRUCTURES.pdf
CS8391-DATA-STRUCTURES.pdfCS8391-DATA-STRUCTURES.pdf
CS8391-DATA-STRUCTURES.pdf
 
DS Module 1.pptx
DS Module 1.pptxDS Module 1.pptx
DS Module 1.pptx
 
Linked list
Linked listLinked list
Linked list
 
Notes of bca Question paper for exams and tests
Notes of bca Question paper for exams and testsNotes of bca Question paper for exams and tests
Notes of bca Question paper for exams and tests
 
DS Module 03.pdf
DS Module 03.pdfDS Module 03.pdf
DS Module 03.pdf
 
linkedlist (1).ppt
linkedlist (1).pptlinkedlist (1).ppt
linkedlist (1).ppt
 
Linked list using Dynamic Memory Allocation
Linked list using Dynamic Memory AllocationLinked list using Dynamic Memory Allocation
Linked list using Dynamic Memory Allocation
 
Linked list
Linked listLinked list
Linked list
 

KĂźrzlich hochgeladen

Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
KreezheaRecto
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

KĂźrzlich hochgeladen (20)

(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 

linked lists in data structures

  • 1. UNIT-II Topics to be covered  Linked Lists: Introduction Linked lists  Representation of linked list  operations on linked list  Comparison of Linked Lists with Arrays and Dynamic Arrays  Types of Linked Lists and operations-Circular Single Linked List, Double Linked List, Circular Double Linked List S. Durga Devi , CSE, CBIT
  • 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}; S. Durga Devi , CSE, CBIT
  • 3. What’s wrong with Array and Why linked lists?  Disadvantages of arrays as storage data structures: – slow searching in unorderedarray – insertion and deletionoperationsare slow. Because, we have to shift subsequentelements – 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. S. Durga Devi , CSE, CBIT
  • 4. Linked Lists S. Durga Devi , CSE, CBIT
  • 5. Linked list  Linked list is a linear data structure that supportsthe 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 sequenceof 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 nodecontains a data part and one or two pointer part which contains the address of the neighborhoodnodes in the list. Node structure- S. Durga Devi , CSE, CBIT
  • 6. S. Durga Devi , CSE, CBIT
  • 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 4. Circular doubly linked list 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. S. Durga Devi , CSE, CBIT
  • 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. S. Durga Devi , CSE, CBIT
  • 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) S. Durga Devi , CSE, CBIT
  • 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) S. Durga Devi , CSE, CBIT
  • 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 S. Durga Devi , CSE, CBIT
  • 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 S. Durga Devi , CSE, CBIT
  • 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 S. Durga Devi , CSE, CBIT
  • 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 S. Durga Devi , CSE, CBIT
  • 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 positionis : 3 ptr 5 New node 1000 2600 1000 S. Durga Devi , CSE, CBIT
  • 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 S. Durga Devi , CSE, CBIT
  • 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 S. Durga Devi , CSE, CBIT
  • 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 S. Durga Devi , CSE, CBIT
  • 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 S. Durga Devi , CSE, CBIT
  • 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 S. Durga Devi , CSE, CBIT
  • 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; S. Durga Devi , CSE, CBIT
  • 22. SLL program int 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 S. Durga Devi , CSE, CBIT
  • 23. //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 S. Durga Devi , CSE, CBIT
  • 24. //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; }}} S. Durga Devi , CSE, CBIT
  • 25. 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 S. Durga Devi , CSE, CBIT
  • 26. Comparison of linked list and array S. Durga Devi , CSE, CBIT • Linked list and arrays are used to store collection of data. Both purpose is same but differ in their usage. • Arrays: - 1. to access an array element, the address of an element computed by using base address and multiply position with element size then added to base address. Hence, it takes constant time to access an array element. - 2. the size of the array is fixed - 3. random access is possible. - 4. inserting new element is expensive as it takes shifting operation. - 5. takes less space to store elements - 6. memory allocated during compile time. - 7. wastage of memory is more.
  • 27. Linked list S. Durga Devi , CSE, CBIT • Accessing linked list elements are slower as it takes time proportional to i to find the i-th member of a linked list by skipping over the first i−1 cells. • Dynamic size. • Insertion and deletion operationsare easy. • No possibility of randomaccessing. • Extra space is required as it points to the next element. • Memory allocatedduring run time. • No wastage of memory
  • 28. Dynamic arrays S. Durga Devi , CSE, CBIT • Dynamic array is also called as resizable array, growable array , mutable array or arraylist. • Dynamic array is a random access and variable size list data structure and enables to add or remove elements. • The size of the array grows up automatically when we try to insert new element when there is no space. Array size will be doubled. • Dynamic array starts with predefined size and as soon as array become full, array size would get doubled of the original size. • Similarly array size reduced to half when elements in the array are less then the half.
  • 29. Dynamic array S. Durga Devi , CSE, CBIT
  • 30. Comparision of linked list with arrays and dynamic arrays S. Durga Devi , CSE, CBIT Refer: Data structures and algorithms by Narasimha karumunchi
  • 31. Applications of Linked list S. Durga Devi , CSE, CBIT Implement stack and queues Represents trees and graphs Keep track of blocks of memory allocated to a file Web browser nagivation. Playing next song in the music player
  • 32. Applications of doubly linked list S. Durga Devi , CSE, CBIT • Represents a deck of cards in a game • Undo and redo operations in text editors • A music player which has next and previous button uses doubly linked list. • Circular dll and doubly linked list are used to implement rewind and forward functions in the playlist. • web browser forward and back.
  • 33. Applications of circular ll S. Durga Devi , CSE, CBIT • Used in operatingsystem. When multiple applicationsare runningin PC operatingsystem to put the running applicationson a list and then to cycle through them, giving each of them a slice of time to execute, and then making them wait while the CPU is given to another application. • To repeat songs in the music player. • Escalatorwhich will run circularlyuses the circular linked list.
  • 34. Doubly linked list  In a singly linked list one can move from the header nodeto any nodein one direction only (left-right).  A doublylinked 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 doublylinked list.  Where, DATA field - stores the element or data, PREV- containsthe address of its previous node, NEXT- contains the address of its next node. PREV DATA NEXT Structure of the node S. Durga Devi , CSE, CBIT
  • 35. 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 S. Durga Devi , CSE, CBIT
  • 36. 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 S. Durga Devi , CSE, CBIT
  • 37. 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 S. Durga Devi , CSE, CBIT
  • 38. 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 S. Durga Devi , CSE, CBIT
  • 39. 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 insertinga node at position3 S. Durga Devi , CSE, CBIT
  • 40. 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 S. Durga Devi , CSE, CBIT
  • 41. 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 S. Durga Devi , CSE, CBIT
  • 42. 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); S. Durga Devi , CSE, CBIT
  • 43. 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 position3 2000header 20 20001010 30 20002200 40 NULL202010 2020NULL 1010 2020 1000 2000 1010 ptr2020 ptr 1 ptr1000 ptr2 S. Durga Devi , CSE, CBIT
  • 44. Displayingelements 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 S. Durga Devi , CSE, CBIT
  • 45. 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 S. Durga Devi , CSE, CBIT
  • 46. S. Durga Devi , CSE, CBIT Disadvantage of doubly linked list Uses extra pointer as it keep track of previous and next nodes, requires extra space. Insertion and deletion operations takes more time(needs more pointer operations)
  • 47. Applications of doubly linked list S. Durga Devi , CSE, CBIT • Represents a deck of cards in a game • Undo and redo operations in text editors • A music player which has next and previous button uses doubly linked list. • Circular dll and doubly linked list are used to implement rewind and forward functions in the playlist.
  • 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 nodepoints the header nodeis called circular linked list. Structure of the circular linked list S. Durga Devi , CSE, CBIT
  • 49. S. Durga Devi , CSE, CBIT Hyderabad ORR one of example for the Circular linked list
  • 50. 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. S. Durga Devi , CSE, CBIT
  • 51. Applications of cll S. Durga Devi , CSE, CBIT • Used in operatingsystem. When multiple applicationsare runningin PC operatingsystem to put the running applicationson a list and then to cycle through them, giving each of them a slice of time to execute, and then making them wait while the CPU is given to another application. • To repeat songs in the music player. • Escalatorwhich will run circularlyuses the circular linked list.
  • 52. /* 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*/ S. Durga Devi , CSE, CBIT
  • 53. int enqueue(){ // insert new node at end 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() { // delete first node in cll 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*/ S. Durga Devi , CSE, CBIT
  • 54. 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*/ S. Durga Devi , CSE, CBIT
  • 55. Circular LL S. Durga Devi , CSE, CBIT // insert new node as first element void insert() { new=malloc(sizeof(struct node)); printf("nEnter the item: "); scanf("%d",&item); new->data=item; if(first==NULL) first=new; else{ new->link=first;} first=new; last->link=new; }
  • 56. Circular LL S. Durga Devi , CSE, CBIT // delete last node void deletion() { if(first==NULL) print” list is empty” else if(first==last) first=last=NULL else { ptr=first->link; while(ptr!=last) { ptr1=ptr; ptr=ptr->lin; } ptr1->link=first; last=ptr1; free(ptr);} }//deletion
  • 57. Circular doubly linked list S. Durga Devi , CSE, CBIT Applications of circular doubly linked list • Managing songs playlist in media player applications • Managing shopping cart in online shopping
  • 58. Circular doubly linked list S. Durga Devi , CSE, CBIT - Circular doublylinked list last node next pointerpoints to first node address and first node prev pointerpoints to last node address. - There is no conceptof NULL pointer. Applications Managing songs playlist in media player applications Managing shoppingcart in onlineshopping
  • 59. S. Durga Devi , CSE, CBIT /* Write a c program to implement circular doubly linked list*/ #include<stdio.h> #include<conio.h> #include<malloc.h> #include<stdlib.h> int choice,i,item; struct node { int data; struct node*next; struct node*prev; }*head,*tail,*new,*ptr1,*ptr;
  • 60. S. Durga Devi , CSE, CBIT main() { head=tail=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*/
  • 61. S. Durga Devi , CSE, CBIT // insertion int enqueue() { // insert new node at last new=malloc(sizeof(structnode)); printf("nEnterthe item: "); scanf("%d",&item); new->data=item; if(head==NULL) {new->next=new; new->prev=new; head=new; tail=new;} else{ new->prev=tail; new->next=head; tail->next=new; head->prev=new; tail=new; }
  • 62. S. Durga Devi , CSE, CBIT // deletion dequeue() { // delete a first node from list if(head==NULL) printf("nThe circular doubly list is empty."); else if(head==tail) { printf("nThe deleted element is: %d",head->data); head=tail=NULL; } else { printf("nThe deleted element is: %d",head->data); tail->next=head->next; head->next->prev=tail; head=head->next; } return; }/*end of dequeue*/
  • 63. S. Durga Devi , CSE, CBIT // display display() { ptr=head; ptr1=NULL; if(head==NULL) printf("n circular doubly list emptynn"); else { printf("nElements in the list are: "); while(ptr!=tail) { printf(" %d",ptr->data); ptr=ptr->next; }/*end of while*/ printf(" %d",ptr->data); }//else return; }/*end of display*/
  • 64. S. Durga Devi , CSE, CBIT