Netbeans Java 1 Write a Stack class This class should use y
Netbeans - Java
1- Write a Stack class. This class should use your LinkedList class. Your stack should hold integers. It must contain the following methods:
Stack() – Default constructor
push(valueToAdd) – Adds a new element to the top of the
stack pop() – Removes the element from the top of the stack and returns its value.
getHeight() – Returns the height of the stack.
display() – Displays the stack and its contents.
It should display the top element first. See output below for an example.
2- Write a Queue class. This class should use your LinkedList class.
Your queue should hold integers. It must contain the following methods:
Queue() – Default constructor
enqueue(valueToAdd) – Adds a new element to back of the queue
dequeue() – Removes the element from the front of the queue and returns its value.
getLength() – Returns the length of the
queue. display() – Displays the queue and its contents.
It should display the first element in the queue first. See output below for an example.
I already have the linked list that is this one:
This linked list implementation should be in its own class named LinkedList. Your linked list should hold integers. It must contain the following methods:
– Default constructor
– Creates a new node and adds it to the front of the linked list.
– Removes the front node from the linked list and returns the value it contained. – Creates a new node and adds it to the back of the linked list.
–Removes the last node from the linked list and returns the value it contained. – Returns the length of the linked list
– Displays the linked list and its contents.
############ LinkedList.java ############
class NodeList {
int data;
NodeList next;
public NodeList(int data) {
this.data = data;
next = null;
}
}
public class LinkedList {
private NodeList head;
private int size;
public LinkedList() {
head = null;
size = 0;
}
public void addFront(int data) {
NodeList newNode = new NodeList(data);
newNode.next = head;
head = newNode;
size = size+1;
}
public int removeFront(){
if(head == null){
System.out.println(\"List is empty\");
return -999;
}
int value = head.data;
head = head.next;
size = size-1;
return value;
}
public void addBack(int value){
if(head == null){
head = new NodeList(value);
}else{
NodeList temp = head;
while(temp.next != null)
temp = temp.next;
temp.next = new NodeList(value);
}
size = size+1;
}
public int removeBack(){
// list is empty
if(head == null){
System.out.println(\"List is empty\");
return -999;
}
// only one node
if(head.next == null){
int value = head.data;
head = null;
return value;
}
NodeList temp = head;
while(temp.next.next != null)
temp = temp.next;
int value = temp.next.data;
temp.next = null;
size = size-1;
return value;
}
public int getLength(){
return size;
}
public void display(){
NodeList temp = head;
while(temp != null){
if(temp.next!=null)
System.out.print(temp.data+\"-->\");
else
System.out.println(temp.data);
temp = temp.next;
}
System.out.println();
}
}
############# LinkedListTest.java ################
public class LinkedListTest {
public static void main(String[] args) {
LinkedList list = new LinkedList();
System.out.println(\"Adding elements to the front......\");
list.addFront(8);
list.display();
list.addFront(33);
list.display();
list.addFront(58);
list.display();
System.out.println(\"Length of linked list: \"+list.getLength());
System.out.println();
System.out.println(\"Adding elements to the back......\");
list.addBack(1);
list.display();
list.addBack(34);
list.display();
list.addBack(20);
list.display();
System.out.println(\"Length of linked list: \"+list.getLength());
System.out.println();
System.out.println(\"Removing elements from the front.....\");
list.removeFront();
list.display();
list.removeFront();
list.display();
System.out.println(\"Length of linked list: \"+list.getLength());
System.out.println();
System.out.println(\"Removing elements from the back.....\");
list.removeBack();
list.display();
list.removeBack();
list.display();
System.out.println(\"Length of linked list: \"+list.getLength());
System.out.println();
}
}
/*
Sample run:
Adding elements to the front......
8
33-->8
58-->33-->8
Length of linked list: 3
Adding elements to the back......
58-->33-->8-->1
58-->33-->8-->1-->34
58-->33-->8-->1-->34-->20
Length of linked list: 6
Removing elements from the front.....
33-->8-->1-->34-->20
8-->1-->34-->20
Length of linked list: 4
Removing elements from the back.....
8-->1-->34
8-->1
Length of linked list: 2
*/
Linked List Adding elements to the front. 33 8 58 >33 8 Length of linked list: 3 Adding elements to the back. 58 >33 58 >33- 8-- 1-- 34 58 >33 Length of linked list: 6 Removing elements from the front... 33 8- 1 -->34 ->20 Length of linked list: 4 Removing elements from the back. >34 >1 >1 Length of linked list: 2 Stack Pushing elements 33 8 58- 33- 8 Height of stack: 3 Popping elements Popped: 58 33 >8 Popped: 33 Popped: 8 Queue 17 >17 102-- 17- 2 Length of queue: 3 De queued: 2 102- 17 De queued: 17 Dequeued: 102Solution
Hi, I have implemented Stack.
Please repost about Queue impleementation.
############ Stack.java ########
public class Stack {
// private attribut=tes
private LinkedList top;
public Stack(){
top = new LinkedList();
}
public void push(int valueToAdd){
top.addFront(valueToAdd);
}
public int pop(){
return top.removeFront();
}
public int getHeight(){
return top.getLength();
}
public void display(){
top.display();
}
}
############# StackTest.java #############
public class StackTest {
public static void main(String[] args) {
Stack stack = new Stack();
System.out.println(\"Stack\");
System.out.println(\"Pushing elements...\");
stack.push(8);
stack.display();
stack.push(33);
stack.display();
stack.push(58);
stack.display();
System.out.println(\"Height of stack: \"+stack.getHeight());
System.out.println();
System.out.println(\"Popping elements...\");
System.out.println(\"Popped: \"+stack.pop());
stack.display();
System.out.println(\"Popped: \"+stack.pop());
stack.display();
}
}
/*
Sample run:
Stack
Pushing elements...
8
33-->8
58-->33-->8
Height of stack: 3
Popping elements...
Popped: 58
33-->8
Popped: 33
8
*/







