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; } } }

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 15SA
icon
Related questions
Question

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;

}

}

}

Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Concept of Threads
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning