Stack

1. Code a Num class. It has the following methods and instance variables.

Num

Instance Variables

  • int n

Methods

  • public Num (int s)
  • public Num ()
  • public int getNum ()
  • public void setNum (int s)
  • public boolean equals (Num s)
  • public int compareTo (Num s)
  • public String toString()

2. Make another file. Paste this into it. Save it to the same space as Num.

public class Stack
{
private int count;
Num data[] = new Num [50];

public Stack ()
{
for (int i = 0 ; i < 50 ; i++)
{
data [i] = new Num ();
}
count = 0;
}


public int size ()
{ //Post: returns the number of elements in the stack
return count;
}


public void push (int item)
{ //Post: item is added to stack
Num p = new Num (item);
data [count] = p;
count++;
}


public void push (Num item)
{ //Post: item is added to stack
data [count] = item;
count++;
}


public Num peek ()
{ //Pre: stack is not empty
//Post: the top value (next to be popped) is returned
return data [count];
}


public boolean isFull ()
{ //Post: returns true iff the stack is full
return (count == 50);
}


public boolean isEmpty ()
{ //Post: returns true iff the stack is empty
return (count == 0);
}


public boolean empty ()
{ //Post: returns true iff the stack is empty
return isEmpty ();
}


public Num pop ()
{ //Pre: stack is not empty
//Post: most recently pushed item is removed and returned
count--;
return data [count];
}


public void clear ()
{ //Post: removes all elements from stack
while (!isEmpty ())
pop ();
}
}

3. Make a third file. Paste this into it. Save it to the same space as the first two.

public class stackRunner
{
public static void main (String args[])
{
Stack s = new Stack ();
s.push (12);
s.push (25);
s.push (45);
s.push (97);
System.out.println (s.pop ());
System.out.println (s.pop ());
System.out.println (s.pop ());
System.out.println (s.pop ());
}
}

4. (Answer on paper) a. What was the order things were "pushed" onto the stack?
b. What was the order things were "popped" off the stack?
c. How do these compare?

5. What is the output of each of the following? Explain why the output of the following two programs is different (when the same things are added to the stack).

public class stackRunner
{
public static void main (String args[])
{
Stack s = new Stack ();
s.push (1);
s.push (2);
System.out.println (s.pop ());
s.push (3);
System.out.println (s.pop ());
s.push (4);
System.out.println (s.pop ());
System.out.println (s.pop ());
}
}
public class stackRunner
{
public static void main (String args[])
{
Stack s = new Stack ();
s.push (1);
s.push (2);
s.push (3);
s.push (4);
System.out.println (s.pop ());
System.out.println (s.pop ());
System.out.println (s.pop ());
System.out.println (s.pop ());
}
}

6. Write the code to declare a stack and add 3, 4, 5 to it. Pop off 5.

7. A mental representation of a Stack is a pile of books. What is a 'push' operation with a pile of books? What is a 'pop' operation on a pile of books?

8. What method in the Stack class would be used to do the following:

a. print out the size
b. Find out the top element
c. see if it is full
d. see if it is empty

9. A Stack is called a LIFO (last in, first out) structure. Why is that?

10. Stacks are used to code many things on computers. Explain why a Stack is used to code the Undo button in Word.

11. Why don't these operations exist in the Stack class?

a. print out the middle element
b. sort it.
c. print out the 6th element

12. Over time, 1, 2, and 3 are pushed onto a Stack in that order. If you vary the times you pop, is it possible to print out each of the following?

123, 231, 213, 132, 321, 312

Example: 213

s.push(1)
s.push(2)
s.pop()
s.pop()
s.push(3)
s.pop()

Can you get the others? Prove it.