SlideShare a Scribd company logo
1 of 56
Download to read offline
CS8391-DATA STRUCTURES
Unit - II
Dr. A. Kathirvel
Professor, Dept of CSE, M N M J E C, Chennai
UNIT II LINEAR DATA STRUCTURES – STACKS, QUEUES
 Stack ADT – Operations – Applications – Evaluating
arithmetic expressions- Conversion of Infix
to postfix expression – Queue ADT – Operations –
Circular Queue – Priority Queue – deQueue –
applications of queues.
 TEXTBOOKS:
1. Mark Allen Weiss, “Data Structures and Algorithm
Analysis in C”, 2nd Edition, Pearson Education,1997.
2. Reema Thareja, “Data Structures Using C”, Second Edition,
Oxford University Press, 2011
29 June 2018
2
MNMJEC, Chennai - 600 097
29 June 2018MNMJEC, Chennai - 600 097
3
Topics
 Stack ADT
 Queue ADT
 Applications of Stacks and Queues
29 June 2018MNMJEC, Chennai - 600 097
4
Stack
29 June 2018MNMJEC, Chennai - 600 097
5
What is a Stack ?
 a stack is a particular kind of abstract data type or
collection in which the fundamental (or only) operations
on the collection are the addition of an entity to the
collection, known as push and removal of an entity,
known as pop.
29 June 2018MNMJEC, Chennai - 600 097
6
Basic Principle
 The relation between the push and pop operations is
such that the stack is a Last-In-First-Out (LIFO) data
structure.
 In a LIFO data structure, the last element added to the
structure must be the first one to be removed.
29 June 2018MNMJEC, Chennai - 600 097
7
Operations on Stack
 This is equivalent to the requirement that, considered as
a linear data structure, or more abstractly a sequential
collection, the push and pop operations occur only at
one end of the structure, referred to as the top of the
stack.
 Often a peek or top operation is also implemented,
returning the value of the top element without removing
it.
29 June 2018MNMJEC, Chennai - 600 097
8
Implementation
 A stack may be implemented to have a bounded
capacity. If the stack is full and does not contain enough
space to accept an entity to be pushed, the stack is
then considered to be in an overflow state. The pop
operation removes an item from the top of the stack. A
pop either reveals previously concealed items or results
in an empty stack, but, if the stack is empty, it goes into
underflow state, which means no items are present in
stack to be removed.
29 June 2018MNMJEC, Chennai - 600 097
9
Restricted Data Structure
 A stack is a restricted data structure, because only a
small number of operations are performed on it. The
nature of the pop and push operations also means that
stack elements have a natural order. Elements are
removed from the stack in the reverse order to the
order of their addition. Therefore, the lower elements
are those that have been on the stack the longest.
29 June 2018MNMJEC, Chennai - 600 097
10
Examples
29 June 2018MNMJEC, Chennai - 600 097
11
In Simple Words
 A stack
 Last-in, first-out (LIFO) property
 The last item placed on the stack will be the first item
removed
 Analogy
 A stack of dishes in a cafeteria
 A stack of Books
 A stack of Dosa
 A stack of Money
29 June 2018MNMJEC, Chennai - 600 097
12
ADT Stack
 ADT stack operations
 Create an empty stack
 Destroy a stack
 Determine whether a stack is empty
 Add a new item
 Remove the item that was added most recently
 Retrieve the item that was added most recently
 A program can use a stack independently of the
stack’s implementation
29 June 2018MNMJEC, Chennai - 600 097
13
Stack Operations
bool isEmpty() const;
// Determines whether a stack is empty.
// Precondition: None.
// Postcondition: Returns true if the
// stack is empty; otherwise returns
// false.
29 June 2018MNMJEC, Chennai - 600 097
14
Stack Operations
bool push(StackItemType newItem);
// Adds an item to the top of a stack.
// Precondition: newItem is the item to
// be added.
// Postcondition: If the insertion is
// successful, newItem is on the top of
// the stack.
29 June 2018MNMJEC, Chennai - 600 097
15
Stack Operations
bool pop(StackItemType& stackTop);
// Retrieves and removes the top of a stack
// Precondition: None.
// Postcondition: If the stack is not empty,
// stackTop contains the item that was added
// most recently and the item is removed.
// However, if the stack is empty, deletion
// is impossible and stackTop is unchanged.
29 June 2018MNMJEC, Chennai - 600 097
16
Stack Operations
bool getTop(StackItemType& stackTop) const;
// Retrieves the top of a stack.
// Precondition: None.
// Postcondition: If the stack is not empty,
// stackTop contains the item that was added
// most recently. However, if the stack is
// empty, the operation fails and stackTop
// is unchanged. The stack is unchanged.
29 June 2018MNMJEC, Chennai - 600 097
17
Implementation Using Arrays
29 June 2018MNMJEC, Chennai - 600 097
18
The Stack Class - Public Contents
class Stack
{
public:
Stack() { size = 10; current = -1;}
int pop(){ return A[current--];}
void push(int x){A[++current] = x;}
int top(){ return A[current];}
int isEmpty(){return ( current == -1 );}
int isFull(){ return ( current == size-1);}
29 June 2018MNMJEC, Chennai - 600 097
19
The Stack Class - Private Contents
private:
int object; // The data element
int current; // Index of the array
int size; // max size of the array
int A[10]; // Array of 10 elements
};
29 June 2018MNMJEC, Chennai - 600 097
20
Stack - a Very Simple Application
 The simplest application of a stack is to reverse a
word.
 You push a given word to stack - letter by letter -
and then pop letters from the stack.
29 June 2018MNMJEC, Chennai - 600 097
21
Applications of Stack
 Recursion
 Decimal to Binary Conversion
 Infix to Postfix Conversion and Evaluation
of Postfix Expressions
 Rearranging Railroad Cars
 Quick Sort
 Function Calls
 Undo button in various software.
29 June 2018MNMJEC, Chennai - 600 097
22
outputInBinary - Algorithm
function outputInBinary(Integer n)
Stack s = new Stack
while n > 0 do
Integer bit = n modulo 2
s.push(bit)
if s is full then
return error
end if
n = floor(n / 2)
end while
while s is not empty do
output(s.pop())
end while
end function
29 June 2018MNMJEC, Chennai - 600 097
23
Algebraic Expressions
 Infix expressions
 An operator appears between its operands
 Example: a + b
 Prefix expressions
 An operator appears before its operands
 Example: + a b
 Postfix expressions
 An operator appears after its operands
 Example: a b +
29 June 2018MNMJEC, Chennai - 600 097
24
A complicated example
 Infix expressions:
a + b * c + ( d * e + f ) * g
 Postfix expressions:
a b c * + d e * f + g * +
 Prefix expressions:
+ + a * b c * + * d e f g
29 June 2018MNMJEC, Chennai - 600 097
25
Evaluation of Postfix Expression
29 June 2018MNMJEC, Chennai - 600 097
26
Evaluation of Postfix Expression
 The calculation: 1 + 2 * 4 + 3 can be written down like
this in postfix notation with the advantage of no
precedence rules and parentheses needed
 1 2 4 * + 3 +
 The expression is evaluated from the left to right using
a stack:
 when encountering an operand: push it
 when encountering an operator: pop two operands,
evaluate the result and push it.
29 June 2018MNMJEC, Chennai - 600 097
27
29 June 2018MNMJEC, Chennai - 600 097
28
Infix to Postfix Conversion
29 June 2018MNMJEC, Chennai - 600 097
29
What’s this ?
29 June 2018MNMJEC, Chennai - 600 097
30
Queue
29 June 2018MNMJEC, Chennai - 600 097
31
Queue
 A stack is LIFO (Last-In First Out) structure. In contrast, a
queue is a FIFO (First-In First-Out ) structure.
 In a FIFO data structure, the first element added to the
queue will be the first one to be removed.
 A queue is a linear structure for which items can be only
inserted at one end and removed at another end.
29 June 2018MNMJEC, Chennai - 600 097
32
Operations on Queue
 We will dedicate once again six operations on the
Queue.
 You can add a person to the queue (enque),
 Look who is the first person to be serviced (front)
 Rremove the first person (dequeue) and
 Check whether the queue is empty (isEmpty).
 Also there are two more operations to create and to
destroy the queue.
29 June 2018MNMJEC, Chennai - 600 097
33
Operations
 Queue create()
 Creates an empty queue
 boolean isEmpty(Queue q)
 Tells whether the queue q is empty
 enqueue(Queue q, Item e)
 Place e at the rear of the queue q
 destroy(Queue q)
 destroys queue q
29 June 2018MNMJEC, Chennai - 600 097
34
Operations Continued
 Item front(Queue q)
 returns the front element in Queue q without
removing it
Precondition: q is not empty
 dequeue(Queue q)
 removes front element from the queue q
Precondition: q is not empty
29 June 2018MNMJEC, Chennai - 600 097
35
Implementation Using Arrays
 If we use an array to hold queue elements, dequeue
operation at the front (start) of the array is
expensive.
 This is because we may have to shift up to “n”
elements.
 For the stack, we needed only one end; for queue
we need both.
 To get around this, we will not shift upon removal of
an element.
29 June 2018MNMJEC, Chennai - 600 097
36
Snapshot of a Queue
29 June 2018MNMJEC, Chennai - 600 097
37
enqueue()
29 June 2018MNMJEC, Chennai - 600 097
38
enqueue() again
29 June 2018MNMJEC, Chennai - 600 097
39
dequeue()
29 June 2018MNMJEC, Chennai - 600 097
40
dequeue() again
29 June 2018MNMJEC, Chennai - 600 097
41
Two more enqueue()
29 June 2018MNMJEC, Chennai - 600 097
42
What’s Wrong ?
 We have inserts and removal running in constant time
but we created a new problem.
 We cannot insert new elements even though there are
two places available at the start of the array.
 Solution: allow the queue to “wrap around”. Basic idea
is to picture the array as a circular array as show
below.
29 June 2018MNMJEC, Chennai - 600 097
43
Queue array wrap-around
29 June 2018MNMJEC, Chennai - 600 097
44
enqueue(element)
void enqueue(int x)
{
rear = (rear+1)%size;
array[rear] = x;
noElements = noElements+1;
}
29 June 2018MNMJEC, Chennai - 600 097
45
dequeue()
int dequeue()
{
int x = array[front];
front = (front+1)%size;
noElements = noElements-1;
return x;
}
29 June 2018MNMJEC, Chennai - 600 097
46
isEmpty() and isFull()
int isFull()
{
return noElements == size;
}
int isEmpty()
{
return noElements == 0;
}
29 June 2018MNMJEC, Chennai - 600 097
47
Pointer Based Implementation
 Possible implementations of a pointer-based queue
 A linear linked list with two external references
 A reference to the front
 A reference to the back
 A circular linked list with one external reference
 A reference to the back
29 June 2018MNMJEC, Chennai - 600 097
48
Queue - Linear and Circular List
29 June 2018MNMJEC, Chennai - 600 097
49
Non-empty Queue - Insertion
29 June 2018MNMJEC, Chennai - 600 097
50
Empty Queue - Insertion
29 June 2018MNMJEC, Chennai - 600 097
51
Queue with more than 1 element - Deletion
29 June 2018MNMJEC, Chennai - 600 097
52
Applications of Queues
 Queue is used when things don’t have to be processed
immediatly, but have to be processed in First In First
Out order like Breadth First Search.
 This property of Queue makes it also useful in
following kind of scenarios.
29 June 2018MNMJEC, Chennai - 600 097
53
Applications of Queues
 When a resource is shared among multiple
consumers. Examples include CPU scheduling, Disk
Scheduling.
 When data is transferred asynchronously (data not
necessarily received at same rate as sent) between
two processes. Examples include IO Buffers, pipes,
file IO, etc.
 Operating systems often maintain a queue of
processes that are ready to execute or that are
waiting for a particular event to occur.
29 June 2018MNMJEC, Chennai - 600 097
54
Applications of Queues
 Computer systems must often provide a “holding area”
for messages between two processes, two programs,
or even two systems. This holding area is usually called
a “buffer” and is often implemented as a queue.
 Call center phone systems will use a queue to hold
people in line until a service representative is free.
29 June 2018MNMJEC, Chennai - 600 097
55
Applications of Queues
 Buffers on MP3 players and portable CD players, iPod
playlist. Playlist for jukebox - add songs to the end,
play from the front of the list.
 Round Robin (RR) algorithm is an important scheduling
algorithm. It is used especially for the time-sharing
system. The circular queue is used to implement such
algorithms.
Questions and Discussion
29 June 2018
56
MNMJEC, Chennai - 600 097

More Related Content

What's hot (20)

sparse matrix in data structure
sparse matrix in data structuresparse matrix in data structure
sparse matrix in data structure
 
Data structures
Data structuresData structures
Data structures
 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structures
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
Stacks
StacksStacks
Stacks
 
Introduction to data structure ppt
Introduction to data structure pptIntroduction to data structure ppt
Introduction to data structure ppt
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
 
Priority queue in DSA
Priority queue in DSAPriority queue in DSA
Priority queue in DSA
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 
stack & queue
stack & queuestack & queue
stack & queue
 
Stack
StackStack
Stack
 
Data structure by Digvijay
Data structure by DigvijayData structure by Digvijay
Data structure by Digvijay
 
Quick sort
Quick sortQuick sort
Quick sort
 
Java Stack Data Structure.pptx
Java Stack Data Structure.pptxJava Stack Data Structure.pptx
Java Stack Data Structure.pptx
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsLecture 1 data structures and algorithms
Lecture 1 data structures and algorithms
 
Array data structure
Array data structureArray data structure
Array data structure
 
Hashing In Data Structure
Hashing In Data Structure Hashing In Data Structure
Hashing In Data Structure
 
Python programming : Arrays
Python programming : ArraysPython programming : Arrays
Python programming : Arrays
 
Stacks and Queue - Data Structures
Stacks and Queue - Data StructuresStacks and Queue - Data Structures
Stacks and Queue - Data Structures
 

Similar to UNIT II LINEAR DATA STRUCTURES – STACKS, QUEUES

Data Structures and Algorithms (DSA) is a fundamental part of Computer Scienc...
Data Structures and Algorithms (DSA) is a fundamental part of Computer Scienc...Data Structures and Algorithms (DSA) is a fundamental part of Computer Scienc...
Data Structures and Algorithms (DSA) is a fundamental part of Computer Scienc...ssuser6478a8
 
Data Structures and Algorithms (DSA) is a fundamental part of Computer Scienc...
Data Structures and Algorithms (DSA) is a fundamental part of Computer Scienc...Data Structures and Algorithms (DSA) is a fundamental part of Computer Scienc...
Data Structures and Algorithms (DSA) is a fundamental part of Computer Scienc...ssuser6478a8
 
stack & queues .pptx
stack & queues .pptxstack & queues .pptx
stack & queues .pptxkaishsahu
 
IRJET- Comparison of Stack and Queue Data Structures
IRJET- Comparison of Stack and Queue Data StructuresIRJET- Comparison of Stack and Queue Data Structures
IRJET- Comparison of Stack and Queue Data StructuresIRJET Journal
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddumaneesh boddu
 
STACK AND QUEUE CIRCULAR QUEUE PPTS.pptx
STACK AND QUEUE CIRCULAR QUEUE PPTS.pptxSTACK AND QUEUE CIRCULAR QUEUE PPTS.pptx
STACK AND QUEUE CIRCULAR QUEUE PPTS.pptxsunitha1792
 
increasing the action gap - new operators for reinforcement learning
increasing the action gap - new operators for reinforcement learningincreasing the action gap - new operators for reinforcement learning
increasing the action gap - new operators for reinforcement learningRyo Iwaki
 
Difference between stack and queue
Difference between stack and queueDifference between stack and queue
Difference between stack and queuePulkitmodi1998
 
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbqueuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbRAtna29
 
stack_presentaton_HUSNAIN[2].pojklklklptx
stack_presentaton_HUSNAIN[2].pojklklklptxstack_presentaton_HUSNAIN[2].pojklklklptx
stack_presentaton_HUSNAIN[2].pojklklklptxHusnainNaqvi2
 
COS30008 Semester 1, 2016 Dr. Markus Lumpe 1 Swinbu.docx
COS30008 Semester 1, 2016 Dr. Markus Lumpe  1 Swinbu.docxCOS30008 Semester 1, 2016 Dr. Markus Lumpe  1 Swinbu.docx
COS30008 Semester 1, 2016 Dr. Markus Lumpe 1 Swinbu.docxvanesaburnand
 

Similar to UNIT II LINEAR DATA STRUCTURES – STACKS, QUEUES (20)

dsa1.ppt
dsa1.pptdsa1.ppt
dsa1.ppt
 
Data Structures and Algorithms (DSA) is a fundamental part of Computer Scienc...
Data Structures and Algorithms (DSA) is a fundamental part of Computer Scienc...Data Structures and Algorithms (DSA) is a fundamental part of Computer Scienc...
Data Structures and Algorithms (DSA) is a fundamental part of Computer Scienc...
 
Data Structures and Algorithms (DSA) is a fundamental part of Computer Scienc...
Data Structures and Algorithms (DSA) is a fundamental part of Computer Scienc...Data Structures and Algorithms (DSA) is a fundamental part of Computer Scienc...
Data Structures and Algorithms (DSA) is a fundamental part of Computer Scienc...
 
stack & queues .pptx
stack & queues .pptxstack & queues .pptx
stack & queues .pptx
 
CH4.pptx
CH4.pptxCH4.pptx
CH4.pptx
 
IRJET- Comparison of Stack and Queue Data Structures
IRJET- Comparison of Stack and Queue Data StructuresIRJET- Comparison of Stack and Queue Data Structures
IRJET- Comparison of Stack and Queue Data Structures
 
DSA Lab Manual C Scheme.pdf
DSA Lab Manual C Scheme.pdfDSA Lab Manual C Scheme.pdf
DSA Lab Manual C Scheme.pdf
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
Unit 5 dsa QUEUE
Unit 5 dsa QUEUEUnit 5 dsa QUEUE
Unit 5 dsa QUEUE
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddu
 
STACK AND QUEUE CIRCULAR QUEUE PPTS.pptx
STACK AND QUEUE CIRCULAR QUEUE PPTS.pptxSTACK AND QUEUE CIRCULAR QUEUE PPTS.pptx
STACK AND QUEUE CIRCULAR QUEUE PPTS.pptx
 
Queue
QueueQueue
Queue
 
increasing the action gap - new operators for reinforcement learning
increasing the action gap - new operators for reinforcement learningincreasing the action gap - new operators for reinforcement learning
increasing the action gap - new operators for reinforcement learning
 
Difference between stack and queue
Difference between stack and queueDifference between stack and queue
Difference between stack and queue
 
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbqueuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
 
20 convex hull last 7
20 convex hull last 720 convex hull last 7
20 convex hull last 7
 
stack_presentaton_HUSNAIN[2].pojklklklptx
stack_presentaton_HUSNAIN[2].pojklklklptxstack_presentaton_HUSNAIN[2].pojklklklptx
stack_presentaton_HUSNAIN[2].pojklklklptx
 
Lesson 4 stacks and queues
Lesson 4  stacks and queuesLesson 4  stacks and queues
Lesson 4 stacks and queues
 
Unit 4 queue
Unit   4 queueUnit   4 queue
Unit 4 queue
 
COS30008 Semester 1, 2016 Dr. Markus Lumpe 1 Swinbu.docx
COS30008 Semester 1, 2016 Dr. Markus Lumpe  1 Swinbu.docxCOS30008 Semester 1, 2016 Dr. Markus Lumpe  1 Swinbu.docx
COS30008 Semester 1, 2016 Dr. Markus Lumpe 1 Swinbu.docx
 

More from Kathirvel Ayyaswamy

22cs201 COMPUTER ORGANIZATION AND ARCHITECTURE
22cs201 COMPUTER ORGANIZATION AND ARCHITECTURE22cs201 COMPUTER ORGANIZATION AND ARCHITECTURE
22cs201 COMPUTER ORGANIZATION AND ARCHITECTUREKathirvel Ayyaswamy
 
20CS2021-Distributed Computing module 2
20CS2021-Distributed Computing module 220CS2021-Distributed Computing module 2
20CS2021-Distributed Computing module 2Kathirvel Ayyaswamy
 
Recent Trends in IoT and Sustainability
Recent Trends in IoT and SustainabilityRecent Trends in IoT and Sustainability
Recent Trends in IoT and SustainabilityKathirvel Ayyaswamy
 
18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network Security18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network SecurityKathirvel Ayyaswamy
 
18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network Security18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network SecurityKathirvel Ayyaswamy
 
18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network Security18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network SecurityKathirvel Ayyaswamy
 
18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network Security 18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network Security Kathirvel Ayyaswamy
 
18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network Security18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network SecurityKathirvel Ayyaswamy
 
18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network Security18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network SecurityKathirvel Ayyaswamy
 
20CS024 Ethics in Information Technology
20CS024 Ethics in Information Technology20CS024 Ethics in Information Technology
20CS024 Ethics in Information TechnologyKathirvel Ayyaswamy
 

More from Kathirvel Ayyaswamy (20)

22CS201 COA
22CS201 COA22CS201 COA
22CS201 COA
 
22cs201 COMPUTER ORGANIZATION AND ARCHITECTURE
22cs201 COMPUTER ORGANIZATION AND ARCHITECTURE22cs201 COMPUTER ORGANIZATION AND ARCHITECTURE
22cs201 COMPUTER ORGANIZATION AND ARCHITECTURE
 
22CS201 COA
22CS201 COA22CS201 COA
22CS201 COA
 
18CS3040_Distributed Systems
18CS3040_Distributed Systems18CS3040_Distributed Systems
18CS3040_Distributed Systems
 
20CS2021-Distributed Computing module 2
20CS2021-Distributed Computing module 220CS2021-Distributed Computing module 2
20CS2021-Distributed Computing module 2
 
18CS3040 Distributed System
18CS3040 Distributed System	18CS3040 Distributed System
18CS3040 Distributed System
 
20CS2021 Distributed Computing
20CS2021 Distributed Computing 20CS2021 Distributed Computing
20CS2021 Distributed Computing
 
20CS2021 DISTRIBUTED COMPUTING
20CS2021 DISTRIBUTED COMPUTING20CS2021 DISTRIBUTED COMPUTING
20CS2021 DISTRIBUTED COMPUTING
 
18CS3040 DISTRIBUTED SYSTEMS
18CS3040 DISTRIBUTED SYSTEMS18CS3040 DISTRIBUTED SYSTEMS
18CS3040 DISTRIBUTED SYSTEMS
 
Recent Trends in IoT and Sustainability
Recent Trends in IoT and SustainabilityRecent Trends in IoT and Sustainability
Recent Trends in IoT and Sustainability
 
20CS2008 Computer Networks
20CS2008 Computer Networks 20CS2008 Computer Networks
20CS2008 Computer Networks
 
18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network Security18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network Security
 
18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network Security18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network Security
 
18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network Security18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network Security
 
18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network Security 18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network Security
 
18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network Security18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network Security
 
18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network Security18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network Security
 
20CS2008 Computer Networks
20CS2008 Computer Networks20CS2008 Computer Networks
20CS2008 Computer Networks
 
20CS2008 Computer Networks
20CS2008 Computer Networks 20CS2008 Computer Networks
20CS2008 Computer Networks
 
20CS024 Ethics in Information Technology
20CS024 Ethics in Information Technology20CS024 Ethics in Information Technology
20CS024 Ethics in Information Technology
 

Recently uploaded

UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)Dr SOUNDIRARAJ N
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
EduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIEduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIkoyaldeepu123
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...Chandu841456
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Effects of rheological properties on mixing
Effects of rheological properties on mixingEffects of rheological properties on mixing
Effects of rheological properties on mixingviprabot1
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 

Recently uploaded (20)

UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
EduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIEduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AI
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Effects of rheological properties on mixing
Effects of rheological properties on mixingEffects of rheological properties on mixing
Effects of rheological properties on mixing
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 

UNIT II LINEAR DATA STRUCTURES – STACKS, QUEUES

  • 1. CS8391-DATA STRUCTURES Unit - II Dr. A. Kathirvel Professor, Dept of CSE, M N M J E C, Chennai
  • 2. UNIT II LINEAR DATA STRUCTURES – STACKS, QUEUES  Stack ADT – Operations – Applications – Evaluating arithmetic expressions- Conversion of Infix to postfix expression – Queue ADT – Operations – Circular Queue – Priority Queue – deQueue – applications of queues.  TEXTBOOKS: 1. Mark Allen Weiss, “Data Structures and Algorithm Analysis in C”, 2nd Edition, Pearson Education,1997. 2. Reema Thareja, “Data Structures Using C”, Second Edition, Oxford University Press, 2011 29 June 2018 2 MNMJEC, Chennai - 600 097
  • 3. 29 June 2018MNMJEC, Chennai - 600 097 3 Topics  Stack ADT  Queue ADT  Applications of Stacks and Queues
  • 4. 29 June 2018MNMJEC, Chennai - 600 097 4 Stack
  • 5. 29 June 2018MNMJEC, Chennai - 600 097 5 What is a Stack ?  a stack is a particular kind of abstract data type or collection in which the fundamental (or only) operations on the collection are the addition of an entity to the collection, known as push and removal of an entity, known as pop.
  • 6. 29 June 2018MNMJEC, Chennai - 600 097 6 Basic Principle  The relation between the push and pop operations is such that the stack is a Last-In-First-Out (LIFO) data structure.  In a LIFO data structure, the last element added to the structure must be the first one to be removed.
  • 7. 29 June 2018MNMJEC, Chennai - 600 097 7 Operations on Stack  This is equivalent to the requirement that, considered as a linear data structure, or more abstractly a sequential collection, the push and pop operations occur only at one end of the structure, referred to as the top of the stack.  Often a peek or top operation is also implemented, returning the value of the top element without removing it.
  • 8. 29 June 2018MNMJEC, Chennai - 600 097 8 Implementation  A stack may be implemented to have a bounded capacity. If the stack is full and does not contain enough space to accept an entity to be pushed, the stack is then considered to be in an overflow state. The pop operation removes an item from the top of the stack. A pop either reveals previously concealed items or results in an empty stack, but, if the stack is empty, it goes into underflow state, which means no items are present in stack to be removed.
  • 9. 29 June 2018MNMJEC, Chennai - 600 097 9 Restricted Data Structure  A stack is a restricted data structure, because only a small number of operations are performed on it. The nature of the pop and push operations also means that stack elements have a natural order. Elements are removed from the stack in the reverse order to the order of their addition. Therefore, the lower elements are those that have been on the stack the longest.
  • 10. 29 June 2018MNMJEC, Chennai - 600 097 10 Examples
  • 11. 29 June 2018MNMJEC, Chennai - 600 097 11 In Simple Words  A stack  Last-in, first-out (LIFO) property  The last item placed on the stack will be the first item removed  Analogy  A stack of dishes in a cafeteria  A stack of Books  A stack of Dosa  A stack of Money
  • 12. 29 June 2018MNMJEC, Chennai - 600 097 12 ADT Stack  ADT stack operations  Create an empty stack  Destroy a stack  Determine whether a stack is empty  Add a new item  Remove the item that was added most recently  Retrieve the item that was added most recently  A program can use a stack independently of the stack’s implementation
  • 13. 29 June 2018MNMJEC, Chennai - 600 097 13 Stack Operations bool isEmpty() const; // Determines whether a stack is empty. // Precondition: None. // Postcondition: Returns true if the // stack is empty; otherwise returns // false.
  • 14. 29 June 2018MNMJEC, Chennai - 600 097 14 Stack Operations bool push(StackItemType newItem); // Adds an item to the top of a stack. // Precondition: newItem is the item to // be added. // Postcondition: If the insertion is // successful, newItem is on the top of // the stack.
  • 15. 29 June 2018MNMJEC, Chennai - 600 097 15 Stack Operations bool pop(StackItemType& stackTop); // Retrieves and removes the top of a stack // Precondition: None. // Postcondition: If the stack is not empty, // stackTop contains the item that was added // most recently and the item is removed. // However, if the stack is empty, deletion // is impossible and stackTop is unchanged.
  • 16. 29 June 2018MNMJEC, Chennai - 600 097 16 Stack Operations bool getTop(StackItemType& stackTop) const; // Retrieves the top of a stack. // Precondition: None. // Postcondition: If the stack is not empty, // stackTop contains the item that was added // most recently. However, if the stack is // empty, the operation fails and stackTop // is unchanged. The stack is unchanged.
  • 17. 29 June 2018MNMJEC, Chennai - 600 097 17 Implementation Using Arrays
  • 18. 29 June 2018MNMJEC, Chennai - 600 097 18 The Stack Class - Public Contents class Stack { public: Stack() { size = 10; current = -1;} int pop(){ return A[current--];} void push(int x){A[++current] = x;} int top(){ return A[current];} int isEmpty(){return ( current == -1 );} int isFull(){ return ( current == size-1);}
  • 19. 29 June 2018MNMJEC, Chennai - 600 097 19 The Stack Class - Private Contents private: int object; // The data element int current; // Index of the array int size; // max size of the array int A[10]; // Array of 10 elements };
  • 20. 29 June 2018MNMJEC, Chennai - 600 097 20 Stack - a Very Simple Application  The simplest application of a stack is to reverse a word.  You push a given word to stack - letter by letter - and then pop letters from the stack.
  • 21. 29 June 2018MNMJEC, Chennai - 600 097 21 Applications of Stack  Recursion  Decimal to Binary Conversion  Infix to Postfix Conversion and Evaluation of Postfix Expressions  Rearranging Railroad Cars  Quick Sort  Function Calls  Undo button in various software.
  • 22. 29 June 2018MNMJEC, Chennai - 600 097 22 outputInBinary - Algorithm function outputInBinary(Integer n) Stack s = new Stack while n > 0 do Integer bit = n modulo 2 s.push(bit) if s is full then return error end if n = floor(n / 2) end while while s is not empty do output(s.pop()) end while end function
  • 23. 29 June 2018MNMJEC, Chennai - 600 097 23 Algebraic Expressions  Infix expressions  An operator appears between its operands  Example: a + b  Prefix expressions  An operator appears before its operands  Example: + a b  Postfix expressions  An operator appears after its operands  Example: a b +
  • 24. 29 June 2018MNMJEC, Chennai - 600 097 24 A complicated example  Infix expressions: a + b * c + ( d * e + f ) * g  Postfix expressions: a b c * + d e * f + g * +  Prefix expressions: + + a * b c * + * d e f g
  • 25. 29 June 2018MNMJEC, Chennai - 600 097 25 Evaluation of Postfix Expression
  • 26. 29 June 2018MNMJEC, Chennai - 600 097 26 Evaluation of Postfix Expression  The calculation: 1 + 2 * 4 + 3 can be written down like this in postfix notation with the advantage of no precedence rules and parentheses needed  1 2 4 * + 3 +  The expression is evaluated from the left to right using a stack:  when encountering an operand: push it  when encountering an operator: pop two operands, evaluate the result and push it.
  • 27. 29 June 2018MNMJEC, Chennai - 600 097 27
  • 28. 29 June 2018MNMJEC, Chennai - 600 097 28 Infix to Postfix Conversion
  • 29. 29 June 2018MNMJEC, Chennai - 600 097 29 What’s this ?
  • 30. 29 June 2018MNMJEC, Chennai - 600 097 30 Queue
  • 31. 29 June 2018MNMJEC, Chennai - 600 097 31 Queue  A stack is LIFO (Last-In First Out) structure. In contrast, a queue is a FIFO (First-In First-Out ) structure.  In a FIFO data structure, the first element added to the queue will be the first one to be removed.  A queue is a linear structure for which items can be only inserted at one end and removed at another end.
  • 32. 29 June 2018MNMJEC, Chennai - 600 097 32 Operations on Queue  We will dedicate once again six operations on the Queue.  You can add a person to the queue (enque),  Look who is the first person to be serviced (front)  Rremove the first person (dequeue) and  Check whether the queue is empty (isEmpty).  Also there are two more operations to create and to destroy the queue.
  • 33. 29 June 2018MNMJEC, Chennai - 600 097 33 Operations  Queue create()  Creates an empty queue  boolean isEmpty(Queue q)  Tells whether the queue q is empty  enqueue(Queue q, Item e)  Place e at the rear of the queue q  destroy(Queue q)  destroys queue q
  • 34. 29 June 2018MNMJEC, Chennai - 600 097 34 Operations Continued  Item front(Queue q)  returns the front element in Queue q without removing it Precondition: q is not empty  dequeue(Queue q)  removes front element from the queue q Precondition: q is not empty
  • 35. 29 June 2018MNMJEC, Chennai - 600 097 35 Implementation Using Arrays  If we use an array to hold queue elements, dequeue operation at the front (start) of the array is expensive.  This is because we may have to shift up to “n” elements.  For the stack, we needed only one end; for queue we need both.  To get around this, we will not shift upon removal of an element.
  • 36. 29 June 2018MNMJEC, Chennai - 600 097 36 Snapshot of a Queue
  • 37. 29 June 2018MNMJEC, Chennai - 600 097 37 enqueue()
  • 38. 29 June 2018MNMJEC, Chennai - 600 097 38 enqueue() again
  • 39. 29 June 2018MNMJEC, Chennai - 600 097 39 dequeue()
  • 40. 29 June 2018MNMJEC, Chennai - 600 097 40 dequeue() again
  • 41. 29 June 2018MNMJEC, Chennai - 600 097 41 Two more enqueue()
  • 42. 29 June 2018MNMJEC, Chennai - 600 097 42 What’s Wrong ?  We have inserts and removal running in constant time but we created a new problem.  We cannot insert new elements even though there are two places available at the start of the array.  Solution: allow the queue to “wrap around”. Basic idea is to picture the array as a circular array as show below.
  • 43. 29 June 2018MNMJEC, Chennai - 600 097 43 Queue array wrap-around
  • 44. 29 June 2018MNMJEC, Chennai - 600 097 44 enqueue(element) void enqueue(int x) { rear = (rear+1)%size; array[rear] = x; noElements = noElements+1; }
  • 45. 29 June 2018MNMJEC, Chennai - 600 097 45 dequeue() int dequeue() { int x = array[front]; front = (front+1)%size; noElements = noElements-1; return x; }
  • 46. 29 June 2018MNMJEC, Chennai - 600 097 46 isEmpty() and isFull() int isFull() { return noElements == size; } int isEmpty() { return noElements == 0; }
  • 47. 29 June 2018MNMJEC, Chennai - 600 097 47 Pointer Based Implementation  Possible implementations of a pointer-based queue  A linear linked list with two external references  A reference to the front  A reference to the back  A circular linked list with one external reference  A reference to the back
  • 48. 29 June 2018MNMJEC, Chennai - 600 097 48 Queue - Linear and Circular List
  • 49. 29 June 2018MNMJEC, Chennai - 600 097 49 Non-empty Queue - Insertion
  • 50. 29 June 2018MNMJEC, Chennai - 600 097 50 Empty Queue - Insertion
  • 51. 29 June 2018MNMJEC, Chennai - 600 097 51 Queue with more than 1 element - Deletion
  • 52. 29 June 2018MNMJEC, Chennai - 600 097 52 Applications of Queues  Queue is used when things don’t have to be processed immediatly, but have to be processed in First In First Out order like Breadth First Search.  This property of Queue makes it also useful in following kind of scenarios.
  • 53. 29 June 2018MNMJEC, Chennai - 600 097 53 Applications of Queues  When a resource is shared among multiple consumers. Examples include CPU scheduling, Disk Scheduling.  When data is transferred asynchronously (data not necessarily received at same rate as sent) between two processes. Examples include IO Buffers, pipes, file IO, etc.  Operating systems often maintain a queue of processes that are ready to execute or that are waiting for a particular event to occur.
  • 54. 29 June 2018MNMJEC, Chennai - 600 097 54 Applications of Queues  Computer systems must often provide a “holding area” for messages between two processes, two programs, or even two systems. This holding area is usually called a “buffer” and is often implemented as a queue.  Call center phone systems will use a queue to hold people in line until a service representative is free.
  • 55. 29 June 2018MNMJEC, Chennai - 600 097 55 Applications of Queues  Buffers on MP3 players and portable CD players, iPod playlist. Playlist for jukebox - add songs to the end, play from the front of the list.  Round Robin (RR) algorithm is an important scheduling algorithm. It is used especially for the time-sharing system. The circular queue is used to implement such algorithms.
  • 56. Questions and Discussion 29 June 2018 56 MNMJEC, Chennai - 600 097