Stack and Queue
Stack and Queue
Stack and Queue
UNIT V
Contents
• Classification of Data Structure
• Stack and Queues
• Stack:
vStack operations
vStack implementations using arrays
• Queue:
vQueue operations
vQueue implementations using arrays
• Types of Queues
• Applications of stack and queues
What is a Data Structure?
• Data structures are used to store data in a computer in an organized
form.
• In General data structure types include the file, array, record, table,
tree.. etc.
• In term of computer programming language, a data structure may be
selected or designed to store data for the purpose of working on it with
various algorithms.
Types of data structure
.
Linear Data Structure
• Data Structure in C Programming Language is a specialized format for
organizing and storing data.
1. Array: Array is collection of similar data type, you can insert and
deleted element form array without follow any order.
2. Stack: Stack work on the basis of Last-In-First-Out (LIFO). Last
entered element removed first.
3. Queue: Queue work on the basis of First-In-First-Out (FIFO). First
entered element removed first.
4. Linked List: Linked list is the collection of node, Here you can insert
and delete data in any order.
Non-Linear Data Structure
• This structure is mostly used for representing data that contains a hierarchical
relationship among various elements.
• Examples of Non-Linear Data Structures are listed below:
ØGraphs
Øthe family of trees and
Øtable of contents
1. Tree: In this case, data often contain a hierarchical relationship among various
elements. The data structure that reflects this relationship is termed as a rooted
tree graph or a tree.
2. Graph: In this case, data sometimes hold a relationship between the pairs of
elements, which is not necessarily following the hierarchical structure. Such a
data structure is termed as a Graph.
Array in Data
Structure
Important Variables:
front and rear
Let us understand theoretically
• Queue is a linear data structure
• Queue is an abstract data type
• Queue is an algorithm
• Queue is based on rule FIFO(First in first out)
To implement queue we can use :
1. Static arrays(Fixed size)
2. Dynamic arrays(Size varies)
Queue operations:
1.Insert 2. Delete 3.Display 4.Queue is full 5.Queue is empty
Variables:
Front and Rear
Structure of Queue/Queue Creation
Example:
N=8
int Queue[n];Initially front=rear=0. rear=n// queue is empty
0 1 2 3 4 5 6 7
10 20 30
Deletion Insertion
front rear
Qfront]=20
Q[i]=q[i+1]
Rear--
Types of Queues in data structure
1.Simple queue
2.Circular queue
3.Dequeue(Double ended queue)
Simple queue
As is clear from the name itself, simple queue lets us perform the
operations simply. i.e.,
the insertion and deletions are performed likewise.
Insertion occurs at the rear (end) of the queue and deletions are performed
at the front (beginning) of the queue list.
All nodes are connected to each other in a sequential manner. The pointer
of the first node points to the value of the second and so on.
Circular queue
Unlike the simple queues, in a circular queue each node is connected to the
next node in sequence, but the last node’s pointer is also connected to the
first node’s address.
Hence, the last node and the first node also gets connected making a
circular link overall.
Dequeue(Double ended queue)
The doubly ended queue or dequeue allows the insert and delete
operations from both ends (front and rear) of the queue.
Dequeue operations
1.Insertion(rear)
2.Insertion(front)
3.Deletion(rear)
4.Deletion(front)
5.Display
Stack
• Stack is a linear data structure
• Stack is an abstract data type
• Stack is an algorithm
• Stack is based on rule LIFO(Last in first out)
To implement stack we can use :
1. Static arrays(Fixed size)
2. Dynamic arrays(Size varies)
Variables:
top
Stack operations
1.How to create a stack
2.push()/Insertion
3.pop()/deletion
4.Traverse()/Display
5.Is stack empty()
6.Is stack full()
7.peek
Example
Stack applications.
• Stacks can be used for expression evaluation.
( 2 + ( ( 3 + 4 ) * ( 5 * 6 ) ) ) ---212
• Stacks can be used to check parenthesis matching in an expression.
(a + b * [c + d ) --invalid
• Stacks can be used for Conversion from one form of expression to another.
• Stacks can be used for Memory Management.
int main() {
int f;
int a[10];
int c = 20;
int e[n]; }
Cont….
Stack data structures are used in backtracking problems.
• Consider the N-Queens problem for an example. The solution of this
problem is that N queens should be positioned on a chessboard so that none
of the queens can attack another queen. In the generalized N-Queens
problem, N represents the number of rows and columns of the board and the
number of queens which must be placed in safe positions on the board.
• We will use the stack to remember where we placed queens on the board,
and if we need to make a backup, the queen at the top of the stack will be
popped, and we will use the position of that queen to resume the search for
next safe location.
• Hence, stacks can be used for the allocation of memory for most of the
backtracking problems.
•
queue applications.
• M/M/1 queue. One of the most important queueing models is known
as an M/M/1 queue, which has been shown to accurately model many
real-world situations. There is one server—a FIFO queue.
• Serving requests on a single shared resource, like a printer, CPU task
scheduling etc.
• In real life scenario, Call Center phone systems uses Queues to hold
people calling them in an order, until a service representative is free.
• Handling of interrupts in real-time systems. The interrupts are handled
in the same order as they arrive i.e First come first served.
Lab questions
• Develop an application to match parenthesis of a given expression
using Stack.
• Develop an application to identify Palindrome string using Stack and
Queue.