C-Stacks and Queues
C-Stacks and Queues
Debasis Samanta
Computer Science & Engineering
Indian Institute of Technology Kharagpur
Spring-2017
Lecture #13
Stack & Queue
*Stack *Queue
* Basic principles * Basic principles
* Operation of stack * Operation of queue
* Stack using Array * Queue using Array
* Stack using Linked List * Queue using Linked List
* Applications of stack * Applications of queue
pop
create
STACK
isempty
isfull
PUSH
top
top
POP
top
top
PUSH OPERATION
top
POP OPERATION
top
void push (stack *s, int element) void push (stack **top, int element)
{ {
stack *new;
if (s->top == (MAXSIZE-1))
{ new = (stack *)malloc (sizeof(stack));
printf (“\n Stack overflow”); if (new == NULL)
exit(-1); {
} printf (“\n Stack is full”);
exit(-1);
else
}
{
s->top++; new->value = element;
s->st[s->top] = element; new->next = *top;
} *top = new;
}
}
• Indirect applications:
• Auxiliary data structure for algorithms
• Component of other data structures
A+B* C (A + (B * C)) (A + (B C *) ) A B C * +
Operands:
Add to postfix expression.
Close parenthesis:
pop stack symbols until an open parenthesis appears.
Operators:
Pop all stack symbols until a symbol of lower precedence appears. Then push
the operator.
End of input:
Pop all remaining stack symbols and add to the expression.
ABC D * +* E+ 5 + *(+ AB
6 C *(+ ABC
7 * *(+* ABC
8 D *(+* ABCD
Postfix notation
9 ) * ABCD*+
is also called as
10 + + ABCD*+*
Reverse Polish
Notation (RPN) 11 E + ABCD*+*E
12 ABCD*+*E+
Autumn 2016 30
Symbol Precedence Rank(r)
(f)
+, - 1 -1
*, / 2 -1
a, b, c 3 1
# 0 -
NEXTCHAR(INFIX) * Write(‘INVALID’)
* Exit
* 4. while (NEXT ≠ ‘#’) * end
* begin * PUSH(S, TOP, NEXT)
* NEXT NEXTCHAR(INFIX)
* end
*Algo. Contd…
CS 11001 : Programming and Data
33 Lecture #00: © DSamanta
Structures
Symbol Input Stack Rank (r)
precedenc precedenc
e e
(f) (g)
+, - 1 2 -1
*, / 3 4 -1
^ 6 5 -1
a, b, c 7 8 1
( 9 0 -
) 0 - -
* 3. NEXT * Write(‘INVALID’)
NEXTCHAR(INFIX) * Exit
end
* 4. while (NEXT ≠ ‘’) * if f(NEXT) ≠ g(S[TOP]
dequeue
create
QUEUE
isempty
size
ENQUEUE
front rear
DEQUEUE
front rear
struct queue
{
struct qnode *qfront, *qrear;
};
typedef struct queue QUEUE;
*Queue using
Array
CS 11001 : Programming and Data
47 Lecture #00: © DSamanta
Structures
Problem With Array Implementation
• The size of the queue depends on the number and order of enqueue
and dequeue.
• It may be situation where memory is available but enqueue is not
possible.
ENQUEUE DEQUEUE
Effective queuing storage area of array gets reduced.
0 N
front
front rearrear
* F 1 * Else F F + 1
5.Return * 5.Return(Y)
*Circular queue
CS 11001 : Programming and Data
49 Lecture #00: © DSamanta
Structures
* Circular Queue with another
implementation
*Types of Queues
CS 11001 : Programming and Data Stru 51 Lecture #00: © DSamanta
ctures
Applications of Queues
• Modelling and Simulation( a sub-field of Computer Science)
• Waiting lists
• Access to shared resources (e.g., printer)
• CPU Scheduling in Multiprogramming Environment(PQ)
• Three categories:
• Interrupts
• Interactive jobs
• Batch jobs