This document discusses stacks and queues, which are linear data structures. Stacks follow LIFO (last in, first out) and use push and pop operations. Queues follow FIFO (first in, first out) and use insertion at the rear and deletion at the front. Real-world examples of stacks include discs in a holder and tokens in a stack. Real-world examples of queues include lines at counters. Stacks and queues have various applications like reversing strings, expression evaluation, job scheduling, and memory/process management in operating systems.
2. STACK
It is a linear data structure in which an elements
may be inserted or deleted only at one end called the
top of the stack.
This data structure implemented in LIFO (Last In
First Out) manner
LIFO means element last inserted would be the first
one to be deleted.
The Stack is also a dynamic data structure as it
can grow or shrink.
3. Real life example :
A stack OF DISCS IN HOLDER
A stack of TOKENS IN HOLDER
Two basic operations of stack
PUSHING : An Insertion in a stack is called
PUSHING.
POPPING : A Deletion from a stack is called
POPPING.
4. PUSHING ALGORITHM
1. Read ITEM
2. If TOP = N then Print “OVERFLOW”
and Stop
3. Else set TOP = TOP + 1
//Creating a space for a new element
4. Set STACK [ TOP ] = ITEM
//Inserting the new ITEM
5. Stop
1 2 3 4 5
TOP
N 5
NULL
5. POPPING ALGORITHM
1. If TOP = NULL then
Print “UNDERFLOW” and return
2. Else set ITEM =STACK [ TOP ]
3. Set TOP = TOP - 1
4. Stop
1 2 3 4 5
TOP
6. APPLICATIONS OF STACK
TO REVERSING A STRING
TO EVALUATE NOTATIONAL
EXPRESSION(INFIX, PREFIX, & POSTFIX)
TO CONVERT FROM ONE EXPRESSION TO
ANOTHER.
7. QUEUE
It is a linear data structure in which an element may
be inserted only at one end, called the REAR end and
deletions can be take place at the other end, called the
FRONT end.
This data structure implemented in FIFO (First In
First Out) manner
FIFO means element first inserted would be the first
one to be deleted.
The Queue is also a dynamic data structure as it can
grow or shrink.
However, if additions and deletions are made at both
ends of a queue, then such a queue is called Double
Ended Queue or simply DEQUE.
8. The following operations can be performed
on Queues
1. Addition of an element at the REAR end of the
queue
2. Deletion of an element from the FRONT end of
the queue
Real life example :
• People waiting at a railway station counter for
taking tickets
9. Algorithm for INSERTION
1. If FRONT = 1 and REAR = N or
If FRONT = REAR + 1 then
Print “OVERFLOW” ; stop
2. if FRONT = NULL then
set FRONT = 1 and REAR = 1
3. Else if REAR = N then
set REAR = 1
4. Else set REAR = REAR + 1
5. Endif
6. Set QUEUE [ REAR ] = ITEM
7. Stop
10. Algorithm for DELETION
1. If FRONT = NULL then
Print “UNDERFLOW” ; stop
2. set ITEM = QUEUE [FRONT]
3. If FRONT = REAR then
set FRONT = NULL and REAR = NULL
4. Else if FRONT = N then set FRONT = 1
5. Else set FRONT = FRONT + 1
6. Stop
11. APPLICATIONS OF QUEUE
IT OCCURS IN THE JOB SCHEDULING
THE COMPUTER OS USE THIS CONCEPT
IN SCHEDULING THE MEMORY,
PROCESSOR AND THE FILES.
USED FOR SCHEDULING JOBS AND
SERVICES.
12. EXERCISE - 1
In CD pack, the CDs are placed one over the
other through a central axis. Assume that there
are 25 CDs in the pack.
a. Name the data structure that resembles with the
CD pack.
b. Considering the CD pack as an array in C++, how
will you name the position of the last CD and what
will be its value?
c. Write an algorithm to remove a CD from the pack
and name the operation performed.
13. EXERCISE - 2
While waiting for a ticket at the railway station
ticket counter, you are following the principle
as that of a data structure
a. Name the data structure and the principle
b. Write a C++ function to add a new element in this
data structure.
c. Name the situation where there is no space for
adding new element
14. Some more Questions
1. What is overflow and underflow?
2. Explain the terms PUSH and POP?
3. What are the applications of stacks and
queues?
4. Name the data structure that resembles a
person placing plates in a vessel. Write
an algorithm for inserting a plate.