Create a class Stack. This stack will be implemented using the LinkedList class that has been provided. This stack will hold values of a generic type (). Your Stack should have the following public methods: public void push(int n) public T pop() public T peek() public T size() public boolean isEmpty() public class LinkedList { private Node head; private Node tail; private int size; public LinkedList() { head = null; tail = null; size = 0; } public void append(T data) { Node newNode = new Node(data); if (head == null) { head = newNode; } else { tail.next = newNode; } tail = newNode; size++; } public void prepend(T data) { Node newNode = new Node(data); if (head == null) { head = newNode; tail = newNode; } else { newNode.next = head; head = newNode; } size++; } public T getHead() { return head.data; } public T getTail() { return tail.data; } public int size() { return size; } public void removeByValue(T data) { Node current = head; while (current.next!= null && current.next.data!= data) { current = current.next; } if (current.next!= null) { current.next = current.next.next; size--; if (current.next == null) { tail = current; } } } public T removeHead() { T removedHead = head.data; head = head.next; size--; if (head == null) { tail = null; } return removedHead; } public T removeTail() { T removedTail = tail.data; if (head == tail) { head = null; tail = null; } else { Node current = head; while (current.next!= tail) { current = current.next; } tail = current; tail.next = null; } size--; return removedTail; } @Override public String toString() { StringBuilder builder = new StringBuilder(); Node current = head; while (current!= null) { builder.append(current.data); if (current.next!= null) { builder.append(" -> "); } current = current.next; } return builder.toString(); } private class Node { public Node next; public T data; public Node(T data) { this.data = data; } } }
Create a class Stack. This stack will be implemented using the LinkedList class that has been provided. This stack will hold values of a generic type (<T>).
Your Stack should have the following public methods:
public void push(int n)
public T pop()
public T peek()
public T size()
public boolean isEmpty()
public class LinkedList <T> {
private Node head;
private Node tail;
private int size;
public LinkedList() {
head = null;
tail = null;
size = 0;
}
public void append(T data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
} else {
tail.next = newNode;
}
tail = newNode;
size++;
}
public void prepend(T data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
tail = newNode;
} else {
newNode.next = head;
head = newNode;
}
size++;
}
public T getHead() {
return head.data;
}
public T getTail() {
return tail.data;
}
public int size() {
return size;
}
public void removeByValue(T data) {
Node current = head;
while (current.next!= null && current.next.data!= data) {
current = current.next;
}
if (current.next!= null) {
current.next = current.next.next;
size--;
if (current.next == null) {
tail = current;
}
}
}
public T removeHead() {
T removedHead = head.data;
head = head.next;
size--;
if (head == null) {
tail = null;
}
return removedHead;
}
public T removeTail() {
T removedTail = tail.data;
if (head == tail) {
head = null;
tail = null;
} else {
Node current = head;
while (current.next!= tail) {
current = current.next;
}
tail = current;
tail.next = null;
}
size--;
return removedTail;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
Node current = head;
while (current!= null) {
builder.append(current.data);
if (current.next!= null) {
builder.append(" -> ");
}
current = current.next;
}
return builder.toString();
}
private class Node {
public Node next;
public T data;
public Node(T data) {
this.data = data;
}
}
}

Step by step
Solved in 3 steps









