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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education