0% found this document useful (0 votes)
48 views17 pages

Queue and Stack Data Structure

Data structures and algorithms

Uploaded by

xhpxrwyzhx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views17 pages

Queue and Stack Data Structure

Data structures and algorithms

Uploaded by

xhpxrwyzhx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

QUEUE AND STACK DATA

STRUCTURE
STACK DATA STRUCTURE
A stack is a non-primitive, linear data structure that stores the elements in a LIFO (Last In First
Out) order. However, the stack can only be used to store similar kinds of elements, that is, those
that have the same data types.
• Some Key Points Related to Stack Data Structure
➢ Stacks are dynamic in nature; this means that they do not have a fixed size and their size can be
increased or decreased depending upon the number of elements.
➢ In stacks, only one end is available to perform actions like insertion or deletion of the elements
➢ Stacks follow LIFO, which is Last In, First Out mechanism. Inserting an element is known
as push, and deleting an element is known as pop
STANDARD STACK OPERATIONS IN DATA STRUCTURE
Standard stack operations facilitate interaction with this LIFO (Last-In-First-Out) data structure, each
performing a specific function:
• Push: It adds an element to the top , increasing its size by one.
• Pop: It removes the element from the top, decreasing its size by one and returning the removed element.
• Peek or Top: Retrieves the top element of the stack without removing it, allowing a look at the value
without modifying the stack.
• IsEmpty: Checks if the stack is empty, returning true if there are no elements and false otherwise.
• IsFull: (In the context of a fixed-size stack) Checks if the stack has reached its maximum capacity,
returning true if no more elements can be added.
• Size: It returns the number of elements/ size of the stack, providing a count of how many items are stored.
PUSH OPERATION
The process of inserting new data into a stack is known as push. The push (x) operation takes the
element to be added as a parameter
The steps involved in a push operation are –
1. Before inserting the element, check whether the stack is full.
2. If the stack is full, print “stack overflow” and terminate the program.
3. If the stack is not full, add data into the stack.
4. We can repeat the above steps until the maximum capacity of the stack is achieved.
Recall the above example of the elevator, the event of people walking into the elevator closely
relates to pushing the elements into a stack.
POP OPERATION
The process of deleting the topmost element from the stack is known as pop.
The steps involved in a pop operation are –
1. Before removing the topmost element, check whether the stack is empty
2. If the stack is empty, print “Stack underflow” and terminate the program
3. If the stack is not empty, access the topmost element and let the top point to the element just before it
4. We can repeat the above until all the elements from the stack are removed
In the example of the elevator, a person moving out from it resembles what we call a pop operation.
APPLICATIONS OF STACK IN DATA STRUCTURE
The application of stack in data structure are:
• 1. In code editors – Stacks are used in various code editors like VS Code, Atom, etc. to match the opening & closing tags in the
languages like HTML, XML, etc.
• 2. In matching bracket pairs – This is one of the most famous application of stack in data structure. Stacks help us in
identifying if the bracket pairs in a sentence or string are complete or missing.
• 3. In browsers – You are reading this article in your web browser, what will happen if you press the back button present at the
left-hand corner of your screen? It would take you back to your last visited website or link. It is done using stacks. Every time
you visit a new link, it is stored in the stack. And, every time you press the back button, the current link is popped out from the
lifo stack, and the previous link is made available.
• 4. In compilers – Stacks are used heavily by the browsers to convert infix expressions to postfix expressions. It is done because
the compilers can not read an expression directly. E.g., if there is an expression – 5 + (1 + 4/2), the compiler cannot process it
like humans. Thus, it solves this expression by converting it into a postfix or prefix expression
QUEUE DATA STRUCTURE
What is a Queue Data Structure?
• A queue is also a non-primitive, linear data structure that stores elements in a sequential
manner. However, the queue data structure follows a FIFO (First In First Out) order to store
and retrieve elements from the memory. It can also only store similar kinds of elements in the
queue.
• An example of a queue can be a line or a queue in a bank in which the person who comes first
will be served first. Therefore, it follows the FIFO order to process the elements.
• Queue they are also used to perform insertions, and deletions, find the peak element, and
check whether the queue is empty or not. However, insertion into a queue is called enqueue,
whereas deletion from a queue is called dequeue.
BASIC OPERATIONS OF QUEUE
A queue is an object (an abstract data structure - ADT) that allows the following operations:
• Enqueue: Add an element to the end of the queue
• Dequeue: Remove an element from the front of the queue
• IsEmpty: Check if the queue is empty
• IsFull: Check if the queue is full
• Peek: Get the value of the front of the queue without removing it
ENQUEUE OPERATION
• check if the queue is full
• for the first element, set the value of FRONT to 0
• increase the REAR index by 1
• add the new element in the position pointed to by REAR
DEQUEUE OPERATION
• check if the queue is empty
• return the value pointed by FRONT
• increase the FRONT index by 1
• for the last element, reset the values of FRONT and REAR to -1
THE FOLLOWING ARE THE EXAMPLE ON HOW THE ENQUEUE AND
DEQUEUE WORK
APPLICATIONS OF QUEUE
• CPU scheduling, Disk Scheduling
• Round robin scheduling.
• Call Center phone systems use Queues to hold people calling them in order.
• Keyboard buffer-is section of computer memory used to hold keystrokes before
processed on a cpu
THE END

You might also like