CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Lecture Stacks. A stack is a Last-In-First-Out (LIFO) or a First-In-Last-Out (FILO) abstract data type E.g. a deck of cards in which cards may be added.
COMPSCI 105 S Principles of Computer Science 13 Stacks.
CHAPTER 7 Queues.
CS Data Structures II Review COSC 2006 April 14, 2017
Chapter 3: Abstract Data Types Lists, Stacks Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
Stacks Chapter 5. Chapter Objectives  To learn about the stack data type and how to use its four methods: push, pop, peek, and empty  To understand.
Chapter 3 Stacks.
Data Structures & Algorithms
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks Chapter 5. Chapter 5: Stacks2 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop, peek, and empty.
Introduction to Stacks What is a Stack Stack implementation using arrays. Application of Stack.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Chapter 3: Abstract Data Types Queues Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
CHAPTER 8 Lists. 2 A list is a linear collection Adding and removing elements in lists are not restricted by the collection structure We will examine.
1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
Stacks.
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
Introduction to Stacks What is a Stack Stack implementation using array. Stack implementation using linked list. Applications of Stack.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
TCSS 342, Winter 2005 Lecture Notes
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
Exam 1 –Monday June 25 th –open Book / Open Notes –No Electronic Devices (calculators, laptops, etc) –Room Number: W –Time: 5:30pm to 8:00pm.
Topic 3 The Stack ADT.
Chapter 7 Stacks II CS Data Structures I COSC 2006
Ceng-112 Data Structures ITurgut Kalfaoglu 1 Chapter 3 Stacks.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
Information and Computer Sciences University of Hawaii, Manoa
Chapter 7 Stacks. © 2004 Pearson Addison-Wesley. All rights reserved 7-2 The Abstract Data Type: Developing an ADT During the Design of a Solution Specifications.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
Lab 6 Stack ADT. OVERVIEW The stack is one example of a constrained linear data structure. In a stack, the elements are ordered from most recently added.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
Stacks Chapter 5 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Click to edit Master text styles Stacks Data Structure.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
Stacks Stack Abstract Data Type (ADT) Stack ADT Interface
Stack: a Linked Implementation
Data Structures Using C++ 2E
Comprehensive Introduction to OOP with Java, C. Thomas Wu Stack ADT
Stacks.
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
Cinda Heeren / Geoffrey Tien
Visit for more Learning Resources
Building Java Programs Chapter 14
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stack ADT Operations Application: Expression Evaluation
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks Data structure Elements added, removed from one end only
Stacks.
Stacks CS-240 Dick Steflik.
Introduction to Stacks
Introduction to Stacks
© 2016 Pearson Education, Ltd. All rights reserved.
CHAPTER 3: Collections—Stacks
Presentation transcript:

CHAPTER 6 Stacks

2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first element to be removed A stack is LIFO – last in, first out

3 A conceptual view of a stack

4 Basic operations Push: store a data item at the top of the stack Pop: retrieve a data item from the top of the stack

5 Example Push Item 1 Push Item 2 Push Item 3 Pop Push Item 4 Pop Item 1 Item 2 Item 1 Item 3 Item 2 Item 1 Item 2 Item 1 Item 4 Item 2 Item 1

6 More operations on a stack

7 ADT definition of STACK Notation: Sstack eitem of same type as the elements of S bboolean value

8 Operations InitStack(S) Procedure to initialize S to an empty stack Preconditions: none Postconditions: S empty

9 Operations Push(S,e) Procedure to place an item e into S (if there is room, i.e. S is not full) Preconditions: S not full Postconditions: size of S increased by 1 Pop(S)  e Procedure to remove and return the last item stored in S (the top element) if S is not empty Preconditions: S not empty Postconditions: size of S decreased by 1

10 Operations Peek(S)  e Procedure to return (without removing) the last item stored in S (the top element) if S is not empty Preconditions: S not empty Postconditions: S not changed

11 Operations IsEmpty(S)  b Boolean function that returns TRUE if S is empty Preconditions: none Postconditions: S not changed

12 Stack AXIOMS s.InitStack().IsEmpty() = true s.MakeEmpty().IsEmpty() = true s.Push(g).IsEmpty() = false s.Push(g).Pop() = s s.Peek() = s

13 Example A procedure to replace the elements of a nonempty stack, assuming they are of type integers, with their sum Pre: A nonempty stack with elements of type integers. Post: s contains only one element - the sum of previously stored elements.

14 Algorithm 1. element1  pop(S) 2. while stack is not empty repeat 2.1. element2  pop(S) 2.2. push(S,element1+element2) 2.3. element1  pop(S) 3. push(S,element1)

15 Stack applications Undo operations in editors Evaluating an arithmetic expression using postfix notation Converting infix expression into postfix expression Depth-first search in a tree/graph

16 Stack implementation The interface class Linked implementation Array implementation

17 The interface class public interface StackADT { // Adds one element to the top of this stack public void push (T element); // Removes and returns the top element from this stack public T pop(); // Returns without removing the top element of this stack public T peek(); // Returns true if this stack contains no elements public boolean isEmpty(); // Returns the number of elements in this stack public int size(); // Returns a string representation of this stack public String toString(); }

18 Linked implementation Internally, a stack is represented as a linked list of nodes, with a reference to the top of the stack and an integer count of the number of nodes in the stack LinearNode class is reused to define the nodes

19 Linked implementation

20 Array implementation Design decisions: maintain an array of Object references the bottom of the stack is at index 0 the elements of the stack are in order and contiguous an integer variable top stores the index of the next available slot in the array This approach allows the stack to grow and shrink at the higher indexes

21 Array implementation