stacks - cs.miami.edu · stack i astackis a standard interface i which is so standard i that java...

115
Stacks Victor Milenkovic Department of Computer Science University of Miami CSC220 Programming II – Spring 2019

Upload: phamthien

Post on 29-Aug-2019

219 views

Category:

Documents


0 download

TRANSCRIPT

Stacks

Victor Milenkovic

Department of Computer ScienceUniversity of Miami

CSC220 Programming II – Spring 2019

Outline

Stack

I A Stack is a standard InterfaceI which is so standard

I that Java didn’t even bothermaking it an Interface.

I Like any kind of stack we canthink of,

I the top entry is easy to add,view, or remove.

I Trying to add, view, or removeentries in the middle is messyand awkward.

Stack

I A Stack is a standard Interface

I which is so standard

I that Java didn’t even bothermaking it an Interface.

I Like any kind of stack we canthink of,

I the top entry is easy to add,view, or remove.

I Trying to add, view, or removeentries in the middle is messyand awkward.

Stack

I A Stack is a standard InterfaceI which is so standard

I that Java didn’t even bothermaking it an Interface.

I Like any kind of stack we canthink of,

I the top entry is easy to add,view, or remove.

I Trying to add, view, or removeentries in the middle is messyand awkward.

Stack

I A Stack is a standard InterfaceI which is so standard

I that Java didn’t even bothermaking it an Interface.

I Like any kind of stack we canthink of,

I the top entry is easy to add,view, or remove.

I Trying to add, view, or removeentries in the middle is messyand awkward.

Stack

I A Stack is a standard InterfaceI which is so standard

I that Java didn’t even bothermaking it an Interface.

I Like any kind of stack we canthink of,

I the top entry is easy to add,view, or remove.

I Trying to add, view, or removeentries in the middle is messyand awkward.

Stack

I A Stack is a standard InterfaceI which is so standard

I that Java didn’t even bothermaking it an Interface.

I Like any kind of stack we canthink of,

I the top entry is easy to add,view, or remove.

I Trying to add, view, or removeentries in the middle is messyand awkward.

Stack

I A Stack is a standard InterfaceI which is so standard

I that Java didn’t even bothermaking it an Interface.

I Like any kind of stack we canthink of,

I the top entry is easy to add,view, or remove.

I Trying to add, view, or removeentries in the middle is messyand awkward.

Stack Methods

I The names for the Stack methods are a little strange:I push add a new entry to the top of the stackI pop remove one entry from the top of the stackI peek look at the top entry of the stack without changing itI empty true if there is nothing in the stack, false otherwise

I When I put something on top of one of the towering stacks of papers onmy desk,

I I don’t think of it as pushing,I nor do I think of it as popping when I remove it.I Peek and empty make sense though.

Stack Methods

I The names for the Stack methods are a little strange:

I push add a new entry to the top of the stackI pop remove one entry from the top of the stackI peek look at the top entry of the stack without changing itI empty true if there is nothing in the stack, false otherwise

I When I put something on top of one of the towering stacks of papers onmy desk,

I I don’t think of it as pushing,I nor do I think of it as popping when I remove it.I Peek and empty make sense though.

Stack Methods

I The names for the Stack methods are a little strange:I push add a new entry to the top of the stack

I pop remove one entry from the top of the stackI peek look at the top entry of the stack without changing itI empty true if there is nothing in the stack, false otherwise

I When I put something on top of one of the towering stacks of papers onmy desk,

I I don’t think of it as pushing,I nor do I think of it as popping when I remove it.I Peek and empty make sense though.

Stack Methods

I The names for the Stack methods are a little strange:I push add a new entry to the top of the stackI pop remove one entry from the top of the stack

I peek look at the top entry of the stack without changing itI empty true if there is nothing in the stack, false otherwise

I When I put something on top of one of the towering stacks of papers onmy desk,

I I don’t think of it as pushing,I nor do I think of it as popping when I remove it.I Peek and empty make sense though.

Stack Methods

I The names for the Stack methods are a little strange:I push add a new entry to the top of the stackI pop remove one entry from the top of the stackI peek look at the top entry of the stack without changing it

I empty true if there is nothing in the stack, false otherwiseI When I put something on top of one of the towering stacks of papers on

my desk,I I don’t think of it as pushing,I nor do I think of it as popping when I remove it.I Peek and empty make sense though.

Stack Methods

I The names for the Stack methods are a little strange:I push add a new entry to the top of the stackI pop remove one entry from the top of the stackI peek look at the top entry of the stack without changing itI empty true if there is nothing in the stack, false otherwise

I When I put something on top of one of the towering stacks of papers onmy desk,

I I don’t think of it as pushing,I nor do I think of it as popping when I remove it.I Peek and empty make sense though.

Stack Methods

I The names for the Stack methods are a little strange:I push add a new entry to the top of the stackI pop remove one entry from the top of the stackI peek look at the top entry of the stack without changing itI empty true if there is nothing in the stack, false otherwise

I When I put something on top of one of the towering stacks of papers onmy desk,

I I don’t think of it as pushing,I nor do I think of it as popping when I remove it.I Peek and empty make sense though.

Stack Methods

I The names for the Stack methods are a little strange:I push add a new entry to the top of the stackI pop remove one entry from the top of the stackI peek look at the top entry of the stack without changing itI empty true if there is nothing in the stack, false otherwise

I When I put something on top of one of the towering stacks of papers onmy desk,

I I don’t think of it as pushing,

I nor do I think of it as popping when I remove it.I Peek and empty make sense though.

Stack Methods

I The names for the Stack methods are a little strange:I push add a new entry to the top of the stackI pop remove one entry from the top of the stackI peek look at the top entry of the stack without changing itI empty true if there is nothing in the stack, false otherwise

I When I put something on top of one of the towering stacks of papers onmy desk,

I I don’t think of it as pushing,I nor do I think of it as popping when I remove it.

I Peek and empty make sense though.

Stack Methods

I The names for the Stack methods are a little strange:I push add a new entry to the top of the stackI pop remove one entry from the top of the stackI peek look at the top entry of the stack without changing itI empty true if there is nothing in the stack, false otherwise

I When I put something on top of one of the towering stacks of papers onmy desk,

I I don’t think of it as pushing,I nor do I think of it as popping when I remove it.I Peek and empty make sense though.

Name Origins

I think what the original inventors had in mind was a 1950s buffet diner springloaded plate dispenser.

I The power cord is to run a dish warmer.I It doesn’t shoot the dishes up when it pops!I Instead, it always keeps the top dish level with the top of the dispenser,I although I don’t think that requires electricity.

Name Origins

I think what the original inventors had in mind was a 1950s buffet diner springloaded plate dispenser.

I The power cord is to run a dish warmer.

I It doesn’t shoot the dishes up when it pops!I Instead, it always keeps the top dish level with the top of the dispenser,I although I don’t think that requires electricity.

Name Origins

I think what the original inventors had in mind was a 1950s buffet diner springloaded plate dispenser.

I The power cord is to run a dish warmer.I It doesn’t shoot the dishes up when it pops!

I Instead, it always keeps the top dish level with the top of the dispenser,I although I don’t think that requires electricity.

Name Origins

I think what the original inventors had in mind was a 1950s buffet diner springloaded plate dispenser.

I The power cord is to run a dish warmer.I It doesn’t shoot the dishes up when it pops!I Instead, it always keeps the top dish level with the top of the dispenser,

I although I don’t think that requires electricity.

Name Origins

I think what the original inventors had in mind was a 1950s buffet diner springloaded plate dispenser.

I The power cord is to run a dish warmer.I It doesn’t shoot the dishes up when it pops!I Instead, it always keeps the top dish level with the top of the dispenser,I although I don’t think that requires electricity.

Stack methods in action

Stack stack = new Stack();

stack.empty(); // returns truestack.push("mango");stack.push("banana");stack.push("coconut");stack.pop(); // returns "coconut"stack.peek(); // returns "banana"stack.push("cantaloupe");stack.pop(); // returns "cantaloupe"stack.pop(); // returns "banana"stack.empty(); // returns falsestack.pop(); // returns "mango"stack.peek(); // throws Exception

Stack methods in action

Stack stack = new Stack();stack.empty(); // returns true

stack.push("mango");stack.push("banana");stack.push("coconut");stack.pop(); // returns "coconut"stack.peek(); // returns "banana"stack.push("cantaloupe");stack.pop(); // returns "cantaloupe"stack.pop(); // returns "banana"stack.empty(); // returns falsestack.pop(); // returns "mango"stack.peek(); // throws Exception

Stack methods in action

Stack stack = new Stack();stack.empty(); // returns truestack.push("mango");

stack.push("banana");stack.push("coconut");stack.pop(); // returns "coconut"stack.peek(); // returns "banana"stack.push("cantaloupe");stack.pop(); // returns "cantaloupe"stack.pop(); // returns "banana"stack.empty(); // returns falsestack.pop(); // returns "mango"stack.peek(); // throws Exception

Stack methods in action

Stack stack = new Stack();stack.empty(); // returns truestack.push("mango");stack.push("banana");

stack.push("coconut");stack.pop(); // returns "coconut"stack.peek(); // returns "banana"stack.push("cantaloupe");stack.pop(); // returns "cantaloupe"stack.pop(); // returns "banana"stack.empty(); // returns falsestack.pop(); // returns "mango"stack.peek(); // throws Exception

Stack methods in action

Stack stack = new Stack();stack.empty(); // returns truestack.push("mango");stack.push("banana");stack.push("coconut");

stack.pop(); // returns "coconut"stack.peek(); // returns "banana"stack.push("cantaloupe");stack.pop(); // returns "cantaloupe"stack.pop(); // returns "banana"stack.empty(); // returns falsestack.pop(); // returns "mango"stack.peek(); // throws Exception

Stack methods in action

Stack stack = new Stack();stack.empty(); // returns truestack.push("mango");stack.push("banana");stack.push("coconut");stack.pop(); // returns "coconut"

stack.peek(); // returns "banana"stack.push("cantaloupe");stack.pop(); // returns "cantaloupe"stack.pop(); // returns "banana"stack.empty(); // returns falsestack.pop(); // returns "mango"stack.peek(); // throws Exception

Stack methods in action

Stack stack = new Stack();stack.empty(); // returns truestack.push("mango");stack.push("banana");stack.push("coconut");stack.pop(); // returns "coconut"stack.peek(); // returns "banana"

stack.push("cantaloupe");stack.pop(); // returns "cantaloupe"stack.pop(); // returns "banana"stack.empty(); // returns falsestack.pop(); // returns "mango"stack.peek(); // throws Exception

Stack methods in action

Stack stack = new Stack();stack.empty(); // returns truestack.push("mango");stack.push("banana");stack.push("coconut");stack.pop(); // returns "coconut"stack.peek(); // returns "banana"stack.push("cantaloupe");

stack.pop(); // returns "cantaloupe"stack.pop(); // returns "banana"stack.empty(); // returns falsestack.pop(); // returns "mango"stack.peek(); // throws Exception

Stack methods in action

Stack stack = new Stack();stack.empty(); // returns truestack.push("mango");stack.push("banana");stack.push("coconut");stack.pop(); // returns "coconut"stack.peek(); // returns "banana"stack.push("cantaloupe");stack.pop(); // returns "cantaloupe"

stack.pop(); // returns "banana"stack.empty(); // returns falsestack.pop(); // returns "mango"stack.peek(); // throws Exception

Stack methods in action

Stack stack = new Stack();stack.empty(); // returns truestack.push("mango");stack.push("banana");stack.push("coconut");stack.pop(); // returns "coconut"stack.peek(); // returns "banana"stack.push("cantaloupe");stack.pop(); // returns "cantaloupe"stack.pop(); // returns "banana"

stack.empty(); // returns falsestack.pop(); // returns "mango"stack.peek(); // throws Exception

Stack methods in action

Stack stack = new Stack();stack.empty(); // returns truestack.push("mango");stack.push("banana");stack.push("coconut");stack.pop(); // returns "coconut"stack.peek(); // returns "banana"stack.push("cantaloupe");stack.pop(); // returns "cantaloupe"stack.pop(); // returns "banana"stack.empty(); // returns false

stack.pop(); // returns "mango"stack.peek(); // throws Exception

Stack methods in action

Stack stack = new Stack();stack.empty(); // returns truestack.push("mango");stack.push("banana");stack.push("coconut");stack.pop(); // returns "coconut"stack.peek(); // returns "banana"stack.push("cantaloupe");stack.pop(); // returns "cantaloupe"stack.pop(); // returns "banana"stack.empty(); // returns falsestack.pop(); // returns "mango"

stack.peek(); // throws Exception

Stack methods in action

Stack stack = new Stack();stack.empty(); // returns truestack.push("mango");stack.push("banana");stack.push("coconut");stack.pop(); // returns "coconut"stack.peek(); // returns "banana"stack.push("cantaloupe");stack.pop(); // returns "cantaloupe"stack.pop(); // returns "banana"stack.empty(); // returns falsestack.pop(); // returns "mango"stack.peek(); // throws Exception

Lab

For the next lab, you will learn three ways to implement Stack.In StackInt.java, you will notice something new:

I < E >

That is a generic declaration. In means you can haveI StackInt<String>I StackInt<DirectoryEntry>I or a stack of any type of class.

When you do this, the Java compiler will make sure you only put that kind ofthing into that stack.It has to be a class, however, so for primitive data types you have to use theclass version of those types:

I char → CharacterI int → IntegerI double → Double

This is less efficient (by a constant factor in space and time) than creating aspecific StackOfChar, etc., but it is usually good enough.

Lab

For the next lab, you will learn three ways to implement Stack.

In StackInt.java, you will notice something new:I < E >

That is a generic declaration. In means you can haveI StackInt<String>I StackInt<DirectoryEntry>I or a stack of any type of class.

When you do this, the Java compiler will make sure you only put that kind ofthing into that stack.It has to be a class, however, so for primitive data types you have to use theclass version of those types:

I char → CharacterI int → IntegerI double → Double

This is less efficient (by a constant factor in space and time) than creating aspecific StackOfChar, etc., but it is usually good enough.

Lab

For the next lab, you will learn three ways to implement Stack.In StackInt.java, you will notice something new:

I < E >

That is a generic declaration. In means you can haveI StackInt<String>I StackInt<DirectoryEntry>I or a stack of any type of class.

When you do this, the Java compiler will make sure you only put that kind ofthing into that stack.It has to be a class, however, so for primitive data types you have to use theclass version of those types:

I char → CharacterI int → IntegerI double → Double

This is less efficient (by a constant factor in space and time) than creating aspecific StackOfChar, etc., but it is usually good enough.

Lab

For the next lab, you will learn three ways to implement Stack.In StackInt.java, you will notice something new:

I < E >

That is a generic declaration. In means you can haveI StackInt<String>I StackInt<DirectoryEntry>I or a stack of any type of class.

When you do this, the Java compiler will make sure you only put that kind ofthing into that stack.It has to be a class, however, so for primitive data types you have to use theclass version of those types:

I char → CharacterI int → IntegerI double → Double

This is less efficient (by a constant factor in space and time) than creating aspecific StackOfChar, etc., but it is usually good enough.

Lab

For the next lab, you will learn three ways to implement Stack.In StackInt.java, you will notice something new:

I < E >

That is a generic declaration. In means you can have

I StackInt<String>I StackInt<DirectoryEntry>I or a stack of any type of class.

When you do this, the Java compiler will make sure you only put that kind ofthing into that stack.It has to be a class, however, so for primitive data types you have to use theclass version of those types:

I char → CharacterI int → IntegerI double → Double

This is less efficient (by a constant factor in space and time) than creating aspecific StackOfChar, etc., but it is usually good enough.

Lab

For the next lab, you will learn three ways to implement Stack.In StackInt.java, you will notice something new:

I < E >

That is a generic declaration. In means you can haveI StackInt<String>

I StackInt<DirectoryEntry>I or a stack of any type of class.

When you do this, the Java compiler will make sure you only put that kind ofthing into that stack.It has to be a class, however, so for primitive data types you have to use theclass version of those types:

I char → CharacterI int → IntegerI double → Double

This is less efficient (by a constant factor in space and time) than creating aspecific StackOfChar, etc., but it is usually good enough.

Lab

For the next lab, you will learn three ways to implement Stack.In StackInt.java, you will notice something new:

I < E >

That is a generic declaration. In means you can haveI StackInt<String>I StackInt<DirectoryEntry>

I or a stack of any type of class.

When you do this, the Java compiler will make sure you only put that kind ofthing into that stack.It has to be a class, however, so for primitive data types you have to use theclass version of those types:

I char → CharacterI int → IntegerI double → Double

This is less efficient (by a constant factor in space and time) than creating aspecific StackOfChar, etc., but it is usually good enough.

Lab

For the next lab, you will learn three ways to implement Stack.In StackInt.java, you will notice something new:

I < E >

That is a generic declaration. In means you can haveI StackInt<String>I StackInt<DirectoryEntry>I or a stack of any type of class.

When you do this, the Java compiler will make sure you only put that kind ofthing into that stack.It has to be a class, however, so for primitive data types you have to use theclass version of those types:

I char → CharacterI int → IntegerI double → Double

This is less efficient (by a constant factor in space and time) than creating aspecific StackOfChar, etc., but it is usually good enough.

Lab

For the next lab, you will learn three ways to implement Stack.In StackInt.java, you will notice something new:

I < E >

That is a generic declaration. In means you can haveI StackInt<String>I StackInt<DirectoryEntry>I or a stack of any type of class.

When you do this, the Java compiler will make sure you only put that kind ofthing into that stack.

It has to be a class, however, so for primitive data types you have to use theclass version of those types:

I char → CharacterI int → IntegerI double → Double

This is less efficient (by a constant factor in space and time) than creating aspecific StackOfChar, etc., but it is usually good enough.

Lab

For the next lab, you will learn three ways to implement Stack.In StackInt.java, you will notice something new:

I < E >

That is a generic declaration. In means you can haveI StackInt<String>I StackInt<DirectoryEntry>I or a stack of any type of class.

When you do this, the Java compiler will make sure you only put that kind ofthing into that stack.It has to be a class, however, so for primitive data types you have to use theclass version of those types:

I char → CharacterI int → IntegerI double → Double

This is less efficient (by a constant factor in space and time) than creating aspecific StackOfChar, etc., but it is usually good enough.

Lab

For the next lab, you will learn three ways to implement Stack.In StackInt.java, you will notice something new:

I < E >

That is a generic declaration. In means you can haveI StackInt<String>I StackInt<DirectoryEntry>I or a stack of any type of class.

When you do this, the Java compiler will make sure you only put that kind ofthing into that stack.It has to be a class, however, so for primitive data types you have to use theclass version of those types:

I char → Character

I int → IntegerI double → Double

This is less efficient (by a constant factor in space and time) than creating aspecific StackOfChar, etc., but it is usually good enough.

Lab

For the next lab, you will learn three ways to implement Stack.In StackInt.java, you will notice something new:

I < E >

That is a generic declaration. In means you can haveI StackInt<String>I StackInt<DirectoryEntry>I or a stack of any type of class.

When you do this, the Java compiler will make sure you only put that kind ofthing into that stack.It has to be a class, however, so for primitive data types you have to use theclass version of those types:

I char → CharacterI int → Integer

I double → Double

This is less efficient (by a constant factor in space and time) than creating aspecific StackOfChar, etc., but it is usually good enough.

Lab

For the next lab, you will learn three ways to implement Stack.In StackInt.java, you will notice something new:

I < E >

That is a generic declaration. In means you can haveI StackInt<String>I StackInt<DirectoryEntry>I or a stack of any type of class.

When you do this, the Java compiler will make sure you only put that kind ofthing into that stack.It has to be a class, however, so for primitive data types you have to use theclass version of those types:

I char → CharacterI int → IntegerI double → Double

This is less efficient (by a constant factor in space and time) than creating aspecific StackOfChar, etc., but it is usually good enough.

Lab

For the next lab, you will learn three ways to implement Stack.In StackInt.java, you will notice something new:

I < E >

That is a generic declaration. In means you can haveI StackInt<String>I StackInt<DirectoryEntry>I or a stack of any type of class.

When you do this, the Java compiler will make sure you only put that kind ofthing into that stack.It has to be a class, however, so for primitive data types you have to use theclass version of those types:

I char → CharacterI int → IntegerI double → Double

This is less efficient (by a constant factor in space and time) than creating aspecific StackOfChar, etc., but it is usually good enough.

ArrayStack

ArrayStack.java

I Array based implementation of StackInt.I Entries are pushed at the end (max index) of the array.I So push is O(1),I (unless the array is full and needs to be reallocated).I This is the fastest way to implement a stack,I but it might not be good for real time programming.

(Sorry the laser stopped in the middle of your eye, but we have to allocate abigger array!)

ArrayStack

ArrayStack.javaI Array based implementation of StackInt.

I Entries are pushed at the end (max index) of the array.I So push is O(1),I (unless the array is full and needs to be reallocated).I This is the fastest way to implement a stack,I but it might not be good for real time programming.

(Sorry the laser stopped in the middle of your eye, but we have to allocate abigger array!)

ArrayStack

ArrayStack.javaI Array based implementation of StackInt.I Entries are pushed at the end (max index) of the array.

I So push is O(1),I (unless the array is full and needs to be reallocated).I This is the fastest way to implement a stack,I but it might not be good for real time programming.

(Sorry the laser stopped in the middle of your eye, but we have to allocate abigger array!)

ArrayStack

ArrayStack.javaI Array based implementation of StackInt.I Entries are pushed at the end (max index) of the array.I So push is O(1),

I (unless the array is full and needs to be reallocated).I This is the fastest way to implement a stack,I but it might not be good for real time programming.

(Sorry the laser stopped in the middle of your eye, but we have to allocate abigger array!)

ArrayStack

ArrayStack.javaI Array based implementation of StackInt.I Entries are pushed at the end (max index) of the array.I So push is O(1),I (unless the array is full and needs to be reallocated).

I This is the fastest way to implement a stack,I but it might not be good for real time programming.

(Sorry the laser stopped in the middle of your eye, but we have to allocate abigger array!)

ArrayStack

ArrayStack.javaI Array based implementation of StackInt.I Entries are pushed at the end (max index) of the array.I So push is O(1),I (unless the array is full and needs to be reallocated).I This is the fastest way to implement a stack,

I but it might not be good for real time programming.

(Sorry the laser stopped in the middle of your eye, but we have to allocate abigger array!)

ArrayStack

ArrayStack.javaI Array based implementation of StackInt.I Entries are pushed at the end (max index) of the array.I So push is O(1),I (unless the array is full and needs to be reallocated).I This is the fastest way to implement a stack,I but it might not be good for real time programming.

(Sorry the laser stopped in the middle of your eye, but we have to allocate abigger array!)

LinkedStack

LinkedStack.java

I Linked list implementationI O(1) per operation (really?).

You will notice some new techniques.I The entire Node class is privateI and declared inside LinkedStack.I No separate Java fileI No need for accessor methods (getNext(), etc).I data.next gets you the next entry instead of data.getNext().

Other changes:I The Node is singly linked instead of doubly linked.I There is no previous.I Saves space and time.I Works fine for this specialized application.

As a result:I Pushing and popping are done at the beginning of the list,I not the end.

LinkedStack

LinkedStack.javaI Linked list implementation

I O(1) per operation (really?).

You will notice some new techniques.I The entire Node class is privateI and declared inside LinkedStack.I No separate Java fileI No need for accessor methods (getNext(), etc).I data.next gets you the next entry instead of data.getNext().

Other changes:I The Node is singly linked instead of doubly linked.I There is no previous.I Saves space and time.I Works fine for this specialized application.

As a result:I Pushing and popping are done at the beginning of the list,I not the end.

LinkedStack

LinkedStack.javaI Linked list implementationI O(1) per operation (really?).

You will notice some new techniques.I The entire Node class is privateI and declared inside LinkedStack.I No separate Java fileI No need for accessor methods (getNext(), etc).I data.next gets you the next entry instead of data.getNext().

Other changes:I The Node is singly linked instead of doubly linked.I There is no previous.I Saves space and time.I Works fine for this specialized application.

As a result:I Pushing and popping are done at the beginning of the list,I not the end.

LinkedStack

LinkedStack.javaI Linked list implementationI O(1) per operation (really?).

You will notice some new techniques.

I The entire Node class is privateI and declared inside LinkedStack.I No separate Java fileI No need for accessor methods (getNext(), etc).I data.next gets you the next entry instead of data.getNext().

Other changes:I The Node is singly linked instead of doubly linked.I There is no previous.I Saves space and time.I Works fine for this specialized application.

As a result:I Pushing and popping are done at the beginning of the list,I not the end.

LinkedStack

LinkedStack.javaI Linked list implementationI O(1) per operation (really?).

You will notice some new techniques.I The entire Node class is private

I and declared inside LinkedStack.I No separate Java fileI No need for accessor methods (getNext(), etc).I data.next gets you the next entry instead of data.getNext().

Other changes:I The Node is singly linked instead of doubly linked.I There is no previous.I Saves space and time.I Works fine for this specialized application.

As a result:I Pushing and popping are done at the beginning of the list,I not the end.

LinkedStack

LinkedStack.javaI Linked list implementationI O(1) per operation (really?).

You will notice some new techniques.I The entire Node class is privateI and declared inside LinkedStack.

I No separate Java fileI No need for accessor methods (getNext(), etc).I data.next gets you the next entry instead of data.getNext().

Other changes:I The Node is singly linked instead of doubly linked.I There is no previous.I Saves space and time.I Works fine for this specialized application.

As a result:I Pushing and popping are done at the beginning of the list,I not the end.

LinkedStack

LinkedStack.javaI Linked list implementationI O(1) per operation (really?).

You will notice some new techniques.I The entire Node class is privateI and declared inside LinkedStack.I No separate Java file

I No need for accessor methods (getNext(), etc).I data.next gets you the next entry instead of data.getNext().

Other changes:I The Node is singly linked instead of doubly linked.I There is no previous.I Saves space and time.I Works fine for this specialized application.

As a result:I Pushing and popping are done at the beginning of the list,I not the end.

LinkedStack

LinkedStack.javaI Linked list implementationI O(1) per operation (really?).

You will notice some new techniques.I The entire Node class is privateI and declared inside LinkedStack.I No separate Java fileI No need for accessor methods (getNext(), etc).

I data.next gets you the next entry instead of data.getNext().

Other changes:I The Node is singly linked instead of doubly linked.I There is no previous.I Saves space and time.I Works fine for this specialized application.

As a result:I Pushing and popping are done at the beginning of the list,I not the end.

LinkedStack

LinkedStack.javaI Linked list implementationI O(1) per operation (really?).

You will notice some new techniques.I The entire Node class is privateI and declared inside LinkedStack.I No separate Java fileI No need for accessor methods (getNext(), etc).I data.next gets you the next entry instead of data.getNext().

Other changes:I The Node is singly linked instead of doubly linked.I There is no previous.I Saves space and time.I Works fine for this specialized application.

As a result:I Pushing and popping are done at the beginning of the list,I not the end.

LinkedStack

LinkedStack.javaI Linked list implementationI O(1) per operation (really?).

You will notice some new techniques.I The entire Node class is privateI and declared inside LinkedStack.I No separate Java fileI No need for accessor methods (getNext(), etc).I data.next gets you the next entry instead of data.getNext().

Other changes:

I The Node is singly linked instead of doubly linked.I There is no previous.I Saves space and time.I Works fine for this specialized application.

As a result:I Pushing and popping are done at the beginning of the list,I not the end.

LinkedStack

LinkedStack.javaI Linked list implementationI O(1) per operation (really?).

You will notice some new techniques.I The entire Node class is privateI and declared inside LinkedStack.I No separate Java fileI No need for accessor methods (getNext(), etc).I data.next gets you the next entry instead of data.getNext().

Other changes:I The Node is singly linked instead of doubly linked.

I There is no previous.I Saves space and time.I Works fine for this specialized application.

As a result:I Pushing and popping are done at the beginning of the list,I not the end.

LinkedStack

LinkedStack.javaI Linked list implementationI O(1) per operation (really?).

You will notice some new techniques.I The entire Node class is privateI and declared inside LinkedStack.I No separate Java fileI No need for accessor methods (getNext(), etc).I data.next gets you the next entry instead of data.getNext().

Other changes:I The Node is singly linked instead of doubly linked.I There is no previous.

I Saves space and time.I Works fine for this specialized application.

As a result:I Pushing and popping are done at the beginning of the list,I not the end.

LinkedStack

LinkedStack.javaI Linked list implementationI O(1) per operation (really?).

You will notice some new techniques.I The entire Node class is privateI and declared inside LinkedStack.I No separate Java fileI No need for accessor methods (getNext(), etc).I data.next gets you the next entry instead of data.getNext().

Other changes:I The Node is singly linked instead of doubly linked.I There is no previous.I Saves space and time.

I Works fine for this specialized application.

As a result:I Pushing and popping are done at the beginning of the list,I not the end.

LinkedStack

LinkedStack.javaI Linked list implementationI O(1) per operation (really?).

You will notice some new techniques.I The entire Node class is privateI and declared inside LinkedStack.I No separate Java fileI No need for accessor methods (getNext(), etc).I data.next gets you the next entry instead of data.getNext().

Other changes:I The Node is singly linked instead of doubly linked.I There is no previous.I Saves space and time.I Works fine for this specialized application.

As a result:I Pushing and popping are done at the beginning of the list,I not the end.

LinkedStack

LinkedStack.javaI Linked list implementationI O(1) per operation (really?).

You will notice some new techniques.I The entire Node class is privateI and declared inside LinkedStack.I No separate Java fileI No need for accessor methods (getNext(), etc).I data.next gets you the next entry instead of data.getNext().

Other changes:I The Node is singly linked instead of doubly linked.I There is no previous.I Saves space and time.I Works fine for this specialized application.

As a result:

I Pushing and popping are done at the beginning of the list,I not the end.

LinkedStack

LinkedStack.javaI Linked list implementationI O(1) per operation (really?).

You will notice some new techniques.I The entire Node class is privateI and declared inside LinkedStack.I No separate Java fileI No need for accessor methods (getNext(), etc).I data.next gets you the next entry instead of data.getNext().

Other changes:I The Node is singly linked instead of doubly linked.I There is no previous.I Saves space and time.I Works fine for this specialized application.

As a result:I Pushing and popping are done at the beginning of the list,

I not the end.

LinkedStack

LinkedStack.javaI Linked list implementationI O(1) per operation (really?).

You will notice some new techniques.I The entire Node class is privateI and declared inside LinkedStack.I No separate Java fileI No need for accessor methods (getNext(), etc).I data.next gets you the next entry instead of data.getNext().

Other changes:I The Node is singly linked instead of doubly linked.I There is no previous.I Saves space and time.I Works fine for this specialized application.

As a result:I Pushing and popping are done at the beginning of the list,I not the end.

ListStack

ListStack.javaI Implementation using java.util.ListI and its implementation java.util.ArrayList.

List is an interfaceI Describes a list.I add(item) means add an item to the end of the list.I We will use add() to implement push().

Look at the List documentation,I particularly size(), get(), and remove().I How do we implement empty()?I How do we implement peek()?I How do we implement pop()?

ListStack

ListStack.java

I Implementation using java.util.ListI and its implementation java.util.ArrayList.

List is an interfaceI Describes a list.I add(item) means add an item to the end of the list.I We will use add() to implement push().

Look at the List documentation,I particularly size(), get(), and remove().I How do we implement empty()?I How do we implement peek()?I How do we implement pop()?

ListStack

ListStack.javaI Implementation using java.util.List

I and its implementation java.util.ArrayList.

List is an interfaceI Describes a list.I add(item) means add an item to the end of the list.I We will use add() to implement push().

Look at the List documentation,I particularly size(), get(), and remove().I How do we implement empty()?I How do we implement peek()?I How do we implement pop()?

ListStack

ListStack.javaI Implementation using java.util.ListI and its implementation java.util.ArrayList.

List is an interfaceI Describes a list.I add(item) means add an item to the end of the list.I We will use add() to implement push().

Look at the List documentation,I particularly size(), get(), and remove().I How do we implement empty()?I How do we implement peek()?I How do we implement pop()?

ListStack

ListStack.javaI Implementation using java.util.ListI and its implementation java.util.ArrayList.

List is an interface

I Describes a list.I add(item) means add an item to the end of the list.I We will use add() to implement push().

Look at the List documentation,I particularly size(), get(), and remove().I How do we implement empty()?I How do we implement peek()?I How do we implement pop()?

ListStack

ListStack.javaI Implementation using java.util.ListI and its implementation java.util.ArrayList.

List is an interfaceI Describes a list.

I add(item) means add an item to the end of the list.I We will use add() to implement push().

Look at the List documentation,I particularly size(), get(), and remove().I How do we implement empty()?I How do we implement peek()?I How do we implement pop()?

ListStack

ListStack.javaI Implementation using java.util.ListI and its implementation java.util.ArrayList.

List is an interfaceI Describes a list.I add(item) means add an item to the end of the list.

I We will use add() to implement push().

Look at the List documentation,I particularly size(), get(), and remove().I How do we implement empty()?I How do we implement peek()?I How do we implement pop()?

ListStack

ListStack.javaI Implementation using java.util.ListI and its implementation java.util.ArrayList.

List is an interfaceI Describes a list.I add(item) means add an item to the end of the list.I We will use add() to implement push().

Look at the List documentation,I particularly size(), get(), and remove().I How do we implement empty()?I How do we implement peek()?I How do we implement pop()?

ListStack

ListStack.javaI Implementation using java.util.ListI and its implementation java.util.ArrayList.

List is an interfaceI Describes a list.I add(item) means add an item to the end of the list.I We will use add() to implement push().

Look at the List documentation,

I particularly size(), get(), and remove().I How do we implement empty()?I How do we implement peek()?I How do we implement pop()?

ListStack

ListStack.javaI Implementation using java.util.ListI and its implementation java.util.ArrayList.

List is an interfaceI Describes a list.I add(item) means add an item to the end of the list.I We will use add() to implement push().

Look at the List documentation,I particularly size(), get(), and remove().

I How do we implement empty()?I How do we implement peek()?I How do we implement pop()?

ListStack

ListStack.javaI Implementation using java.util.ListI and its implementation java.util.ArrayList.

List is an interfaceI Describes a list.I add(item) means add an item to the end of the list.I We will use add() to implement push().

Look at the List documentation,I particularly size(), get(), and remove().I How do we implement empty()?

I How do we implement peek()?I How do we implement pop()?

ListStack

ListStack.javaI Implementation using java.util.ListI and its implementation java.util.ArrayList.

List is an interfaceI Describes a list.I add(item) means add an item to the end of the list.I We will use add() to implement push().

Look at the List documentation,I particularly size(), get(), and remove().I How do we implement empty()?I How do we implement peek()?

I How do we implement pop()?

ListStack

ListStack.javaI Implementation using java.util.ListI and its implementation java.util.ArrayList.

List is an interfaceI Describes a list.I add(item) means add an item to the end of the list.I We will use add() to implement push().

Look at the List documentation,I particularly size(), get(), and remove().I How do we implement empty()?I How do we implement peek()?I How do we implement pop()?

ListStack

Use ArrayList implementation of List.

I Partially filled array.I Just like we have been doing.I When size==length, it reallocates.I Array variable and size are private.

java.util.LinkedListI Doubly linked list implementation of List.I We could easily use it if we wanted to,I thanks to the List interface.

ListStack

Use ArrayList implementation of List.I Partially filled array.

I Just like we have been doing.I When size==length, it reallocates.I Array variable and size are private.

java.util.LinkedListI Doubly linked list implementation of List.I We could easily use it if we wanted to,I thanks to the List interface.

ListStack

Use ArrayList implementation of List.I Partially filled array.I Just like we have been doing.

I When size==length, it reallocates.I Array variable and size are private.

java.util.LinkedListI Doubly linked list implementation of List.I We could easily use it if we wanted to,I thanks to the List interface.

ListStack

Use ArrayList implementation of List.I Partially filled array.I Just like we have been doing.I When size==length, it reallocates.

I Array variable and size are private.

java.util.LinkedListI Doubly linked list implementation of List.I We could easily use it if we wanted to,I thanks to the List interface.

ListStack

Use ArrayList implementation of List.I Partially filled array.I Just like we have been doing.I When size==length, it reallocates.I Array variable and size are private.

java.util.LinkedListI Doubly linked list implementation of List.I We could easily use it if we wanted to,I thanks to the List interface.

ListStack

Use ArrayList implementation of List.I Partially filled array.I Just like we have been doing.I When size==length, it reallocates.I Array variable and size are private.

java.util.LinkedList

I Doubly linked list implementation of List.I We could easily use it if we wanted to,I thanks to the List interface.

ListStack

Use ArrayList implementation of List.I Partially filled array.I Just like we have been doing.I When size==length, it reallocates.I Array variable and size are private.

java.util.LinkedListI Doubly linked list implementation of List.

I We could easily use it if we wanted to,I thanks to the List interface.

ListStack

Use ArrayList implementation of List.I Partially filled array.I Just like we have been doing.I When size==length, it reallocates.I Array variable and size are private.

java.util.LinkedListI Doubly linked list implementation of List.I We could easily use it if we wanted to,

I thanks to the List interface.

ListStack

Use ArrayList implementation of List.I Partially filled array.I Just like we have been doing.I When size==length, it reallocates.I Array variable and size are private.

java.util.LinkedListI Doubly linked list implementation of List.I We could easily use it if we wanted to,I thanks to the List interface.

Summary

StackI The StackInt interface describes a Stack.I Only adding or removing at the top is possible.I Operations called push, pop, peek, empty.I Implemented using array, linked list, or List interface.

ArrayStackI Implement using an array.I Adding is O(1) except for reallocate().

LinkedStackI Private Node class.I node.next instead of node.getNext()I Push and pop at front (head) of list.

ListStackI Use Java List interface.I Use add(item), size(), get(index), remove(index).I ArrayList implementation uses partially filled array.I LinkedList is another implementation of List using a doubly linked list.

SummaryStack

I The StackInt interface describes a Stack.I Only adding or removing at the top is possible.I Operations called push, pop, peek, empty.I Implemented using array, linked list, or List interface.

ArrayStackI Implement using an array.I Adding is O(1) except for reallocate().

LinkedStackI Private Node class.I node.next instead of node.getNext()I Push and pop at front (head) of list.

ListStackI Use Java List interface.I Use add(item), size(), get(index), remove(index).I ArrayList implementation uses partially filled array.I LinkedList is another implementation of List using a doubly linked list.

SummaryStack

I The StackInt interface describes a Stack.

I Only adding or removing at the top is possible.I Operations called push, pop, peek, empty.I Implemented using array, linked list, or List interface.

ArrayStackI Implement using an array.I Adding is O(1) except for reallocate().

LinkedStackI Private Node class.I node.next instead of node.getNext()I Push and pop at front (head) of list.

ListStackI Use Java List interface.I Use add(item), size(), get(index), remove(index).I ArrayList implementation uses partially filled array.I LinkedList is another implementation of List using a doubly linked list.

SummaryStack

I The StackInt interface describes a Stack.I Only adding or removing at the top is possible.

I Operations called push, pop, peek, empty.I Implemented using array, linked list, or List interface.

ArrayStackI Implement using an array.I Adding is O(1) except for reallocate().

LinkedStackI Private Node class.I node.next instead of node.getNext()I Push and pop at front (head) of list.

ListStackI Use Java List interface.I Use add(item), size(), get(index), remove(index).I ArrayList implementation uses partially filled array.I LinkedList is another implementation of List using a doubly linked list.

SummaryStack

I The StackInt interface describes a Stack.I Only adding or removing at the top is possible.I Operations called push, pop, peek, empty.

I Implemented using array, linked list, or List interface.

ArrayStackI Implement using an array.I Adding is O(1) except for reallocate().

LinkedStackI Private Node class.I node.next instead of node.getNext()I Push and pop at front (head) of list.

ListStackI Use Java List interface.I Use add(item), size(), get(index), remove(index).I ArrayList implementation uses partially filled array.I LinkedList is another implementation of List using a doubly linked list.

SummaryStack

I The StackInt interface describes a Stack.I Only adding or removing at the top is possible.I Operations called push, pop, peek, empty.I Implemented using array, linked list, or List interface.

ArrayStackI Implement using an array.I Adding is O(1) except for reallocate().

LinkedStackI Private Node class.I node.next instead of node.getNext()I Push and pop at front (head) of list.

ListStackI Use Java List interface.I Use add(item), size(), get(index), remove(index).I ArrayList implementation uses partially filled array.I LinkedList is another implementation of List using a doubly linked list.

SummaryStack

I The StackInt interface describes a Stack.I Only adding or removing at the top is possible.I Operations called push, pop, peek, empty.I Implemented using array, linked list, or List interface.

ArrayStack

I Implement using an array.I Adding is O(1) except for reallocate().

LinkedStackI Private Node class.I node.next instead of node.getNext()I Push and pop at front (head) of list.

ListStackI Use Java List interface.I Use add(item), size(), get(index), remove(index).I ArrayList implementation uses partially filled array.I LinkedList is another implementation of List using a doubly linked list.

SummaryStack

I The StackInt interface describes a Stack.I Only adding or removing at the top is possible.I Operations called push, pop, peek, empty.I Implemented using array, linked list, or List interface.

ArrayStackI Implement using an array.

I Adding is O(1) except for reallocate().

LinkedStackI Private Node class.I node.next instead of node.getNext()I Push and pop at front (head) of list.

ListStackI Use Java List interface.I Use add(item), size(), get(index), remove(index).I ArrayList implementation uses partially filled array.I LinkedList is another implementation of List using a doubly linked list.

SummaryStack

I The StackInt interface describes a Stack.I Only adding or removing at the top is possible.I Operations called push, pop, peek, empty.I Implemented using array, linked list, or List interface.

ArrayStackI Implement using an array.I Adding is O(1) except for reallocate().

LinkedStackI Private Node class.I node.next instead of node.getNext()I Push and pop at front (head) of list.

ListStackI Use Java List interface.I Use add(item), size(), get(index), remove(index).I ArrayList implementation uses partially filled array.I LinkedList is another implementation of List using a doubly linked list.

SummaryStack

I The StackInt interface describes a Stack.I Only adding or removing at the top is possible.I Operations called push, pop, peek, empty.I Implemented using array, linked list, or List interface.

ArrayStackI Implement using an array.I Adding is O(1) except for reallocate().

LinkedStack

I Private Node class.I node.next instead of node.getNext()I Push and pop at front (head) of list.

ListStackI Use Java List interface.I Use add(item), size(), get(index), remove(index).I ArrayList implementation uses partially filled array.I LinkedList is another implementation of List using a doubly linked list.

SummaryStack

I The StackInt interface describes a Stack.I Only adding or removing at the top is possible.I Operations called push, pop, peek, empty.I Implemented using array, linked list, or List interface.

ArrayStackI Implement using an array.I Adding is O(1) except for reallocate().

LinkedStackI Private Node class.

I node.next instead of node.getNext()I Push and pop at front (head) of list.

ListStackI Use Java List interface.I Use add(item), size(), get(index), remove(index).I ArrayList implementation uses partially filled array.I LinkedList is another implementation of List using a doubly linked list.

SummaryStack

I The StackInt interface describes a Stack.I Only adding or removing at the top is possible.I Operations called push, pop, peek, empty.I Implemented using array, linked list, or List interface.

ArrayStackI Implement using an array.I Adding is O(1) except for reallocate().

LinkedStackI Private Node class.I node.next instead of node.getNext()

I Push and pop at front (head) of list.

ListStackI Use Java List interface.I Use add(item), size(), get(index), remove(index).I ArrayList implementation uses partially filled array.I LinkedList is another implementation of List using a doubly linked list.

SummaryStack

I The StackInt interface describes a Stack.I Only adding or removing at the top is possible.I Operations called push, pop, peek, empty.I Implemented using array, linked list, or List interface.

ArrayStackI Implement using an array.I Adding is O(1) except for reallocate().

LinkedStackI Private Node class.I node.next instead of node.getNext()I Push and pop at front (head) of list.

ListStackI Use Java List interface.I Use add(item), size(), get(index), remove(index).I ArrayList implementation uses partially filled array.I LinkedList is another implementation of List using a doubly linked list.

SummaryStack

I The StackInt interface describes a Stack.I Only adding or removing at the top is possible.I Operations called push, pop, peek, empty.I Implemented using array, linked list, or List interface.

ArrayStackI Implement using an array.I Adding is O(1) except for reallocate().

LinkedStackI Private Node class.I node.next instead of node.getNext()I Push and pop at front (head) of list.

ListStack

I Use Java List interface.I Use add(item), size(), get(index), remove(index).I ArrayList implementation uses partially filled array.I LinkedList is another implementation of List using a doubly linked list.

SummaryStack

I The StackInt interface describes a Stack.I Only adding or removing at the top is possible.I Operations called push, pop, peek, empty.I Implemented using array, linked list, or List interface.

ArrayStackI Implement using an array.I Adding is O(1) except for reallocate().

LinkedStackI Private Node class.I node.next instead of node.getNext()I Push and pop at front (head) of list.

ListStackI Use Java List interface.

I Use add(item), size(), get(index), remove(index).I ArrayList implementation uses partially filled array.I LinkedList is another implementation of List using a doubly linked list.

SummaryStack

I The StackInt interface describes a Stack.I Only adding or removing at the top is possible.I Operations called push, pop, peek, empty.I Implemented using array, linked list, or List interface.

ArrayStackI Implement using an array.I Adding is O(1) except for reallocate().

LinkedStackI Private Node class.I node.next instead of node.getNext()I Push and pop at front (head) of list.

ListStackI Use Java List interface.I Use add(item), size(), get(index), remove(index).

I ArrayList implementation uses partially filled array.I LinkedList is another implementation of List using a doubly linked list.

SummaryStack

I The StackInt interface describes a Stack.I Only adding or removing at the top is possible.I Operations called push, pop, peek, empty.I Implemented using array, linked list, or List interface.

ArrayStackI Implement using an array.I Adding is O(1) except for reallocate().

LinkedStackI Private Node class.I node.next instead of node.getNext()I Push and pop at front (head) of list.

ListStackI Use Java List interface.I Use add(item), size(), get(index), remove(index).I ArrayList implementation uses partially filled array.

I LinkedList is another implementation of List using a doubly linked list.

SummaryStack

I The StackInt interface describes a Stack.I Only adding or removing at the top is possible.I Operations called push, pop, peek, empty.I Implemented using array, linked list, or List interface.

ArrayStackI Implement using an array.I Adding is O(1) except for reallocate().

LinkedStackI Private Node class.I node.next instead of node.getNext()I Push and pop at front (head) of list.

ListStackI Use Java List interface.I Use add(item), size(), get(index), remove(index).I ArrayList implementation uses partially filled array.I LinkedList is another implementation of List using a doubly linked list.