Difference between revisions of "Java - Dynamic Stack Assignment"
From WLCS
(Created page with "== Objectives == * You will implement a dynamically-sized queue using linked Nodes * You will implement queue methods (enqueue/add, dequeue/remove, etc.) * You will test your...") |
|||
Line 22: | Line 22: | ||
=== Dynamic Stack class === | === Dynamic Stack class === | ||
+ | # Create a new project named DynamicStackProject | ||
# Create a new class called DynamicStack | # Create a new class called DynamicStack | ||
+ | # Create a new class called Node | ||
+ | #* Implement the code for the Node class using [Media:Node.java] as a guide | ||
+ | #* You may want to add a specific constructor that takes in data as a parameter | ||
==== Attributes ==== | ==== Attributes ==== | ||
* What attribute must we keep track of when we talk about stacks? (Hint: rhymes with "mop") | * What attribute must we keep track of when we talk about stacks? (Hint: rhymes with "mop") | ||
* Create a Node reference for the most important stack attribute | * Create a Node reference for the most important stack attribute | ||
+ | ** Do not forget to initialize it to '''null''' | ||
==== Methods ==== | ==== Methods ==== | ||
− | * | + | * '''void push(int data)''' |
− | * | + | *# push() should not return anything |
− | * | + | *# push() creates a new Node with the data parameter |
− | * | + | *# assign the new Node's next reference to the '''top''' (so that the new Node is linked to the current top Node) |
− | * | + | *# update the '''top''' to reference the new Node! |
− | ** | + | |
− | * | + | * '''int pop()''' |
− | + | *# pop() returns '''null''' if the stack is empty | |
− | * | + | *# pop() removes the value on top of the stack and returns it |
− | ** | + | *# update the '''top''' so that it references its next Node (you need to update top before you return the data) |
+ | |||
+ | * '''boolean isEmpty()''' | ||
+ | *# return true if the stack is empty, and false otherwise | ||
+ | *# Hint: What does '''top''' reference when the stack is empty? | ||
+ | |||
+ | * '''void print()''' | ||
+ | *# print your entire stack from top down (to null) | ||
+ | *# Use | ||
+ | |||
+ | |||
** TEST YOUR STACK | ** TEST YOUR STACK | ||
*** Be sure to push A LOT of data to test the dynamic size | *** Be sure to push A LOT of data to test the dynamic size | ||
*** Also test popping A LOT of data to make sure it works too | *** Also test popping A LOT of data to make sure it works too |
Revision as of 16:02, 9 November 2016
Contents
Objectives
- You will implement a dynamically-sized queue using linked Nodes
- You will implement queue methods (enqueue/add, dequeue/remove, etc.)
- You will test your dynamic queue in a main() method
Resources
Directions
Before & After Visualizations
- Take out a pencil and paper or a dry-erase board and marker
- Load the Dynamic Stack Visualization
- Consider each of the following questions and use the visualization tool to help you answer them. Practice drawing each of the visualizations yourself.
- What does an empty stack look like? (The top reference variable is null)
- For each of the following actions, assess what the picture looks like Before? then After? What changes occurred to make the Before image become the After image?
- push(4)
- push(2)
- pop()
- pop()
Dynamic Stack class
- Create a new project named DynamicStackProject
- Create a new class called DynamicStack
- Create a new class called Node
- Implement the code for the Node class using [Media:Node.java] as a guide
- You may want to add a specific constructor that takes in data as a parameter
Attributes
- What attribute must we keep track of when we talk about stacks? (Hint: rhymes with "mop")
- Create a Node reference for the most important stack attribute
- Do not forget to initialize it to null
Methods
- void push(int data)
- push() should not return anything
- push() creates a new Node with the data parameter
- assign the new Node's next reference to the top (so that the new Node is linked to the current top Node)
- update the top to reference the new Node!
- int pop()
- pop() returns null if the stack is empty
- pop() removes the value on top of the stack and returns it
- update the top so that it references its next Node (you need to update top before you return the data)
- boolean isEmpty()
- return true if the stack is empty, and false otherwise
- Hint: What does top reference when the stack is empty?
- void print()
- print your entire stack from top down (to null)
- Use
- TEST YOUR STACK
- Be sure to push A LOT of data to test the dynamic size
- Also test popping A LOT of data to make sure it works too
- TEST YOUR STACK