You are on page 1of 37

UNIT 3 Stacks and Queues

Stacks and Queues

Stack: - Stack is a linear data structure in which a data item is inserted and deleted at one
end. A stack is called a Last in First out (LIFO) structure because the data item that is last
inserted into the stack and is the first data item to be deleted from the stack.

In general, writing a value to the stack is called as a push operation whereas reading a value from
it is called as a pop operation.

Here in stacks a variable top is used to represent the top of the stack. Initially top=-1. For every
insertion, i.e. push, operation the top is increased by one and element is stored in stack and for
every deletion, i.e. pop, operation the top element is deleted and decremented by one.

EXAMPLE:

Operation contents of the stack after operation

Push A A :A is pushed

Push B AB :B is pushed

Pop A :B is popped(deleted)

Push C AC :C is inserted

Push D ACD :D is inserted

Push E ACDE :E is inserted

Pop ACD :E is deleted

Pop AC :D is deleted

Pop A :C is deleted

Pop empty :A is deleted

The above example shows the working of stacks for moving an element into stack and deleting
an element from stack.
DS in C NEC, CSE Dept. Page 1
UNIT 3 Stacks and Queues

Stacks are extensively used in computer applications. For example, when one c function calls
another function, and passes some parameters, these parameters are passed using the stack.

Stacks using arrays:

The stacks can be represented using array. The variable top indicates the current top of the stack
and it moves up and down depending on whether new items are added to the stack (or) existing
item is removed.

Algorithm for PUSH:

push(item)
item: item to be inserted on to the stack
begin
if top=(MAX-1) then /* where MAX is the size of stack*/
write stack is full
otherwise
top=top+1
stack[top]=item
end of if
end of push
EXAMPLE:

Algorithm for pop:

returntype pop()
begin
initialize item;
DS in C NEC, CSE Dept. Page 2
UNIT 3 Stacks and Queues

if top=-1 then
write stack is empty
otherwise
item=stack[top]
top=top-1
end of if
end of pop
EXAMPLE:

Algorithm for stack top:

returntype stacktop()
begin
if top=-1
write stack is empty
otherwise
return stack[top]
end of if
end of stacktop

Algorithm for stackdisplay()

display()
begin
initialize i

DS in C NEC, CSE Dept. Page 3


UNIT 3 Stacks and Queues

if top=-1
write stack is empty
otherwise
for i=top to 0
print stack[i]
end of if
end of display

Program for stacks using arrays:

#include<stdio.h>
#include<conio.h>
#include<process.h>
int stack[100],top=-1,
void push(int);
int pop();
int stacktop();
void display();
void main()
{
int item,ch;
clrscr();
while(1)
{
clrscr();
printf("stackoperations\n");
printf("1.push\n2.pop\n3.stacktop\n4.display\n5.exit\n");
printf("enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("enter an element");
scanf("%d",&item);
push(item);
getch();
break;
case 2:
item=pop();
if(item==-1)
printf("stack is empty");
else
printf("element deleted is %d\n ",item);
getch();
break;
case 3:
item=stacktop();
DS in C NEC, CSE Dept. Page 4
UNIT 3 Stacks and Queues

if(item==-1)
printf("stack is empty\n")
else
printf("topelement is %d\n",item);
getch();
break;
case 4:
display();
getch();
break;
default:getch();
exit(0);
}
}
}
void push(int item)
{
if(top==99)
printf("stack is full");
else
{
top=top+1;
stack[top]=item;
}
}
int pop()
{
int item;
if(top==-1)
return -1;
else
{
item=stack[top];
top=-1;
return item;
}}
int stacktop()
{
if(top==-1)
return -1;
else
return stack[top];
}
void display()
{
int i;
DS in C NEC, CSE Dept. Page 5
UNIT 3 Stacks and Queues

if(top==-1)
printf("stack is empty");
else
{
for(i=top;i>=0;i--)
printf("%d\t",stack[i]);
printf("\n");
}
}

Applications of stack:

Reversing the list:

The stack usually works by inserting a new element into top of the stack and deleting the element
from the top of the stack.

The elements of stack are displayed from top to bottom of stack, i.e. which displays the element
reverse order based on insertion.

To understand this, imagine a pile of books usually, we pick up the topmost book from the pile.
The elements in the list are displayed in reverse order.

Algorithm for displaying the elements using arrays:

display()
begin
initialize i
if top=-1
write stack is empty
otherwise
for i=top to 0
ptint stack[i]
end of if
end of display
Factorial calculation:

The recursive pseudo code for factorial computation

int factorial(int n)
{
int x,y;

DS in C NEC, CSE Dept. Page 6


UNIT 3 Stacks and Queues

if(n==0||n==1)
{
x=1;
return 1;
}
else
{
x=n-1;
y=factorial(x);
return y*n;
}
}
The function does not lead to an endless series of calls to itself because of the base case. The
series of calls made by procedure factorial () to itself based on the value of n, this can be viewed
as factorial () calling itself as many times as itself with varying values of n. A procedure call
would have a normal termination only when either the base case is executed (or) the recursive
case has successfully ended.

During the execution, to keep track of the calls made to it and to record the status of the
parameters at the time of the call, a stack data structure is used.

When the procedure factorial (5) is initiated and executed x obtains the value 4 and the control
flow moves to y=factorial (4). This initiates the next call to the procedure as factorial (4).
Observe that 1st call, factorial (5), has not finished its execution when the next call factorial (4),
to the procedure has been issued. Therefore, there is a need to store preceding calls.

Every new procedure call pushes the current values of the parameters involved into the stack,
thereby preserving the values used by the earlier calls

The values of 3 parameters n, x, y are keep track of in the stack data structure along with the
return values and return address.

The following figure illustrates the various snap shots of the stack during the execution of
factorial (5)
Factorial (5): causes all the parameters, local variables, return values and return address of
factorial (5) will be stored in stack. [Stack elements during Factorial (5)]
n=5 x=4 y= factorial (4)

DS in C NEC, CSE Dept. Page 7


UNIT 3 Stacks and Queues

Factorial (4): causes all the parameters, local variables, return values and return address of
factorial (4) will be stored in stack. [Stack elements during Factorial (4)]
n=4 x=3 y= factorial (3)
n=5 x=4 y= factorial (4)

Factorial (3): [Stack elements during Factorial (3)]


n=3 x=2 y= factorial (2)
n=4 x=3 y= factorial (3)
n=5 x=4 y= factorial (4)

Factorial (2): [Stack elements during Factorial (2)]


n=2 x=1 y= factorial (1)
n=3 x=2 y= factorial (2)
n=4 x=3 y= factorial (3)
n=5 x=4 y= factorial (4)

Factorial (1): During this function call return statement has encountered which causes the stack
to deallocate the function variables and control shifts to previous function call i.e. factorial (1) by
returning a value with the help of return address. [Stack elements during Factorial (1)]
n=1 x=1 return 1;
n=2 x=1 y= factorial (1)
n=3 x=2 y= factorial (2)
n=4 x=3 y= factorial (3)
n=5 x=4 y= factorial (4)

Factorial (2): [Stack elements after Factorial (1) has done]


n=2 x=1 y= 1 return 1*2
n=3 x=2 y= factorial (2)
n=4 x=3 y= factorial (3)
n=5 x=4 y= factorial (4)

Factorial (3): [Stack elements after Factorial (2) has done]

DS in C NEC, CSE Dept. Page 8


UNIT 3 Stacks and Queues

n=3 x=2 y= 2 return 2*3


n=4 x=3 y= factorial (3)
n=5 x=4 y= factorial (4)

Factorial (4): [Stack elements after Factorial (3) has done]


n=4 x=3 y= 6 return 6*4
n=5 x=4 y= factorial (4)

Factorial (5): [Stack elements after Factorial (4) has done]


n=5 x=4 y= 24 return 24*5

120

Figure: stack usage when a function factorial (5) has been called.

Now the function factorial (5) has returned a value 120 to previous function calling location.
Stacks are used in this manner when a function has been called.

Algorithm for Transforming Infix to Postfix Expression:

1. First initialize the stack to be empty.

2. For each character in input string-if input string is an operand append to the output
Else
If stack is empty (or) the operator has higher priority than the operator on the top of the stack (or)
the top of the stack is opening parenthesis
Then
Push the operator onto the stack
Else
Pop the operator from the stack and append to the output.

3. If the input string is a closing parenthesis pop operator from the stack and append the
operators to the output until an opening parenthesis is encountered. Pop the opening parenthesis
from the stack and discard it.

4. If the end of the input string is encountered then iterate the loop until the stack is not empty.
Pop the stack and append the remaining input string to the output.

EXAMPLE: A*B/(C-D)+E*(F-G)

DS in C NEC, CSE Dept. Page 9


UNIT 3 Stacks and Queues

Input string stack operations prefix notation

A empty A

* * A

B * AB

/ / AB*

( /( AB*

C /( AB*C

- /(- AB*C

D /(- AB*CD

) / AB*CD-

+ + AB*CD-/

E + AB*CD-/E

* +* AB*CD-/E

( +*( AB*CD-/E

F +*( AB*CD-/EF

- +*(- AB*CD-/EF

G +*(- AB*CD-/EFG

) +* AB*CD-/EFG-

END of input string EMPTY AB*CD-/EFG-*+

The postfix for infix expression A*B/(C-D)+E*(F-G) is AB*CD-/EFG-*+

Program:-

DS in C NEC, CSE Dept. Page 10


UNIT 3 Stacks and Queues

#include<stdio.h>
#include<conio.h>
#include<string.h>
char stack[20];
int top=-1;
char pop();
void push(char item);
int precedence(char symbol)
{
switch(symbol)
{
case '+':
case '-':return 2;
case '*':
case '/':
case '%':return 3;
case '(':
case ')':return 1;
}
return 0;
}
int isoperator(char symbol)
{
switch(symbol)
{
case '+':
case '-':
case '*':
case '/':
case '%':
case '(':
case ')': return 1;
default: return 0;
}
}
void infixtopostfix(char input[],char output[])
{
int i,symbol,j=0;
stack[++top]='#';
for(i=0;i<strlen(input);i++)
{
symbol=input[i];
if(isoperator(symbol)==0)
{
output[j]=symbol;
j++;
DS in C NEC, CSE Dept. Page 11
UNIT 3 Stacks and Queues

}
else
{
if(symbol=='(')
push(symbol);
else
if(symbol==')')
{
while(stack[top]!='(')
{
output[j]=pop();
j++;
}
pop();
}
else
{
if(precedence(symbol)>precedence(stack[top])
push(symbol);
else
{
while(precendence(symbol)<=precendence(stack[top])
{
output[j]=pop();
j++;
}
push(symbol);
}
}
}
}
while(stack[top]!='#')
{
output[j]=pop();
j++;
}
output[j]='\0';
}
void main() string is:\t");
{ puts(output);
char input[20],output[20]; getch();
clrscr(); }
printf("enter the infix string\n");
gets(input); void push(char item)
infixtopostfix(input,output); {
printf("the corresponding postfix stack[++top]=item;

DS in C NEC, CSE Dept. Page 12


UNIT 3 Stacks and Queues

} return stack[top--];
}
char pop()
{

Evaluation of arithmetic expression:-

The stacks are used in evaluation of arithmetic expression. The stacks will
evaluate the infix expression by converting into postfix expression.

Convert the infix expression to postfix expression

Stacks evaluate the arithmetic expression as follows:

If the input string is an operand, then push it onto the stacks


If the input string is an operator, then the first two operands on the
stack are evaluated using this operator by popping them from the
stack and the result is also placed onto the stack.
end of expression:- pop the value from the stack
Example: - Infix Expression: (6+8)/ (9-2)
Postfix Expression for given Expression is 6 8 + 9 2 - /

Result=2 for the above example the result is 2. Therefore, (6+8) / (9-2) = 2

Program:-

#include<stdio.h>
#include<conio.h>
DS in C NEC, CSE Dept. Page 13
UNIT 3 Stacks and Queues

#include<string.h>
int stack[20], top=-1;
int pop();
void main()
{
int i=0,x,term1,term2;
char str[20];
clrscr();
printf("enter the postfix expression");
gets(str);
while(str[i]='\0')
{
if(str[i]>='0'&&str[i]<='9')
{
x=str[i]-'0';
push(x);
}
else
{
term1=pop();
term2=pop();
switch(str[i])
{
case '+':push(term2+term1);
break;
case '-': push(term2-term1);
break;
case '*':push(term2*term1);
break;
case '/':push(term2/term1);
break;
case '%':push(term2%term1);
break;
default: exit(0);
}
}
}
printf("the value of postfix expression is %d",pop());
getch();
}
void push(int x)
{
if(top==9)
{
printf("stack is full");
}
DS in C NEC, CSE Dept. Page 14
UNIT 3 Stacks and Queues

else
{
top++;
stack[top]=x;
}
}
int pop()
{
int item;
if(top==-1)
return -1;
item=stack[top];
top--;
return item;
}

Queues:-

Queue is a linear data structure that is accessed in the order of First in First
out (FIFO), i.e. the 1st item inserted in a queue is also the first one to be
accessed, and the last one to be inserted is also the last one to be accessed.

A queue which is similar to the way we queue up at train reservation


counters (or) at banks.

Here all insertions are made at one end of the last known as rear (or) tail of
the queue and all deletions are made at the other end of queues known as
front (or) head of the queue.

Insertion operation is also referred as enqueuing a queue and deletion


operation is known as dequeuing of the queue.

operation contents of the queue queue operation

DS in C NEC, CSE Dept. Page 15


UNIT 3 Stacks and Queues

insert a a a is inserted into the


queue

insert b ab b is inserted into the


queue

insert c abc c is inserted into the


queue

delete bc a is deleted from


queue

insert d bcd d is inserted into


queue

delete cd b is deleted from


queue

delete d c is deleted from


queue

insert e de e is inserted into the


queue

In the above example, the insert operation adds a new item to queue and
the delete operation removes an item from stack .The item which is
inserted into queue at the first is also the first item to be removed from the
queue.

Implementation of queue using arrays:-

Queue is a linear data structure it can be implemented using arrays.

The variables front and rear keeps track of the front and rear ends in a
queue to facilitate execution of insertion and deletion operations
DS in C NEC, CSE Dept. Page 16
UNIT 3 Stacks and Queues

respectively.

Here front and rear are initialized to -1 and for every insert operation rear
variable is increased by one and front variable is increased by one for every
deletion operation from queue.

Algorithm for insert operation:-

insert(item)
item:item to be inserted on to queue
begin
if(rear=MAX-1) then /*where MAX is Queue size
write queue is full
otherwise
rear=rear+1;
q[rear]=item;

Here in this algorithm the condition is checked whether the queue is full or
not. If possible the element is added or inserted into queue by increasing
the rear variable.

Algorithm for deleting an element from queue:-

delete()
begin
initialize item
if front=rear then
write queue is empty
otherwise
front=front+1;
item=q[front];
return item;
end of if
end of delete

Here in this algorithm the condition front=rear is checked for the emptiness
of the queue. If the queue is not empty, front is increased by one and the
element pointing to front is removed from queue.

EXAMPLE:-

DS in C NEC, CSE Dept. Page 17


UNIT 3 Stacks and Queues

The following example illustrates the working of queue

Figure: Queue Working for Enqueue and Dequeue operations:

Limitations of linear queue:-

In the above example the insertion operation was unsuccessful for inserting
the ff element in to queue even the memory is available, since the rear
variable pointing MAXIMIUM position of queue. This is a major
limitation when a queue full condition is involved it does not necessary to
imply that the queue is 'physically full. This leads to the limitation of
rejecting insertions despite the space available. This limitation can be
eliminated using circular queues.

DS in C NEC, CSE Dept. Page 18


UNIT 3 Stacks and Queues

PROGRAM for queue using arrays:-

#include<stdio.h>
#include<conio.h>
#include<process.h>
int queue[20], front=-1,rear=-1;
int delete();
void insert(int);
void display();
void main()
{
int ch,item;
clrscr();
while(1)
{
clrscr();
printf("queue operations\n");
printf("1.insert\n2.delete\n3.display\n4.exit\n");
printf("enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("enter an element");
scanf("%d",&item);
insert(item);
getch();
break;
case 2:
item=delete();
if(item==-1)
printf("queue is empty");
else
printf("element deleted is %d\n ",item);
getch();
break;
case 3:
display();
getch();
break;
default:
exit(0);
}
}
}

void insert(int item)

DS in C NEC, CSE Dept. Page 19


UNIT 3 Stacks and Queues

{
if(rear==19)
printf("queue is full");
else
{
rear=rear+1;
queue[rear]=item;
}
}
int delete()
{
int item;
if(front==rear)
return -1;
else
{
front=front+1;
item=queue[front];
return item;
}
}
void display()
{
int i;
if(front==rear)
printf("queue is empty");
else
{
for(i=front+1;i<=rear;i++)
printf("%d\t",queue [i]);
printf("\n");
}
}

Implementation of Queue using stacks:-

Queue can be implemented using stacks. Queue is a FIFO and stack is a


LIFO. So, to implement queue using two stacks there are 2 ways to follow
queue approach using two stacks.

Method 1: The push operation is complex

Method 2: The pop operation is complex

DS in C NEC, CSE Dept. Page 20


UNIT 3 Stacks and Queues

Method 1: (by making enqueue (insertion) operation costly)

This method makes sure that newly entered element is always at the first
position of the stack 1. So the dequeue operation just removes the top
elements from stack 1 to stack 2 and put the first element in the stack 1 and
pop the elements from stack 2 to stack 1 is used.

Enqueue (x): (insert x into stack 1)

1. While stack 1 is not empty push everything from stack 1 to stack2.

2. Now push x to stack1.

3. Now pop everything from stack2 to stack 1.

Dequeue: (delete top item from stack1)

1. if stack1 is empty print empty.

2. else pop an element from stack 1 and return it

Example:

insert 1 2 3

delete

delete

insert 4

operation stack 1 stack 2 Remarks

insert 1 1 :move 1 to stack 1

insert 2 2 1 :move 1 to stack 2


from stack 1

21 & push 2 into stack1


& move 1 to stack 1
DS in C NEC, CSE Dept. Page 21
UNIT 3 Stacks and Queues

insert 3 3 12

321

delete 32 :delete top element


from stack1

delete 3

insert 4 4 3

43

Here in this example the element 1 is inserted first & the same element is
deleted first so the approach is FIFO which is the property of queues.

Method 2: (by making dequeue (deletion) operation costly)

In this method the enqueue operation works by inserting the new element
in the stack 1 by using top variable. Dequeue operation works by deleting
the elements from stack 2. If stack 2 is empty then all the elements are
moved from stack 1 to stack 2 and finally top of stack 2 is returned.

Enqueue:-

(1) push x to stack 1

Dequeue:

1. If both stacks are empty then print empty

2. If stack 2 is empty then while stack 1 is not empty push everything


from stack 1 to stack 2 and pop the element from the stack 2 and
return it.

3. Else pop the element stack 2 and return it.

Example:

DS in C NEC, CSE Dept. Page 22


UNIT 3 Stacks and Queues

insert 1 2 3

delete

delete

insert 4

operation stack 1 stack 2 Remarks

insert 1 1 empty move 1 to stack 1 ::


topelement=1

insert 2 12 empty move 2 to stack 2 ::


topelement=2

insert 3 123 empty move 3 to stack 3 ::


topelement=3

delete empty 321 since stack 2 is empty


and stack 1 is not 32
empty, move the elements from stack 1 to
stack 2 and the topelement is deleted

delete empty 32 since stack 2 is not


empty pop the 3
topelement from stack2

insert 4 4 3 move 4 to stack 1

Here, in this method queue approach is followed and more, the number of
operations is less compared to method 1.

#include<stdio.h>
#include<conio.h>

int top1=-1,top2=-1;
int stack1[20],stack2[20];
void push1(int item);
DS in C NEC, CSE Dept. Page 23
UNIT 3 Stacks and Queues

void push2();
int pop1();
int pop2();
void main()
{
int i,ch,item;
while(1)
{
clrscr();
printf("1.Insert\n2.Delete\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter the new element\n");
scanf("%d",&item);
push1(item); getch(); break;
case 2: item=pop2();
if(item==-1)
printf("Empty\n");
else
printf("Element Deleted is %d\n",item);
getch(); break;
default: exit(0);
} /*switch*/
} /*while*/
} /*main*/
void push1(int item)
{
if (top1==19)
printf("full\n");
else
{
top1=top1+1;
stack1[top1]=item;
}
}
int pop1()
{
int item;
if(top1>=0)
item=stack1[top1--];
return item;
}
void push2()
{
DS in C NEC, CSE Dept. Page 24
UNIT 3 Stacks and Queues

if(top1==-1)
printf("empty\n");
else
{
while(top1>=0) {
top2=top2+1;
stack2[top2]=pop1();
}}
}
int pop2()
{
int item;
if(top2<0&&top1<0)
return -1;
if(top2==-1)
push2();
if(top2>=0)
{
top2=top2-1;
item=stack 2[top2];
}
return item;}

Priority Queue:

A Priority queue is a queue in which insertion (or) deletion of items from


queue are done based on some priority.

For example, Let P is a priority queue with three elements a, b, c whose


priority factors are 2, 1, and 1 respectively. Here, larger the number, higher
is the priority accorded to that element.

DS in C NEC, CSE Dept. Page 25


UNIT 3 Stacks and Queues

(a)Initial priority queue (b) Insert d (4) (c) delete

At the head of the queue moving the remaining elements as shown in


figure (b).When elements in the queue have the same priority, the priority
queue behaves as an ordinary queue following the principle of FIFO
amongst such elements.

The figure(c) represents the deletion of the element from priority queue in
this queue; the 1st element of queue is deleted.

A priority Queue can be implemented in two ways

1. A common method of implementation of a priority queue is to open as


many queues as there are priority factors. A low priority queue will be
operated for the deletion only when all its high priority predecessors are
empty. In other words, deletion of an element in a priority queue q, with
priority pi is possible only when those queue qj with priorities pj (pj, pi) are
empty.

In this method, element with priority p1 inserted into priority queue p1 by


FIFO.

2. Another method of implementation could be to sort out the elements in


the queue according to the descending order of priorities every time an
insertion takes place .The top priority element is at the head of the queue.
The element deleted from this queue is head of the queue. Here same
priority elements inserted into queue by FIFO approach.

DS in C NEC, CSE Dept. Page 26


UNIT 3 Stacks and Queues

Method Time complexity of Insertion Deletion

1st Method 0(1) waits until all


other
queues preceding it in
priority to be empty

2nd Method 0(nlogn) 0(1)

Ex: Let queue (priority) with jobs are J1(1) J2(1) J3(0) . Here J1 is job with
priority 1, J2 with 1 priority.

Operations: J4(2) arrives , J5(2) arrives , execute job , execute job, execute
job.

Execution of job means deletion of element from priority queue

Implementation of a priority Implementation of priority


Remarks
Queue as a cluster of queues queue by sorting queue elements
(Method 1) (Method 2)

High Priority (2)


Job queue

Opening job queue


Medium Priority (1)
Job Queue

DS in C NEC, CSE Dept. Page 27


UNIT 3 Stacks and Queues

Low Priority (0)


Job Queue

1. J4 (2) arrives
High Priority (2)
Job queue

Insert J4(2) Medium Priority (1)


Job Queue

Low Priority (0)


Job Queue

2. J5 (2) arrives
High Priority (2)
Job queue

Insert J5(2) Medium Priority (1)


Job Queue

DS in C NEC, CSE Dept. Page 28


UNIT 3 Stacks and Queues

Low Priority (0)


Job Queue

3. Executes Job
High Priority (2)
Job queue

J4
(2) is Deleted
Medium Priority (1)
Job Queue

Low Priority (0)


Job Queue

4. Execute Job
High Priority (2)
Job queue

J5(2)
is deleted
Medium Priority (1)
Job Queue

DS in C NEC, CSE Dept. Page 29


UNIT 3 Stacks and Queues

Low Priority (0)


Job Queue

5. Execute Job
High Priority (2)
Job queue (EMPTY)

J1(1)
is deleted Medium Priority (1)
Job Queue

Low Priority (0)


Job Queue

The above example shows the working of priority queue for cluster of
queues and priority queue.

Circular Queue: Circular queue is not linear in structure but instead


it is circular data structure. In other words, the front and rear variables
display a circular movement over the queue data structure.

Operations on a Circular Queue: Let CIRC_Q be a circular queue with a


capacity of 3 elements as shown in figure. The queue is obviously full with

DS in C NEC, CSE Dept. Page 30


UNIT 3 Stacks and Queues

front pointing to the element at the head of the queue and rear pointing to
the element at the tail end of the queue.

a. Represents the initial circular queue with 3 elements.


b. After two deletions, front moves towards rear and points to 'c' as the
current front element of CIRC_Q.
c. Whend is inserted, unlike linear queues, rear moves back in a
clockwise fashion to stored in the vacant space available.

(a)Initial Circular (b) Circular queue after (c) Circular queue


after
Queue. two deletions. insertion of d, e.

The Circular queues are using the memory more efficiently than the linear
queues.

In the above example, memory is used efficiently than linear queue data
structure.

DS in C NEC, CSE Dept. Page 31


UNIT 3 Stacks and Queues

Figure: Circular Queue Example

DS in C NEC, CSE Dept. Page 32


UNIT 3 Stacks and Queues

Implementation of circular queues using arrays:


#include<stdio.h>
int CQ[20],front=-1,rear=-1;
void insert(int);
void delete();
void display();
void main()
{
int item ch;
clrscr();
while(1)
{
clrscr();
printf("CQ operations\n");
printf("1.Insert\n2.Delete\n3.Display\n4.Exit\n");
printf("Enter the choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("Enter an element\n");
scanf("%d",&item);
insert(item);
getch();
break;
case 2: delete();
getch();
break;
case 3:display();
getch();
break;
default : exit(0);
}
}
}

void insert(int item)


{
if(front==-1&&rear==-1) {
front=rear=0; CQ[rear]=item; }
else
{
rear=(rear+1)%20;
if(front==rear)
{
printf("CQ is full\n");
if(rear==0)
DS in C NEC, CSE Dept. Page 33
UNIT 3 Stacks and Queues

rear=19;
else
rear = rear-1;
}
else
CQ[rear]=item;
}
}
void delete()
{
int item;
if(front==-1&&rear==-1)
printf("CQ is empty\n");
else
if(front==rear)
{
printf(Deleted Element is %d\n,CQ[front]); front=rear=-1; }
else
{
item=CQ[front];
front=(front+1)%20;
printf("Element deleted is %d\n",item);
}
}
void display()
{
int i;
if(front==-1 && rear==-1)
printf("CQ is empty\n");
else
if(front==rear)
printf(Single Element in the queue is %d\n,CQ[front]);
else
{
printf(Circular Queue Elements are\n);
printf(%d\t,CQ[front]);
i=front;
while(i!=rear)
{
i=(i+1)%20;
printf("%d\t",CQ[i]);
}
} }

DS in C NEC, CSE Dept. Page 34


UNIT 3 Stacks and Queues

Deques: A deque (double ended queue) is a linear list in which all


insertions and deletions are made at the end of the list .A deque is also
called as deck (or) 'deque'.

A deque is more general a stack (or) queue, if it follows the approach LIFO
(or) FIFO respectively. Here two ends are referred to as right end (or) left
end of a deque.

A deque can be of two types: Input restricted deque and output restricted
deque.

An Input restricted deque is one where insertions are allowed at one


end while deletions are allowed at both ends.
An Output restricted deque is one where deletions are allowed at
one end while insertions are allowed at both ends.

A deque is commonly implemented as a circular array with two variables


left and right taking care of the ends of the deque.

Before performing deletion, the condition left=right is checked.


Before Insertion, the condition left= ((right+1) % size) is checked.
Insertion at Left end:

left =(left-1)%size

Insertion at Right end:

right =(right+1)%size

Deletion at Left end:

left =(left+1)%size

Deletion at Right end:

right =(right-1)%size

Here size is size of deque.

Example: Let DEQ [6] be a deque implemented as a circular queue

DS in C NEC, CSE Dept. Page 35


UNIT 3 Stacks and Queues

The contents of DEQ and that of Left and right are as given below.
DEQ: Left: 2 Right: 4
0 1 2 3 4 5

(i) Insert X at the Left end and y at the right end


DEQ: Left: 1 Right: 5
0 1 2 3 4 5

(ii) Delete twice from the right end


DEQ: Left: 1 Right: 3
0 1 2 3 4 5

(iii) Insert G, Q, M at the Left end


DEQ: Left: 4 Right: 3
0 1 2 3 4 5

Here no insertion is possible since the deque is full.


(v)Delete twice from the Left end
DEQ: Left: 0 Right: 3
0 1 2 3 4 5

Round Robin Algorithm: - The round robin is a simple scheduling


algorithm in which the queue data structure is used. A small unit of time
called time quantum is defined. All the runnable processors are kept in
queue. The CPU scheduler goes around this queue and allocates the CPU
for each process one by one for that time interval. New processes are added
at the rear end of the queue.

DS in C NEC, CSE Dept. Page 36


UNIT 3 Stacks and Queues

The Scheduler chooses the 1st process from the queue and assigns the CPU
to it. If the process does not finish its job, then that process will be added at
the rear end, if the process finishes its job in given time interval then it is
deleted from the queue. The CPU scheduler assigns the CPU to the next
process in the queue.

DS in C NEC, CSE Dept. Page 37

You might also like