0% found this document useful (0 votes)
15 views27 pages

Stacks 1

The stack is a data structure that follows the LIFO (last in, first out) principle. Elements can only be added to the top of the stack using push and only removed from the top using pop. The last element pushed onto the stack will be the first one popped off. The document demonstrates stack operations like push, pop, and peek using a MyStack class and shows that the order elements are removed is the reverse of the order they were added.

Uploaded by

d
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views27 pages

Stacks 1

The stack is a data structure that follows the LIFO (last in, first out) principle. Elements can only be added to the top of the stack using push and only removed from the top using pop. The last element pushed onto the stack will be the first one popped off. The document demonstrates stack operations like push, pop, and peek using a MyStack class and shows that the order elements are removed is the reverse of the order they were added.

Uploaded by

d
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

The Stack

Last In, First Out


The Stack
• A dynamic-sized data structure
– Elements can only be added to the top (push)
– Elements can only be removed from the top (pop)
– Only the top element can be accessed individually (peek)
• LIFO order (Last In, First Out)
– The last element pushed onto the stack will be the first
element popped off of the stack.
Last In, First Out
Item add order: top

s
Item remove order:
Last In, First Out
Item add order: A top

s.push(“A”);

s
Item remove order:
Last In, First Out
Item add order: A, B top

s.push(“A”);
s.push(“B”);

s
Item remove order:
Last In, First Out
Item add order: A, B, C top

s.push(“A”);
s.push(“B”);
s.push(“C”); C

s
Item remove order:
Last In, First Out
Item add order: A, B, C top

s.push(“A”);
s.push(“B”);
s.push(“C”);
s.pop(); B

s
Item remove order: C
Last In, First Out
Item add order: A, B, C top

s.push(“A”);
s.push(“B”);
s.push(“C”);
s.pop();
s.pop(); A

s
Item remove order: C, B
Last In, First Out
Item add order: A, B, C top

s.push(“A”);
s.push(“B”);
s.push(“C”);
s.pop();
s.pop();
s.pop();
s
Item remove order: C, B, A
A Stack Interface
public interface Stackable<anyType>
{
public void push(anyType x); //add x to the top

public anyType pop(); //remove and return the top


//return null if empty or throw exception

public anyType peek(); //return the top element


//return null if empty or throw exception

public boolean isEmpty(); //true if empty

public int size(); //returns the number of elements


}
Stack mechanics
MyStack<String> s = new MyStack(); top temp t2
s.push(“B”);
s.push(“*”);
s.push(“W”);
String temp = s.pop();
String t2 = s.peek();
s.push(t2);
s.push(temp);
while(!s.isEmpty())
System.out.println(s.pop());
Stack mechanics
MyStack<String> s = new MyStack(); top temp t2
s.push(“B”);
s.push(“*”);
B
s.push(“W”);
String temp = s.pop();
String t2 = s.peek();
s.push(t2);
s.push(temp);
while(!s.isEmpty())
System.out.println(s.pop());
Stack mechanics
MyStack<String> s = new MyStack(); top temp t2
s.push(“B”);
s.push(“*”); *
B
s.push(“W”);
String temp = s.pop();
String t2 = s.peek();
s.push(t2);
s.push(temp);
while(!s.isEmpty())
System.out.println(s.pop());
Stack mechanics
MyStack<String> s = new MyStack(); top temp t2
s.push(“B”); W
s.push(“*”); *
B
s.push(“W”);
String temp = s.pop();
String t2 = s.peek();
s.push(t2);
s.push(temp);
while(!s.isEmpty())
System.out.println(s.pop());
Stack mechanics
MyStack<String> s = new MyStack(); top temp t2
W
s.push(“B”);
s.push(“*”); *
B
s.push(“W”);
String temp = s.pop();
String t2 = s.peek();
s.push(t2);
s.push(temp);
while(!s.isEmpty())
System.out.println(s.pop());
Stack mechanics
MyStack<String> s = new MyStack(); top temp t2
W *
s.push(“B”);
s.push(“*”); *
B
s.push(“W”);
String temp = s.pop();
String t2 = s.peek();
s.push(t2);
s.push(temp);
while(!s.isEmpty())
System.out.println(s.pop());
Stack mechanics
MyStack<String> s = new MyStack(); top temp t2
W *
s.push(“B”); *
s.push(“*”); *
B
s.push(“W”);
String temp = s.pop();
String t2 = s.peek();
s.push(t2);
s.push(temp);
while(!s.isEmpty())
System.out.println(s.pop());
Stack mechanics
MyStack<String> s = new MyStack(); top temp t2
W W *
s.push(“B”); *
s.push(“*”); *
B
s.push(“W”);
String temp = s.pop();
String t2 = s.peek();
s.push(t2);
s.push(temp);
while(!s.isEmpty())
System.out.println(s.pop());
Stack mechanics
MyStack<String> s = new MyStack(); top temp t2
W W *
s.push(“B”); *
s.push(“*”); *
B
s.push(“W”);
String temp = s.pop();
String t2 = s.peek();
s.push(t2);
s.push(temp);
while(!s.isEmpty())
System.out.println(s.pop());
Stack mechanics
MyStack<String> s = new MyStack(); top temp t2
W *
s.push(“B”); *
s.push(“*”); *
B
s.push(“W”);
String temp = s.pop();
String t2 = s.peek(); OUTPUT
W
s.push(t2);
s.push(temp);
while(!s.isEmpty())
System.out.println(s.pop());
Stack mechanics
MyStack<String> s = new MyStack(); top temp t2
W *
s.push(“B”); *
s.push(“*”); *
B
s.push(“W”);
String temp = s.pop();
String t2 = s.peek(); OUTPUT
W
s.push(t2);
s.push(temp);
while(!s.isEmpty())
System.out.println(s.pop());
Stack mechanics
MyStack<String> s = new MyStack(); top temp t2
W *
s.push(“B”);
s.push(“*”); *
B
s.push(“W”);
String temp = s.pop();
String t2 = s.peek(); OUTPUT
W
s.push(t2); *
s.push(temp);
while(!s.isEmpty())
System.out.println(s.pop());
Stack mechanics
MyStack<String> s = new MyStack(); top temp t2
W *
s.push(“B”);
s.push(“*”); *
B
s.push(“W”);
String temp = s.pop();
String t2 = s.peek(); OUTPUT
W
s.push(t2); *
s.push(temp);
while(!s.isEmpty())
System.out.println(s.pop());
Stack mechanics
MyStack<String> s = new MyStack(); top temp t2
W *
s.push(“B”);
s.push(“*”);
B
s.push(“W”);
String temp = s.pop();
String t2 = s.peek(); OUTPUT
W
s.push(t2); *
s.push(temp); *

while(!s.isEmpty())
System.out.println(s.pop());
Stack mechanics
MyStack<String> s = new MyStack(); top temp t2
W *
s.push(“B”);
s.push(“*”);
B
s.push(“W”);
String temp = s.pop();
String t2 = s.peek(); OUTPUT
W
s.push(t2); *
s.push(temp); *

while(!s.isEmpty())
System.out.println(s.pop());
Stack mechanics
MyStack<String> s = new MyStack(); top temp t2
W *
s.push(“B”);
s.push(“*”);
s.push(“W”);
String temp = s.pop();
String t2 = s.peek(); OUTPUT
W
s.push(t2); *
s.push(temp); *
B
while(!s.isEmpty())
System.out.println(s.pop());
Creating the MyStack
public class MyStack<anyType> implements Stackable<anyType>
{
private List<anyType> list;
public MyStack()
{
list = new ArrayList(); //could also be a Linked List
}
//now define interface methods and a toString method
}

You might also like