Stacks Chapter 3 Objectives Upon completion you will be able to

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

Stack & Queues COP 3502.
§3 The Stack ADT 1. ADT A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are.
Ceng-112 Data Structures I Chapter 5 Queues.
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
E.G.M. Petrakislists, stacks, queues1 Stacks Stack: restricted variant of list –Elements may by inserted or deleted from only one end  LIFO lists –Top:
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Contd... Objectives Explain the design, use, and operation of a linear list Implement a linear.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
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.
E.G.M. Petrakisstacks, queues1 Stacks  Stack: restricted variant of list  elements may by inserted or deleted from only one end : LIFO lists  top: the.
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Stacks.
More About Stacks: Stack Applications Dan Nguyen CS 146, Spring 2004 Professor Sin-Min Lee.
Data Structures: A Pseudocode Approach with C1 Chapter 3 Objectives Upon completion you will be able to Explain the design, use, and operation of a stack.
12 Abstract Data Types Foundations of Computer Science ã Cengage Learning.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
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.
Ceng-112 Data Structures ITurgut Kalfaoglu 1 Chapter 3 Stacks.
CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,
Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: 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,
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.
CSC 202 Analysis and Design of Algorithms Lecture 06: CSC 202 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List, Stack and.
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
Information and Computer Sciences University of Hawaii, Manoa
Data Structures Week 4 Our First Data Structure The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
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 – Week #3 Stacks. 9.Mart.2012Borahan Tümer, Ph.D.2 Outline Stacks Operations on Stacks Array Implementation of Stacks Linked List Implementation.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
CHP-3 STACKS.
April 27, 2017 COSC Data Structures I Review & Final Exam
Stacks & Queues. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy to use.
Data Structures: A Pseudocode Approach with C1 Chapter 3 Objectives Upon completion you will be able to Explain the design, use, and operation of a stack.
CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.
Lecture - 6(Stacks) On Data structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Lecture Outline What is a Stack? Array implementation of stacks Operations.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Objectives Upon completion you will be able to: Explain the design, use, and operation of a linear.
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 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
Data Structures Using C++ 2E
Stacks and Queues Chapter 4.
Chapter 15 Lists Objectives
Stacks Chapter 7 introduces the stack data type.
Objectives In this lesson, you will learn to: Define stacks
Stacks and Queues.
Stacks.
Stacks Stack: restricted variant of list
Data Structures – Week #3
Algorithms and Data Structures
More About Stacks: Stack Applications
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
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.
Stacks and Queues 1.
Stacks Data structure Elements added, removed from one end only
Stacks, Queues, and Deques
More About Stacks: Stack Applications
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
LINEAR DATA STRUCTURES
Presentation transcript:

Stacks Chapter 3 Objectives Upon completion you will be able to Explain the design, use, and operation of a stack Implement a stack using a linked list structure Understand the operation of the stack ADT Write application programs using the stack ADT Discuss reversing data, parsing, postponing and backtracking Data Structures: A Pseudocode Approach with C

Introduction A stack is a Last In, First Out (LIFO) data structure in which all insertions and deletion are restricted to one end called a top When data are inserted into a stack and then they are removed, their order would be reversed Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

3-1 Basic Stack Operations The stack concept is introduced and three basic stack operations are discussed. Push Pop Stack Top Data Structures: A Pseudocode Approach with C

3-1 Basic Stack Operations There are 3 basic stack operations 1. push: adds an item at the top of the stack after the push, the new item becomes the top the stack is in an overflow state if there is no room for the new item Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Basic Stack Operations (2) 2. Pop when a stack is popped, the item at the top of the stack is removed and return it to the user as the top item has been removed, the next older item in the stack becomes the top when the last item in the stack is deleted, it must be set to its empty state if pop is called when the stack is empty, then it is in an underflow state Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Basic Stack Operations (3) 3. Stack Top copies the item at the top of the stack it returns the data in the top element to the user but does not delete it stack top can also result in underflow if the stack is empty Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

An Example Figure 3-5 with an empty stack push green into stack push blue into stack pop push red into stack stack top =? Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

3-2 Stack Linked List Implementation In this section we present a linked-list design for a stack. After developing the data structures, we write pseudocode algorithms for the stack ADT. Data Structure Algorithms Data Structures: A Pseudocode Approach with C

Stack-linked-list Implementation (2) There are several data structures that could be used to implement a stack, e.g. array or linked list In this section, we implement it as a linked list To implement the linked-list stack, we need two different structures a head node a data node Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Stack Head and Stack Data Node stack head requires at least 2 attributes a count of the number of elements in the stack a top pointer other stack attributes can be placed in a stack head such as the time the stack was created stack data node also requires at least 2 attributes data pointer to the next node Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Stack Algorithms 8 operations are defined to solve any basic stack problem create stack - empty stack push stack - full stack pop stack - stack count stack top - destroy stack there may be additional stack operations Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

3-3 C Language Implementations This section presents a simple non-ADT implementation of a stack. We develop a simple program that inserts random characters into the stack and then prints them. Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

(Continued) Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

3-4 Stack ADT We begin the discussion of the stack ADT with a discussion of the stack structure and its application interface. We then develop the required functions. Data Structure ADT Implemenation Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

3-5 Stack Applications Three basic application problems-parsing, postponement, and backtracking-are discussed and sample programs developed. In addition, several other stack applications are presented, including the classic Eight Queens problem. Reversing Data Converting Decimal to Binary Parsing Postponement Backtracking Data Structures: A Pseudocode Approach with C

Reversing Data given a set of data, the first and last elements are exchanged with all of the positions between the first and last being relatively exchanged also for example: {1, 2, 3, 4} becomes {4, 3, 2, 1} we examine 2 different reversing applications: reveres a list convert decimal to binary Data Structures: A Pseudocode Approach with C

Algorithm Reverse a number series Algorithm reverseNumber This program reverses a list of integers read from the keyboard by pushing them into a stack and retrieving them one by one 1  stack = createStack 2  print (Enter a number) 3  read (number) Data Structures: A Pseudocode Approach with C

Algorithm Reverse a number series (2) Fill stack 4  loop ( not end of data AND stack not full) 1 pushStack (number ) 2 prompt (Enter next number: <EOF> to stop) 3 read( number ) Print numbers in reverse 5  loop (not emptyStack (stack) 1 popStack (stack, dataOut) 2 print (dataOut) 6  stack = destroyStack (stack) end reverseNumber Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

(continued) Data Structures: A Pseudocode Approach with C

Convert Decimal to Binary 1 read (number) 2 loop (number > 0) 1 digit = number modulo 2 2 print (digit) 3 number = number / 2 Data Structures: A Pseudocode Approach with C

Algorithm Convert decimal to binary algorithm decimalToBinary This algorithm reads an integer from the keyboard and print its binary equivalent. It uses a stack to reverse the order of 0’s and 1’s produces. 1 stack = createStack 2  prompt (Enter a decimal to convert to binary) 3  read (number) 4  loop (number > 0) 1 digit = number modulo 2 2 pushOK = push (stack,digit) 3 if (pushOK false) 1 print (Stack overflow creating digit) 2 quit algorithm 4 number = number/2 Data Structures: A Pseudocode Approach with C

Algorithm Convert decimal to binary (2) Binary number create in stack. Now print it. 5  loop (not emptyStack(stack)) 1 popStack (stack,digit) 2 print(digit) Binary number create. Destroy stack and return 6 destroy(stack) end decimalToBinary Data Structures: A Pseudocode Approach with C

Parsing any logic that breaks data into independent pieces for further processing for example, to translate a source program to machine language, a compiler must parse the program into individual parts such as keywords, names, and tokens one common programming problem, unmatched parentheses in an algebraic expression Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Postponement When we used a stack to reverse a list, the entire list was read before we began outputting the results Often the logic of an application requires that the usage of data be deferred until some later point A stack can be useful when the application requires that data’s use be postponed for a while two stack postponement applications: infix to postfix translation postfix expression evaluation Data Structures: A Pseudocode Approach with C

Infix To Postfix Transformation An arithmetic expression can be represented in 3 different formats infix: the operator comes between the 2 operands postfix: the operator comes after the 2 operands prefix: the operator comes before the 2 operands Infix: a + b Postfix: a b + Prefix: + a b Data Structures: A Pseudocode Approach with C

Transformation Using Stack Can we use stack to handle infix to postfix transformation? What happen if we push operators into a stack and after the whole infix expression has been read, we pop the stack? A * B = A B * Data Structures: A Pseudocode Approach with C

Transformation Using Stack (2) Priority 2: * / Priority 1: + - Priority 0: ( Data Structures: A Pseudocode Approach with C

Example 3 Given the expression: A + B * C Using the following rules copy operand A to output expression push operator + into stack copy operand B t output expression push operator * into stack (*’s priority is higher than +’s) copy operand C to output expression pop operator * and copy to output expression pop operator + and copy to output expression A result is: A B C * + Data Structures: A Pseudocode Approach with C

Example 4 Given the expression: A + B * C – D / E The result is Data Structures: A Pseudocode Approach with C

Figure 3-12, Part I: Infix transformations Data Structures: A Pseudocode Approach with C

รูปที่ 3-12, ส่วนที่ 2 : การแปลงนิพจน์ infix ให้เป็น postfix Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Example 5 Given the expression: A * B – (C + D) + E Data Structures: A Pseudocode Approach with C

Table 3-1 Example: convert infix to postfix Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Evaluating Postfix Expressions Given the expression A B C + * assuming that A = 2 B= 4 C = 6 Data Structures: A Pseudocode Approach with C

Figure 3-13: Evaluation of postfix expression Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Backtracking found in applications such as computer gaming, decision analysis, and expert systems in this section, we examine a backtracking application called goal seeking Data Structures: A Pseudocode Approach with C

Goal Seeking one way to portray the problem is to layout the steps in the form of a graph that contains several alternate paths in it only one of the pats in the figure leads us to a desired goal while we can immediately see the correct path when we look at the figure, the computer needs an algorithm to determine the correct path Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Goal Seeking (2) what do we put into the stack? if all we wanted to do was to located the node that contains the goal, we would just put the branch point nodes into the stack as we want to print out the path that leads us to our goal, we must put the nodes in the valid path into the stack Data Structures: A Pseudocode Approach with C

Figure 3-15: Backtracking stack operation Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

(continued) Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

3-6 How Recursion Works This section discusses the concept of the stack frame and the use of stacks in writing recursive software Enqueue Dequeue Queue Front Queue Rear Queue Example Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

3-7 Array Implementation of Stacks if we know a stack’s maximum size, an array implementation of a stack is more efficient than linked list implementation with array implementation, the base is index 0 (in C) the top moves up and down as data are inserted or deleted to push an element, we add one to top and use it as the array index to pop, we copy at index location top and then subtract one from top Data Structures: A Pseudocode Approach with C

Figure 3-20: Stack array implementation Data Structures: A Pseudocode Approach with C

Algorithm Stack array definition Stack Definitions for Array Implementation stack stackAry <pointer to array of dataType> count <integer> stackMax <integer> top <index> end stack Data Structures: A Pseudocode Approach with C

Algorithm Create stack array algorithm createStack (stackElem <integer>) Allocates memory for a stack head node from dynamic memory and returns its address to the caller. Pre stackElem contains size of stack Post Head node and array allocated or error returned Return pointer to head node or null pointer if no memory 1 if (memory not available) 1 stackPtr = null 2 else 1 allocate (stackPtr) Data Structures: A Pseudocode Approach with C

Algorithm Create stack array (2) Head allocated. Now initialize and allocate stack array. 2 stackPtr -> count = 0 3 stackPtr -> top = -1 4 stackPtr -> stackMax = stackElem 5 if (memory not available) 1 recycle (stackPtr) 2 stackPtr = null 6 else Allocate memory for stack array 1 allocate (stackPtr -> stackAry) 3 return stackPtr end createStack Data Structures: A Pseudocode Approach with C

Algorithm Push stack array algorithm pushStack (val stack <head pointer>, val data <dataType>) Insert (push) one item into the stack. Pre stack is a pointer to the stack head structure data contains data to be pushed into stack Post data have been pushed in stack Return true if successful; false if memory overflow Data Structures: A Pseudocode Approach with C

Algorithm Push stack array (2) 1 if (stack -> count is at maximum) 1 success = false 2 else 1 stack -> count = stack -> count + 1 2 stack -> top = stack -> top + 1 3 stack -> stackAry[stack -> top] = data 4 success = true 3 return success end pushStack Data Structures: A Pseudocode Approach with C

Algorithm Pop stack algorithm popStack (val stack <head point>, ref dataOut <dataType>) This algorithm pops the item on the top of the stack and returns it to the user. Pre stack is a pointer to the stack head structure dataOut is a reference variable to receive the data Post Data have been returned to calling algorithm Return true if successful; false if underflow Data Structures: A Pseudocode Approach with C

Algorithm 3-19 Pop stack (2) 1 if (stack empty) 1 success = false 2 else 1 dataOut = stack -> stackAry [stack -> top] 2 stack -> top = stack -> top – 1 3 stack -> count = stack -> count – 1 4 success = true 3 return success end popStack Data Structures: A Pseudocode Approach with C

Algorithm Stack top algorithm stackTop (val stack <head pointer>, ref dataOut <dataType>) This algorithm retrieves the data from the top of the stack without changing the stack. Pre stack is a pointer to the stack head structure Post data have been returned to calling algorithm Return true if data returned, false if underflow Data Structures: A Pseudocode Approach with C

Algorithm Stack top (2) 1 if (stack -> count zero) 1 success = false 2 else 1 dataOut = stack -> stackAry [stack -> top].data 2 success = true 3 return success end stackTop Data Structures: A Pseudocode Approach with C

Algorithm Empty stack algorithm emptyStack (val stack <head pointer>) Determines if stack is empty and return a Boolean. Pre stack is a pointer to the stack head structure Post returns stack status Return Boolean, true: stack empty, false: stack contains data 1 if (stack -> count > 0) 1 result = false 2 else 1 result = ture 3 return result end emptyStack Data Structures: A Pseudocode Approach with C

Algorithm Full stack algorithm fullStack (val stack <head pointer>) Determines if stack is full and return a Boolean. Pre stack is a pointer to the stack head structure Post returns stack status Return Boolean, true: stack full, false: memory available 1 if (stack ->count < MAX_STACK) 1 result = false 2 else 1 result = true 3 return result end fullStack Data Structures: A Pseudocode Approach with C

Algorithm Stack count algorithm stackCount (val stack <head pointer>) Return the number of elements currently in stack. Pre stack is a pointer to the stack head structure Post returns stack count Return integer count of number of elements in stack 1 return (stack->count) end stackCount Data Structures: A Pseudocode Approach with C

Algorithm 3-24 Destroy stack algorithm destroyStack (val stack <typeStack>) This algorithm releases all nodes back to the dynamic memory. Pre stack is a pointer to the stack head structure Post head structure and array recycled Return null pointer 1 if (stack not empty) 1 recycle (stack->stackAry) 2 recycle (stack) 2 return null pointer end destroyStack Data Structures: A Pseudocode Approach with C