Download presentation
Presentation is loading. Please wait.
1
Implementing Stacks Using Arrays CSC 1401: Introduction to Programming with Java Week 14 – Lecture 1 Wanda M. Kunkle
2
2 What is a stack? A stack is a collection of related data items based on the principle of last-in, first-out (LIFO): A stack is a collection of related data items based on the principle of last-in, first-out (LIFO): The last item placed on the stack is the first item removed from the stack. The last item placed on the stack is the first item removed from the stack. A stack is analogous to a pile of trays in a cafeteria: A stack is analogous to a pile of trays in a cafeteria: A person taking a tray removes the tray from the top of the pile. A person taking a tray removes the tray from the top of the pile. A person returning a tray places the tray on the top of the pile. A person returning a tray places the tray on the top of the pile.
3
3 Manipulating Stacks A stack differs from a pile of trays, however, in that a stack only allows us to see and access the item on the top; to access items farther down in the stack we must first remove those above it. A stack differs from a pile of trays, however, in that a stack only allows us to see and access the item on the top; to access items farther down in the stack we must first remove those above it. The two primary operations for manipulating items on a stack are: The two primary operations for manipulating items on a stack are: push push Places an item on the top of the stack Places an item on the top of the stack pop pop Removes an item from the top of the stack Removes an item from the top of the stack
4
4 How can we implement a stack using an array? Steps: Steps: 1. Determine the size of the stack 2. Determine the kind of data the stack will store 3. Implement (minimally) the push and pop operations
5
5 How can we implement a stack using an array? 1. Determine the size of the stack Example from today’s demo program, demoStack.java: static final int MAXSIZE = 10; ( Aside: Actually, the stack will be size 11, with the 0 th element reserved for a special purpose. Specifically, the 0 th slot will be used to keep track of the next available stack location. ) Example from today’s demo program, demoStack.java: static final int MAXSIZE = 10; ( Aside: Actually, the stack will be size 11, with the 0 th element reserved for a special purpose. Specifically, the 0 th slot will be used to keep track of the next available stack location. )
6
6 How can we implement a stack using an array? 2. Determine the kind of data the stack will store Example from today’s demo program, demoStack.java: // Define a stack that will store integers int intStack[] = new int[MAXSIZE+1]; Example from today’s demo program, demoStack.java: // Define a stack that will store integers int intStack[] = new int[MAXSIZE+1];
7
7 How can we implement a stack using an array? 3. Implement (minimally) the push and pop operations Example push and pop functions from today’s demo program, demoStack.java, appear on the next two slides. Example push and pop functions from today’s demo program, demoStack.java, appear on the next two slides.
8
8 Implementation of “push” // Pushes an element onto the stack static void push ( int[] a, int val ) { int topelementindex; input in = new input(); output out = new output(); if (!isfull(a)) { topelementindex = a[0]; a[topelementindex] = val; a[0]--; } else { out.writeln("Severe error!!!!!"); out.writeln("Try to push onto full stack"); out.writeln("Press ENTER to end the program"); String junk = in.readline(); System.exit(1); } } // end push // Pushes an element onto the stack static void push ( int[] a, int val ) { int topelementindex; input in = new input(); output out = new output(); if (!isfull(a)) { topelementindex = a[0]; a[topelementindex] = val; a[0]--; } else { out.writeln("Severe error!!!!!"); out.writeln("Try to push onto full stack"); out.writeln("Press ENTER to end the program"); String junk = in.readline(); System.exit(1); } } // end push
9
9 Implementation of “pop” // Removes an element from the top of the stack static int pop ( int[] a ) { int returnval = 0; int topelementindex; input in = new input(); output out = new output(); if (!isempty(a)) { a[0]++; topelementindex = a[0]; returnval = a[topelementindex]; } else { out.writeln("Severe error!!!!!"); out.writeln("Try to pop from empty stack"); out.writeln("Press ENTER to end the program"); String junk = in.readline(); System.exit(1); } return returnval; } // end pop // Removes an element from the top of the stack static int pop ( int[] a ) { int returnval = 0; int topelementindex; input in = new input(); output out = new output(); if (!isempty(a)) { a[0]++; topelementindex = a[0]; returnval = a[topelementindex]; } else { out.writeln("Severe error!!!!!"); out.writeln("Try to pop from empty stack"); out.writeln("Press ENTER to end the program"); String junk = in.readline(); System.exit(1); } return returnval; } // end pop
10
10 Additional Methods for Manipulating Stacks isempty isempty Returns true if the stack is empty, false otherwise Returns true if the stack is empty, false otherwise isfull isfull Returns true if the stack is full, false otherwise Returns true if the stack is full, false otherwise print print Prints the contents of the stack Prints the contents of the stack
11
11 Sample Program & Animation Now let’s take a look at a sample program that implements a stack: Now let’s take a look at a sample program that implements a stack: demoStack.java demoStack.java demoStack.java As well as a Java applet that demonstrates basic stack operations: As well as a Java applet that demonstrates basic stack operations: Stack Stack Stack
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.