add public method size() to the class Stack so the output will be  pop is: 4 peek is: 8 size is: 2 false   public class Stack {    private LinkedList list;      public Stack() {        list = new LinkedList<>();    }      public boolean isEmpty() {        return list.isEmpty();    }      public void push(T data) {        list.prepend(data);    }      public T pop() {        if (isEmpty()) {            throw new EmptyStackException();        }        return list.removeHead();    }      public T peek() {        if (isEmpty()) {            throw new EmptyStackException();        }        return list.getHead();    } }   public class Main {     public static void main(String[] args) {         Stack stack = new Stack<>();         stack.push(6);         stack.push(8);         stack.push(4);         System.out.println("pop is: " + stack.pop());         System.out.println("peek is: " + stack.peek());         System.out.println("size is: " + stack.size());         System.out.println(stack.isEmpty());     }  }   public class LinkedList {     private Node head;     private Node tail;     private int size;       public LinkedList() {         head = null;         tail = null;         size = 0;     }       public boolean isEmpty() {         return 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

add public method size() to the class Stack so the output will be 

pop is: 4

peek is: 8

size is: 2

false

 

public class Stack<T> {

   private LinkedList<T> list;

 

   public Stack() {

       list = new LinkedList<>();

   }

 

   public boolean isEmpty() {

       return list.isEmpty();

   }

 

   public void push(T data) {

       list.prepend(data);

   }

 

   public T pop() {

       if (isEmpty()) {

           throw new EmptyStackException();

       }

       return list.removeHead();

   }

 

   public T peek() {

       if (isEmpty()) {

           throw new EmptyStackException();

       }

       return list.getHead();

   }

}

 

public class Main {

    public static void main(String[] args) {

        Stack<Integer> stack = new Stack<>();

        stack.push(6);

        stack.push(8);

        stack.push(4);

        System.out.println("pop is: " + stack.pop());

        System.out.println("peek is: " + stack.peek());

        System.out.println("size is: " + stack.size());

        System.out.println(stack.isEmpty());

    }

 }

 

public class LinkedList <T> {

    private Node head;

    private Node tail;

    private int size;

 

    public LinkedList() {

        head = null;

        tail = null;

        size = 0;

    }

 

    public boolean isEmpty() {

        return 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 4 steps with 2 images

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