Introduced stack abstract data type
public interface SimpleStack<T> {
// push a new item on the top of the stack
public void push(T item);
// return the item currently at the top of the stack
public T peek();
// remove and return the item currently at the top of the stack
public T pop();
// check if the stack is empty
public boolean isEmpty();
}
Push Sequential…Algorithms onto stack
Push Art of Multiprocessor Programming onto stack
Push Java Concurrency in Practice onto stack
Peek returns Java Concurrency in Practice
Pop returns & removes Java Concurrency in Practice
Pop returns & removes Art of Multiprocessor Programming
How could you use a linked list to store the contents of a stack?
Node
sNode top
referring to top of stackNode
’s next
field refers to element below itStackList
Illustratedpush()
Step 1: Create Nodepush()
Step 2: Set next
push()
Step 3: Set head
push()
Completepop()
?pop()
Step 1: Store value
pop()
Step 2: Update head
pop()
Step 3: Return value
StackList<T>
public class StackList<T> implements SimpleStack<T> {
Node top = null;
public void push(T item) {...}
public T peek() {...}
public T pop() {...}
public boolean isEmpty() {...}
class Node {
T item;
Node next;
public Node (T item) {
this.item = item;
next = null;
}
}
What is wrong with the following program?
public class HelloWorld {
public static void main(String[] args)} {
System.out.println("Hello, World!");
}
}
Java uses a lot of brackets:
{ ... }
( ... )
[ ... ]
If expression are properly bracketed, Java compiler complains!
This expression
public class HelloWorld {
public static void main(String[] args)} {
System.out.println("Hello, World!");
}
}
but with just the brackets:
{ ( [ ] ) } { ( ) } }
When is an expression properly bracketed?
{ } { { } { { } } } { }
okay?{ ( [ ] ) } { ( ) } }
not?A bracketed expression is balanced if
{ { { } } { } { } }
{ { } } } { }
{ { { } } { } { } }