Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

1. Discuss the four components of language.

Answered: 1 week ago

Question

2. How do they influence my actions?

Answered: 1 week ago