Java Stacks -Write a generic collection for a Stack using Node UML for Stack Sta
ID: 3695983 • Letter: J
Question
Java
Stacks
-Write a generic collection for a Stack using Node
UML for Stack
Stack
-top : Node
-numElements : int
+Stack( )
+push( element : E ) : void
+pop( ) : E
+size( ) : int
private fields:
- top which points to a Node
- numElements an int
a no-arg Constructor +Stack( ) :
- sets top to null
- numElements to 0
push(receives element of type E)
- adds a new node to top of stack
- adds 1 to numElements
pop( ) returns a value of type E
- removes node from top of stack
- decrements numElements by 1
- <<unless stack="" is="" empty="" then="" return="" null="" and="" don't="" decrement="" numelements="">>
size( ) returns an int
- returns value of numElements
Explanation / Answer
Driver.java
public class Driver{
public static void main(String[]args){
String s;
Queue n = new Queue();
System.out.println(n);
n.nQ("first");
System.out.println(n);
n.nQ("second");
System.out.println(n);
n.nQ("third");
System.out.println(n);
s = n.dQ();
System.out.println(s + " | " + n);
s = n.dQ();
System.out.println(s + " | " + n);
s = n.dQ();
System.out.println(s + " | " + n);
s = n.dQ();
System.out.println(s + " | " + n);
}
}
Node.java
public class Node{
private String data;
private Node next;
public Node(String s){
data = s;
next = null;
}
public void setData(String s){
data = s;
}
public String getData(){
return data;
}
public void setNext(Node n){
next = n;
}
public Node getNext(){
return next;
}
public String toString(){
return ""+ data;
}
}
Queue.java
public class Queue{
private Node head;
private Node tail;
private int length;
public Queue(){
head = null;
tail = null;
length = 0;
}
public void nQ(String s){
Node n = new Node(s);
if (length == 0){
head = n;
tail = n;
}
else {
tail.setNext(n);
tail = n;
}
length++;
}
public String dQ(){
String s;
if (head == null){
return null;
}
s = head.getData();
head = head.getNext();
length--;
if (head == null){
tail = null;
}
return s;
}
public String front(){
return head.getData();
}
public int size(){
return length;
}
public String toString(){
String s = "Head --> ";
if (head == null){
s = s + "null -->";
}
Node temp = head;
while (temp != null){
s = s + temp.getData() + " --> ";
temp = temp.getNext();
}
s = s.substring(0, s.length() - 4);
s = s + " <-- tail";
return s;
}
}
arrayQueue.java
public class arrayQueue{
private String[10] q;
private String head,tail;
private int size;
public arrayQueue(){
head = "";
tail = "";
}
public void nQ(String s){
if (size == 0){
q[size] = s;
head = s;
tail = s;
}
if (size < 10 && size > 0){
q[size + 1] = s;
tail = q[size + 1];
}
else{
return;
}
}
public String dQ(){
String s;
s = head;
for (int i = 0; i < size; i++){
q[i] = q[i + 1];
}
return s;
}
public String front(){
return head;
}
}
sample output
Head --> null <-- tail
Head --> first <-- tail
Head --> first --> second <-- tail
Head --> first --> second --> third <-- tail
first | Head --> second --> third <-- tail
second | Head --> third <-- tail
third | Head --> null <-- tail
null | Head --> null <-- tail
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.