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 lastinfirstout fashion see the sections and 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 InputOutput
should output the following:
Stack elements:
Peek returns:
After popping twice:
StackList.java
import java.util.Scanner;
public class StackList LinkedListBasic stackList new LinkedListBasic; Adds new elements to the top of the stackpublic void pushint 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 itpublic int peek Complete this methodreturn ;public void printstackListprint;public static void mainString argsStackList newStackList new StackList;Scanner input new ScannerSystemin;whiletrueint data input.nextInt;ifdata break;newStackListpushdata; printing stack elementsSystemout.printlnStack elements:";newStackListprint;
printing the output of peekint peekdata newStackList.peek;Systemout.printf
Peek returns: dn peekdata;printing stack elements after popnewStackListpop;newStackListpop;Systemout.println
After popping twice:";newStackListprint;
Node.java
public class Nodeint data;Node next;public Nodeint datathisdata 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 appendint dataifhead nullNode newNode new Nodedata;head newNode;
else Node newNode new Nodedata;Node currentNode head;whilecurrentNodenext nullcurrentNode currentNode.next;currentNodenext newNode; inserts data to the beginning of the listpublic void prependint dataifhead nullNode newNode new Nodedata;head newNode;return;Node newNode new Nodedata;newNodenext head;head newNode; insert a new data after the given onepublic void insertAfterint givenData, int newDataifhead nullNode newNode new NodenewData;head newNode;return; elseNode currentNode head;whilecurrentNodenext nullifcurrentNodedata givenDataNode newNode new NodenewData;newNodenext currentNode.next;currentNodenext newNode; check if the list is emptypublic boolean isEmptyreturn head null; prints the listpublic void printifisEmptySystemout.printlnEmpty;Node currentNode head;whilecurrentNode nullSystemout.printfd currentNode.data;currentNode currentNode.next;
Removes the first elementpublic Node removeFirstNode first head;head head.next;return first; Removes the last elementpublic void removeLastifhead nullreturn;Node current head;whilecurrentnext.next nullcurrent current.next;currentnext null;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
