Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.

Slides:



Advertisements
Similar presentations
TK1924 Program Design & Problem Solving Session 2011/2012
Advertisements

DATA STRUCTURES USING C++ Chapter 5
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title : Overview of Stack.
DATA STRUCTURE & ALGORITHMS
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.
Data Structures & Algorithms
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 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 CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
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.
1 Introduction to Stacks What is a Stack? Stack implementation using array. Stack implementation using linked list. Applications of Stacks.
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Chapter 18: Stacks and Queues
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.
Chapter 18: Stacks and Queues
Data Structures Using C++ 2E
Chapter 17: Stacks and Queues
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Topic 3 The Stack ADT.
Comp 245 Data Structures Stacks. What is a Stack? A LIFO (last in, first out) structure Access (storage or retrieval) may only take place at the TOP NO.
Chapter 7 Stacks Dr. Youssef Harrath
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Chapter 4 Stacks Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving. Its called.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Data Structures Using C++ 2E Chapter 8 Queues. Data Structures Using C++ 2E2 Objectives Learn about queues Examine various queue operations Learn how.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
Chapter 18: Stacks and Queues
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
1 Recall Definition of Stack l Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
1 CS 132 Spring 2008 Chapter 7 Stacks Read p Problems 1-7.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues (part 2)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Data Structures Using C++1 Chapter 7 Stacks. Data Structures Using C++2 Chapter Objectives Learn about stacks Examine various stack operations Learn how.
Data Structures Using Java1 Chapter 6 Stacks. Data Structures Using Java2 Chapter Objectives Learn about stacks Examine various stack operations Learn.
1 Chapter 17: Stacks and Queues Learn about stacks Examine various stack operations Learn how to implement a stack as an array Learn how to implement a.
CHP-3 STACKS.
Chapter 17: Stacks and Queues. Objectives In this chapter, you will: – Learn about stacks – Examine various stack operations – Learn how to implement.
11/07/141 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: –Data stored –Operations on the.
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
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.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Chapter 17: Stacks and Queues Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Chapter 16: Linked Lists.
Data Structures Using C++ 2E
C++ Programming:. Program Design Including
Data Structures Using C++ 2E
Stacks and Queues Chapter 4.
Cinda Heeren / Geoffrey Tien
Stacks.
Data Structures Array Based Stacks.
Algorithms and Data Structures
Stacks, Queues, and Deques
Stacks Data structure Elements added, removed from one end only
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Data Structures Using C++ 2E
Presentation transcript:

Data Structures Using C++ 2E Chapter 7 Stacks

Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how to implement a stack as an array Learn how to implement a stack as a linked list Discover stack applications Learn how to use a stack to remove recursion

Data Structures Using C++ 2E3 Stacks Data structure –Elements added, removed from one end only –Last In First Out (LIFO) FIGURE 7-1 Various examples of stacks

Data Structures Using C++ 2E4 Stacks (cont’d.) push operation –Add element onto the stack top operation –Retrieve top element of the stack pop operation –Remove top element from the stack

Data Structures Using C++ 2E5 Stacks (cont’d.) FIGURE 7-2 Empty stack FIGURE 7-3 Stack operations

Stacks (cont’d.) Stack element removal –Occurs only if something is in the stack Stack element added only if room available isFullStack operation –Checks for full stack isEmptyStack operation –Checks for empty stack initializeStack operation –Initializes stack to an empty state Data Structures Using C++ 2E6

7 Stacks (cont’d.) Review code on page 398 –Illustrates class specifying basic stack operations FIGURE 7-4 UML class diagram of the class stackADT

Data Structures Using C++ 2E8 Implementation of Stacks as Arrays First stack element –Put in first array slot Second stack element –Put in second array slot, and so on Top of stack –Index of last element added to stack Stack element accessed only through the top –Problem: array is a random access data structure –Solution: use another variable ( stackTop ) Keeps track of the top position of the array

Data Structures Using C++ 2E9 Implementation of Stacks as Arrays (cont’d.) Review code on page 400 –Illustrates basic operations on a stack as an array FIGURE 7-5 UML class diagram of the class stackType

Data Structures Using C++ 2E10 Implementation of Stacks as Arrays (cont’d.) FIGURE 7-6 Example of a stack

Data Structures Using C++ 2E11 Initialize Stack Value of stackTop if stack empty –Set stackTop to zero to initialize the stack Definition of function initializeStack FIGURE 7-7 Empty stack

Data Structures Using C++ 2E12 Empty Stack Value of stackTop indicates if stack empty –If stackTop = zero: stack empty –Otherwise: stack not empty Definition of function isEmptyStack

Data Structures Using C++ 2E13 Full Stack Stack full –If stackTop is equal to maxStackSize Definition of function isFullStack

Data Structures Using C++ 2E14 Push Two-step process –Store newItem in array component indicated by stackTop –Increment stackTop FIGURE 7-8 Stack before and after the push operation

Data Structures Using C++ 2E15 Push (cont’d.) Definition of push operation

Data Structures Using C++ 2E16 Return the Top Element Definition of top operation

Data Structures Using C++ 2E17 Pop Remove (pop) element from stack –Decrement stackTop by one FIGURE 7-9 Stack before and after the pop operation

Data Structures Using C++ 2E18 Pop (cont’d.) Definition of pop operation Underflow –Removing an item from an empty stack Check within pop operation (see below) Check before calling function pop

Data Structures Using C++ 2E19 Copy Stack Definition of function copyStack

Data Structures Using C++ 2E20 Constructor and Destructor

Data Structures Using C++ 2E21 Copy Constructor Definition of the copy constructor

Data Structures Using C++ 2E22 Overloading the Assignment Operator (=) Classes with pointer member variables –Assignment operator must be explicitly overloaded Function definition to overload assignment operator for class stackType

Data Structures Using C++ 2E23 Stack Header File myStack.h –Header file name containing class stackType definition

Data Structures Using C++ 2E24 Stack Header File (cont’d.) Stack operations analysis –Similar to class arrayListType operations TABLE 7-1 Time complexity of the operations of the class stackType on a stack with n elements

Linked Implementation of Stacks Disadvantage of array (linear) stack representation –Fixed number of elements can be pushed onto stack Solution –Use pointer variables to dynamically allocate, deallocate memory –Use linked list to dynamically organize data Value of stackTop : linear representation –Indicates number of elements in the stack Gives index of the array –Value of stackTop – 1 Points to top item in the stack Data Structures Using C++ 2E25

Data Structures Using C++ 2E26 Linked Implementation of Stacks (cont’d.) Value of stackTop : linked representation –Locates top element in the stack Gives address (memory location) of the top element of the stack instead of the index of the array Review program on page 415 –Class specifying basic operation on a stack as a linked list

Data Structures Using C++ 2E27 Linked Implementation of Stacks (cont’d.) Example 7-2 –Stack: object of type linkedStackType FIGURE 7-10 Empty and nonempty linked stacks

Data Structures Using C++ 2E28 Default Constructor When stack object declared –Initializes stack to an empty state –Sets stackTop to NULL Definition of the default constructor

Data Structures Using C++ 2E29 Empty Stack and Full Stack Stack empty if stackTop is NULL Stack never full –Element memory allocated/deallocated dynamically –Function isFullStack always returns false value

Data Structures Using C++ 2E30 Initialize Stack Reinitializes stack to an empty state Because stack might contain elements and you are using a linked implementation of a stack –Must deallocate memory occupied by the stack elements, set stackTop to NULL Definition of the initializeStack function

Data Structures Using C++ 2E31 Initialize Stack (cont’d.)

Data Structures Using C++ 2E32 Push newElement added at the beginning of the linked list pointed to by stackTop Value of pointer stackTop updated FIGURE 7-11 Stack before the push operation

Data Structures Using C++ 2E33 Push (cont’d.) FIGURE 7-12 Push operation

Data Structures Using C++ 2E34 Push (cont’d.) Definition of the push function

Data Structures Using C++ 2E35 Return the Top Element Returns information of the node to which stackTop pointing Definition of the top function

Data Structures Using C++ 2E36 Pop Removes top element of the stack –Node pointed to by stackTop removed –Value of pointer stackTop updated

Data Structures Using C++ 2E37 Pop (cont’d.) FIGURE 7-14 Pop operation

Data Structures Using C++ 2E38 Pop (cont’d.) Definition of the pop function

Data Structures Using C++ 2E39 Copy Stack Makes an identical copy of a stack Definition similar to the definition of copyList for linked lists Definition of the copyStack function

Data Structures Using C++ 2E40

Data Structures Using C++ 2E41 Constructors and Destructors Definition of the f unctions to implement the copy constructor and the destructor

Data Structures Using C++ 2E42 Overloading the Assignment Operator (=) Definition of the functions to overload the assignment operator

Data Structures Using C++ 2E43 Overloading the Assignment Operator (=) (cont’d.) TABLE 7-2 Time complexity of the operations of the class linkedStackType on a stack with n elements

Data Structures Using C++ 2E44 Stack as Derived from the class unorderedLinkedList Stack push function, list insertFirst function –Similar algorithms –initializeStack and initializeList, isEmptyList and isEmptyStack, etc. –Shows that class linkedStackType is derived from class linkedListType Functions pop and isFullStack implemented for linked stack –class linkedListType : abstract class –class unorderedLinkedListType : derived from class linkedListType

Data Structures Using C++ 2E45 Application of Stacks: Postfix Expressions Calculator Arithmetic notations –Infix notation: operator between operands –Prefix (Polish) notation: operator precedes operands –Postfix (Reverse Polish) notation: operator follows operands Stack use in compilers –Translate infix expressions into some form of postfix notation –Translate postfix expression into machine code

Data Structures Using C++ 2E46 Application of Stacks: Postfix Expressions Calculator (cont’d.) Postfix expression: * = FIGURE 7-15 Evaluating the postfix expression: * =

Data Structures Using C++ 2E47 Application of Stacks: Postfix Expressions Calculator (cont’d.) Main algorithm pseudocode –Broken into four functions for simplicity Function evaluateExpression Function evaluateOpr Function discardExp Function printResult

Data Structures Using C++ 2E48 Removing Recursion: Nonrecursive Algorithm to Print a Linked List Backward Stack –Used to design nonrecursive algorithm Print a linked list backward Use linked implementation of stack FIGURE 7-16 Linked list

Data Structures Using C++ 2E49 FIGURE 7-17 List after the statement current = first ; executes FIGURE 7-18 List and stack after the statements stack.push(current); and current = current- >link; execute

Data Structures Using C++ 2E50 FIGURE 7-19 List and stack after the while statement executes FIGURE 7-20 List and stack after the statements current = stack.top(); and stack.pop(); execute

Data Structures Using C++ 2E51 STL class stack Standard Template Library (STL) library class defining a stack Header file containing class stack definition –stack TABLE 7-3 Operations on a stack object

Data Structures Using C++ 2E52 Summary Stack –Last In First Out (LIFO) data structure –Implemented as array or linked list –Arrays: limited number of elements –Linked lists: allow dynamic element addition Stack use in compliers –Translate infix expressions into some form of postfix notation –Translate postfix expression into machine code Standard Template Library (STL) –Provides a class to implement a stack in a program