Question: Implement Stack using Linked List Stacks works in last - in - first - out fashion ( see the sections 3 . 1 3 and

Implement Stack using Linked List Stacks works in last-in-first-out fashion (see the sections 3.13 and 3.14 for details). You will implement the following methods using Linked List methods made available to you:
public void push() which pushes an element to the stack.
public int pop() which removes the top element from the stack and returns it.
public int peek() which returns the top element without removing from the stack.
------------------------------------------
Sample Input/Output
4981253616-1
should output the following:
Stack elements:
1636258149
Peek returns: 16
After popping twice:
258149
StackList.java
import java.util.Scanner;
public class StackList {LinkedListBasic stackList = new LinkedListBasic();// Adds new elements to the top of the stackpublic void push(int data){// Complete this method}// Removes the top element from the stackpublic int pop(){// Complete this method}
// returns the top element of the stack but does not remove it.public int peek(){// Complete this methodreturn -1;}public void print(){stackList.print();}public static void main(String[] args){StackList newStackList = new StackList();Scanner input = new Scanner(System.in);while(true){int data = input.nextInt();if(data ==-1) break;newStackList.push(data);}// printing stack elementsSystem.out.println("Stack elements:");newStackList.print();
// printing the output of peekint peek_data = newStackList.peek();System.out.printf("
Peek returns: %d%n", peek_data);//printing stack elements after popnewStackList.pop();newStackList.pop();System.out.println("
After popping twice:");newStackList.print();}
}
----------------------------------------
Node.java
public class Node{int data;Node next;public Node(int data){this.data = data;next = null;}}
---------------------------------------
LinkedListBasic.java
import java.util.Scanner;
public class LinkedListBasic {Node head;Node tail;// inserts data to the end of the list only using the head pointerpublic void append(int data){if(head == null){Node newNode = new Node(data);head = newNode;
} else {Node newNode = new Node(data);Node currentNode = head;while(currentNode.next != null){currentNode = currentNode.next;}currentNode.next = newNode;}}// inserts data to the beginning of the listpublic void prepend(int data){if(head == null){Node newNode = new Node(data);head = newNode;return;}Node newNode = new Node(data);newNode.next = head;head = newNode;}// insert a new data after the given onepublic void insertAfter(int givenData, int newData){if(head == null){Node newNode = new Node(newData);head = newNode;return;} else{Node currentNode = head;while(currentNode.next != null){if(currentNode.data == givenData){Node newNode = new Node(newData);newNode.next = currentNode.next;currentNode.next = newNode;}}}}// check if the list is emptypublic boolean isEmpty(){return (head == null);}// prints the listpublic void print(){if(isEmpty()){System.out.println("Empty.");}Node currentNode = head;while(currentNode != null){System.out.printf("%d ", currentNode.data);currentNode = currentNode.next;}}
// Removes the first elementpublic Node removeFirst(){Node first = head;head = head.next;return first;}// Removes the last elementpublic void removeLast(){if(head == null){return;}Node current = head;while(current.next.next != null){current = current.next;}current.next = null;}}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!