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

Data Structures - UNIT 2

1) Linked lists are a linear collection of data nodes where each node contains a data field and a pointer to the next node. The last node points to NULL. 2) Linked lists allow for efficient memory usage and easier insertion/deletion compared to arrays. They can also grow/shrink dynamically and access memory arbitrarily. 3) The document describes different types of linked lists including singly linked lists and provides examples of linked list creation, traversal, and insertion operations.

Uploaded by

vishwa.tdm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
133 views

Data Structures - UNIT 2

1) Linked lists are a linear collection of data nodes where each node contains a data field and a pointer to the next node. The last node points to NULL. 2) Linked lists allow for efficient memory usage and easier insertion/deletion compared to arrays. They can also grow/shrink dynamically and access memory arbitrarily. 3) The document describes different types of linked lists including singly linked lists and provides examples of linked list creation, traversal, and insertion operations.

Uploaded by

vishwa.tdm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

UNIT 2 Data Structures Using C 1

Chapter 2
Linked List
Linked list is a linear collection of data elements called nodes. Each node is divided into 2
fields. The first part is for info or data field and second part for pointer or link field to the
next node. The general representation is

INFO LINK
Where INFO is used to store the data value, LINK is used to store the address of next node.
Example:
START
5 8 3 NULL

Here START is an external pointer it stores the address of the first node. NULL indicates it is
the end of linked list.
Advantages of Linked List

• Efficient memory utilization


• Insertion and Deletion operation are easier
• Dynamic Data Structure
• Extensive manipulation
• Arbitrary memory location
• Efficient memory utilization
Memory can be allocated when ever we are inserting a new node and we deallocate when it is
deleted.
• Insertion and Deletion operation are easier
Linked List flexibility in inserting a data item at a specified position and Deletion of a data
item from a given position.

• Dynamic Data Structure


Linked List can grow or shrink during the execution of program.
• Extensive manipulation
We perform a complex manipulation a Linked Link without having any prior knowledge of
memory space available.

• Arbitrary memory location


In Linked List memory location are not consecutive, it may arbitrary. But the data is linked
with previous node. So, we can store the data in affecting memory effectively by using the link
value in Linked List.
Disadvantages of Linked List
1. It requires more memory than array because each node has a pointer, which requires
more memory.

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 2

2. Difficult to traverse the nodes in a linked list.


3. Reverse traversing in a linked list is very difficult, because it requires more memory for
the pointer.
Representation of Linked list in memory
List can be represented in 2 ways.
• Static representation
• Dynamic representation
Static Representation
Static representation of singly linked list maintains 2 arrays
1) INFO[] 2) LINK[]
Example
START
INFO[] LINK[]
1 A 3
2
3 B 5
4
5 C NULL
Here, INFO[1]=A, LINK[1]=3 ,
INFO[3]=B, LINK[3]=5 and
INFO[5]=C, LINK[5]=NULL.
i) The memory size of those 2 arrays should be sufficient to store the entire linked list
ii) Two arrays should be in equal size and parallel to each other.
Dynamic Representation
The process of allocating memory at runtime is known as dynamic memory allocation.
Functions for allocating memory:
i) malloc() ii) calloc()
Functions for deallocating memory:
i) free()
In dynamic representation of Linked List, a node is created dynamically whenever it required.
To create a node of Linked List, a structure is to be defined and it holds the data and address
of the next structure data.

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 3

struct node
{
int info;
struct node *link;
};
It is a self-referential structure in C. Here link is the pointer that points the next data of same
structure.
Types of Linked List
Linked List are classified into 5 types:

• Singly Linked List


• Circular Linked List
• Doubly Linked List
• Header Linked List
• Circular Doubly Linked List
Singly Linked List
A singly linked list is a linear collection of data elements called nodes, where each node is
divided into 2 fields: Info field and link field. The link field points the next node and the link
field of last node points NULL.
Example:
START
10 58 24 NULL

Operations on Singly Linked List


• Creation
• Traversal
• Length
• Insertion
• Deletion
• Searching
• Sorting
• Merging
• Reversing
Creation
This operation creates a linked list. The following algorithm creates the Linked List
Algorithm
1. Create a node and assign the address to start pointer
CURNODE=(NODE*) malloc(sizeof(NODE));
START=CURNODE
2. Store the data
CURNODE→INFO=item;

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 4

3. Store null character in link field


CURNODE→LINK=NULL
4. Return
Explanation
The first step is to create a node with 2 fields and assign the address of the node to start
START

INFO LINK
The second step get the data item and place in INFO field
START
50
INFO LINK
The third step place NULL value in Link fields
START
50 NULL
INFO LINK
The fourth step will return to main program.

Traversal
The process of visiting all nodes in Linked List is known as traversing. The following algorithm
traverse the Linked List
Algorithm
1. Is list is empty?
If(START==NULL)
Display “Linked list is empty”
Return
End if
2. Assign start value to CURPTR
CURPTR=start
3. Repeat while (CURPTR != NULL)
Print CURPTR→INFO
CURPTR=CURPTR→LINK
4. Return
Example
Consider a Linked List with 3 nodes are as shown below:
START
2002 2002 2015 198
10 58 24 NULL

The first step is to check START is NULL. Here START=2002

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 5

The second step is to assign START value to CURPTR=START ie., CURPTR=2002


The third step it checks CURPTR is NULL, if it is not then it print the INFO value 10.
Repeatedly until CURPTR become NULL.
The following code illustrates the Linked List traversal
void display()
{
struct node *ptr;
if(start==NULL)
{
printf("\nList is empty\n");
return;
}
else
{
ptr=start;
printf("\nThe List elements are:");
while(ptr!=NULL)
{
printf("%dt",ptr->info );
ptr=ptr->next ;
}
}
}

Insertion
This operation is used to insert a new node in the Linked List. The new node can be inserted in
3 ways:
1) Insert at the beginning of a Linked List
2) Insert at the end of a Linked List
3) Insert at the specified position in Linked List
1) Insert at the beginning of a Linked List
The following algorithm insert a node at the beginning.
Algorithm:
1. Create a node using malloc
NEWNODE=(NODE*) malloc(sizeof (NODE))
2. Store the data
NEWNODE→INFO=ITEM
3. Assign the start value to the link of NEWNODE
NEWNODE→LINK=start
4. Assign NEWNODE as START
Start=NEWNODE
5. Exit
Explanation
Consider the following linked list contain 3 nodes

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 6

START
2010 2010 215 1003
10 90 24 NULL

The first step creates a node with two fields

2025

NEWNODE=2025
The second step stores data in INFO field

2025
50
The third step assign NEWNODE link to START

2025
50 2010

START
2010 2010 215 1003
10 90 24 NULL

The fourth step to assign START as NEWNODE.


START
2025 2025 2010 215 1003
50 10 90 24 NULL

The following code illustrates inserting a node in the beginning of the Linked List
void insert_begin()
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nOut of Memory Space\n");
return;
}
printf("\nEnter the data value for the node:" );
scanf("%d",&temp->info);
temp->next =NULL;
if(start==NULL)
{
start=temp;
}
else

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 7

{
temp->next=start;
start=temp;
}
}

2) Insert at the end of a Linked List


The following algorithm insert a node at the end of Linked List
Algorithm:
1. Is List is empty?
if(Start = = NULL)
Call insert at the beginning
Exit()
2. Assign the Start to CURPTR
CURPTR=Start
3. Traverse the List to obtain Last node address
while(CURPTR → LINK != NULL)
CURPTR=CURPTR → LINK
4. Create a node using malloc
NEWNODE=(NODE*) malloc(sizeof (NODE))
5. Store the data
NEWNODE→INFO=ITEM
6. Create a link between Last Node and NEWNODE
CURPTR → LINK = NEWNODE
NEWNODE→LINK=NULL
7. Exit
Explanation
Consider the following linked list contain 3 nodes
START
2010 2010 215 1003
10 90 24 NULL

The first step check START is NULL, if it is NULL it call Insert at beginning otherwise
it assign Start value to CURPTR
The third step is traverse to point the last node
START
2010 2010 215 1003
10 90 24 NULL

In fourth step creates a node with two fields

2025

NEWNODE=2025
The fifth step stores data in INFO field

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 8

2025
50
The sixth step assign NEWNODE link to CURPTR → LINK and NEWNODE → LINK
= NULL. The pictorial representation is

START
2010 2010 215 1003 2025
10 90 24 50 NULL

The following code illustrates inserting a node at the end of the Linked List
void insert_end()
{
struct node *temp,*ptr;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nOut of Memory Space\n");
return;
}
printf("\nEnter the data value for the node:" );
scanf("%d",&temp->info );
temp->next =NULL;
if(start==NULL)
{
start=temp;
}
else
{
ptr=start;
while(ptr->next !=NULL)
{
ptr=ptr->next ;
}
ptr->next =temp;
}
}

3) Insert at the specified position in Linked List


The following algorithm insert a node a specified position in Linked List
Algorithm
1. Assign Start to CURPTR
CURPTR=Start
2. Check position if( POS == 1)
Call insert at beginning
Exit()

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 9

3. Set the CURPTR to specified position


for ( i=0; i<pos-2;i++ )
CURPTR=CURPTR → LINK
4. Create a new node using malloc()
NEWNODE=(NODE*) malloc(sizeof(NODE))
5. Store data into NEWNODE → INFO
NEWNODE → INFO = ITEM
6. Create the link between NEWNODE and CURPTR
NEWNODE → LINK =CURPTR → LINK
CURPTR → LINK = NEWNODE
7. Exit
Explanation
Consider the following linked list contain 3 nodes
START
2010 2010 215 1003
10 90 24 NULL

In first step it assign Start value to CURPTR.


The second step it check position value if it is one it call insert at beginning()
In third step it set CURPTR to specified position.
In fourth step it creates new node

2025

NEWNODE=2025
In fifth step it assigns INFO Value to NEWNODE

2025
50
In sixth step it creates link between CURPTR and NEWNODE. Consider pos is 2.The
pictorial representation is as follows.
START
2010 2010 2025 215 1003
10 50 90 24 NULL

The following code illustrates inserting a node at a specific position of the Linked List
void insert_pos()
{
struct node *ptr,*temp;
int i,pos;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 10

printf("\nOut of Memory Space\n");


return;
}
printf("\nEnter the position for the new node to be inserted:");
scanf("%d",&pos);
printf("\nEnter the data value of the node:");
scanf("%d",&temp->info) ;

temp->next=NULL;
if(pos==0)
{
temp->next=start;
start=temp;
}
else
{
for(i=0,ptr=start;i<pos-1;i++)
{
ptr=ptr->next;
if(ptr==NULL)
{
printf("\nPosition not found\n");
return;
}
}
temp->next =ptr->next ;
ptr->next=temp;
}
}

DELETION
This operation is used to delete a node from the Linked List. A node can be deleted using three
ways:
1) Delete a node from the beginning of a Linked List
2) Delete a node from end of a Linked List
3) Delete a node from a specified position in Linked List
1) Delete a node from the beginning of a Linked List
This operation deletes a node from the beginning of Linked List.
Algorithm
1. Is List is Empty?
if (START= = NULL)
print “List is Empty”
else
go to step 2
2. Assign Start value to CURPTR
CURPTR = Start
3. Assign Link field of CURPTR to Start

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 11

Start= CURPTR → LINK


4. Using free() delete CURPTR
free(CURPTR)
5. Exit
Explanation
Consider a Linked List with 3 nodes are as shown below:
START
2002 2002 2015 198
10 58 24 NULL

The first step check Start is NULL. If it is print list is empty otherwise step2
In second step assign CURPTR by Start (Here Start = 2002 therefore, CURPTR= 2002)
In third step set the CURPTR → LINK to Start
In fourth step free the CURPTR
The pictorial representation after deletion is
START
2015 2015 198
58 24 NULL

The following code illustrates delete a node from the beginning of the Linked List
void delete_begin()
{
struct node *ptr;
if(ptr==NULL)
{
printf("\nList is Empty\n");
return;
}
else
{
ptr=start;
start=start->next ;
printf("\nThe deleted element is : %d",ptr->info);
free(ptr);
}
}

2) Delete a node from end of a Linked List


This operation delete the node from end of Linked List
Algorithm
1. Is List is empty?
if( Start = = NULL)
print (“List is Empty”)
else
step 2
2. Check list contain one element

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 12

if (Start → LINK = = NULL) then


Start = NULL
else
Step 4
3. delete start node
free (Start)
4. assign Start value to CURPTR
CURPTR = Start
5. assign NULL to PREPTR
PREPTR= NULL
6. Traverse the Linked List until the CURPTR pointer points the last node.
while(CURPTR → LINK != NULL)
{
PREPTR = CURPTR
CURPTR= CURPTR → LINK
}
7. Delete CURPTR
free (CURPTR)
8. Set PREPTR → LINK = NULL
9. Exit
Explanation
Consider a Linked List with 3 nodes are as shown below:
START
2002 2002 2015 198
10 58 24 NULL

The first step check Start is NULL. If it is print list is empty otherwise step2
In second step it check is list contain one element if it is set Start is NULL otherwise
step4
In third step delete start
In fourth step Assign start to CURPTR
In fifth step set PREPTR to NULL
In sixth step traverse the list until CURPTR points the last node.
In seventh step delete CURPTR
In eight step set PREPTR → LINK as NULL
START
2002 2002 2015
10 58 NULL

The following code illustrates delete a node from end of the Linked List

void delete_end()
{
struct node *temp,*ptr;
if(start==NULL)

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 13

{
printf("\nList is Empty\n");
exit(0);
}
else if(start->next ==NULL)
{
ptr=start;
start=NULL;
printf("\nThe deleted element is: %d",ptr->info);
free(ptr);
}
else
{
ptr=start;
while(ptr->next!=NULL)
{
temp=ptr;
ptr=ptr->next;
}
temp->next=NULL;
printf("\nThe deleted element is: %d",ptr->info);
free(ptr);
}
}

3) Delete a node from a specified position in Linked List


This operation delete the node at a given position.
Algorithm
1. if (POS = = 1)
call delete_beginning()
else
goto step 2
2. set CURPTR to Start
CURPTR=Start
3. set PREVPTR to NULL
PREVPTR = NULL
4. set CURPTR to specified position using loop
for(i=1; i<POS; i++)
{
PREVPTR=CURPTR
CURPTR=CURPTR → LINK
}
5. create link between PREVPTR and the nextnode of CURPTR
PREVPTR → LINK = CURPTR → LINK
free(CURPTR)
6. Exit

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 14

Explanation
Consider a Linked List with 3 nodes are as shown below:
START
2002 2002 2015 198
10 58 24 NULL

The first step check POS= = 1 if it call delete_beginning otherwise goto step2
In second step Assign start to CURPTR
In third step set PREPTR to NULL
In fourth step CURPTR as specified position using loop.
In fifth step set link between PREPTR and next node of CURPTR. Then delete the
CURPTR.

In seventh step delete CURPTR


In eighth step set PREPTR → LINK as NULL
START
2002 2002 198
10 58 NULL

The following code illustrates delete a node from a specific position of the Linked List
void delete_pos()
{
int i,pos;
struct node *temp,*ptr;
if(start==NULL)
{
printf("\nThe List is Empty\n");
exit(0);
}
else
{
printf("\nEnter the position of the node to be deleted:");
scanf("%d",&pos);
if(pos==0)
{
ptr=start;
start=start->next ;
printf("\nThe deleted element is: %d",ptr->info );
free(ptr);
}
else
{
ptr=start;
for(i=0;i<pos;i++)
{
temp=ptr;
ptr=ptr->next ;

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 15

if(ptr==NULL)
{
printf("\nPosition not Found\n");
return;
}
}
temp->next =ptr->next ;
printf("\nThe deleted element is:%d",ptr->info );
free(ptr);
}
}
}

Length
This operation is used to find the number of nodes in Linked List
Algorithm
1. Initialize len=0
2. If (Start = = NULL)
{
Print “List is Empty”
Return
}
3. Set CURPTR=Start
4. while(CURPTR != NULL)
{
len++
CURPTR= CURPTR → LINK
}
5. return len
Explanation
Consider a Linked List with the following nodes:
START
2002 2002 2015 198
10 58 24 NULL
In the first step it initializes the variable len as 0
In the second step it checks whether the Start is NULL , Here Start =2002
In the third step it assigns the Start value to CURPTR that is CURPTR =2002
In the fourth step it checks CURPTR is NULL if it is not then it increment Len value by 1
and set CURPTR= CURPTR → LINK. Repeat until CURPTR is NULL
In fifth step it returns len value , Here len=3
Searching

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 16

It is a process of searching an element in a linked list.


Algorithm
1. Assign CURPTR= start, LOC = NULL
2. Repeat step 3 while (CURPTR != NULL)
3. If(ITEM = = CURPTR → INFO)
{
LOC= CURPTR
Print “Search Successful”
}
Else
CURPTR= CURPTR → LINK
4. If (LOC = = NULL)
Print “Search Unsuccessful”
5. Exit

Consider a Linked List with the following nodes and it search 58


START
2002 2002 2015 198
10 58 24 NULL

In the first step it assigns CURPTR= Start and LOC =NULL


In the second step it checks CURPTR is NULL if not it repeat step 3
In the third step it checks ITEM = = CURPTR → INFO if so assign CURPTR to LOC and
display “Successful” otherwise CURPTR = CURPTR → LINK
In the fourth step, if LOC is NULL then display “Unsuccessful”
In the fifth step exit

Circular Linked List


Circular linked list is similar as singly linked list except that the last node always points the
first node instead of null.
Consider the following pictorial representation shows circular linked list.

2002 2015 198


10 58 24

In circular linked list, only one pointer is used for insertion and deletion.
Circular linked list contains the following operations

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 17

i. Creation
ii. Insertion
iii. Deletion
iv. Traversal
v. Searching
vi. Sorting
The following program creates a node in circular linked list and display its data.
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
struct node
{
int INFO;
struct node *LINK;
};
typedef struct node NODE;
NODE *last=NULL;

void create_clist()
{
char ch;
int i=0;
NODE *NEWNODE;
NEWNODE=(NODE*)malloc(sizeof(NODE));
last=NEWNODE;
NEWNODE →LINK=last;
while(1)
{
printf("\n Enter the node %d",i+1);
scanf("%d",&NEWNODE→INFO);
printf("Do you wish to add one more node (Y/N): \n");
ch=getche();
if(toupper(ch)=='Y')
{
NEWNODE=(NODE*) malloc(sizeof(NODE));
NEWNODE→LINK=last→LINK;
last→LINK=NEWNODE;
last=NEWNODE;
i++;
}
else
break;
}

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 18

}
void display()
{
NODE *CURPTR;
if(last= = NULL)
{
printf("List is Empty \n");
return;
}
CURPTR=last→LINK;
printf("\n");
while(CURPTR!=last)
{
printf("%d",CURPTR→INFO);
printf("→");
CURPTR=CURPTR→LINK;
}
printf("%d",last→INFO);
}
void main()
{
clrscr();
printf("\nCircular Linked List Demonstration\n");
create_clist();
display();
getch();
}

OUTPUT
Circular Linked List Demonstration
Enter the node 1
50
Do you wish to add one more node (Y/N): Y
Enter the node 2
45
Do you wish to add one more node (Y/N): Y
Enter the node 3
67
Do you wish to add one more node (Y/N): N

50-->45-->67

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 19

Advantages of circular linked list


1. It can be traversed from any node
2. All the nodes link address will have valid address instead NULL
3. Node can be inserted or deleted from any position of the linked list
4. Traverse the whole list from any node
Disadvantages of circular linked list
1. Difficult to reverse the circular linked list.
2. If proper care is not taken, then the problem of infinite loop can occur
Doubly Linked List
A doubly linked list is the kind of linked list, in which a node has three fields:

• INFO
• BACK
• FORW
The INFO field stores the data and the BACK field stores the address of the previous node and
the FORW field stores the address of the next node.
The node in doubly linked list is represented below:

BACK INFO FORW


The following code declare the doubly linked list using dynamic implementation.
struct node
{
int INFO;
struct node *BACK;
struct node *FORW;
};
The pictorial representation of doubly linked list is illustrated below.
START
1800 1800 4020 5042
NULL 24 4200 1800 67 5042 4020 80 NULL
The following program creates a node in doubly linked list and display its data.
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
struct node
{
int INFO;
struct node *BACK;
struct node *FORW;
};
typedef struct node NODE;

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 20

NODE *start=NULL;

void create_dlist()
{
char ch;
int i=0;
NODE *NEWNODE, *CURPTR;
CURPTR=(NODE*)malloc(sizeof(NODE));
start=CURPTR;
CURPTR →FORW=NULL;
CURPTR →BACK=NULL;
while(1)
{
printf("\n Enter the node %d",i+1);
scanf("%d",&CURPTR→INFO);
printf("Do you wish to add one more node (Y/N): \n");
ch=getche();
if(toupper(ch)=='Y')
{
NEWNODE=(NODE*) malloc(sizeof(NODE));
CURPTR→FORW=NEWNODE;
NEWNODE→BACK=CURPTR;
NEWNODE→FORW=NULL;
CURPTR=NEWNODE;
}
else
{
CURPTR →FORW=NULL;
break;
}
i++;
}
}

void display()
{
NODE *CURPTR =start;
if(start= = NULL)
printf("List is Empty \n");
else
{
printf("\n");
while(CURPTR!=NULL)
{

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 21

printf("%d",CURPTR→INFO);
printf("→");
CURPTR=CURPTR→FORW;
}
printf("NULL");
}
}

void main()
{
clrscr();
printf("\nDoubly Linked List Demonstration\n");
create_dlist();
display();
getch();
}

OUTPUT
Doubly Linked List Demonstration
Enter the node 1
50
Do you wish to add one more node (Y/N): Y
Enter the node 2
45
Do you wish to add one more node (Y/N): Y
Enter the node 3
67
Do you wish to add one more node (Y/N): N

50→45→67→NULL
Advantages of Doubly Linked List
1. It can be traversed in forward and backward direction
2. Insertion and deletion are easy because every node has forward and backward link
3. It is used to represent the trees effectively
4. This simplifies list management
Disadvantages of Doubly Linked List
1. Extra memory is required to store the back pointer.
2. Extra effort is required to deal with the extra link.
Header Linked List
• A special node in the beginning of the list is known as header node.
• This node does not contain actual data but contains useful information about the entire
linked list such as total number of nodes, pointer to the last node etc.

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 22

• The start points to the header node in the header linked list.
• Link field of header node points the first node in the list.
• The header linked list will always contain a header node even if list is empty.
Header linked list are of two types:

• Grounded header linked list


• Circular header linked list
Grounded header linked list
The grounded header linked list is also referred as singly header linked list. In this the last node
LINK field contains the NULL pointer.
Example
START
2002 2002 2015 198
58 24 NULL
Header node

Circular header linked list


A linked list in which last node points to the header node is called circular header linked list.
Example

Head Node 2015 198


58 24

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 23

STACK
Definition
Stack is a collection of homogeneous data items where the insertion and deletion operations
take place at one end called TOP of the stack. Stack works on LIFO (Last In First Out) Fashion

5 TOP
8
1

StackADT
A set of stack data member and a set of stack operations that perform on stack data. The
stack data members are TOP, MAXSTK. The stack operations such as
1. Createstack()- Create stack with n elements.
2. push()- push an element on to the TOP of the stack
3. pop()- POP an element from the TOP of the stack.
4. Isempty()- returns true if the stack is empty otherwise false
5. Traverse()- accessing all the elements in the stack.
6. Count()- count the number of elements in the stack.
PUSH operation
It is used to insert an element on the TOP of the stack. If stack is full, PUSH operation is not
possible.
Algorithm: PUSH(S,TOP, MAXSTK, item)
1. check Stack is overflow
if TOP= = MAXSTK - 1 then
print "Stack Overflow"
return
2. Increment TOP value
TOP=TOP+1
3. Insert the item at the TOP
S[TOP]=item
4. return
Explanation
Consider the following stack contain 3 element and its MAXSTK value is 5

5 TOP
8
1

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 24

1. Check stack overflow using TOP= = MAXSTK -1, here it is false


2. Increment TOP value by TOP=TOP+1

TOP
5
8
1

3. Insert item at the TOP using S[TOP]=item

10 TOP
5
8
1

4. return
POP operation
This operation deletes an element from the stack i.e., it deletes the TOP element from the stack.
If stack is empty the pop operation is not possible.
Algorithm: POP(S,TOP, item)
1. Check underflow
if TOP== -1 then
print "stack underflow"
return
2. Delete the item
item=S[TOP]
3. Decrement TOP value
TOP=TOP-1
4. return
Explanation
Consider a stack contain 3 elements and its MAXSTK value is 5.

5 TOP
8
1
1. Check stack is underflow or not, here TOP=2and MAXSTK=5. Therefore, go to step 2

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 25

2. Delete the TOP item

TOP
8
1

3. Decrement TOP value by 1

8 TOP
1
4. Return

Traverse operation
Accessing all elements in the stack is known as traversal operation.
Algorithm: DISPLAY(S, TOP)
1. Check underflow
if TOP== -1 then
print "stack underflow"
return
2. Access all elements using loop
for i=TOP to 0
print S[i]
end for
3. return
Explanation
Consider a stack contain 3 elements and its MAXSTK value is 5.

5 TOP
8
1

1. Check stack is underflow or not, here TOP=2and MAXSTK=5. Therefore, go to step 2


2. Access the TOP of the element and print using Loop 5 8 1
3. Return
Application of stack
• Recursion
• Reversal of a string
• Checking the parenthesis matching

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 26

• Postfix expression evaluation


• Infix to postfix conversion
• Infix to prefix conversion
RECURSION
It is a process of calling the function repeatedly itself. The repeated calling is terminated by a
specific condition.
Types
• Direct
• Indirect
Direct Recursion
This type calls itself repeatedly until certain condition is satisfied
Example
fact(int n)
{
if(n==0)
return 1;
else
return (n* fact(n-1));
}
Indirect Recursion
This type calls another function which eventually calls the same function repeatedly
Example
fun1 (int a)
{
fun2( b);
}
fun2 (int c)
{
fun1(x);
}

Advantages of Recursion:
1. Reduces the complexity of the problem
2. It allows user to write simple and elegant program
3. Programs are smaller in length
4. Programs can have any no. of nesting levels
5. Top-Down Programming model
Recursion : Example 1 - Finding factorial using Recursive method

The factorial is defined as the product of all integers between n and 1.


The recursive description can be expressed as:

1 𝑖𝑓 𝑁 = 0
FACTORIAL(N) = { }
𝑁 ∗ 𝐹𝐴𝐶𝑇𝑂𝑅𝐼𝐴𝐿 (𝑁 − 1) 𝑖𝑓 𝑂𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 27

Algorithm: FACTORIAL(N)
Step 1: if (N= = 0) then
fact=1
Step 2: else
Fact=N * FACTORIAL (N-1)
End if
Step 3: return(fact)
Step 4: Stop
Consider the following represents 4! Using recursive function
4!=4*3!
3!=3*2!
2!=2*1!
1!=1*0!
1!=1
2!=2
3!=6

4!=24
The whole process is implemented in the stack as shown below

Recursion : Example 2 - Fibonacci Series


Fibonacci series is a peculiar series of integers where each element in the series is the sum of
the two preceding elements. Eg: 0 1 1 2 3 5 8 13 21 ……………
The general Fibonacci series can be represented as

0 𝑖𝑓 𝑁 = 0 𝑜𝑟 𝑁 = 1
Fib(N)={ 1 𝑖𝑓 𝑁 = 2 }
𝑓𝑖𝑏(𝑁 − 1) + 𝑓𝑖𝑏(𝑁 − 2) 𝑖𝑓 𝑁 > 2

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 28

Algorithm: fibon(N)
if(N==0) || (N==1) then
fib=0
else if (N==2) then
fib=1
else if (N>2) then
fib=fibon(N-1) +fibon(N-2)
end if
return fib
stop
Algorithm Steps
fibon(6)=fibon(5)+fibon(4)
fibon(5)=fibon(4)+fibon(3)
fibon(4)=fibon(3)+fibon(2)
fibon(3)=fibon(2)+fibon(1)
fibon(2)=1, fibon(1)=0
fibon(3)=1
fibon(4)=2
fibon(5)=
3
fibon(6)=5

The following represents the Fibonacci recursive call illustration.

Recursion : Example 3 - Tower of Hanoi Problem


Tower of Hanoi problem is a game where a disk is to be moved from one source pillar to a
destination pillar using a temporary pillar. To solve this problem, we have to follow three
conditions. They are:
• Only one disk can be moved at one time, from one pillar to another
• A larger disk cannot be placed on the smaller disk
• Only the top disc on any pillar may be moved to any other pillar

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 29

Algorithm: move(n, S, T, D)
{
move(n-1, S, D, T) //move (n-1) disks from S to T
move(1, S, T, D) //move 1 disk from S to D
move(n-1, T, S, D) //move (n-1) disks from T to D
}
Consider the Tower of Hanoi has three disks (n=3)

The solution is represented in the following figure:

Hence the sequence for n=3 is


S→D S→T D→T S→D T→S T→D S→D

Recursion : Example 4 - Greatest Common Divisor (GCD)


Greatest Common Divisor problem can be solved using recursion
The recursive description can be expressed as:

𝐺𝐶𝐷(𝑛, 𝑚), 𝑖𝑓 𝑛 > 𝑚


GCD(𝑚, 𝑛) = { 𝑚, 𝑖𝑓 𝑛 = 0 }
𝐺𝐶𝐷(𝑛, 𝑀𝑂𝐷(𝑚, 𝑛)), 𝑂𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 30

The steps during this process can be represented as follows:

GCD(288, 108)= GCD(108,72)


GCD(108, 72)=GCD(72, 36)
GCD(72, 36)=GCD(36, 0)
GCD(36, 0)= 36
GCD(72, 36)= 36
GCD(108,72)= 36
GCD(288, 108)= 36
The following program find GCD of three numbers
#include<stdio.h>
int gcd(int m, int n)
{
if(n= = 0)
return m;
else if(n>m)
return (gcd(n,m));
else
return (gcd(n, m%n));
}
void main()
{
int k, m, n;
printf("Enter the three numbers: ");
scanf("%d %d %d", &k, &m, &n);
printf("GCD (%d, %d, %d) = %d ", k, m, n, gcd(k, gcd(m, n)));
}
Output
Enter the three numbers: 4 6 8
GCD (4, 6, 8 ) = 2

Checking the Parenthesis Matching


Stacks are most useful for checking the parenthesis matching in an expression.
Algorithm:
Step1: Scan the symbols of expression from left to right
Step2: If the symbol is a left parenthesis then push it on the stack
Step3: If the symbol is right parenthesis
if the stack is empty
valid = false
else
pop an element from the stack
if the popped parenthesis does not match the parenthesis being scanned
valid= false
Step4: After scanning all the symbol if stack is not empty then
valid = false

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 31

Show that the expression ((1+2)*3) is valid


Scanned Symbol ((1+2)*3) Stack Operation Performed

Push()
( (
Push()
( ( (

1 ( (

+ ( (

2 ( (
Pop()
) (

* (

3 (
Pop()
)

Polish Notation of Arithmetic Expressions


The great Polish Mathematician Jan Lukasiewich introduced a new technique for the
representation of arithmetic expressions. According to him arithmetic expressions are
classified into three categories.
• Infix notation
• Postfix notation
• Prefix Notation
Infix Notation
Operator is placed in between the operands eg: a + b
Prefix Notation (Polish Notation)
Operator is placed before operands eg : +ab
Postfix Notation (Revers polish notation) or Suffix notation
Operator is placed after operands eg: ab+
To parse any arithmetic expression, we need to take care of
• operator precedence
• associativity

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 32

Conversion from infix to postfix expression


Algorithm
1. Push ‘(‘ on the stack and add ‘)’ at the end of the infix expression Q.
2. Read all the symbols one by one from left to right in the given Infix Expression.
3. If the scanned symbol is operand, then print it (Output).
4. If the scanned symbol is left parenthesis '(', then Push it on to the Stack.
5. If the scanned symbol is operator ⊗ then
1. Repeatedly pop the operators from stack and print it which has the same
precedence or higher precedence than operator ⊗
2. PUSH ⊗ to STACK
6. If the scanned symbol is right parenthesis ')’, then
1. Repeatedly Pop all the contents of stack and print each poped symbol to the
result.
2. Remove the left parenthesis’ (‘
7. Exit

Example
Consider the following expression Q=A + (B + C)
To covert infix to postfix push a ‘(‘ on to the stack and add a ‘)’ at the end of the infix
expression
Q= A + (B + C))
Line Scanned Stack Postfix Expression
Symbol
A A
1 (
+ A
2 ( +

3 ( A
( + (
B AB
4 ( + (
+ + AB
5 ( + (
C + ABC
6 ( + (
) ABC+
7 ( +
) ABC++
8

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 33

Evaluation of Postfix expression


Algorithm
Step 1: Add “#” at the end of P
Step 2: Repeat scanning P from left to right and repeat step 3 and 4 for each element of P
until “#” is encountered.
Step 3: If an operand is encountered, push it onto STACK
Step 4: If an operator ⊗ is encountered:
1. Remove the two top elements of STACK. If A is the top element, B is the next
top element.
2. Evaluate B⊗ A
3. Push the result of (2) of step 4 back onto STACK
Step 5: Evaluated value is equal to the top of the STACK
Step 6: Exit
Example
Let P=6, 5, 3, +, *, 12, 3, /, -
Initially add ‘#’ at the end of the given expression P
P=6, 5, 3, +, *, 12, 3, /, -,#
Now we will scan the symbols until ‘#’ is encountered.
Scanned Operation Stack
Line
Symbol performed
6 Push 6
1 6
5 Push 5
2 6 5
3 Push 3
3 6 5 3
+ Pop 3 and 5
4
5+3=8 6 8
Push 8
* Pop 8 and 6
5
6*8=48 48
Push 48
12 Push 12
6 48 12
3 Push 3
7 48 12 3
/ Pop 3 and
8
12
48 4
12/3=4
Push 4
- Pop 4 and
9
48
44
48-4=44
Push 44
Value of postfix expression=Top of stack= 44

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 34

LINKED LIST REPRESENTATION OF STACKS


In a linked stack every node has two parts:
• One part stores data
• Another part stores the address of the next node.
The START pointer of the linked list is used as TOP. All insertions and deletions are done at
the node pointed by TOP. If TOP== NULL, then it indicated that the stack is empty
The pictorial representation of linked list representation of stack
TOP
2025 2025 2010 215
50 10 90 NULL

Push Operation
The push operation is used to insert an element into the stack. The new element is added at the
topmost position of the stack.
Algorithm: PUSH()
step 1: Allocate memory for the new node and name it as NEWNODE
step 2: set NEWNODE→ INFO = ITEM
step 3: if TOP= = NULL
set NEWNODE→ LINK = NULL
set TOP= NEWNODE
else
set NEWNODE→LINK= TOP
set TOP= NEWNODE
end if
step 4: End
Consider the linked stack as shown below:
TOP
2025 2025 2010 215
50 10 90 NULL

To insert an element with value 80, we first check if TOP= = NULL, then we allocate memory
for a new node, store the value 80 in its INFO part and NULL in its LINK part. We add the
new node at the beginning of the linked stack.
The updated linked stack is shown below:
TOP
1020 1020 2025 2010 215
80 50 10 90 NULL

Pop operation
The pop operation is used to delete the topmost element from a stack.

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 35

Algorithm: POP()
Step 1: If TOP= = NULL
print "UNDERFLOW"
go to step 5
end if
Step 2: set PTR= TOP
Step 3: set TOP= TOP→NEXT
Step 4: free PTR
Step 5: End
Consider the linked stack as shown below:
TOP
2025 2025 2010 215
50 10 90 NULL

Before deleting the values, we first check TOP = = NULL if so, the stack is empty otherwise
it deletes a value from a stack
TOP
2010 2010 215
10 90 NULL

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 36

QUEUE
Queue is defined as an ordered collection of items from which, the items may be deleted at one
end called FRONT end and the items may be inserted at the other end called REAR end.
Queue works on FIFO (First In First Out) fashion.
Classification of Queue

Queue

Circular Priority Double Ended


Linear Queue
Queue Queue Queue

Linear Queue
It is also known as sequential queue. It is implemented using linear array or linear list. This
linear queue is represented using linear array with 2 variables FRONT & REAR.

0 1 2 3 4 5 6 7
26 9 23 34 45 67 45 34

FRONT REAR
Deletion Insertion
Queue ADT
A collection of queue data members and queue operations that preform on queue data. The data
members or variables are FRONT and REAR. Queue operations such as
1. Create()- Create queue with n elements.
2. Insert()- insert an element at the end of the queue
3. Delete()- delete an element from the beginning of the queue.
4. Isempty()- returns true if the queue is empty otherwise false
5. Traverse()- accessing all the elements in the queue.
6. Count()- count the number of elements in the queue.
Insertion()
This operation inserts an element on the end of a queue. If queue is full, insertion is not possible.
Algorithm: Qinsert(Q, FRONT, REAR, N, item)
Step 1: check overflow
if REAR= = N-1 then
print “Overflow”
return
Step 2: Increment REAR pointer
REAR= REAR+1

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 37

Step 3: Insert the item


Q[REAR]= item
Step 4: Return
Explanation:
Consider a queue contain 5 elements and Max is 8

0 1 2 3 4 5 6 7
26 9 23 34 45

FRONT REAR
1. Check queue overflow condition if it is print overflow. Here, REAR=4 so it is false.
2. Increment REAR=REAR + 1

0 1 2 3 4 5 6 7
26 9 23 34 45

FRONT REAR
3. Insert the item
Q[REAR]= item i.e., Q[5]=86

0 1 2 3 4 5 6 7
26 9 23 34 45 86

FRONT REAR
4. Return
The following C function illustrates Queue insertion
void Qinsert()
{
if(REAR == N-1)
printf("\n Queue Overflow");
else
{
printf("\n Enter an item:");
scanf("%d", &item);
REAR++;
QUEUE[REAR]= item;
}
}

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 38

Deletion
This operation deletes an item from the beginning of the queue. If Queue is Empty, deletion
operation is impossible.
Algorithm: Qdelete(Q, FRONT, REAR, N, item)
Step 1: Check underflow
if REAR= = FRONT-1 then
print "Underflow"
return
Step 2: Delete the item
item = Q[FRONT]
Step 3: if FRONT= = REAR
FRONT=0, REAR=-1
else
FRONT=FRONT+1
`Step 4: return

Explanation
Consider a queue contain 5 elements and Max is 8
0 1 2 3 4 5 6 7
26 9 23 34 45

FRONT REAR
1. Check queue is underflow. Here, FRONT=0 and REAR=4, so it is false
2. Delete the item. i.e., item= Q [0]⟹item =26
0 1 2 3 4 5 6 7
9 23 34 45

FRONT REAR
3. Check FRONT and REAR value is equal. Here FRONT=0 and REAR =4. So, it is
false, increment FRONT=FRONT+1
0 1 2 3 4 5 6 7
9 23 34 45

FRONT REAR
4. Return
The following C function illustrates Queue deletion
void Qdelete()
{
if (REAR= = FRONT - 1)
printf("\nQueue Underflow");
else if(REAR= = FRONT)

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 39

{
printf("This is the last element in the Queue");
printf("\n The Last element deleted is : %d", QUEUE[FRONT]);
FRONT=0;
REAR=-1;
}
else
{
printf("\n Deleted item is %d", QUEUE[FRONT]);
FRONT++;
}
}
Qempty Operation
This operation returns true if the queue is empty otherwise false.
A Queue is empty only when REAR=-1 and FRONT=0
The following function check Queue is empty
int Qempty()
{
if(REAR= = FRONT-1)
return 1;
else
return 0;
}
Qfull Operation
This operation returns true, if the queue is full otherwise false. A Queue is full only when
REAR== N-1. The following function check Queue is full.
int Qfull()
{
if(REAR= = N-1)
return 1;
else
return 0;
}
Traversal operation
This operation access all the elements in the queue.
Algorithm: Qdisplay(Q, FRONT, REAR, N)
Step 1: Check underflow
if REAR== FRONT-1 then
print "Underflow"
return
Step 2: Repeatedly access the queue elements
for (i=FRONT; i<= REAR; i++)
print Q[i];
Step 3: return

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 40

Explanation
Consider the following Queue Max Size is 8 and it contain 5 elements.

0 1 2 3 4 5 6 7
26 9 23 34 45

FRONT REAR
1. Check underflow, here REAR=4, FRONT=0 so it is false therefore Go to step 2
2. Repeatedly print the value of Queue 26 9 23 34 45
3. Return
Linked Representation of Queues
 In linked list implementation, every queue element has two parts, one that stores the
data and the other that stores the address of the next element.
 The FRONT pointer points the first element and the REAR pointer points the last
element.
 All insertions will be done at the REAR end and all insertions at the FRONT end
 FRONT = REAR = NULL indicates the queue is empty

Insert Operation
The insert operation is used to insert an element into a queue. The new element is added as
the last element of the queue.
Algorithm: QINSERT()
Step 1: Allocate memory for the new node and name it as NEW_NODE
Step 2: Set NEW_NODE → INFO=item
Step 3: If FRONT = NULL
Set FRONT=REAR=NEW_NODE
Set FRONT→ LINK = REAR→ LINK =NULL
Else
Set REAR→> LINK=NEW_NODE
Set REAR = NEW_NODE
Set REAR→NEXT=NULL
End if
Step 4: END
Consider the following figure shows linked queue.
FRONT REAR

2025 2010 215


50 10 90 NULL
To insert an element with value 80, we first allocate memory for a new node, store the value
80 in its INFO part and NULL in its LINK part. We add the new node at the end of the linked
stack. The updated linked queue is shown below:
FRONT REAR

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 41

2025 2010 215 1040


50 10 90 80 NULL

Delete Operation
The delete operation is used to delete the FRONT element from the queue.
Algorithm QDELETE()
Step 1: If FRONT = NULL
Print "Underflow"
goto Step 5
Step 2: Set PTR = FRONT
Step 3: Set FRONT = FRONT → LINK
Step 4: Free PTR
Step 5 End
Consider the linked Queue as shown below:
FRONT REAR

2025 2010 215 1040


50 10 90 80 NULL

Before deleting the values, we first check FRONT = = NULL if so, the queue is empty
otherwise it deletes a value from a queue.
FRONT REAR

2010 215 1040


10 90 80 NULL

CIRCULAR QUEUE
Circular Queue is a linear data structure in which the operations are performed based on FIFO
(First In First Out) principle and the last position is connected back to the first position to make
a circle. It is also called ‘Ring Buffer’. The representation of circular queue is shown below:

Insert Operation
This operation inserts an element on the REAR position of a queue. If queue is full, insertion
is not possible.

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 42

Algorithm CQINSERT(Q,FRONT,REAR,N,ITEM)
Step 1: If FRONT == (REAR+1) %N
display “overflow”
Step 2: IF FRONT == -1
FRONT = REAR = 0
Step 3: ELSE REAR = (REAR + 1)%N
Step 4: Q[REAR] = ITEM
Step 5: END
Delete Operation
This operation deletes an item from the FRONT position of the queue. If Queue is Empty,
deletion operation is impossible.
Algorithm QDELETE (Q, FRONT, REAR, N, ITEM)
Step 1: If FRONT == -1
Write “underflow”
Return
Step 2: ITEM = Q[FRONT]
Step 3: IF FRONT = REAR
FRONT = REAR = -1
ELSE
FRONT = (FRONT + 1)%N
Step 4 : END
The following figure illustrates insertion and deletion of a queue

Circular Queue Traversal


This operation access all the elements in the circular queue.
Algorithm CQDISPLAY(Q,FRONT,REAR,N)

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 43

Step 1: If FRONT <= REAR


Display the array from Q[FRONT] to Q[REAR]
Step 2: IF FRONT > REAR
Display the array from Q[FRONT] to Q[N-1]
Display the array from Q[0] to Q[REAR]

PRIORITY QUEUE
A priority queue is a queue such that element of the queue has been assigned with a
priority such that the order in which elements are processed comes from the following
rules:
o An element of higher priority is processed before any element of lower
priority
o If two elements have same priority, then the element which come first will be
processed first.
Types of priority queue
There are two types of priority queue. They are:

Priority Queue

Ascending Priority Queue Descending Priority Queue

 Ascending priority queue


In ascending priority queue, elements can be inserted in arbitrary order, but only
smallest can be deleted first.
 Descending priority queue
In descending priority queue, elements can be inserted in arbitrary order, but only the
largest element can be deleted first.
The representation of priority queue is shown below:
Suppose an array with elements 10, 5, 30, 20 and 80 in the same order, so, insertion can
be done with the same sequence, but the order of deleting the elements is 80, 30, 20,
10, 5 for descending priority queue and the order for deleting elements is 5, 10, 20, 30,
80 for ascending order

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 44

Implementing a priority queue


The various ways of implementing a priority queue is as follows
• Using a simple linear array
• Using multi-queue implementation
Priority queue using arrays

 It is implemented in two ways


First Method
 Let insertion happen in FIFO like simple queue,
 For deletion, traverse the array for an element of the highest priority and then
delete this element from the queue.
 If this is not the front most element, then shift all its trailing elements to the left
side to fill up the vacant position.
Second Method
 Sort the inserted elements in ascending order
 Now delete the front element like in the simple queue.
Multi Queue Implementation
 The multi-queue implementation of a priority queue is similar to array implementation
except that
 A separate queue for each level of priority has to be maintained
 And each such queue represents a circular array.
 Number of priority levels used = number of queues to be used
 It is represented using a 2D array
 Two linear arrays FRONT[] and REAR[] are to be maintained to store Front
and Rear elements of each priority levels

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 45

DEQUE (or, Double Ended Queue)

In Deque or Double Ended Queue, insertion and deletion can be done from both ends of
the queue either from the front or rear.

Types of DEQUE
The 2 variations of deque is as follows:

Double Ended Queue

Input restricted deque Output restricted deque

 Input restricted deque


In input restricted queue, insertion operation can be performed at only one
end, while deletion can be performed from both ends.

 Output restricted deque


In output restricted queue, deletion operation can be performed at only one
end, while insertion can be performed from both ends.

Operations on Deque:
The basic operations are:
insertFront(): Adds an item at the front of Deque
insertLast(): Adds an item at the rear of Deque
deleteFront(): Deletes an item from front of Deque.
deleteLast(): Deletes an item from rear of Deque.

SINDHI COLLEGE BANGALORE - 24


UNIT 2 Data Structures Using C 46

Consider an input restricted deque and now we can add element only on the REAR side of the
queue but we can delete from both sides.

Applications of Queues:
 Queues are used as waiting lists for a single shared resource like printer, disk, CPU.
 Queues are used as buffers in most of the applications like MP3 media player, CD
player, etc.
 Queue are used to maintain the play list in media players in order to add and remove
the songs from the play-list.
 Queues are used in operating systems for handling interrupts.

SINDHI COLLEGE BANGALORE - 24

You might also like