Question: In java implement the following methods as indicated within the code of QueueList.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 QueueList.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;
}
}
public Node removeFirst(){
Node first = head;
head = head.next;
return first;
}
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 QueueList {
LinkedListBasic queueList = new LinkedListBasic();
// Adds new elements to the end of the queue
public void push(int data) {
queueList.push(10);// Complete this method
}
// Removes the front element from the queue
public void pop() {
queueList.pop(10);// Complete this method
}
// returns the front element of the queue but does not remove it.
public int peek() {
queueList.peek();// Complete this method
}
public void print() {
queueList.print();
}
public static void main(String[] args) {
QueueList queue = new QueueList();
Scanner input = new Scanner(System.in);
while (true) {
int data = input.nextInt();
if (data == -1)
break;
queue.push(data);
}
// printing queue elements
System.out.println("Queue elements:");
queue.print();
// printing queue elements after peek
queue.peek();
System.out.println(" After peeking:");
queue.print();
// printing queue elements after pop
queue.pop();
queue.pop();
System.out.println(" After popping twice:");
queue.print();
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
