Stack & Queue
DATA STRUCTURES
Govt. Madhav Science College Ujjain
Department of Computer ScienceAnd application
PROF. ANIL KUMAR PRAJAPATI
What is a stack?
◦ Stack is linear data structure, working in last in first
out(LIFO) manner. It is an Abstract Data Type
(ADT), commonly used in most programming
languages.
◦ In this type of data structure, we can extract the
date from the end from which we enter the data.
◦ Push operation is used to enter data in stack and
pop operation is used to remove data.
Stack Representation
Top
Data Element
04
Data Element
03
Data Element
02
Data Element
01
Top
Data Element
04
Data Element
03
Data Element
02
Data Element
01
Data Element 05
Push
Data Element 05
Pop
Insertion Deletion
Algorithm for insert data into stack
Step 1. Check stack if full
If Top=Max-1
Print stack is full and exit
Step 2: Increment to by one
Top=Top+1
Step 3: Insert data into stack
Stack[TOP]=data
Step 4: End
1
2
1
3
2
1
4
3
2
1
5
4
3
2
1
Push
2
Push
3
Push
4
Push
5
Push
1
Max
size
Push (Insert data item into Stack)
For inserting data item into stack we use push operation.
Insertion
Top
Data 3
Data 2
Data 1
Data 1, Data 2, Data 3, Data 4, Data 5,
Insertion
Top
Data 4
Data 3
Data 2
Data 1
Insertion
Top
Data 2
Data 1
Insertion
Top
Data 1
Push
Algorithm for insert data into stack
Step 1: Check stack is empty
If TOP=-1
Print Stack is empty and Exit
Step 2: delete an element from stack top
Set Del_element=Stack[Top]
Step 3: decrement stack size by one
Top=Top-1
Step 4: End
5
4
3
2
1
4
3
2
1
3
2
1
2
1
Max
size Pop
5
Pop
4 Pop
3
1
Pop
2
Pop (Deleting data item from Stack)
for deleting data item into stack we use Pop operation.
Deletion
Top
Data 2
Data 1
Data 1, Data 2, Data 3, Data 4, Data 5,
Deletion
Top
Data 1
Deletion
Top
Data 3
Data 2
Data 1
Deletion
Top
Data 4
Data 3
Data 2
Data 1
pop
What is Queue
 A Queue is a linear structure which follows a particular order in
which the operations are performed. The order is First In First
Out (FIFO). In Queue the allocation of data are continuous
manner.
 insertion and deletion is done from different ends in the Queue,
using rear for insertion and front for Dilation.
 Queue having two different formats linear queue and circular
queue.
Types of Queue
broadly Queue are divided into two types such as linear and circular but
linear queue having also two different variations, double ended queue or
Dequeue and insertion restricted and deletion restricted.
10 20 30 40 50
Front
Rear
Linear Queue
10
50 30
20
40
60
Circular Queue
Rear
Front
10 20 30 40 50
10 20 30 40 50
Deletion restricted Queue
Double Ended Queue
Insertion restricted Queue
10 20 30 40 50
Rear
Front
Front
Rear
Front
Rear
Rear
Front
Rear
Front
Insertion Data item into Queue
Rear
Front
Front
Front
Front
Front
Rear
Rear
To insert data element into queue, we use rear pointer all data element inserted only rear end.
Algorithm for Insert an element into Queue
Step 1: Check if the queue is full.
If rear == max-size-1
Print Queue is full and exit
Step 2: Add and element into Queue
SET REAR = REAR + 1
Step 3: data element insert at rear pointer .
Set QUEUE[REAR] = NUM
Step 4: return success.
10
10 20
10 20 30
10 20 30 40
Rear
Rear
10 20 30 40 50
Rear
Front
Deletion Data item From Queue
Algorithm for Delete an element From Queue
Step 1: Check if the queue is Empty.
If FRONT == -1 or FRONT >REAR
Print Queue is Empty and exit
Step 2: Delete an data element from Queue
SET FRONT = FRONT + 1
Step 3: data element insert at rear pointer .
Set Data element = QUEUE[FRONT]
Step 4: return success.
To Delete data element from queue, we use front pointer all data elements delete
only front end.
10 20 30 40 50
Front
Rear
20 30 40 50
30 40 50
40 50
50
Front
Front
Front
Front
Rear
Rear
Rear
Rear
Circular Queue
Circular Queue is a linear data structure in
which the operations are performed based
on FIFO (First In First Out) principle and
the last position is connected back to the
first position to make a circle. Circular
queue also follow contiguous allocation of
data element. In the circular queue, for
insertion and deletion we use rear and front
pointer like linear queue
Thanks and Regards:
Anil Kumar Prajapati

Stack and queue

  • 1.
    Stack & Queue DATASTRUCTURES Govt. Madhav Science College Ujjain Department of Computer ScienceAnd application PROF. ANIL KUMAR PRAJAPATI
  • 2.
    What is astack? ◦ Stack is linear data structure, working in last in first out(LIFO) manner. It is an Abstract Data Type (ADT), commonly used in most programming languages. ◦ In this type of data structure, we can extract the date from the end from which we enter the data. ◦ Push operation is used to enter data in stack and pop operation is used to remove data.
  • 3.
    Stack Representation Top Data Element 04 DataElement 03 Data Element 02 Data Element 01 Top Data Element 04 Data Element 03 Data Element 02 Data Element 01 Data Element 05 Push Data Element 05 Pop Insertion Deletion
  • 4.
    Algorithm for insertdata into stack Step 1. Check stack if full If Top=Max-1 Print stack is full and exit Step 2: Increment to by one Top=Top+1 Step 3: Insert data into stack Stack[TOP]=data Step 4: End 1 2 1 3 2 1 4 3 2 1 5 4 3 2 1 Push 2 Push 3 Push 4 Push 5 Push 1 Max size
  • 5.
    Push (Insert dataitem into Stack) For inserting data item into stack we use push operation. Insertion Top Data 3 Data 2 Data 1 Data 1, Data 2, Data 3, Data 4, Data 5, Insertion Top Data 4 Data 3 Data 2 Data 1 Insertion Top Data 2 Data 1 Insertion Top Data 1 Push
  • 6.
    Algorithm for insertdata into stack Step 1: Check stack is empty If TOP=-1 Print Stack is empty and Exit Step 2: delete an element from stack top Set Del_element=Stack[Top] Step 3: decrement stack size by one Top=Top-1 Step 4: End 5 4 3 2 1 4 3 2 1 3 2 1 2 1 Max size Pop 5 Pop 4 Pop 3 1 Pop 2
  • 7.
    Pop (Deleting dataitem from Stack) for deleting data item into stack we use Pop operation. Deletion Top Data 2 Data 1 Data 1, Data 2, Data 3, Data 4, Data 5, Deletion Top Data 1 Deletion Top Data 3 Data 2 Data 1 Deletion Top Data 4 Data 3 Data 2 Data 1 pop
  • 8.
    What is Queue A Queue is a linear structure which follows a particular order in which the operations are performed. The order is First In First Out (FIFO). In Queue the allocation of data are continuous manner.  insertion and deletion is done from different ends in the Queue, using rear for insertion and front for Dilation.  Queue having two different formats linear queue and circular queue.
  • 9.
    Types of Queue broadlyQueue are divided into two types such as linear and circular but linear queue having also two different variations, double ended queue or Dequeue and insertion restricted and deletion restricted. 10 20 30 40 50 Front Rear Linear Queue 10 50 30 20 40 60 Circular Queue Rear Front 10 20 30 40 50 10 20 30 40 50 Deletion restricted Queue Double Ended Queue Insertion restricted Queue 10 20 30 40 50 Rear Front Front Rear Front Rear Rear Front Rear Front
  • 10.
    Insertion Data iteminto Queue Rear Front Front Front Front Front Rear Rear To insert data element into queue, we use rear pointer all data element inserted only rear end. Algorithm for Insert an element into Queue Step 1: Check if the queue is full. If rear == max-size-1 Print Queue is full and exit Step 2: Add and element into Queue SET REAR = REAR + 1 Step 3: data element insert at rear pointer . Set QUEUE[REAR] = NUM Step 4: return success. 10 10 20 10 20 30 10 20 30 40 Rear Rear 10 20 30 40 50 Rear Front
  • 11.
    Deletion Data itemFrom Queue Algorithm for Delete an element From Queue Step 1: Check if the queue is Empty. If FRONT == -1 or FRONT >REAR Print Queue is Empty and exit Step 2: Delete an data element from Queue SET FRONT = FRONT + 1 Step 3: data element insert at rear pointer . Set Data element = QUEUE[FRONT] Step 4: return success. To Delete data element from queue, we use front pointer all data elements delete only front end. 10 20 30 40 50 Front Rear 20 30 40 50 30 40 50 40 50 50 Front Front Front Front Rear Rear Rear Rear
  • 12.
    Circular Queue Circular Queueis a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. Circular queue also follow contiguous allocation of data element. In the circular queue, for insertion and deletion we use rear and front pointer like linear queue
  • 13.
    Thanks and Regards: AnilKumar Prajapati