Data Structures Using Java1 Chapter 6 Stacks. Data Structures Using Java2 Chapter Objectives Learn about stacks Examine various stack operations Learn.

Slides:



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

Stacks, Queues, and Linked Lists
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.
CS 240Chapter 6 - StacksPage 21 Chapter 6 Stacks The stack abstract data type is essentially a list using the LIFO (last-in-first-out) policy for adding.
Chapter 6: Stacks STACK APPLICATIONS STACK IMPLEMENTATIONS CS
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.
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 18: Stacks and Queues
Chapter 18: Stacks and Queues
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
Chapter 17: Stacks and Queues
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.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
What is a Stack? n Logical (or ADT) level: A stack is an ordered group of homogeneous items in which the removal and addition of items can take place only.
Stack Applications.
Chapter 7 Stacks Dr. Youssef Harrath
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
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.
Mastering STACKS AN INTRODUCTION TO STACKS Data Structures.
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.
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.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Chapter 18: Stacks and Queues
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Data Structures Using Java1 Chapter 7 Queues. Data Structures Using Java2 Chapter Objectives Learn about queues Examine various queue operations Learn.
Data Structures Using C++
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
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.
CHAPTER 17 LINKED LISTS. In this chapter, you will:  Learn about linked lists  Become aware of the basic properties of linked lists  Explore the insertion.
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.
Chapter 17: Stacks and Queues. Objectives In this chapter, you will: – Learn about stacks – Examine various stack operations – Learn how to implement.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
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.
Chapter 17: Stacks and Queues Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
CSCE 3110 Data Structures & Algorithm Analysis
Chapter 4 Stacks
Sections 3.4 Formal Specification
Data Structures Using C++ 2E
Stacks.
Stacks and Queues Chapter 4.
Data Structures Array Based Stacks.
Algorithms and Data Structures
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Data Structures Array Based Stacks.
Stacks Data structure Elements added, removed from one end only
Stacks CS-240 Dick Steflik.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Stack.
Presentation transcript:

Data Structures Using Java1 Chapter 6 Stacks

Data Structures Using Java2 Chapter 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 to use a stack to remove recursion

Data Structures Using Java3 Stacks Definition: –list of homogeneous elements –addition and deletion of elements occurs only at one end, called the top of the stack Last In First Out (LIFO) data structure Used to implement method calls Used to convert recursive algorithms (especially not tail recursive) into nonrecursive algorithms

Data Structures Using Java4 Various Types of Stacks

Data Structures Using Java5 LIFO Last In First Out (LIFO) data structure –Top element of stack is last element to be added to stack –Elements added and removed from one end (top) –Item added last are removed first

Data Structures Using Java6 Empty Stack

Data Structures Using Java7 Stack Operations

Data Structures Using Java8 Basic Operations on a Stack initializeStack: Initializes the stack to an empty state isEmptyStack: Checks whether the stack is empty. If empty, it returns true; otherwise, it returns false isFullStack: Checks whether the stack is full. If full, it returns true; otherwise, it returns false

Data Structures Using Java9 Basic Operations on a Stack push: –Add new element to the top of the stack –The input consists of the stack and the new element –Prior to this operation, the stack must exist and must not be full

Data Structures Using Java10 Basic Operations on a Stack top: Returns the top element of the stack. Prior to this operation, the stack must exist and must not be empty pop: Removes the top element of the stack. Prior to this operation, the stack must exist and must not be empty

Data Structures Using Java11 Example of a Stack

Data Structures Using Java12 Empty Stack

Data Structures Using Java13 initializeStack public void initializeStack() { for(int i = 0; i < stackTop; i++) list[i] = null; stackTop = 0; }//end initializeStack

Data Structures Using Java14 emptyStack and fullStack public boolean isEmptyStack() { return(stackTop == 0); }//end isEmptyStack public boolean isFullStack() { return(stackTop == maxStackSize); }//end isFullStack

Data Structures Using Java15 Push

Data Structures Using Java16 Push public void push(DataElement newItem) throws StackOverflowException { if(isFullStack()) throw new StackOverflowException(); list[stackTop] = newItem.getCopy(); //add newItem at the //top of the stack stackTop++; //increment stackTop }//end push

Data Structures Using Java17 Return Top Element public DataElement top() throws StackUnderflowException { if(isEmptyStack()) throw new StackUnderflowException(); DataElement temp = list[stackTop - 1].getCopy(); return temp; }//end top

Data Structures Using Java18 Pop public void pop() throws StackUnderflowException { if(isEmptyStack()) throw new StackUnderflowException(); stackTop--; //decrement stackTop list[stackTop] = null; }//end pop

Data Structures Using Java19 Pop

Data Structures Using Java20 copy private void copy(StackClass otherStack) { list = null; System.gc(); maxStackSize = otherStack.maxStackSize; stackTop = otherStack.stackTop; list = new DataElement[maxStackSize]; //copy otherStack into this stack for(int i = 0; i < stackTop; i++) list[i] = otherStack.list[i].getCopy(); }//end copy 6

Data Structures Using Java21 Constructors //constructor with a parameter public StackClass(int stackSize) { if(stackSize <= 0) { System.err.println(“The size of the array to implement “ + “the stack must be positive.”); System.err.println(“Creating an array of size 100.”); maxStackSize = 100; } else maxStackSize = stackSize; //set the stack size to //the value specified by //the parameter stackSize stackTop = 0; //set stackTop to 0 list = new DataElement[maxStackSize]; //create the array }//end constructor

Data Structures Using Java22 Constructors //default constructor public StackClass() { maxStackSize = 100; stackTop = 0; //set stackTop to 0 list = new DataElement[maxStackSize]; //create array }//end default constructor

Data Structures Using Java23 Copy Constructor and copyStack public StackClass(StackClass otherStack) { copy(otherStack); }//end copy constructor public void copyStack(StackClass otherStack) { if(this != otherStack) //avoid self-copy copy(otherStack); }//end copyStack

Data Structures Using Java24 Time Complexity of Operations of class stackType

Data Structures Using Java25 Programming Example: Highest GPA InputThe program reads an input file consisting of each student’s GPA, followed by the student’s name. Sample data is: 3.8 Lisa 3.6 John 3.9 Susan 3.7 Kathy 3.4 Jason 3.9 David 3.4 Jack

Data Structures Using Java26 Programming Example: Highest GPA (Algorithm) 1.Declare the variables 2.Create a DecimalFormat object to output a decimal number to two decimal places 3.Open the input file 4.If the input file does not exist, exit the program 5.Read the next input line

Data Structures Using Java27 Highest GPA (Algorithm) 6.while (not end of file) { 6.a. Tokenize the input line 6.b. Get the next GPA 6.c. Get the next name 6.d. if (GPA > highestGPA) { 6.d.i initialize stack 6.d.ii push(stack, student name) 6.d.iii highestGPA = GPA } 6.e. else if(GPA is equal to highestGPA) push(stack, student name) 6.f Read the next input line }

Data Structures Using Java28 Programming Example: Highest GPA (Algorithm) 7.Output the highest GPA. 8.Output the names of the students having the highest GPA.

Data Structures Using Java29 Programming Example: Highest GPA (Sample Run) Input File (Ch6_HighestGPAData.txt) 3.4 Holt 3.2 Bolt 2.5 Colt 3.4 Tom 3.8 Ron 3.8 Mickey 3.6 Pluto 3.5 Donald 3.8 Cindy 3.7 Dome 3.9 Andy 3.8 Fox 3.9 Minnie 2.7 Goofy 3.9 Doc 3.4 Danny

Data Structures Using Java30 Programming Example: Highest GPA (Sample Run) Output Highest GPA = 3.90 The students holding the highest GPA are: Doc Minnie Andy

Data Structures Using Java31 Empty and Nonempty Linked Stack Empty linked stackNonempty linked stack

Data Structures Using Java32 Default Constructor public LinkedStackClass() { stackTop = null; }

Data Structures Using Java33 initializeStack, isStackEmpty, and isStackFull public void initializeStack() { stackTop = null; }//end initializeStack public boolean isEmptyStack() { return(stackTop == null); } public boolean isFullStack() { return false; }

Data Structures Using Java34 Push Stack before the push operation Stack and newNode

Data Structures Using Java35 Push Stack after the statement newNode.link = stackTop; executes Stack after the statement stackTop = newNode; executes

Data Structures Using Java36 Return Top Element public DataElement top() throws StackUnderflowException { if(stackTop == null) throw new StackUnderflowException(); return stackTop.info.getCopy(); }//end top

Data Structures Using Java37 Pop Stack before the pop operation Stack after the statement stackTop = stackTop.link; executes Stack after popping the top element

Data Structures Using Java38 Application of Stacks: Postfix Expression Calculator Prefix/Polish Notation Suffix/Postfix/Reverse Polish Notation

Data Structures Using Java39 Application of Stacks: Postfix Expression Calculator

Data Structures Using Java40 Application of Stacks: Postfix Expression Calculator Stack after pushing 6 Stack after pushing 3 Stack after retrieving the top two elements and popping twice Stack after pushing the result of op1 + op2, which is 9

Data Structures Using Java41 Application of Stacks: Postfix Expression Calculator Stack after pushing 2 Stack after retrieving the top two elements and popping twice Stack after pushing the result of op1 * op2, which is 18 Stack after popping the element

Data Structures Using Java42 Postfix Expression Calculator (Main Algorithm) Get the next expression while more data to process { a. initialize the stack b. process the expression c. output the result d. get the next expression }

Data Structures Using Java43 Nonrecursive Algorithm to Print Linked List current = first; //Line 1 while(current != NULL) //Line 2 { stack.push(current); //Line 3 current = current.link; //Line 4 }

Data Structures Using Java44 List After Execution of Statement current = first;

Data Structures Using Java45 Repeated Execution of: stack.push(current); current = current.link;

Data Structures Using Java46 Java class Stack Java provides a class to implement a stack in a program The name of the Java class defining a stack is Stack The class Stack is contained in the package java.util Table 6-3 lists the members of the class Stack

Data Structures Using Java47 Java class Stack

Data Structures Using Java48 Chapter Summary Stack Data Structure Last In First Out (LIFO) Stacks Implemented as Arrays Stacks Implemented as Linked Lists Postfix Expression Calculator Nonrecursive Algorithm to Print Linked List Java class Stack