lab4 - stacks

lab4 - stacks - Lab 4: Intro to Data Structures: Stacks...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Page 1 of 3 Lab 4: Intro to Data Structures: Stacks Exercise 1: Stack Demo The following program shows how to construct a stack and illustrates the push , pop and size methods. The standard Java library implements the Stack class as an ArrayList. a. Compile, then run the program. Add three or four names and observe how the stack builds up with the last element added always on the top of the stack . Press Q when done adding names and now pop the names of the stack one at a time (by pressing any key) until you encounter an error message. b. Fix the program so there is no error message if you attempt to pop from an empty stack. c. Adjust the print routine so that if the stack is empty, it simply prints "Empty Stack". StackDemo.java import java.util.*; public class StackDemo { public static void main(String[] args) { Stack<String> studentName = new Stack<String>(); Scanner keyboard = new Scanner(System.in); //1. First the user adds names to the stack using the push method.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 3

lab4 - stacks - Lab 4: Intro to Data Structures: Stacks...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online