Welcome To Our
Presentation On
Stack and Queue
Supervised By
Mujibur Rahman Majumdar
Lecturer
Dept. of Computer Science and Engineering
International Standard University
Group Name: "Dynamic Squad"
Group Member:
Md. Riazul Islam
ID: 201030015
Md. Zahidul Islam
ID: 201030032
Israt Jahan Mysha
ID: 201030009
Saurav Roy
ID: 201030012
Munya Dilshad
ID: 201030049
Muhammad Akib Jabed
ID: 201030035
Data Structure
Linear Data Structure
Non Linear Data Structure
ARRAY
QUEUE
STACK
What is Linear Data Structure?
In linear data structure, data is arranged in linear sequence.
Data items can be traversed in a single run.
In linear data structure elements are accessed or placed in
contiguous(together in sequence) memory location.
What is Stack?
A stack is called a last-in-first-out
(LIFO) collection. This means that the
last thing we added (pushed) is the first
thing that gets pulled (popped) off.
A stack is a sequence of items that are
accessible at only one end of the
sequence.
Operations that can be
performed on STACK
• PUSH: It is used to insert items into the
stack.
• POP: It is used to delete items from
stack.
• TOP: It represents the current location
of data in stack.
ALGORITHM OF INSERTION
IN STACK: (PUSH)
1. Insertion (a,top, item, max)
2. If top = max then
print 'Stack Overflow'
exit
else
3. top = top + 1
end if
4. a [top]= item
5. Exit
ALGORITHM OF DELETION
IN STACK: (POP) 1. Deletion(a,top,item)
2. If top=0 then
print ‘STACK
UNDERFLOW’
exit else
3. item=a[top] end if
4. top=top-1
5. Exit
ALGORITHM
OF DISPLAY
IN STACK:
1.Display(top,i,a[i])
2.If top=0 then
Print ‘STACK EMPTY’
Exit
Else
3. For i=top to 0
Print a[i]
End for
4.exit
E
S
R
E
V
E
R
TOP
STACK
APPLICATIONS OF
STACKS ARE:
Reversing Strings:
• A simple application of stack is
reversing strings.
To reverse a string , the characters of
string are pushed onto the stack one
by one as the string is read from left
to right.
• Once all the characters
of string are pushed onto stack, they
are popped one by one. Since the
character last pushed in comes out first,
subsequent pop operation results in the
reversal of the string.
For example:
To reverse the string ‘REVERSE’
the string isread from left to right
and its characters arepushed .
Like:
TOP
STACK
String Is:
REVERSE
E
S
R
E
V
E
R
What is Queue?
Queue is an ADT data structure similar to stack, except
that the first item to be inserted is the first one to be
removed.
This mechanism is called First-In-First-Out (FIFO).
Placing an item in a queue is called “insertion or enqueue”,
which is done at the end of the queue called “rear”.
Removing an item from a queue is called “deletion or
dequeue”, which is done at the other end of the queue
called “front”.
Some of the applications are : printer queue, keystroke
queue, etc.
Dqueue
It is a double-
ended queue.
Items can be
inserted and
deleted from either
ends.More
versatile data
structure than
stack or queue.
E.g. policy-based
application (e.g.
low priority go to
the end, high go to
the front)
In a case where
you want to sort
the queue once in
a while, What
sorting algorithm
will you use?
The operation of Queue:
1. Placing an item in a queue is called “insertion
or enqueue”, which is done at the end of
the queue called “rear”. Placing an item in a
queue is called “insertion or enqueue”, which is
done at the end of the queue called “rear”.
2. Removing an item from
a queue is called “deletion or dequeue”, which
is done at the other end of the queue
called “front".
Algorithm of Queue
1. If (rear = maxsize – 1)
print ("queue overflow") and return
2. Else
rear = rear + 1
queue [rear] = item
1. If (front = rear)
print ("queue empty") and return
2. Else
Front = Front + 1
item = queue [Front];
Return item
Q-INSERT Q-DELETE
Application of Queue
Real life examples
Waiting in line
Waiting on hold for
tech support
Applications
related to
Computer Science
Round robin
scheduling
Job scheduling
(FIFO Scheduling)
Key board buffer
Difference
between Stack
and Queue:
Stacks Queues
Stacks are based on the LIFO principle, i.e.,
the element inserted at the last, is the first
element to come out of the list.
Queues are based on the FIFO principle, i.e.,
the element inserted at the first, is the first
element to come out of the list.
Insertion and deletion in stacks takes place
only from one end of the list called the top.
Insertion and deletion in queues takes place
from the opposite ends of the list. The
insertion takes place at the rear of the list and
the deletion takes place from the front of the
list.
Insert operation is called push operation. Insert operation is called enqueue operation.
Delete operation is called pop operation. Delete operation is called dequeue operation.
In stacks we maintain only one pointer to
access the list, called the top, which always
points to the last element present in the list.
In queues we maintain two pointers to access
the list. The front pointer always points to the
first element inserted in the list and is still
present, and the rear pointer always points to
the last inserted element.
Stack is used in solving problems works on
recursion.
Queue is used in solving problems having
sequential processing.
Stack Push
Pop operation
#include <stdio.h>
int stack[8];
int top = -1,n;
int push(int data) {
if(top == n)
{
printf("nStack overflow");
}
else
{
top = top + 1;
stack[top] = data;
}
}
void pop()
{
if(top == -1)
printf("Underflow");
else
top = top -1;
}
void show()
{
printf("n");
for (int i=top;i>=0;i--)
{
printf("%dn",stack[i]);
}
if(top == -1)
{
printf("Stack is empty");
}
}
int main() {
Printf ("Maximum index in stack:
");
Scanf ("%d",&n);
push(3);
push(5);
push(9);
push(1);
push(12);
push(15);
show();
printf("n PoP operation");
pop();
show();
return 0;
}
Queue
Enqueue() and
dequeue()
operation
#include<stdio.h>
int queue[100],n;
int front = -1;
int rear = -1;
void enqueue(int num)
{
if(rear==n)
{
printf("nQueue overflow");
}
else
{
if(front==-1)
{
front =0;
}
rear = rear+1;
queue[rear] = num;
showQueue();
}
}
void dequeue()
{
if(front ==-1 || front>rear)
{
printf("nQueue underflow");
}
else
{
front = front +1;
showQueue();
}
}
void showQueue()
{
printf("n");
if(front==-1)
{
printf("Queue is empty");
}
else
{
for(int i = front;i<=rear;i++)
{
printf("%d ", queue[i]);
}
printf(" Front = %d and Rear =
%d",front,rear);
}
}
int main()
{
printf("Maximum index of queue: ");
scanf("%d",&n);
dequeue();
enqueue(5);
enqueue(7);
enqueue(1);
enqueue(1);
enqueue(1);
dequeue();
dequeue();
dequeue();
dequeue();
}
Any Query
Thank You!!

Stack & Queue

  • 1.
    Welcome To Our PresentationOn Stack and Queue
  • 2.
    Supervised By Mujibur RahmanMajumdar Lecturer Dept. of Computer Science and Engineering International Standard University
  • 3.
    Group Name: "DynamicSquad" Group Member: Md. Riazul Islam ID: 201030015 Md. Zahidul Islam ID: 201030032 Israt Jahan Mysha ID: 201030009 Saurav Roy ID: 201030012 Munya Dilshad ID: 201030049 Muhammad Akib Jabed ID: 201030035
  • 4.
    Data Structure Linear DataStructure Non Linear Data Structure ARRAY QUEUE STACK
  • 5.
    What is LinearData Structure? In linear data structure, data is arranged in linear sequence. Data items can be traversed in a single run. In linear data structure elements are accessed or placed in contiguous(together in sequence) memory location.
  • 6.
    What is Stack? Astack is called a last-in-first-out (LIFO) collection. This means that the last thing we added (pushed) is the first thing that gets pulled (popped) off. A stack is a sequence of items that are accessible at only one end of the sequence.
  • 7.
    Operations that canbe performed on STACK • PUSH: It is used to insert items into the stack. • POP: It is used to delete items from stack. • TOP: It represents the current location of data in stack.
  • 8.
    ALGORITHM OF INSERTION INSTACK: (PUSH) 1. Insertion (a,top, item, max) 2. If top = max then print 'Stack Overflow' exit else 3. top = top + 1 end if 4. a [top]= item 5. Exit
  • 9.
    ALGORITHM OF DELETION INSTACK: (POP) 1. Deletion(a,top,item) 2. If top=0 then print ‘STACK UNDERFLOW’ exit else 3. item=a[top] end if 4. top=top-1 5. Exit
  • 10.
    ALGORITHM OF DISPLAY IN STACK: 1.Display(top,i,a[i]) 2.Iftop=0 then Print ‘STACK EMPTY’ Exit Else 3. For i=top to 0 Print a[i] End for 4.exit E S R E V E R TOP STACK
  • 11.
    APPLICATIONS OF STACKS ARE: ReversingStrings: • A simple application of stack is reversing strings. To reverse a string , the characters of string are pushed onto the stack one by one as the string is read from left to right. • Once all the characters of string are pushed onto stack, they are popped one by one. Since the character last pushed in comes out first, subsequent pop operation results in the reversal of the string. For example: To reverse the string ‘REVERSE’ the string isread from left to right and its characters arepushed . Like: TOP STACK String Is: REVERSE E S R E V E R
  • 12.
    What is Queue? Queueis an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. This mechanism is called First-In-First-Out (FIFO). Placing an item in a queue is called “insertion or enqueue”, which is done at the end of the queue called “rear”. Removing an item from a queue is called “deletion or dequeue”, which is done at the other end of the queue called “front”. Some of the applications are : printer queue, keystroke queue, etc.
  • 13.
    Dqueue It is adouble- ended queue. Items can be inserted and deleted from either ends.More versatile data structure than stack or queue. E.g. policy-based application (e.g. low priority go to the end, high go to the front) In a case where you want to sort the queue once in a while, What sorting algorithm will you use?
  • 14.
    The operation ofQueue: 1. Placing an item in a queue is called “insertion or enqueue”, which is done at the end of the queue called “rear”. Placing an item in a queue is called “insertion or enqueue”, which is done at the end of the queue called “rear”. 2. Removing an item from a queue is called “deletion or dequeue”, which is done at the other end of the queue called “front".
  • 15.
    Algorithm of Queue 1.If (rear = maxsize – 1) print ("queue overflow") and return 2. Else rear = rear + 1 queue [rear] = item 1. If (front = rear) print ("queue empty") and return 2. Else Front = Front + 1 item = queue [Front]; Return item Q-INSERT Q-DELETE
  • 16.
    Application of Queue Reallife examples Waiting in line Waiting on hold for tech support Applications related to Computer Science Round robin scheduling Job scheduling (FIFO Scheduling) Key board buffer
  • 17.
    Difference between Stack and Queue: StacksQueues Stacks are based on the LIFO principle, i.e., the element inserted at the last, is the first element to come out of the list. Queues are based on the FIFO principle, i.e., the element inserted at the first, is the first element to come out of the list. Insertion and deletion in stacks takes place only from one end of the list called the top. Insertion and deletion in queues takes place from the opposite ends of the list. The insertion takes place at the rear of the list and the deletion takes place from the front of the list. Insert operation is called push operation. Insert operation is called enqueue operation. Delete operation is called pop operation. Delete operation is called dequeue operation. In stacks we maintain only one pointer to access the list, called the top, which always points to the last element present in the list. In queues we maintain two pointers to access the list. The front pointer always points to the first element inserted in the list and is still present, and the rear pointer always points to the last inserted element. Stack is used in solving problems works on recursion. Queue is used in solving problems having sequential processing.
  • 18.
    Stack Push Pop operation #include<stdio.h> int stack[8]; int top = -1,n; int push(int data) { if(top == n) { printf("nStack overflow"); } else { top = top + 1; stack[top] = data; } } void pop() { if(top == -1) printf("Underflow"); else top = top -1; } void show() { printf("n"); for (int i=top;i>=0;i--) { printf("%dn",stack[i]); } if(top == -1) { printf("Stack is empty"); } } int main() { Printf ("Maximum index in stack: "); Scanf ("%d",&n); push(3); push(5); push(9); push(1); push(12); push(15); show(); printf("n PoP operation"); pop(); show(); return 0; }
  • 19.
    Queue Enqueue() and dequeue() operation #include<stdio.h> int queue[100],n; intfront = -1; int rear = -1; void enqueue(int num) { if(rear==n) { printf("nQueue overflow"); } else { if(front==-1) { front =0; } rear = rear+1; queue[rear] = num; showQueue(); } } void dequeue() { if(front ==-1 || front>rear) { printf("nQueue underflow"); } else { front = front +1; showQueue(); } } void showQueue() { printf("n"); if(front==-1) { printf("Queue is empty"); } else { for(int i = front;i<=rear;i++) { printf("%d ", queue[i]); } printf(" Front = %d and Rear = %d",front,rear); } } int main() { printf("Maximum index of queue: "); scanf("%d",&n); dequeue(); enqueue(5); enqueue(7); enqueue(1); enqueue(1); enqueue(1); dequeue(); dequeue(); dequeue(); dequeue(); }
  • 20.
  • 21.