Download 1.00 Tutorial 9 Data Structures: Stacks

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Transcript
1.00 Tutorial 9
Stacks and Queues
November 8-9, 2004
Data Structures: Stacks
• Objects are inserted into and removed from a
stack in last-in first-out (LIFO) order.
• Examples:
– a can of tennis balls: the last ball inserted into the
can is the first one taken out.
– a “stack” of books: the last book placed on the pile
is the first one to be removed from the book.
• Standard methods:
public interface Stack {
public boolean isEmpty();
public void push(Object o);
public Object pop();
public void clear();
}
1
Stack Methods
• void push(Object item)
– “Pushes” an item onto the stack
– The item can be any Object type—so the Stack can hold
anything but primitive types (int, double, boolean…)
• Object pop()
– Pops the top item off the stack and returns it. If empty,
returns null (or throws an exception)
– Note that this returns a generic Object
• Why? How do we get it back to the original data type?
• void clear()
– Empties the stack
• boolean isEmpty()
– Returns true if the stack is empty, and false if otherwise
Source files ArrayStack1.java and Stack.java
Stack Operations
2
Queues
• Objects are inserted into and removed from a
stack in first-in first-out (FIFO) order.
• Examples:
– people waiting in the lunch line
– cars waiting to pass a the toll booth
• Standard methods:
public interface Queue
{
public boolean isEmpty();
public void add(Object o); // or “enqueue”
public Object remove(); // or “dequeue”
public void clear();
}
Queue Methods
• void add(Object item) // or enqueue
– Enqueues an item
– Again, the item can be of any Object type
• Object remove() // or dequeue
– Removes (“dequeues”) the first (“frontmost”) item of the
queue, and returns it to the caller
– If the Queue is empty, returns null (or throws an exception)
– Returns a generic Object; must cast to original type
• void clear()
– Empties the queue
• boolean isEmpty()
– Returns true if the Queue is empty; otherwise false
Source files ArrayQueue1.java and Queue.java
3
Queue Operations
Queues in Action!
enqueue
enqueue
dequeue
dequeue
cones in a
randomly-ordered
queue
sugar
cones
waffle
cones
plain
cones
Your mission: Remove cones from the
random queue and put each one into the
correct cone-type queue.
4
Complete the classes
// Cone.java
public class Cone
{
public static final int SUGAR=1;
public static final int PLAIN=2;
public static final int WAFFLE=3;
private int coneType;
public Cone(int type){coneType=type;}
public static void sortConeQueue(Queue original,
Queue sugar, Queue plain, Queue waffle)
{
// your code here
}
}
// ConeTest.java
public static void main(String[] args)
{
/* create the four queues, fill the source
queue with random cone types, and sort it */
}
ConeTest main(…)
public static void main(String args[])
{
Queue source = new Queue();
// Add some cones in random order.
source.enqueue(new Cone(Cone.SUGAR));
source.enqueue(new Cone(Cone.WAFFLE));
source.enqueue(new Cone(Cone.PLAIN));
source.enqueue(new Cone(Cone.PLAIN)); // ...and so on
Queue s = new Queue();
Queue w = new Queue();
Queue p = new Queue();
Cone.sortConeQueue(source,s,w,p);
System.out.println(“Finished sorting”);
System.exit(0);
}
5
Cone.sortConeQueue(…)
public static void sortConeQueue(Queue original,
Queue sugar, Queue plain, Queue waffle)
{
while(!original.isEmpty())
{
Cone c=(Cone)original.dequeue();
if(c.coneType==SUGAR){sugar.enqueue(c);}
else if(c.coneType==PLAIN){plain.enqueue(c);}
else if(c.coneType==WAFFLE){waffle.enqueue(c);}
else{System.out.println(“Invalid cone type!”);}
}
}
Stacks in Action!
cone queue
pop
push
sugar
cones
waffle
cones
plain
cones
dequeue
Your mission: Remove from random queue
and put into correct cone-type stack
6
Cone.sortConeQueue(…)
public static void sortConeQueue(Queue original, Stack sugar,
Stack plain, Stack waffle)
{
while(!original.isEmpty())
{
Cone c=(Cone)original.dequeue();
if(c.coneType==SUGAR){sugar.push(c);}
else if(c.coneType==PLAIN){plain.push(c);}
else if(c.coneType==WAFFLE){waffle.push(c);}
else{System.out.println(“Invalid cone type!”);}
}
}
ConeTest2 main(…)
public static void main(String args[])
{
// Add some cones in random order.
source.enqueue(new Cone(Cone.SUGAR));
source.enqueue(new Cone(Cone.WAFFLE));
source.enqueue(new Cone(Cone.PLAIN));
source.enqueue(new Cone(Cone.PLAIN)); // ...and so on
Stack s = new Stack();
Stack w = new Stack();
Stack p = new Stack();
Cone.sortConeQueue(source,s,w,p);
System.out.println(“Finished sorting”);
System.exit(0);
}
7
Data Structures
• Up until now, we’ve stored our data in arrays
and arraylists, which are good multi-purpose
data structures.
• But more specialized situations are better
modeled by more specialized data structures.
– linked-lists: used for sparse matrices and
datasets, often-reordered data, and easily
comparable and sortable data.
– queues: used to model first-come, first-served
situations
– stacks: used to model first-come, last-served
situations
Problem Set 7 – Moving Average
Signal Filter
• Very similar to Problem Set 6 – plotting the ballistic trajectory.
In this case, we plot the original and the filtered signal instead.
• If the signal is measured at discrete points in time (rather than
continuously), the value of filtered signal, y[n], at time n, is
given by
y[n] = ½ x[n] + ½ x[n-1]
where x[n] denotes the value of original signal at time n. In the
special case where n = 0 (the first measurement), we assume
y[n] = x[0].
• For example, with the original signal as {2, 20, 4, 6, 2}, the
filtered signal by the moving average filter is {2, 11, 12, 5, 4}.
8
PS 7 – Contd.
PS 7-Contd.
•
5 source files SignalQueue.java, SignalFilter.java, SignalPanel.java,
SignalFrame.java and ProblemSet7.java.
•
SignalQueue.java will model a Queue data structure using an instance of class
ArrayList. Remember that instances of this class should store only up to 20
elements and values between 0.0 and 100.0.
SignalFilter.java implements the Moving Average Signal filter method.
SignalPanel.java creates the panel with the custom drawing of both the signal queue
and the filtered queue (use drawLine, drawString and fillOval similar to PS 6).
SignalFrame.java renders the panel.
•
•
•
•
•
•
Upon clicking the ‘Add Data’ button, your application should:
- Take the input value from the JTextField at the lower left of the window
- Add it to the instance of class FilterQueue
- Plot the up-to-date signal
In addition, the ‘Filter Data’ button must:
- Process the signal with moving average filter
- Plot the filtered signal
Finally, the ‘Clear Data’ button should bring the application back to its initial state.
9