0% found this document useful (0 votes)
19 views

Stack and Queue

A Stack is a linear data structure that follows the LIFO (Last-In-First-Out) principle. Stack has one end, whereas the Queue has two ends (front and rear). It contains only one pointer top pointer pointing to the topmost element of the stack.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Stack and Queue

A Stack is a linear data structure that follows the LIFO (Last-In-First-Out) principle. Stack has one end, whereas the Queue has two ends (front and rear). It contains only one pointer top pointer pointing to the topmost element of the stack.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

DATA STRUCTURE & ALGORITHMS

Stack and queue


S.Kavitha
Head & Assistant Professor
Department of Computer Science
Sri Sarada Niketan College of
Science for Women,Karur.
Stack
 A Stack is a linear data structure that follows the LIFO (Last-In-First-Out)

principle. Stack has one end, whereas the Queue has two ends (front and rear). It

contains only one pointer top pointer pointing to the topmost element of the stack.

Some key points related to stack


 It is called as stack because it behaves like a real-world stack, piles of books, etc.

 A Stack is an abstract data type with a pre-defined capacity, which means that it

can store the elements of a limited size.

 It is a data structure that follows some order to insert and delete the elements, and

that order can be LIFO or FILO.


Working of Stack
 Stack works on the LIFO pattern. As we can observe in the below figure there
are five memory blocks in the stack; therefore, the size of the stack is 5.
 Suppose we want to store the elements in a stack and let's assume that stack is
empty. We have taken the stack of size 5 as shown below in which we are
pushing the elements one by one until the stack becomes full.
Standard Stack Operations
 The following are some common operations implemented on the stack:
 push(): When we insert an element in a stack then the operation is known as a push. If the
stack is full then the overflow condition occurs.
 pop(): When we delete an element from the stack, the operation is known as a pop. If the
stack is empty means that no element exists in the stack, this state is known as an
underflow state.

PUSH operation
The steps involved in the PUSH operation is given below:
 Before inserting an element in a stack, we check whether the stack is full.
 If we try to insert the element in a stack, and the stack is full, then the overflow condition
occurs.
 When we initialize a stack, we set the value of top as -1 to check that the stack is empty.
 When the new element is pushed in a stack, first, the value of the top gets
incremented, i.e., top=top+1, and the element will be placed at the new position
of the top.
 The elements will be inserted until we reach the max size of the stack.
POP operation
 The steps involved in the POP operation is given below:
 Before deleting the element from the stack, we check whether the stack is empty.
 If we try to delete the element from the empty stack, then the underflow condition
occurs.
 If the stack is not empty, we first access the element which is pointed by the top

Once the pop operation is performed, the top is decremented by 1, i.e., top=top-1 .
Applications of Stack
The following are the applications of the stack:
 Balancing of symbols: Stack is used for balancing a symbol. For example, we have the
following program:

int main()

cout<<"Hello";

cout<<"javaTpoint";

As we know, each program has an opening and closing braces; when the opening braces
come, we push the braces in a stack, and when the closing braces appear, we pop the
opening braces from the stack. Therefore, the net value comes out to be zero. If any
symbol is left in the stack, it means that some syntax occurs in a program.
Queue
 A queue can be defined as an ordered list which enables insert operations to
be performed at one end called REAR and delete operations to be
performed at another end called FRONT.
 Queue is referred to be as First In First Out list.
 For example, people waiting in line for a rail ticket form a queue.
FIFO Principle of Queue:
 A Queue is like a line waiting to purchase tickets, where the first person in
line is the first person served. (i.e. First come first serve).
 Position of the entry in a queue ready to be served, that is, the first entry
that will be removed from the queue, is called the front of the
queue(sometimes, head of the queue), similarly, the position of the last
entry in the queue, that is, the one most recently added, is called the rear
(or the tail) of the queue. See the below figure.
Applications of Queue
 Queues are widely used as waiting lists for a single shared resource like
printer, disk, CPU.
 Queues are used in asynchronous transfer of data (where data is not being
transferred at the same rate between two processes) for eg. pipes, file IO,
sockets.
 Queues are used as buffers in most of the applications like MP3 media
player, CD player, etc.
 Queue are used to maintain the play list in media players in order to add
and remove the songs from the play-list.
 Queues are used in operating systems for handling interrupts.

You might also like