SlideShare a Scribd company logo
Introduction to Stacks
• What is a Stack
• Stack implementation using array.
• Stack implementation using linked list.
• Applications of Stack.
What is a Stack?
• Stack is a data structure in which data is added and
removed at only one end called the top.
• To add (push) an item to the stack, it must be placed on
the top of the stack.
• To remove (pop) an item from the stack, it must be
removed from the top of the stack too.
• Thus, the last element that is pushed into the stack, is
the first element to be popped out of the stack.
i.e., Last In First Out (LIFO)
An Example of Stack
2
8
1
7
2
7
2
1
7
2
1
7
2
8
1
7
2
8
1
7
2
top
top
top
top
top
top
Push(8) Push(2)
pop()
pop()
pop()
Stack Implementations
• In our implementation, a stack is a container that extends the
AbstractContainer class and implements the Stack interface.
• Two implementations:
– StackAsArray
• The underlying data structure is an array of Object
– StackAsLinkedList
• The underlying data structure is an object of MyLinkedList
public interface Stack extends Container {
public abstract Object getTop();
public abstract void push(Object obj);
public abstract Object pop();
}
StackAsArray – Constructor
• In the StackAsArray implementation that follows, the top of the stack is
array[count – 1] and the bottom is array[0]:
• The constructor’s single parameter, size, specifies the maximum
number of items that can be stored in the stack.
• The variable array is initialized to be an array of length size.
public class StackAsArray extends AbstractContainer
implements Stack {
protected Object[] array;
public StackAsArray(int size){
array = new Object[size];
}
// …
StackAsArray – purge() Method
• The purpose of the purge method is to remove all the
contents of a container.
• To empty the stack, the purge method simply assigns
the value null to the first count positions of the array.
public void purge(){
while (count > 0)
array[--count] = null;
}
Complexity is O(n(
StackAsArray – push() Method
• push() method adds an element at the top the stack.
• It takes as argument an Object to be pushed.
• It first checks if there is room left in the stack. If no room
is left, it throws a ContainerFullException exception.
Otherwise, it puts the object into the array, and then
increments count variable by one.
public void push(Object object){
if (count == array.length)
throw new ContainerFullException();
else
array[count++] = object;
}
Complexity is O(1(
StackAsArray – pop() Method
• The pop method removes an item from the stack and
returns that item.
• The pop method first checks if the stack is empty. If the
stack is empty, it throws a ContainerEmptyException.
Otherwise, it simply decreases count by one and returns
the item found at the top of the stack.
public Object pop(){
if(count == 0)
throw new ContainerEmptyException();
else {
Object result = array[--count];
array[count] = null;
return result;
}
} Complexity is O(1(
StackAsArray – getTop() Method
• getTop() method first checks if the stack is empty.
• getTop() method is a stack accessor which returns the top
item in the stack without removing that item. If the stack is
empty, it throws a ContainerEmptyException. Otherwise,
it returns the top item found at position count-1.
public Object getTop(){
if(count == 0)
throw new ContainerEmptyException();
else
return array[count – 1];
}
Complexity is O(1(
StackAsArray – iterator() Method
public Iterator iterator() {
return new Iterator() {
private int position = count-1;
public boolean hasNext() {
return position >=0;
}
public Object next () {
if(position < 0)
throw new NoSuchElementException();
else
return array[position--];
}
};
}
StackAsLinkedList Implementation
public class StackAsLinkedList
extends AbstractContainer
implements Stack {
protected MyLinkedList list;
public StackAsLinkedList(){
list = new MyLinkedList();
}
public void purge(){
list.purge();
count = 0;
}
// …
Complexity is O(1(
StackAsLinkedList Implementation (Cont.)
public void push(Object obj){
list.prepend(obj);
count++;
}
public Object pop(){
if(count == 0)
throw new ContainerEmptyException();
else{
Object obj = list.getFirst();
list.extractFirst();
count--;
return obj;
}
}
public Object getTop(){
if(count == 0)
throw new ContainerEmptyException();
else
return list.getFirst();
}
Complexity is O(1(
Complexity is O(1(
Complexity is O(1(
StackAsLinkedList Implementation (Cont.)
public Iterator iterator() {
return new Iterator() {
private MyLinkedList.Element position =
list.getHead();
public boolean hasNext() {
return position != null;
}
public Object next() {
if(position == null)
throw new NoSuchElementException();
else {
Object obj = position.getData();
position = position.getNext();
return obj;
}
}
};
}
Applications of Stack
• Some direct applications:
– Page-visited history in a Web browser
– Undo sequence in a text editor
– Chain of method calls in the Java Virtual Machine
– Evaluating postfix expressions
• Some indirect applications
– Auxiliary data structure for some algorithms
– Component of other data structures
Application of Stack - Evaluating Postfix Expression
(5+9)*2+6*5
• An ordinary arithmetical expression like the above is called infix-
expression -- binary operators appear in between their operands.
• The order of operations evaluation is determined by the
precedence rules and parenthesis.
• When an evaluation order is desired that is different from that
provided by the precedence, parentheses are used to override
precedence rules.
Application of Stack - Evaluating Postfix Expression
• Expressions can also be represented using postfix notation -
where an operator comes after its two operands.
• The advantage of postfix notation is that the order of operation
evaluation is unique without the need for precedence rules or
parenthesis.
Infix Postfix
16 / 2 16 2 /
(2 + 14)* 5 2 14 + 5 *
2 + 14 * 5 2 14 5 * +
(6 – 2) * (5 + 4) 6 2 - 5 4 + *
Application of Stack - Evaluating Postfix Expression
• The following algorithm uses a stack to evaluate a postfix expressions.
Start with an empty stack
for (each item in the expression) {
if (the item is a number)
Push the number onto the stack
else if (the item is an operator){
Pop two operands from the stack
Apply the operator to the operands
Push the result onto the stack
}
}
Pop the only one number from the stack – that’s the result of the evaluation
Application of Stack - Evaluating Postfix Expression
• Example: Consider the postfix expression, 2 10 + 9 6 - /, which is
(2 + 10) / (9 - 6) in infix, the result of which is 12 / 3 = 4.
• The following is a trace of the postfix evaluation algorithm for the
above.

More Related Content

What's hot

Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applicationsJsaddam Hussain
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
Archie Jamwal
 
Linked list
Linked listLinked list
Linked list
akshat360
 
Stacks
StacksStacks
Stacks
sweta dargad
 
Prefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsPrefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix Notations
Afaq Mansoor Khan
 
Queue ppt
Queue pptQueue ppt
Queue ppt
SouravKumar328
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
Apurbo Datta
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
Ashim Lamichhane
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
Vineeta Garg
 
Stack data structure
Stack data structureStack data structure
Stack data structureTech_MX
 
Introduction to stack
Introduction to stackIntroduction to stack
Introduction to stackvaibhav2910
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
eShikshak
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
Adam Mukharil Bachtiar
 
Linked List
Linked ListLinked List
Linked List
Ashim Lamichhane
 
stack & queue
stack & queuestack & queue
stack & queue
manju rani
 
Infix to postfix conversion
Infix to postfix conversionInfix to postfix conversion
Infix to postfix conversion
Then Murugeshwari
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
shameen khan
 

What's hot (20)

Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applications
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
 
Linked list
Linked listLinked list
Linked list
 
Stacks
StacksStacks
Stacks
 
Prefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsPrefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix Notations
 
Queue ppt
Queue pptQueue ppt
Queue ppt
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
Data structure by Digvijay
Data structure by DigvijayData structure by Digvijay
Data structure by Digvijay
 
Introduction to stack
Introduction to stackIntroduction to stack
Introduction to stack
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
Linked List
Linked ListLinked List
Linked List
 
single linked list
single linked listsingle linked list
single linked list
 
Heaps
HeapsHeaps
Heaps
 
stack & queue
stack & queuestack & queue
stack & queue
 
Infix to postfix conversion
Infix to postfix conversionInfix to postfix conversion
Infix to postfix conversion
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 

Viewers also liked

Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj Meena
Dipayan Sarkar
 
Extendible hashing
Extendible hashingExtendible hashing
Extendible hashing
Indika Munaweera Kankanamge
 
CloudConnect 2012: The cloud application stack
CloudConnect 2012: The cloud application stackCloudConnect 2012: The cloud application stack
CloudConnect 2012: The cloud application stack
Geva Perry
 
Chapter13
Chapter13Chapter13
Chapter13
gourab87
 
Ppt on Linked list,stack,queue
Ppt on Linked list,stack,queuePpt on Linked list,stack,queue
Ppt on Linked list,stack,queue
Srajan Shukla
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
Muhammad Hammad Waseem
 
Stack using Linked List
Stack using Linked ListStack using Linked List
Stack using Linked List
Sayantan Sur
 
Computer ethics
Computer ethicsComputer ethics
Computer ethics
onur karaman
 
Stack implementation using c
Stack implementation using cStack implementation using c
Stack implementation using c
Rajendran
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examplesgreatqadirgee4u
 
Stack Applications
Stack ApplicationsStack Applications
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application Tech_MX
 
Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListPTCL
 

Viewers also liked (14)

Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj Meena
 
Extendible hashing
Extendible hashingExtendible hashing
Extendible hashing
 
CloudConnect 2012: The cloud application stack
CloudConnect 2012: The cloud application stackCloudConnect 2012: The cloud application stack
CloudConnect 2012: The cloud application stack
 
Chapter13
Chapter13Chapter13
Chapter13
 
Ppt on Linked list,stack,queue
Ppt on Linked list,stack,queuePpt on Linked list,stack,queue
Ppt on Linked list,stack,queue
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 
Stack using Linked List
Stack using Linked ListStack using Linked List
Stack using Linked List
 
Computer ethics
Computer ethicsComputer ethics
Computer ethics
 
Seminar algorithms of web
Seminar algorithms of webSeminar algorithms of web
Seminar algorithms of web
 
Stack implementation using c
Stack implementation using cStack implementation using c
Stack implementation using c
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examples
 
Stack Applications
Stack ApplicationsStack Applications
Stack Applications
 
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application
 
Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked List
 

Similar to stack presentation

stack_presentaton_HUSNAIN[2].pojklklklptx
stack_presentaton_HUSNAIN[2].pojklklklptxstack_presentaton_HUSNAIN[2].pojklklklptx
stack_presentaton_HUSNAIN[2].pojklklklptx
HusnainNaqvi2
 
Stack linked list
Stack linked listStack linked list
Stack linked listbhargav0077
 
Educational slides by venay magen
Educational slides by venay magenEducational slides by venay magen
Educational slides by venay magen
venaymagen19
 
U3.stack queue
U3.stack queueU3.stack queue
U3.stack queue
Ssankett Negi
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
AliRaza899305
 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
Balwant Gorad
 
Chapter 5-stack.pptx
Chapter 5-stack.pptxChapter 5-stack.pptx
Chapter 5-stack.pptx
Halid Assen
 
My lecture stack_queue_operation
My lecture stack_queue_operationMy lecture stack_queue_operation
My lecture stack_queue_operation
Senthil Kumar
 
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for public
iqbalphy1
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
chandankumar364348
 
Stack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTStack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADT
Soumen Santra
 
9 python data structure-2
9 python data structure-29 python data structure-2
9 python data structure-2
Prof. Dr. K. Adisesha
 
Chapter 5 Stack and Queue.pdf
Chapter 5 Stack and Queue.pdfChapter 5 Stack and Queue.pdf
Chapter 5 Stack and Queue.pdf
GirT2
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arraystameemyousaf
 
Stack and its operations, Queue and its operations
Stack and its operations, Queue and its operationsStack and its operations, Queue and its operations
Stack and its operations, Queue and its operations
poongothai11
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
SupriyaGhosh43
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
Waf1231
 
(674335607) cs2309 java-lab-manual
(674335607) cs2309 java-lab-manual(674335607) cs2309 java-lab-manual
(674335607) cs2309 java-lab-manual
Chandrapriya Jayabal
 

Similar to stack presentation (20)

stack_presentaton_HUSNAIN[2].pojklklklptx
stack_presentaton_HUSNAIN[2].pojklklklptxstack_presentaton_HUSNAIN[2].pojklklklptx
stack_presentaton_HUSNAIN[2].pojklklklptx
 
Stack linked list
Stack linked listStack linked list
Stack linked list
 
Educational slides by venay magen
Educational slides by venay magenEducational slides by venay magen
Educational slides by venay magen
 
U3.stack queue
U3.stack queueU3.stack queue
U3.stack queue
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
 
LectureNotes-06-DSA
LectureNotes-06-DSALectureNotes-06-DSA
LectureNotes-06-DSA
 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
 
Chapter 5-stack.pptx
Chapter 5-stack.pptxChapter 5-stack.pptx
Chapter 5-stack.pptx
 
My lecture stack_queue_operation
My lecture stack_queue_operationMy lecture stack_queue_operation
My lecture stack_queue_operation
 
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for public
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
 
Stack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTStack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADT
 
9 python data structure-2
9 python data structure-29 python data structure-2
9 python data structure-2
 
Chapter 5 Stack and Queue.pdf
Chapter 5 Stack and Queue.pdfChapter 5 Stack and Queue.pdf
Chapter 5 Stack and Queue.pdf
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays
 
Stack and its operations, Queue and its operations
Stack and its operations, Queue and its operationsStack and its operations, Queue and its operations
Stack and its operations, Queue and its operations
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 
Stacks
StacksStacks
Stacks
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 
(674335607) cs2309 java-lab-manual
(674335607) cs2309 java-lab-manual(674335607) cs2309 java-lab-manual
(674335607) cs2309 java-lab-manual
 

More from Shivalik college of engineering

Front pages of practical file
Front pages of practical fileFront pages of practical file
Front pages of practical file
Shivalik college of engineering
 
Algorithms Question bank
Algorithms Question bankAlgorithms Question bank
Algorithms Question bank
Shivalik college of engineering
 
Video streaming
Video streamingVideo streaming
Infosystestpattern
InfosystestpatternInfosystestpattern
Mydbms
MydbmsMydbms
Introduction to xml
Introduction to xmlIntroduction to xml
Introduction to xml
Shivalik college of engineering
 
Net overview
Net overviewNet overview
java vs C#
java vs C#java vs C#
Dbms lab file format front page
Dbms lab file format front pageDbms lab file format front page
Dbms lab file format front page
Shivalik college of engineering
 
Question bank toafl
Question bank toaflQuestion bank toafl
Question bank toafl
Shivalik college of engineering
 
computer architecture.
computer architecture.computer architecture.
computer architecture.
Shivalik college of engineering
 
Parallel processing
Parallel processingParallel processing
Parallel processing
Shivalik college of engineering
 
SQA presenatation made by krishna ballabh gupta
SQA presenatation made by krishna ballabh guptaSQA presenatation made by krishna ballabh gupta
SQA presenatation made by krishna ballabh gupta
Shivalik college of engineering
 
Webapplication ppt prepared by krishna ballabh gupta
Webapplication ppt prepared by krishna ballabh guptaWebapplication ppt prepared by krishna ballabh gupta
Webapplication ppt prepared by krishna ballabh gupta
Shivalik college of engineering
 
Cloud computing prepare by krishna ballabh gupta
Cloud computing prepare by krishna ballabh guptaCloud computing prepare by krishna ballabh gupta
Cloud computing prepare by krishna ballabh gupta
Shivalik college of engineering
 
Cloud computing kb gupta
Cloud computing kb guptaCloud computing kb gupta
Cloud computing kb gupta
Shivalik college of engineering
 
comparing windows and linux ppt
comparing windows and linux pptcomparing windows and linux ppt
comparing windows and linux ppt
Shivalik college of engineering
 
Gsm an introduction....
Gsm an introduction....Gsm an introduction....
Gsm an introduction....
Shivalik college of engineering
 
Gsm an introduction....
Gsm an introduction....Gsm an introduction....
Gsm an introduction....
Shivalik college of engineering
 

More from Shivalik college of engineering (20)

Front pages of practical file
Front pages of practical fileFront pages of practical file
Front pages of practical file
 
Algorithms Question bank
Algorithms Question bankAlgorithms Question bank
Algorithms Question bank
 
Video streaming
Video streamingVideo streaming
Video streaming
 
Infosystestpattern
InfosystestpatternInfosystestpattern
Infosystestpattern
 
Mydbms
MydbmsMydbms
Mydbms
 
Introduction to xml
Introduction to xmlIntroduction to xml
Introduction to xml
 
Net overview
Net overviewNet overview
Net overview
 
java vs C#
java vs C#java vs C#
java vs C#
 
sear
searsear
sear
 
Dbms lab file format front page
Dbms lab file format front pageDbms lab file format front page
Dbms lab file format front page
 
Question bank toafl
Question bank toaflQuestion bank toafl
Question bank toafl
 
computer architecture.
computer architecture.computer architecture.
computer architecture.
 
Parallel processing
Parallel processingParallel processing
Parallel processing
 
SQA presenatation made by krishna ballabh gupta
SQA presenatation made by krishna ballabh guptaSQA presenatation made by krishna ballabh gupta
SQA presenatation made by krishna ballabh gupta
 
Webapplication ppt prepared by krishna ballabh gupta
Webapplication ppt prepared by krishna ballabh guptaWebapplication ppt prepared by krishna ballabh gupta
Webapplication ppt prepared by krishna ballabh gupta
 
Cloud computing prepare by krishna ballabh gupta
Cloud computing prepare by krishna ballabh guptaCloud computing prepare by krishna ballabh gupta
Cloud computing prepare by krishna ballabh gupta
 
Cloud computing kb gupta
Cloud computing kb guptaCloud computing kb gupta
Cloud computing kb gupta
 
comparing windows and linux ppt
comparing windows and linux pptcomparing windows and linux ppt
comparing windows and linux ppt
 
Gsm an introduction....
Gsm an introduction....Gsm an introduction....
Gsm an introduction....
 
Gsm an introduction....
Gsm an introduction....Gsm an introduction....
Gsm an introduction....
 

Recently uploaded

The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
Pipe Restoration Solutions
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
AafreenAbuthahir2
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
Kamal Acharya
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
ssuser9bd3ba
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
Kamal Acharya
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
Kamal Acharya
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdfCOLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
Kamal Acharya
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
Jayaprasanna4
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
abh.arya
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
VENKATESHvenky89705
 

Recently uploaded (20)

The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdfCOLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
 

stack presentation

  • 1. Introduction to Stacks • What is a Stack • Stack implementation using array. • Stack implementation using linked list. • Applications of Stack.
  • 2. What is a Stack? • Stack is a data structure in which data is added and removed at only one end called the top. • To add (push) an item to the stack, it must be placed on the top of the stack. • To remove (pop) an item from the stack, it must be removed from the top of the stack too. • Thus, the last element that is pushed into the stack, is the first element to be popped out of the stack. i.e., Last In First Out (LIFO)
  • 3. An Example of Stack 2 8 1 7 2 7 2 1 7 2 1 7 2 8 1 7 2 8 1 7 2 top top top top top top Push(8) Push(2) pop() pop() pop()
  • 4. Stack Implementations • In our implementation, a stack is a container that extends the AbstractContainer class and implements the Stack interface. • Two implementations: – StackAsArray • The underlying data structure is an array of Object – StackAsLinkedList • The underlying data structure is an object of MyLinkedList public interface Stack extends Container { public abstract Object getTop(); public abstract void push(Object obj); public abstract Object pop(); }
  • 5. StackAsArray – Constructor • In the StackAsArray implementation that follows, the top of the stack is array[count – 1] and the bottom is array[0]: • The constructor’s single parameter, size, specifies the maximum number of items that can be stored in the stack. • The variable array is initialized to be an array of length size. public class StackAsArray extends AbstractContainer implements Stack { protected Object[] array; public StackAsArray(int size){ array = new Object[size]; } // …
  • 6. StackAsArray – purge() Method • The purpose of the purge method is to remove all the contents of a container. • To empty the stack, the purge method simply assigns the value null to the first count positions of the array. public void purge(){ while (count > 0) array[--count] = null; } Complexity is O(n(
  • 7. StackAsArray – push() Method • push() method adds an element at the top the stack. • It takes as argument an Object to be pushed. • It first checks if there is room left in the stack. If no room is left, it throws a ContainerFullException exception. Otherwise, it puts the object into the array, and then increments count variable by one. public void push(Object object){ if (count == array.length) throw new ContainerFullException(); else array[count++] = object; } Complexity is O(1(
  • 8. StackAsArray – pop() Method • The pop method removes an item from the stack and returns that item. • The pop method first checks if the stack is empty. If the stack is empty, it throws a ContainerEmptyException. Otherwise, it simply decreases count by one and returns the item found at the top of the stack. public Object pop(){ if(count == 0) throw new ContainerEmptyException(); else { Object result = array[--count]; array[count] = null; return result; } } Complexity is O(1(
  • 9. StackAsArray – getTop() Method • getTop() method first checks if the stack is empty. • getTop() method is a stack accessor which returns the top item in the stack without removing that item. If the stack is empty, it throws a ContainerEmptyException. Otherwise, it returns the top item found at position count-1. public Object getTop(){ if(count == 0) throw new ContainerEmptyException(); else return array[count – 1]; } Complexity is O(1(
  • 10. StackAsArray – iterator() Method public Iterator iterator() { return new Iterator() { private int position = count-1; public boolean hasNext() { return position >=0; } public Object next () { if(position < 0) throw new NoSuchElementException(); else return array[position--]; } }; }
  • 11. StackAsLinkedList Implementation public class StackAsLinkedList extends AbstractContainer implements Stack { protected MyLinkedList list; public StackAsLinkedList(){ list = new MyLinkedList(); } public void purge(){ list.purge(); count = 0; } // … Complexity is O(1(
  • 12. StackAsLinkedList Implementation (Cont.) public void push(Object obj){ list.prepend(obj); count++; } public Object pop(){ if(count == 0) throw new ContainerEmptyException(); else{ Object obj = list.getFirst(); list.extractFirst(); count--; return obj; } } public Object getTop(){ if(count == 0) throw new ContainerEmptyException(); else return list.getFirst(); } Complexity is O(1( Complexity is O(1( Complexity is O(1(
  • 13. StackAsLinkedList Implementation (Cont.) public Iterator iterator() { return new Iterator() { private MyLinkedList.Element position = list.getHead(); public boolean hasNext() { return position != null; } public Object next() { if(position == null) throw new NoSuchElementException(); else { Object obj = position.getData(); position = position.getNext(); return obj; } } }; }
  • 14. Applications of Stack • Some direct applications: – Page-visited history in a Web browser – Undo sequence in a text editor – Chain of method calls in the Java Virtual Machine – Evaluating postfix expressions • Some indirect applications – Auxiliary data structure for some algorithms – Component of other data structures
  • 15. Application of Stack - Evaluating Postfix Expression (5+9)*2+6*5 • An ordinary arithmetical expression like the above is called infix- expression -- binary operators appear in between their operands. • The order of operations evaluation is determined by the precedence rules and parenthesis. • When an evaluation order is desired that is different from that provided by the precedence, parentheses are used to override precedence rules.
  • 16. Application of Stack - Evaluating Postfix Expression • Expressions can also be represented using postfix notation - where an operator comes after its two operands. • The advantage of postfix notation is that the order of operation evaluation is unique without the need for precedence rules or parenthesis. Infix Postfix 16 / 2 16 2 / (2 + 14)* 5 2 14 + 5 * 2 + 14 * 5 2 14 5 * + (6 – 2) * (5 + 4) 6 2 - 5 4 + *
  • 17. Application of Stack - Evaluating Postfix Expression • The following algorithm uses a stack to evaluate a postfix expressions. Start with an empty stack for (each item in the expression) { if (the item is a number) Push the number onto the stack else if (the item is an operator){ Pop two operands from the stack Apply the operator to the operands Push the result onto the stack } } Pop the only one number from the stack – that’s the result of the evaluation
  • 18. Application of Stack - Evaluating Postfix Expression • Example: Consider the postfix expression, 2 10 + 9 6 - /, which is (2 + 10) / (9 - 6) in infix, the result of which is 12 / 3 = 4. • The following is a trace of the postfix evaluation algorithm for the above.