SlideShare a Scribd company logo
STACK ADT
Jun Y. Ercia
The Stack ADT
Objectives
• examine stack processing
• define a stack abstract data type
• demonstrate how a stack can be used to solve problems
• examine various stack implementations
• compare stack implementations
The Stack ADT
Data
 a collection whose elements are added and removed
from one end, called the top of the stack.
 a stack is a Last In, First Out (LIFO) data structure
Example
• Stack of plates in a cafeteria
Note ▪ items are removed from a stack in the
reverse order from the way they were
inserted
▪ It is not legal to access any item other
than the one that is at the top of the
stack!
Operations on a Stack
Stack Errors
1. Underflow – occurs when trying to pop (or peek at) an empty stack
2. Overflow – occurs when trying to push onto an already full stack
Uses of Stacks in Computing
Stack is useful for any kind of problem involving LIFO data
1. Calculators:
− Convert infix expressions to postfix to make evaluation easier
− Evaluate postfix expressions
2. Compilers:
− to convert infix expressions to postfix, to make translation to lower level language easier
3. Word processors, editors, etc:
− to check expressions or strings of text for matching parentheses
− to implement undo operations
▪ keeps track of the most recent operations
▪ why are they kept in reverse order?
Implementation of Stack ADT
1. Array implementation of Stack
▪ The entire stack is represented by an array together with a pointer top pointing to the next available cell in the array.
▪ An empty stack is represented with top pointing to the first cell in the array.
▪ Suitable for applications in which the maximum stack size is known ahead of time.
Code for adding an item to the top of the stack
Code for removing the item most recently
added
Code for determining whether the stack is
empty
Code for determining whether the stack is full
Code for making the stack empty
Code for retrieving the item most recently
added
Implementation of Stack ADT
1. Linked List implementation of Stack
▪ The entire stack is represented by a linked list of nodes together with
a pointer top pointing to the first node of the linked list.
▪ An empty stack is represented with top pointing NULL.
▪ Suitable for applications in which the maximum stack size is not
known beforehand.
Note ▪ With a linked-list representation,
overflow will not happen (unless you
exhaust memory, which is another
kind of problem)
▪ Underflow can happen, and should be
handled.
Code for adding an item to the top of the stack
Code for removing the item most recently
added
Code for determining whether the stack is
empty
Code for making the stack empty
Code for retrieving the item most recently
added

More Related Content

PPTX
CD3291 2.5 stack.pptx
PPTX
Stack in Sata Structure
PDF
The Stack (Data Structccccccccccccccccccc
PDF
Stacks and Queues - C++ introduction to stacks and queues
PPTX
Stacks and Queue,Concept of Stack,LIFO,Fifo,
PPTX
The presentation on stack data structure
PPTX
Lecture#5 - Stack ADT.pptx
PPTX
5.-Stacks.pptx
CD3291 2.5 stack.pptx
Stack in Sata Structure
The Stack (Data Structccccccccccccccccccc
Stacks and Queues - C++ introduction to stacks and queues
Stacks and Queue,Concept of Stack,LIFO,Fifo,
The presentation on stack data structure
Lecture#5 - Stack ADT.pptx
5.-Stacks.pptx

Similar to Lesson 3 - Stack ADT.pdf (20)

PPTX
DSA_STACK_INTRODUCTORY_TUTORIAL_PPT.pptx
DOCX
Stacks in data structure
PPT
stack.ppt
PPT
week 7,8,10,11 alll files included from .ppt
PPTX
Stack and its applications
PPTX
Data structure
PPT
Stack a Data Structure
PPTX
PPT
Lecture 2c stacks
PPTX
My lecture stack_queue_operation
PPTX
Uni-2-Session-1-Stacks Introduction.pptx
PDF
Chapter 5 Stack and Queue.pdf
PPTX
Data Structure ARRAY REPRESENTATION OF STACKS
PDF
Chapter 4 stack
PPT
Slide 11_2.ppt Data structure and sslgorithmd slides of stacka nd trees
PPTX
Stack Data Structre Introduction and source code
PPTX
Stack Data structure
PDF
04 stacks
PPTX
STACK.pptx
DSA_STACK_INTRODUCTORY_TUTORIAL_PPT.pptx
Stacks in data structure
stack.ppt
week 7,8,10,11 alll files included from .ppt
Stack and its applications
Data structure
Stack a Data Structure
Lecture 2c stacks
My lecture stack_queue_operation
Uni-2-Session-1-Stacks Introduction.pptx
Chapter 5 Stack and Queue.pdf
Data Structure ARRAY REPRESENTATION OF STACKS
Chapter 4 stack
Slide 11_2.ppt Data structure and sslgorithmd slides of stacka nd trees
Stack Data Structre Introduction and source code
Stack Data structure
04 stacks
STACK.pptx
Ad

Recently uploaded (20)

PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
Trump Administration's workforce development strategy
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Computing-Curriculum for Schools in Ghana
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
Classroom Observation Tools for Teachers
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Lesson notes of climatology university.
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PDF
Yogi Goddess Pres Conference Studio Updates
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
RMMM.pdf make it easy to upload and study
PPTX
Cell Types and Its function , kingdom of life
PDF
O7-L3 Supply Chain Operations - ICLT Program
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Trump Administration's workforce development strategy
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Computing-Curriculum for Schools in Ghana
GDM (1) (1).pptx small presentation for students
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Classroom Observation Tools for Teachers
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Module 4: Burden of Disease Tutorial Slides S2 2025
Lesson notes of climatology university.
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
Yogi Goddess Pres Conference Studio Updates
Supply Chain Operations Speaking Notes -ICLT Program
RMMM.pdf make it easy to upload and study
Cell Types and Its function , kingdom of life
O7-L3 Supply Chain Operations - ICLT Program
Ad

Lesson 3 - Stack ADT.pdf

  • 2. The Stack ADT Objectives • examine stack processing • define a stack abstract data type • demonstrate how a stack can be used to solve problems • examine various stack implementations • compare stack implementations
  • 3. The Stack ADT Data  a collection whose elements are added and removed from one end, called the top of the stack.  a stack is a Last In, First Out (LIFO) data structure Example • Stack of plates in a cafeteria Note ▪ items are removed from a stack in the reverse order from the way they were inserted ▪ It is not legal to access any item other than the one that is at the top of the stack!
  • 5. Stack Errors 1. Underflow – occurs when trying to pop (or peek at) an empty stack 2. Overflow – occurs when trying to push onto an already full stack
  • 6. Uses of Stacks in Computing Stack is useful for any kind of problem involving LIFO data 1. Calculators: − Convert infix expressions to postfix to make evaluation easier − Evaluate postfix expressions 2. Compilers: − to convert infix expressions to postfix, to make translation to lower level language easier 3. Word processors, editors, etc: − to check expressions or strings of text for matching parentheses − to implement undo operations ▪ keeps track of the most recent operations ▪ why are they kept in reverse order?
  • 7. Implementation of Stack ADT 1. Array implementation of Stack ▪ The entire stack is represented by an array together with a pointer top pointing to the next available cell in the array. ▪ An empty stack is represented with top pointing to the first cell in the array. ▪ Suitable for applications in which the maximum stack size is known ahead of time.
  • 8. Code for adding an item to the top of the stack
  • 9. Code for removing the item most recently added
  • 10. Code for determining whether the stack is empty
  • 11. Code for determining whether the stack is full
  • 12. Code for making the stack empty
  • 13. Code for retrieving the item most recently added
  • 14. Implementation of Stack ADT 1. Linked List implementation of Stack ▪ The entire stack is represented by a linked list of nodes together with a pointer top pointing to the first node of the linked list. ▪ An empty stack is represented with top pointing NULL. ▪ Suitable for applications in which the maximum stack size is not known beforehand. Note ▪ With a linked-list representation, overflow will not happen (unless you exhaust memory, which is another kind of problem) ▪ Underflow can happen, and should be handled.
  • 15. Code for adding an item to the top of the stack
  • 16. Code for removing the item most recently added
  • 17. Code for determining whether the stack is empty
  • 18. Code for making the stack empty
  • 19. Code for retrieving the item most recently added