DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.

Slides:



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

Lecture 5 Stack Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
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.
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.
Topic 15 Implementing and Using Stacks
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.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
Topic 15 Implementing and Using Stacks
Chapter 18: Stacks and Queues
Chapter 18: Stacks and Queues
Stack  A stack is a linear data structure or abstract data type for collection of items, with the restriction that items can be added one at a time and.
Chapter 17: Stacks and Queues
Objectives of these slides:
Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
Topic 3 The Stack ADT.
Ceng-112 Data Structures ITurgut Kalfaoglu 1 Chapter 3 Stacks.
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.
1 Stacks – Chapter 3 A stack is a data structure in which all insertions and deletions of entries are made at one end, called the top of the stack. Alternatively,
Chapter 7 Stacks Dr. Youssef Harrath
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.
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.
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
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.
CHAPTER 61 STACK, QUEUES, RECURSION. Introduction when one wants to restrict insertion and deletion so that they can take place only at the beginning.
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.
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.
CIS 068 Welcome to CIS 068 ! Lesson 11: Data Structures 2.
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.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
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.
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.
CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.
Stacks Chapter 3 Objectives Upon completion you will be able to
Lecture - 6(Stacks) On Data structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Lecture Outline What is a Stack? Array implementation of stacks Operations.
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.
Data Structures Using C++ 2E
Stacks and Queues Chapter 4.
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
Stacks.
Data Structures Array Based Stacks.
STACKS.
Algorithms and Data Structures
Stack.
More About Stacks: Stack Applications
STACK, QUEUES, RECURSION
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
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.
Stack.
More About Stacks: Stack Applications
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
Presentation transcript:

DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS

2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack applications

3 Stacks Stack: list of homogenous elements –Addition and deletion occur only at one end, called the top of the stack Example: in a cafeteria, the second tray can be removed only if first tray has been removed –Last in first out (LIFO) data structure Operations: –Push: to add an element onto the stack –Pop: to remove an element from the stack

4 Stacks (continued)

6 Stack Operations In the abstract class stackADT : –initializeStack –isEmptyStack –isFullStack –push –top –pop

Stack Application Stack applications classified into 4 broad categories: Reversing data – e.g. reverse a list & convert decimal to binary. –Eg. 26 = Parsing data – e.g. translate a source program to machine language. Postponing data usage – e.g. evaluation, transformation. Backtracking – e.g. computer gaming, decision analysis, expert systems.

8 Implementation of Stacks as Arrays First element can go in first array position, the second in the second position, etc. The top of the stack is the index of the last element added to the stack Stack elements are stored in an array Stack element is accessed only through top To keep track of the top position, use a variable called stackTop

9 Implementation of Stacks as Arrays Because stack is homogeneous –You can use an array to implement a stack Can dynamically allocate array –Enables user to specify size of the array The class stackType implements the functions of the abstract class stackADT

10 Implementation of Stacks as Arrays C++ arrays begin with the index 0 –Must distinguish between: The value of stackTop The array position indicated by stackTop If stackTop is 0, the stack is empty If stackTop is nonzero, the stack is not empty –The top element is given by stackTop - 1

11 Implementation of Stacks as Arrays

12 Initialize Stack

Empty Stack If stackTop is 0, the stack is empty

14 Full Stack The stack is full if stackTop is equal to maxStackSize

15 Push Store the newItem in the array component indicated by stackTop Increment stackTop Must avoid an overflow

16 Push

17 Return the Top Element

18 Pop Simply decrement stackTop by 1 Must check for underflow condition

Stacks Example Example Suppose the following 6 elements are pushed, in order, onto an empty stacks. A, B, C, D, E, F We write the stack: STACK: A, B, C, D, E, F

Pseudocode for Array of Stacks Procedure 3.1 PUSH(STACK, TOP, MAXSTK, ITEM) This procedure pushes an ITEM onto a stack. 1. [Stack already filled ?] If TOP = MAXSTK, then: Print:OVERFLOW, and Return. 2. Set TOP := TOP + 1. [Increses TOP by 1] 3. Set STACK[TOP] :=ITEM. [Inserts ITEM in new TOP position. 4. Return.

Pseudocode for Array of Stacks Procedure 3.2 POP(STACK, TOP, ITEM) This procedures deletes the top element of STACK and assigns it to the variable ITEM. 1. [Stacks has an item to be removed?] If TOP = 0, the Print: UNDERFLOW, and RETURN. 2. Set ITEM := STACK[TOP].[Assigns TOP elements to ITEM.] 3. Set TOP := TOP – 1.[ Decreases TOP by 1] 4. Return.

Stack Exercise Exercise 1: Consider the following stack of characters, where STACK is allocated N = 8 memory cells : STACK : A,C,D, F, K, _, _, _ ( For notational convenience, we use “_” to denote an empty memory cell). Describe the stack as the following operations take place : (a)POP (STACK, ITEM )(e) POP (STACK, ITEM) (b)POP (STACK,ITEM) (f) PUSH(STACK, R) (c)PUSH (STACK, L)(g) PUSH(STACK, S) (d) PUSH (STACK, P)(h) POP(STACK, ITEM)

Stack Exercise Exercise 2: Consider the following stack, where STACK is allocated N = 6 memory cells : STACK : A, D, E, F, G, _______. (a) PUSH(STACK, K) (b) POP(STACK, ITEM ) (c) PUSH(STACK, L) (d) PUSH(STACK,S) (e) POP(STACK, ITEM) (f) PUSH(STACK, M)

25 Copy Stack

26 Stack Header File Place definitions of class and functions (stack operations) together in a file

29 Linked Implementation of Stacks Array only allows fixed number of elements If number of elements to be pushed exceeds array size –Program may terminate Linked lists can dynamically organize data In a linked representation, stackTop is pointer to top element in stack

31 Default Constructor Initializes the stack to an empty state when a stack object is declared –Sets stackTop to NULL

32 Empty Stack and Full Stack In the linked implementation of stacks, the function isFullStack does not apply –Logically, the stack is never full

33 Initialize Stack

34 Push The newElement is added at the beginning of the linked list pointed to by stackTop

35 Push (continued)

36 Push (continued)

37 Push (continued)

38 Push (continued) We do not need to check whether the stack is full before we push an element onto the stack

C++ Programming: Program Design Including Data Structures, Fourth Edition 39 Return the Top Element

40. Pop Node pointed to by stackTop is removed

C++ Programming: Program Design Including Data Structures, Fourth Edition 42 Pop (continued)

43 Copy Stack

44 Copy Stack Notice that this function is similar to the definition of copyList for linked lists

45 Application of Stacks: Postfix Expressions Calculator Infix notation: usual notation for writing arithmetic expressions –The operator is written between the operands –Example: a + b –The operators have precedence Parentheses can be used to override precedence

46 Application of Stacks: Postfix Expressions Calculator Prefix (Polish) notation: the operators are written before the operands –Introduced by the Polish mathematician Jan Lukasiewicz Early 1920s –The parentheses can be omitted –Example: + a b

47 Application of Stacks: Postfix Expressions Calculator Reverse Polish notation: the operators follow the operands (postfix operators) –Proposed by the Australian philosopher and early computer scientist Charles L. Hamblin Late 1950's –Advantage: the operators appear in the order required for computation –Example: a + b * c In a postfix expression: a b c * +

48 Application of Stacks: Postfix Expressions Calculator

49 Application of Stacks: Postfix Expressions Calculator Postfix notation has important applications in computer science –Many compilers first translate arithmetic expressions into postfix notation and then translate this expression into machine code Evaluation algorithm: –Scan expression from left to right –When an operator is found, back up to get the operands, perform the operation, and continue

50 Application of Stacks: Postfix Expressions Calculator Example: * = –Read first symbol 6 is a number  push it onto the stack –Read next symbol 3 is a number  push it onto the stack

51 Application of Stacks: Postfix Expressions Calculator Example: * = –Read next symbol + is an operator (two operands)  pop stack twice, perform operation, put result back onto stack

52 Example: * = –Read next symbol 2 is a number  push it onto the stack Application of Stacks: Postfix Expressions Calculator

53 Example: * = –Read next symbol * is an operator  pop stack twice, perform operation, put result back onto stack Application of Stacks: Postfix Expressions Calculator

54 Example: * = –Read next symbol = is an operator  indicates end of expression Print the result (pop stack first) Application of Stacks: Postfix Expressions Calculator

55 Application of Stacks: Postfix Expressions Calculator Symbols can be numbers or anything else: –+, -, *, and / are operators Pop stack twice and evaluate expression If stack has less than two elements  error –If symbol is =, the expression ends Pop and print answer from stack If stack has more than one element  error –If symbol is anything else Expression contains an illegal operator

56 Application of Stacks: Postfix Expressions Calculator Examples: ; 6 - = ; is an illegal operator * = Does not have enough operands for = Error: stack will have two elements when we encounter equal ( = ) sign

57 Application of Stacks: Postfix Expressions Calculator We assume that the postfix expressions are in the following form: #6 #3 + #2 * = –If symbol scanned is #, next input is a number –If the symbol scanned is not #, then it is: An operator (may be illegal) or An equal sign (end of expression) We assume expressions contain only +, -, *, and / operators

58 Conclusions Stack: items are added/deleted from one end –Last In First Out (LIFO) data structure –Operations: push, pop, initialize, destroy, check for empty/full stack –Can be implemented as array or linked list –Middle elements should not be accessed Postfix notation: operators are written after the operands (no parentheses needed)

59 References C++ Programming: Program Design Including Data Structures, Fourth Edition