Question
In java implement the following methods as indicated within the code of stacklist.java public class Node{ int data; Node next; public Node(int data){ this.data =
In java implement the following methods as indicated within the code of stacklist.java
public class Node{
int data;
Node next;
public Node(int data){
this.data = data;
next = null;
}
}
import java.util.Scanner;
public class LinkedListBasic {
Node head;
Node tail;
// inserts data to the end of the list only using the head pointer
public 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 list
public 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 one
public 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 empty
public boolean isEmpty(){
return (head == null);
}
// prints the list
public 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 element
public Node removeFirst(){
Node first = head;
head = head.next;
return first;
}
// Removes the last element
public void removeLast(){
if(head == null){
return;
}
Node current = head;
while(current.next.next != null){
current = current.next;
}
current.next = null;
}
}
import java.util.Scanner;
public class StackList {
LinkedListBasic stackList = new LinkedListBasic();
// Adds new elements to the top of the stack
public void push(int data){
Node head = null;
if(head == null){
Node newNode = new Node(data);
head = newNode;
return;// Complete this method
}
}
// Removes the top element from the stack
public Node pop(){
Node head = null;
Node first = head;
head = head.next;
return first;// Complete this method
}
// returns the top element of the stack but does not remove it.
public int peek(){
int head = 0;
return head; // Complete this method
}
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 elements
System.out.println("Stack elements:");
newStackList.print();
// printing stack elements after peek
newStackList.peek();
System.out.println(" After peeking:");
newStackList.print();
//printing stack elements after pop
newStackList.pop();
newStackList.pop();
System.out.println(" After popping twice:");
newStackList.print();
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started