1 / 34

Computer Science 1 (Studio) 08A-Stacks

Learn what a stack is, how it works, and its operations like push, pop, top, size, empty, and full. Understand how to implement a stack using an array.

gregorylee
Download Presentation

Computer Science 1 (Studio) 08A-Stacks

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Science 1 (Studio) 08A-Stacks CS1s - 08A-Stacks (v1.00)

  2. What is a Stack? • A stack is a data structure whose behavior is First In Last Out / Last In First Out • A stack only has a top (no bottom) • When an element is added to the stack, it is pushed onto the top • Interior elements shift down (away from the top) • When an element is removed from the stack, it is popped from the top • Interior elements shift up (towards the top) CS1s - 08A-Stacks (v1.00)

  3. What is a Stack? • The stack in action H H F G F E G E D D B C B C A G top top top top A G F F E E D D C C B B A A CS1s - 08A-Stacks (v1.00)

  4. Examples of Stacks • Examples of real world stacks • A pez dispenser • A can of Pringles • A spindle of blank cd’s • A single lane driveway of cars • Examples of stacks in programming • A stack of geometry matrices in graphics programming • The PostScript language uses stacks for operands, graphics and dictionaries • Parameters are passed to methods on a stack • Elements can be quickly reversed using a stack CS1s - 08A-Stacks (v1.00)

  5. Stack Operations - Push • Elements are pushed onto the stack, one at a time push(‘A’) push(‘B’) push(‘C’) A top B A top C B A top CS1s - 08A-Stacks (v1.00)

  6. Stack Operations - Pop • When an element is popped off the stack, the top element is removed and returned ‘C’ <- pop() ‘B’ <- pop() C B A top B A top A top CS1s - 08A-Stacks (v1.00)

  7. Stack Operations - Top • The top element of the stack can be peeked at by using top • The stack is left unchanged ‘C’ <- top() ‘C’ <- top() C B A top C B A top CS1s - 08A-Stacks (v1.00)

  8. Stack Operations – Size / Elements • We can ask the stack what its size is • What’s the maximum number of elements that can be stored? 9 <- getSize() • We can also ask the stack how many elements it currently has 3 <- getNumberOfElements() C B A top C B A top CS1s - 08A-Stacks (v1.00)

  9. Stack Operations – Empty/ Full • We can ask the stack if it is currently empty false<- isEmpty() We can also ask the stack if it is currently full true<- isFull() C B A top I H G F E D C B A top CS1s - 08A-Stacks (v1.00)

  10. Understanding How to Implement a Stack • We are going to implement our stack using an array as the underlying collection • We will first write our stack to work with integers • Consider a stack whose maximum size is 5 0 1 2 3 4 size = 5 # elements = 0 CS1s - 08A-Stacks (v1.00)

  11. Understanding How to Implement a Stack • How do we tell the size of the stack? • How do we tell how many elements are in the stack? • How do we tell if the stack is empty? • How do we tell if the stack is full? 0 1 2 3 4 size = 5 # elements = 0 CS1s - 08A-Stacks (v1.00)

  12. Understanding How to Implement a Stack • We only need one index, front, to track the front of the stack • Since there are no elements in the stack to begin with, the initial value of front is –1 0 1 2 3 4 size = 5 # elements = 0 front = -1 CS1s - 08A-Stacks (v1.00)

  13. Understanding How to Implement a Stack • Whenever an element is pushed onto the stack, the front index is first incremented • The element is then added to the array at the front index Push: 10 0 1 2 3 4 10 size = 5 # elements = 1 front = 0 CS1s - 08A-Stacks (v1.00)

  14. Understanding How to Implement a Stack Push: 20 0 1 2 3 4 20 10 size = 5 # elements = 2 front = 1 Push: 30 0 1 2 3 4 10 20 30 size = 5 # elements = 3 front = 2 CS1s - 08A-Stacks (v1.00)

  15. Understanding How to Implement a Stack Push: 40 0 1 2 3 4 40 10 20 30 size = 5 # elements = 4 front = 3 Push: 50 0 1 2 3 4 40 10 20 30 50 size = 5 # elements = 5 front = 4 CS1s - 08A-Stacks (v1.00)

  16. Understanding How to Implement a Stack • What should happen when an element is pushed onto a full stack? • Write Answer: Push: 60 0 1 2 3 4 40 10 20 30 50 size = 5 # elements = 5 front = 4 CS1s - 08A-Stacks (v1.00)

  17. Understanding How to Implement a Stack • To get the top element on the stack, return the value at the front index of the array 50 <- Top 0 1 2 3 4 40 10 20 30 50 size = 5 # elements = 5 front = 4 CS1s - 08A-Stacks (v1.00)

  18. Understanding How to Implement a Stack • What should happen if the top element is requested from an empty stack? • Write Answer: ??? <- Top 0 1 2 3 4 size = 5 # elements = 0 front = -1 CS1s - 08A-Stacks (v1.00)

  19. Understanding How to Implement a Stack • To pop an element off the stack, return the element at the front index • Then decrement the front index 50 <- Pop 0 1 2 3 4 40 10 20 30 50 size = 5 # elements = 4 front = 3 (after pop) CS1s - 08A-Stacks (v1.00)

  20. Understanding How to Implement a Stack 40 <- Pop 0 1 2 3 4 40 10 20 30 50 size = 5 # elements = 3 front = 2 30 <-Pop 0 1 2 3 4 40 10 20 30 50 size = 5 # elements = 2 front = 1 CS1s - 08A-Stacks (v1.00)

  21. Understanding How to Implement a Stack 20 <-Pop 0 1 2 3 4 40 10 20 30 50 size = 5 # elements = 1 front = 0 10 <-Pop 0 1 2 3 4 40 10 20 30 50 size = 5 # elements = 0 front = -1 CS1s - 08A-Stacks (v1.00)

  22. Understanding How to Implement a Stack • What should happen if we pop the stack when it is empty? • Write Answer: ??? <-Pop 0 1 2 3 4 40 10 20 30 50 size = 5 # elements = 0 front = -1 CS1s - 08A-Stacks (v1.00)

  23. Testing Your Understanding • Show what happens with the following operations on an integer stack of size 5 push (10) pop() push (20) pop() top() top() push (30) push (80) isFull() pop() push (40) isEmpty() push (50) pop() isFull() top() push (60) isFull() pop() push (90) top() push (100) push (70) pop() CS1s - 08A-Stacks (v1.00)

  24. Implementing a Stack • Look at the Javadoc for IntStack • http://www.cs.rit.edu/~cs1s/week8/IntStack.html • The size of the stack will be based on how IntStack is constructed // If no size is specified, the default size is 25 IntStack s1 = new IntStack(); // Here the size is 10 IntStack s2 = new IntStack(10); CS1s - 08A-Stacks (v1.00)

  25. Implementing a Stack • In class activity • Write the skeleton for IntStack.java and make sure it compiles • Write the two constructors • You will have to create several private data members • A reference to the array of integers which represent the stack • The size of the stack • The number of elements in the stack • The index to the front of the stack (initially –1) • If you have difficulty, look back at your IntQueue to see how you did things – it is very similar CS1s - 08A-Stacks (v1.00)

  26. Implementing a Stack public class IntStack { // private integer array reference // private front index // private number of elements public IntStack() { // construct the stack by calling the other // constructor with the default size of 25 } public IntStack(int size) { // create array of integers and assign to reference // set the front index to –1 // set the number of elements in the stack to 0 } … } // IntStack CS1s - 08A-Stacks (v1.00)

  27. Implementing a Stack • In class activity: • Write the accessors for IntStack • Implement getNumberOfElements() • Implement getSize() • Implement isEmpty() • Implement isFull() • Once this compiles cleanly, make a test program, TestStack.java, which tests whether each method works correctly CS1s - 08A-Stacks (v1.00)

  28. Implementing a Stack public int getNumberOfElements() { // return the number of elements } public int getSize() { // return the size of the stack } public boolean isFull() { // return a value which tests: // does the number of elements equal the size? } public boolean isEmpty() { // return a value which tests: // does the number of elements equal 0? } CS1s - 08A-Stacks (v1.00)

  29. Implementing a Stack • In class activity • Now write the implementation for push • Remember that when pushing an element onto a stack • The front index is incremented first • The element is added at the front index • If the stack is full, the element should not be added • Next, write the implementation for top so you can verify your stack • Return the element at the front index • If the stack is empty, the value 0 should be returned • Write some test cases in TestStack which verifies the correctness of your push and top methods CS1s - 08A-Stacks (v1.00)

  30. Implementing a Stack public void push(int newItem) { // if the stack is not full: // increment the front index // add newItem into the array at the front index // increment the number of elements in the stack by 1 } public int top() { // create a temporary integer and set it to 0 // if the stack is not empty: // set the temp to the value at array’s front index // return the temp } CS1s - 08A-Stacks (v1.00)

  31. Implementng a Stack • In class activity • Now write the implementation for pop • Remember when removing an element from the stack • Get the element before decrementing the index • If the stack is empty, return 0 • Write a test case in IntStack.java which verifies the correct functionality of pop CS1s - 08A-Stacks (v1.00)

  32. Implementing a Stack public int pop() { // set a temporary integer to 0 // if the stack is not empty: // set the temp to the value at front index in array // decrement the front index // decrement the number of elements in the stack by 1 // return the temp } CS1s - 08A-Stacks (v1.00)

  33. Reverse.java • In class activity • Write a program, Reverse.java, which reverses a series of integers that are input by the user • The client must use IntStack and cannot create a local array How many elements? 5 Enter number: 1 Enter number: 2 Enter number: 3 Enter number: 4 Enter number: 5 The elements in reverse: 5 4 3 2 1 CS1s - 08A-Stacks (v1.00)

  34. Revision History • Revision History • v1.00, 10/28/2004 10:19 AM, sps Initial revision. -- v2.00, 1/24/2005, chr Minor corrections CS1s - 08A-Stacks (v1.00)

More Related