Stacks and Queues
Stacks and Queues
STRUCTURES
DEBASIS SAMANTA
COMPUTER SCIENCE & ENGINEERING
SPRING-2017
CS 11001 : Programming and Data Structures Lecture #00: © DSamanta
Lecture #13
Stack & Queue
TODAY’S DISCUSSION…
CS 11001 : Programming and Data Structures Lecture #00: © DSamanta
*Stack • Queue
• Basic principles
* Basic principles
• Operation of queue
* Operation of stack • Queue using Array
* Stack using Array • Queue using Linked List
• Applications of queue
* Stack using Linked List
* Applications of stack
CS 11001 : Programming and Data Structures Lecture #00: © DSamanta
STACK
BASIC IDEA
CS 11001 : Programming and Data Structures Lecture #00: © DSamanta
5
• A stack is an Abstract Data Type (ADT), commonly used in most
programming languages. It is named stack as it behaves like a real-world
stack, for example – a deck of cards or a pile of plates, etc.
STACK REPRESENTATION
CS 11001 : Programming and Data Structures Lecture #00: © DSamanta
7
push
pop
create
STACK
isempty
isfull
STACK: LAST-IN-FIRST-OUT (LIFO)
CS 11001 : Programming and Data Structures Lecture #00: © DSamanta
8
• void push (stack *s, int element);
/* Insert an element in the stack */
• int pop (stack *s);
/* Remove and return the top element */
• void create (stack *s);
/* Create a new stack */
• int isempty (stack *s);
/* Check if stack is empty */
• int isfull (stack *s);
/* Check if stack is full */
Assumption: stack contains integer elements!
CS 11001 : Programming and Data Structures Lecture #00: © DSamanta
10
PUSH
top
top
Pop using Stack
Autumn 2016 Autumn 2016
11
POP
top
top
CS 11001 : Programming and Data Structures Lecture #00: © DSamanta
12
13
PUSH OPERATION
top
Pop using Linked List
Autumn 2016 Autumn 2016
14
POP OPERATION
top
Basic Idea Autumn 2016 Autumn 2016
15
16
17
18
void push (stack *s, int element) void push (stack **top, int element)
{ {
stack *new;
if (s->top == (MAXSIZE-1))
{ new = (stack *)malloc
printf (“\n Stack (sizeof(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;
}
}
19
20
21
22
#include <stdio.h>
#define MAXSIZE 100
struct lifo
{
int st[MAXSIZE];
int top;
};
typedef struct lifo stack;
main() {
stack A, B;
create(&A);
create(&B);
push(&A,10);
push(&A,20);
push(&A,30);
push(&B,100);
push(&B,5);
printf (“%d %d”, pop(&A), pop(&B));
push (&A, pop(&B));
if (isempty(&B))
printf (“\n B is empty”);
return;
}
EXAMPLE: A STACK USING LINKED
CS 11001 : Programming and Data Structures Lecture #00: © DSamanta
LIST
23
#include <stdio.h>
struct lifo
{
int value;
struct lifo *next;
};
typedef struct lifo stack;
main() {
stack *A, *B;
create(&A);
create(&B);
push(&A,10);
push(&A,20);
push(&A,30);
push(&B,100);
push(&B,5);
printf (“%d %d”, pop(&A), pop(&B));
push (&A, pop(&B));
if (isempty(B))
printf (“\n B is empty”);
return;
}
APPLICATIONS OF STACKS
CS 11001 : Programming and Data Structures Lecture #00: © DSamanta
24
• Direct applications:
• Page-visited history in a Web browser
• Undo sequence in a text editor
• Chain of method calls in the Java Virtual Machine
• Validate XML
• Indirect applications:
• Auxiliary data structure for algorithms
• Component of other data structures
INFIX AND POSTFIX NOTATIONS
CS 11001 : Programming and Data Structures Lecture #00: © DSamanta
25
• Infix: operators placed between operands:
A+B*C
• Postfix: operands appear before their operators:-
ABC*+
• There are no precedence rules to learn in postfix notation, and parentheses are
never needed
INFIX TO POSTFIX
Autumn 2016
26
Infix Postfix
A+B AB+
A+B*C ABC*+
(A + B) * C AB+C*
A+B*C+D ABC*+D+
(A + B) * (C + D) AB+CD+*
A*B+C*D AB*CD*+
A+B*C (A + (B * C)) (A + (B C *) ) A B C * +
27
• Use a stack for processing operators (push and pop operations).
• Scan the sequence of operators and operands from left to right and perform
one of the following:
• output the operand,
• push an operator of higher precedence,
• pop an operator and output, till the stack top contains operator of a lower precedence and
push the present operator.
THE ALGORITHM STEPS
CS 11001 : Programming and Data Structures Lecture #00: © DSamanta
28
1. Print operands as they arrive.
2. If the stack is empty or contains a left parenthesis on top, push the incoming operator
onto the stack.
3. If the incoming symbol is a left parenthesis, push it on the stack.
4. If the incoming symbol is a right parenthesis, pop the stack and print the operators until
you see a left parenthesis. Discard the pair of parentheses.
5. If the incoming symbol has higher precedence than the top of the stack, push it on the
stack.
6. If the incoming symbol has equal precedence with the top of the stack, use association.
If the association is left to right, pop and print the top of the stack and then push the
incoming operator. If the association is right to left, push the incoming operator.
7. If the incoming symbol has lower precedence than the symbol on the top of the stack,
pop the stack and print the top operator. Then test the incoming operator against the
new top of stack.
8. At the end of the expression, pop and print all operators on the stack. (No parentheses
should remain.)
INFIX TO POSTFIX CONVERSION
CS 11001 : Programming and Data Structures Lecture #00: © DSamanta
29
Requires operator precedence information
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.
Infix to Postfix Rules
Autumn 2016
30
31
QUEUE
BASIC IDEACS 11001 : Programming and Data Structures Lecture #00: © DSamanta
32
• Queue is an abstract data structure, somewhat similar to Stacks. Unlike
stacks, a queue is open at both its ends. One end is always used to insert data
(enqueue) and the other is used to remove data (dequeue).
QUEUE REPRESENTATION
CS 11001 : Programming and Data Structures Lecture #00: © DSamanta
33
34enqueue
dequeue
create
QUEUE
isempty
size
QUEUE: First-In-First-Out (LIFO)
void enqueue (queue *q, int element);
/* Insert an element in the queue */
int dequeue (queue *q);
/* Remove an element from the queue */
queue *create();
/* Create a new queue */
int isempty (queue *q);
/* Check if queue is empty */
int size (queue *q);
/* Return the no. of elements in queue */
Assumption: queue contains integer elements!
35
Autumn 2016 Autumn 2016
CS 11001 : Programming and Data Structures Lecture #00: © DSamanta
36
37
• Basic idea:
• Create a linked list to which items would be added to one end and
deleted from the other end.
• Two pointers will be maintained:
• One pointing to the beginning of the list (point from where
elements will be deleted).
• Another pointing to the end of the list (point where new Rear
elements will be inserted).
38
ENQUEUE
front rear
QUEUE: LINKED LIST STRUCTURE
Autumn 2016 Autumn 2016
39
DEQUEUE
front rear
EXAMPLE :QUEUE USING LINKED LIST
CS 11001 : Programming and Data Structures Lecture #00: © DSamanta
40
struct qnode
{
int val;
struct qnode *next;
};
struct queue
{
struct qnode *qfront, *qrear;
};
typedef struct queue QUEUE;
41
int size (queue *q)
{
queue *q1;
int count=0;
q1=q;
while(q1!=NULL) int dequeue (queue *q)
{ {
q1=q1->next; int val;
count++; queue *q1,*prev;
} q1=q;
return count; while(q1->next!=NULL)
} {
prev=q1;
q1=q1->next;
}
val=q1->val;
int peek (queue *q)
prev->next=NULL;
{
free(q1);
queue *q1;
return (val);
q1=q;
}
while(q1->next!=NULL)
q1=q1->next;
return (q1->val);
}
PROBLEM WITH ARRAY
Autumn 2016 Autumn 2016
IMPLEMENTATION
42
• 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
43
• Direct applications:-
• Waiting lists
• Access to shared resources (e.g., printer)
• Multiprogramming
• Indirect applications:-
• Auxiliary data structure for algorithms
• Component of other data structures
CS 11001 : Programming and Data Structures Lecture #00: © DSamanta
44 Any question?
Problems
45 to ponder…
CS 11001 : Programming and Data Structures Lecture #00: © DSamanta
Problems
46 for practice…