Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Stack and Queue

Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

Data Structures

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

An Array is a collection of similar


data type value in a single variable.
An array is a derived data type in
C, which is constructed from
fundamental data type of C
language.
What is a Queue?
A queue is a linear list of elements in which deletion of an element can
take place only at one end called the front and insertion can take place
on the other end which is termed as the rear.
Queue in C
• Queue is work on the principal of First-In-First-Out (FIFO), it means
first entered item remove first.
• Queue have two end front and rear , from front you can insert
element and from rear you can delete element.
Real life example of Queue
Other Examples
• Ticket Counter: First person get ticket first and go out first
• Queue of processes in OS.
• Queue of people at any service point such as ticketing etc.
• Queue of packets in data communication.
• Queue of airplanes waiting for landing instructions.
What is ADT?
The abstract datatype is special kind of datatype, whose behavior is
defined by a set of values and set of operations. The keyword “Abstract”
is used as we can use these data types, we can perform different
operations. But how those operations are working that is totally hidden
from the user. The ADT is made of with primitive datatypes, but
operation logics are hidden.
Queue as an abstract data type
Thus for defining a Queue as an abstract data type, these are the
following criteria:
• Initialize a queue to be empty
• Check whether a queue is empty or not
• Check whether a queue is full or not
• Insert a new element after the last element in a queue, if the queue is
not full
• Retrieve the first element of the queue, if it is not empty
• Delete the first element in a queue, if it is not empty
Operation on a queue
The basic operation that can be perform on queue are;
1. Insert an element in a queue.
2. Delete an element from the queue.
3. Display elements of a queue
4. To check queue is full
5. To check queue is empty

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.

You might also like