Stack and Que
Stack and Que
2
What is a Stack?
• Stack of Books
3
What is a Stack?
• Definition: A stack is a sequential collection of elements into which
new elements are inserted and deleted only at one end called top.
• As all the insertion and deletion in a stack is done from the top of
the stack, the lastly added element will be first to be removed from
the stack. That is the reason why stack is also called Last-In-First-
Out (LIFO) data structure.
• The operations: push (insert) and pop (delete)
pop push(o)
Top 3
2
7
6
Operations on Stack
push
pop
create
STACK
Isempty/
Underflow
Isfull/
Overflow
5
Stack Implementation
To perform push
operation:
-1 top++;
stack[top]=value;
• Reversing a string.
• Parsing
For example :
A+B
operator ‘+’ is written in between the operands A and B.
Expression conversion and evaluation.
The same expression when written in
Prefix notation: Polish notation:
+AB
As the operator ‘+’ is written before the operands A and B,
this notation is called prefix (pre means before).
Postfix notation :
AB+
the operator(s) are written after the operands, so it is called
the postfix notation (post means after), it is also known as
suffix notation or reverse polish notation.
Expression conversion and evaluation.
2*4+3^2÷(4+5)–6=
ABC^/DE* + AC* –
prefix notation.
• Here, we first write the operator and then the
operands. To convert the infix expression to
prefix expression, the steps :
(i) Completely parenthesize the infix
expression, according to order of priority.
(ii) Move each operator to its corresponding
left parentheses.
prefix notation.
• Convert, A/B^C + D * E – A * C to prefix
notation.
• Parenthesization gives :
a*b+c String
STACK
Conversion of Infix Expression to Prefix Expression :
c
a*b+c String
STACK
Conversion of Infix Expression to Prefix Expression :
c
a*b+c String
PUSH
STACK
Conversion of Infix Expression to Prefix Expression :
c
a*b+c String
+ STACK
Conversion of Infix Expression to Prefix Expression :
c
a*b+c String
+ STACK
Conversion of Infix Expression to Prefix Expression :
b c
a*b+c String
+ STACK
Conversion of Infix Expression to Prefix Expression :
b c
a*b+c String
PUSH
+ STACK
Conversion of Infix Expression to Prefix Expression :
b c
a*b+c String
*
+ STACK
Conversion of Infix Expression to Prefix Expression :
b c
a*b+c String
*
+ STACK
Conversion of Infix Expression to Prefix Expression :
a b c
a*b+c String
*
+ STACK
Conversion of Infix Expression to Prefix Expression :
a b c
a*b+c String
*
+ STACK
Conversion of Infix Expression to Prefix Expression :
a b c
a*b+c String
POP
*
+ STACK
Conversion of Infix Expression to Prefix Expression :
* a b c
a*b+c String
POP
+ STACK
Conversion of Infix Expression to Prefix Expression :
* a b c
a*b+c String
POP
+ STACK
Conversion of Infix Expression to Prefix Expression :
+ * a b c
a*b+c String
POP
STACK
Conversion of Infix Expression to Prefix Expression :
Conversion of Infix Expression to Prefix Expression :
Conversion of Infix Expression to Prefix Expression :
Reversing a String
M A T H S
String
String
PUSH
STACK
Reversing a String
M A T H S
String
String
M STACK
Reversing a String
M A T H S
String
String
PUSH
M STACK
Reversing a String
M A T H S
String
String
A
M STACK
Reversing a String
M A T H S
String
String
PUSH
A
M STACK
Reversing a String
M A T H S
String
String
PUSH
T
A
M STACK
Reversing a String
M A T H S
String
String
H PUSH
T
A
M STACK
Reversing a String
M A T H S
String
String
S
H PUSH
T
A
M STACK
Reversing a String
String
S
H POP
T
A
M STACK
Reversing a String
String
S
H POP ‘S’
T
A
M STACK
Reversing a String
S
String
H POP
T
A
M STACK
Reversing a String
S H
String
H POP ‘H’
T
A
M STACK
Reversing a String
S H T
String
POP ‘T’
T
A
M STACK
Reversing a String
S H T A
String
POP ‘A’
A
M STACK
Reversing a String
S H T A M
String
POP ‘M’
M STACK
Reversing a String
S H T A M
String
STACK
Reversing a String :
Reversing a String :
Recursive factorial
int fact_rec(int n)
{
if (n == 0)
return 1;
return n * fact_rec(n-1);
}
STACK
Recursive factorial
int fact_rec(int n)
{
if (n == 0)
return 1;
return n * fact_rec(n-1);
}
n←4
return ← ?
STACK
Recursive factorial
int fact_rec(int n)
{
if (n == 0)
return 1;
return n * fact_rec(n-1);
}
n←4
return ← 4 * ?
STACK
Recursive factorial
int fact_rec(int n)
{
if (n == 0)
return 1;
return n * fact_rec(n-1);
}
n←3
return ← ?
n←4
return ← 4 * ?
STACK
Recursive factorial
int fact_rec(int n)
{
if (n == 0)
return 1;
return n * fact_rec(n-1);
}
n←3
return ← 3 * ?
n←4
return ← 4 * ?
STACK
Recursive factorial
int fact_rec(int n)
{
if (n == 0)
return 1;
return n * fact_rec(n-1);
}
n←2
return ← ?
n←3
return ← 3 * ?
n←4
return ← 4 * ?
STACK
Recursive factorial
int fact_rec(int n)
{
if (n == 0)
return 1;
return n * fact_rec(n-1);
}
n←2
return ← 2 * ?
n←3
return ← 3 * ?
n←4
return ← 4 * ?
STACK
Recursive factorial
int fact_rec(int n)
{
if (n == 0)
return 1;
STACK
Recursive factorial
int fact_rec(int n)
{
if (n == 0)
return 1;
STACK
Recursive factorial
int fact_rec(int n)
{
if (n == 0)
return 1; n←0
return ← ?
return n * fact_rec(n-1); n←1
} return ← 1 * ?
n←2
return ← 2 * ?
n←3
return ← 3 * ?
n←4
return ← 4 * ?
STACK
Recursive factorial
int fact_rec(int n)
{
if (n == 0)
return 1; n←0
return ← 1
return n * fact_rec(n-1); n←1
} return ← 1 * ?
n←2
return ← 2 * ?
n←3
return ← 3 * ?
n←4
return ← 4 * ?
STACK
Recursive factorial
int fact_rec(int n)
{
if (n == 0)
return 1;
STACK
Recursive factorial
int fact_rec(int n)
{
if (n == 0)
return 1;
return n * fact_rec(n-1);
}
n←2
return ← 2 * 1
n←3
return ← 3 * ?
n←4
return ← 4 * ?
STACK
Recursive factorial
int fact_rec(int n)
{
if (n == 0)
return 1;
return n * fact_rec(n-1);
}
n←3
return ← 3 * 2
n←4
return ← 4 * ?
STACK
Recursive factorial
int fact_rec(int n)
{
if (n == 0)
return 1;
return n * fact_rec(n-1);
}
n←3
return ← 6
n←4
return ← 4 * 6
STACK
Recursive factorial
int fact_rec(int n)
{
if (n == 0)
return 1;
return n * fact_rec(n-1);
}
24
n←4
return ← 24
STACK
Queues :
• Definition :
A queue is logically a first in first out (FIFO
or first come first serve) linear data structure.
QUEUE
Before Enqueue
Queues
• Operations
65
QUEUE
Before Enqueue
Queues
• Operations
65
QUEUE
Before Enqueue
Queues
• Operations
72 65
QUEUE
After Enqueue
Queues
• Operations
81 72 65
QUEUE
After Enqueue
Queues
• Operations
81 72 65
QUEUE
After Enqueue
Queues
• Operations
81 72 65
QUEUE
Before Dequeue
Queues
• Operations
81 72 65
QUEUE
After Dequeue
Queues
• Operations
81 72
QUEUE
After Dequeue
Queue using Array
1. elements are added from one end (i.e. rear
end or tail of Queue) and
45 61 56 72 81
front end or head QUEUE rear end or tail
45 61 56 72 81
rear end or tail QUEUE front end or head
Queue using Array
rear end = -1
4 3 2 1 0
81
front end = 0
Queue using Array
rear end = 0
4 3 2 1 0
81 81
front end = 0
Rear =Rear+1
Queue using Array
rear end = 0
4 3 2 1 0
23 81
front end = 0
Queue using Array
rear end = 1
0
4 3 2 1 0
23 23 81
front end = 0
Queue using Array
rear end = 2
1
4 3 2 1 0
45 45 23 81
front end = 0
Queue using Array
rear end = 3
2
4 3 2 1 0
61 61 45 23 81
front end = 0
Queue using Array
rear end = 4
3
4 3 2 1 0
37 37 61 45 23 81
front end = 0
Queue using Array
rear end = 4
4 3 2 1 0
11 37 61 45 23 81
front end = 0
4 3 2 1 0
37 61 45 23 81
front end = 0
4 3 2 1 0
37 61 45 23 81
front end = 1
0
Queue using Array
rear end = 4
3 2 1 0 0
37 61 45 23
front end = 2
1
Queue using Array
rear end = 4
4 3 2 1 0
37 61 45
front end = 32
Queue using Array
rear end = 4
4 3 2 1 0
37 61
front end = 43
Queue using Array
rear end = 4
4 3 2 1 0
37
front end = 4
4 3 2 1 0
front end = 5
PROGRAM FOR
QUEUE USING ARRAY
Expt 7-A:
Program overview
• Delaration
─ Header
─ Global array
• Delaration
─ Header
─ Global array
Queue using Array
#include<stdio.h>
int que[MAX];
int rear = - 1;
int front = 0;
Queue using Array
Main function ?
Queue using Array
void main ()
{
while(1)
{
switch case
{
[Link]
[Link]
[Link]
[Link]
}
}
}
void main()
{
int choice;
while (1)
{
printf("\[Link] \n [Link]\n [Link]\n [Link]\n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1: insert();
break;
case 2: del();
break;
case 3: display();
break;
case 4: exit(1);
default: printf("Wrong choice\n");
break;
}
}
}
Queue using Array
• DISPLAY function
rear end = -1
4 3 2 1 0
front end = 0
• DISPLAY function
rear end = 4 front end = 0
4 3 2 1 0
37 61 45 23 81
• INSERT function
rear end = 4
4 3 2 1 0
13 96 67 45 34
front end = 0
• DELETE function
rear end = -1
4 3 2 1 0
front end = 0
• DELETE function
rear end = 2
4 3 2 1 0
45 11 21
If we delete 2 elements front end = 0
then….
Queue using Array
• DELETE function
rear end = 2
4 3 2 1 0
45
Again 1 delete front end = 2
Queue using Array
• DELETE function
rear end = 2
4 3 2 1 0
front end = 2
void deque()
{
if (front > rear)
{
printf("\n Queue Underflow\n");
}
else
{
printf("\n Element deleted from queue is : %d\n",que[front]);
front ++;
}
}
Queue using Linked List ...?
Data 2 •
Head Data 1 •
NULL
NULL
Data 2 •
Head Data 1 •
NULL
Queue using Linked List
Where will be
front & rear?
Queue using Linked List
Front NULL
Data 1 •
Initial
Rear NULL
Queue using Linked List
Front = NULL
Data 2 •
Data 1 • Initial
new1
NULL
Rear = NULL
Queue using Linked List
Front
Data 3 •
new1
Data 1 • Data 2 •
Rear
Queue using Linked List
Front
Data 4 •
new1
Rear
Queue using Linked List
Front
new1
Rear
Queue using Linked List
Front
Rear
Now deleting
Queue using Linked List
Copy front to temp Data •
TEMP
Front
Rear
Now deleting
Queue using Linked List
Point front
towhere
Copy front temp
temp is pointing
Data 1 •
TEMP
Front
Rear
Now deleting
Queue using Linked List
Point front
towhere
Copy front temp
temp is pointing
Data 2 •
TEMP
Front
Rear
Now deleting
EXPT 7 – B:
PROGRAM FOR
QUEUE USING LINKED LIST
Expt 7-B:
Program overview
• Delaration
─ Header
─ Global Linked List
• Delaration
─ Header
─ Global Linked List
Queue using Linked List
#include<stdio.h>
#include<stdlib.h>
/* Global declarations of Linked List*/
typedef struct node
{
int data;
struct node *next; Data •
} Que; info next
Que *front, *rear; struct node
Queue using Linked List
If
Queue is empty
Rear NULL
DISPLAY function
Front If
Rear
• Temp = front
• While front ! = NULL
• Print (info)
void display()
{
node * temp;
if (front == NULL)
{
printf("\nQueue is empty");
}
else
{
temp = front;
printf("\nQueue is : \n");
while (temp != NULL)
{
printf(" %d->", temp->data);
temp = temp->next;
} printf(“-> NULL ");
}
}
ENQUEUE function
Front
NULL
TEMP
Rear
If
Queue Underflow
Rear NULL
DEQUEUE function
Point front
towhere
Copy front temp
temp is pointing
Data 1 •
TEMP
Front
Rear
void dequeue()
{
struct node *temp;
int ele;
if (front == NULL)
{
printf("\nUnder Flow");
}
else
{
temp = front;
ele = temp->data;
front = front->next;
free(temp);
printf("\nThe deleted element = %d\n", ele);
}
}
Front & Rear positions
EmptyQueue :?
IsFull :?
Comparing queue implementations
Big-O Comparison of Queue Operations
Operation Array Linked
Implementation Implementation
MakeEmpty O(1) O(N)
IsFull O(1) O(1)
IsEmpty O(1) O(1)
Enqueue O(1) O(1)
Dequeue O(1) O(1)
A circular linked queue design
Circular Queue :
Circular Queue :
Where...?
Initial ...
Double-Ended Queues
• Queue has a front and a rear.
• Elements can be added at the rear &
• Removed at the front.
Example:
header trailer
header trailer
header trailer
Toronto
Deleting an Element at the tail
header trailer
Seattle
The Deque Abstract Data Type
181
Infix to Postfix Conversion