Unit 2 Notes
Unit 2 Notes
and
Linked List (Unit-2)
Queue
Data Structure
Queues
• Basic Queue operations (insertion, deletion)
• Representation of Queue using array
• Implementation of Queue operations using Stack
• Applications of Queue
• Circular Queue, DeQueue, Priority Queue
Linked List:
• Single linked list, Different operations on single linked list, Reversing single linked list
• Circular linked list
• Double linked list
• Header linked list
Queue:
It is FIFO(First In First Out) linear data structure, in which the element which is inserted first will be the first
element to be removed.
In Queue insertion and deletion take place at separate ends: FRONT and REAR.
Insertion takes place at REAR end and deletion take place at FRONT end.
FRONT REAR
Features of Queue:
1. Like stack, it is also ordered list of items
2. It is FIFO structure
3. Once an element in queue, It is removed only when all the elements already in queue are removed
Applications of Queue
1. Serving request on a single shared resource like Printer, disk, CPU
2. Call center phone system uses queue to hold people calling them in an order until a service representative
is free
0 1 2 3 4 5 6 0 1 2 3 4 5 6
5
initially Queue is empty front=-1, rear=-1 Insert first element front=0, rear=0
0 1 2 3 4 5 6 0 1 2 3 4 5 6
5 9 5 9 8
Insert second element front=0, rear=1 Insert third element front=0, rear=2
0 1 2 3 4 5 6 0 1 2 3 4 5 6 MAX-1
9 8 9 8 6
Delete first element front=1, rear=2 Insert next element front=1, rear=3
0 1 2 3 4 5 6 MAX-1 0 1 2 3 4 5 6 MAX-1
6 4 6 4 3
insert next element front=3, rear=4 insert next element front=3, rear=5
0 1 2 3 4 5 6 MAX-1
6 4 3 7
insert next element front=3, rear=6 Queue is Full
A B
A B A B C A
stack1 stack2 stack1 stack2 stack1 stack2 stack1 stack2 stack1 stack2
Insert ‘A’ Insert ‘B’ Insert ‘C’
This is the way we will maintain the right order of elements in the stack, with first element always staying at the
top of stack. This make removing an element from the queue very simple.
*PUSH operation will be done on stack1 and POP operation will be done on stack2
Manoj Kataria BKBIET, pilani
2. To delete(dequeue) an item from queue we simply return the top item from the second stack.
Move all the elements from stack1 to stack2 one by one. Then remove the top element from stack2 and then
move back all the elements from the second stack to the first stack.
The purpose of moving all the elements from stack1 to stack2 is to reverse the order of elements ( the first
element inserted into the queue is positioned at the top of second stack.
C C A
B B B B
A A C C
stack1 stack2 stack1 stack2 stack1 stack2 stack1 stack2
delete(dequeue) top element from stack2 removed
C B
B C C C
stack1 stack2 stack1 stack2 stack1 stack2 stack1 stack2
delete(dequeue) top element from stack2 removed
Manoj Kataria BKBIET, pilani
Applications of Queue:
1. Queue is useful in Round robin algorithm: It is a CPU scheduling algorithm where each process is
assigned a fixed time slot in a cyclic way. It deals with all process without any priority. All process get a
fair allocation of CPU.
Time slot
P1
0 2 4 6 8 10
P1 P2
0 2 4 6 8 10
P1 P2 P3
0 2 4 6 8 10
P1 P2 P3 P4
0 2 4 6 8 10Kataria BKBIET, pilani
Manoj
Circular Queue:
In linear queue we can insert elements until queue becomes full.
0 1 2 3 4 5
10 85 9 64 15 30
front=0, rear=MAX-1 Queue is Full
0 1 2 3 4 5
30
front=MAX-1, rear=MAX-1 Still Queue becomes because rear at last position
We have empty positions in the queue , we cannot make use of them to insert new element. This is the
problem in a Linear queue.
To overcome this problem we use a Circular queue data structure.
Manoj Kataria BKBIET, pilani
A circular queue is a linear data structure in which operations are performed based on FIFO basis and the last
position connected back to the first position to make a circle.
0 1 2 3 4 5 0 1 2 3 4 5
90 10 15 54 64 91
0 1 2 3 4 5 0 1 2 3 4 5
15 54 64 91 54 64 91
0 1 2 3 4 5 0 1 2 3 4 5
20 54 64 91 20 25 30 54 64 91
deletion deletion
front rear
DeQueue can be represented in two ways : a). Input restricted b). Output restricted
In input restricted, the insertion is performed at only one end and deletion operation is performed at both
ends.
In output restricted, the deletion is performed at only one end and insertion operation is performed at both
ends.
0 1 2 3 4 5 0 1 2 3 4 5
30 40 60 15 30 40 60
FRONT=2 REAR=4 Manoj Kataria BKBIET,FRONT=1
pilani REAR=4
FRONT !=0 then FRONT is decremented
Insert from REAR:
0 1 2 3 4 5 0 1 2 3 4 5
20 30 40 50 20 30
FRONT=2 REAR=5 (MAX-1) because REAR=MAX-1 FRONT=2 REAR=3 REAR!=MAX-1
not possible to insert from REAR then REAR is incremented
0 1 2 3 4 5
20 30 45
FRONT=2 REAR=4
An element with higher priority is processed before any element of lower priority.
Two elements with same priority are processed in the order in which they are inserted in the queue
(FCFS- First Come First Serve basis).
e.g. Hospital waiting room, A patient having more problem would be admitted before other patients
*Priority queues are widely used in operating systems to execute the highest priority process first.
Suppose there are three processes, where the first process need 5 seconds to complete , second process needs
4 seconds and the third process needs 7 seconds, then the second process will have the highest priority and
will thus be the first to be executed.
A linked list is an ordered collection of data elements called nodes. Each node is divided into two parts, the first
part contains the information of the element and second part contains the address of next node in the list.
Node
INFO Address part(pointer) contains address of next node in the list
START
In linked list memory can be allocated for only one node at a time and can be extended at any time. So for
linked list memory is dynamically allocated.
25 1008 35 1026 45 0
Disadvantages:
1. Extra memory space is required to store address
2. No element can be accessed randomly, access each node sequentially
Syntax:
ptr = (int *) malloc (byte size);
Type cast
Pointer to cast type
e.g.
ptr = (int *) malloc (8*sizeof(int));
struct list
{
It is a self referential
int info; structure, it contains a
struct list *next; pointer points to itself
};
info next
25 1008 35 1002 45 0
info next
START 1006
1000
1001
1002 45 0
1003
1004
1005
1006 25 1008
1007
Memory 1008 35 1002
locations
1009
1010
Manoj Kataria BKBIET, pilani
Single linked list creation:
struct list
{
int info;
struct list *next;
};
typedef struct list node; /*node is variable of structure*/
node *start; /*start pointer contains address of first node*/
1. start = (node *)malloc(sizeof(node)); /*allocate space for first node*/
start->info=25;
start->next=NULL; start 1006
25 0
node *temp1;
temp1=start; start 1006
while(temp1->next!=NULL) next
{ 25 1008 35 0
temp1=temp1->next; temp1
}
temp1->next=newnode;
start 1006
25 1008 35 1002 45 0
newnode 1024
info next
newnode->info=58; start
newnode->next=start; 1024
start=newnode;
58 1006 25 1008 35 1002 45 0
58
start
1006
25 1008 35 1002 45 0
start =start->next;
start 1008
35 1002 45 0
25 1008 35 1002 45 0
temp
temp=start;
while(temp->next->next!=NULL) start 1006
temp=temp->next;
temp->next=NULL; 25 1008 35 0
temp=start;
while(temp->next!=NULL) start
{ 1006 temp
printf("%d\t",temp->info); temp
temp=temp->next; 25 1014 58 1008 35 1002 45 0
} temp
printf("%d",temp->info);
After reversing
start
1002
Steps:
1. Create two pointers:
*pnode -> holds the reference of previous node
*cnode -> holds the reference of current node
2. Now disconnect the pnode from others (make sure it points to none)
1002
start
45 0 35 1014 58 1006 25 0
pnode cnode
3. Move start to its next node start=start->next
45 0 35 1014 58 1006 25 0
pnode cnode start
5. Point the previous node to current node and current to start node
pnode=cnode cnode=start
start
45 0 35 1002 58 1006 25 0
pnode cnode
45 0 35 1002 58 1006 25 0
pnode cnode
start
45 0 35 1008 58 1006 25 0
pnode cnode
Manoj Kataria BKBIET, pilani
//function for reversing linked list
void rev()
{
node *pnode,*cnode;
pnode=start;
cnode=start->next;
start=pnode->next;
if(start==NULL)
printf("\nOnly one node in list");
else
{
pnode->next=NULL;
while(start!=NULL)
{
start=start->next;
cnode->next=pnode;
pnode=cnode;
cnode=start;
}
start=pnode;
} Manoj Kataria BKBIET, pilani
}
Circular Linked list:
It is a linked list in which the last node points back to the first node to form a circle. In this every node has a link
to its next element in the sequence.
start 1006
Advantages:
1. Every node is accessible from a given node. All nodes can be reached by merely chaining through the list.
2. Useful for the implementation of circular queue
3. It is useful in applications to repeatedly go through the list
4. Multiplayer games ,all the players kept in circular linked list and pointer keeps on moving forward as a player’s
chance ends
Disadvantages:
1. Requires an extra care to detect the end of the list. It may be possible to get into an infinite loop
Insertion operation:
Insertion operation can be performed in three ways:
1. Inserting at the beginning
2. Inserting at the end
3. Inserting at specific location
start=newnode;
temp->next=start;
1024
start 1006
temp=start;
printf("\nDeleted node:%d",start->info);
while(temp->next!=start) temp
temp=temp->next; start 1006
start=start->next; temp temp temp
temp->next=start; 25 1014 58 1008 35 1002 45 1006
start 1014
temp=start;
while(temp->next->next!=start)
temp=temp->next;
printf("\nDeleted node:%d",temp->next->info);
temp->next=start;
temp
start 1006
temp temp
25 1014 58 1008 35 1002 45 1006
start 1006
PREV NEXT
(address of previous node) INFO
(address of next node)
LAST
START
Disadvantages of DLL:
1. A DLL occupy more space, to store two pointers
2. All operations require an extra pointer to be maintained. So insertion and deletion take more time as
compare to SLL
Structure of DLL:
struct list
{
struct list *prev;
int info;
struct list *next;
};
Creation:
A linked list can be created node by node
newnode=(node *)malloc(sizeof(node));
newnode->info=50;
start=last=newnode; newnode
newnode->prev=NULL; start 102 102 last
newnode->next=NULL;
0 50 0
prev next
0 50 106 102 58 0
prev next prev next
Manoj Kataria BKBIET, pilani
Insertion Operation:
Insertion operation can be performed in three ways:
1. Inserting at the beginning
2. Inserting at the end
3. Inserting at specific location
start 102 106 last
Inserting at the beginning of list:
0 50 106 102 58 0
//allocate space for new node prev next
newnode=(node *)malloc(sizeof(node));
printf("Enter info for new node:"); newnode
scanf("%d",&newnode->info); 112
newnode->prev=NULL;
newnode->next=start; 0 33 102
start->prev=newnode; prev next
start=newnode;
0 50 106 102 58 0
prev next
//allocate space for new node
newnode=(node *)malloc(sizeof(node));
newnode
printf("Enter info for new node:");
112
scanf("%d",&newnode->info);
newnode->next=NULL; 106 33 0
newnode->prev=last; prev next
last->next=newnode;
last
last=newnode;
start 102 112
temp=start->next;
printf("\nDeleted node:%d",start->info);
temp->prev=NULL;
start=temp;
last
temp=last->prev;
printf("\nDeleted node:%d",last->info);
temp->next=NULL;
last=temp;
last
start 102 118
start
1024
header node
(helps in getting list count without traversing)