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

Info icon This 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.
Image of page 1

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern