Question: Implement Queue using Linked List A queue works in a first - in - first - out fashion ( see sections 3 . 1 5
Implement Queue using Linked ListA queue works in a firstinfirstout fashion see sections and for details You will implement the following methods using Linked List methods made available to you:
public void enqueuewhich adds an element at the end of the queue.
public int dequeuewhich removes the first element from the queue and returns it
public int peekwhich returns the first element without removing it from the queue.
Sample InputOutput
should output the following:
Queue elements:
Peek returns:
After popping twice:
public class Node
int data;
Node next;
public Nodeint 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 appendint data
ifhead null
Node newNode new Nodedata;
head newNode;
else
Node newNode new Nodedata;
Node currentNode head;
whilecurrentNodenext null
currentNode currentNode.next;
currentNode.next newNode;
inserts data to the beginning of the list
public void prependint data
ifhead null
Node newNode new Nodedata;
head newNode;
return;
Node newNode new Nodedata;
newNode.next head;
head newNode;
insert a new data after the given one
public void insertAfterint givenData, int newData
ifhead null
Node newNode new NodenewData;
head newNode;
return;
else
Node currentNode head;
whilecurrentNodenext null
ifcurrentNodedata givenData
Node newNode new NodenewData;
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
ifisEmpty
System.out.printlnEmpty;
Node currentNode head;
whilecurrentNode null
System.out.printfd currentNode.data;
currentNode currentNode.next;
public Node removeFirst
Node first head;
head head.next;
return first;
public void removeLast
ifhead null
return;
Node current head;
whilecurrentnext.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 enqueueint data
Complete this method
Removes the front element from the queue
public void dequeue
Complete this method
returns the front element of the queue but does not remove it
public int peek
Complete this method
public void print
queueList.print;
public static void mainString args
QueueList queue new QueueList;
Scanner input new ScannerSystemin;
while true
int data input.nextInt;
if data
break;
queue.enqueuedata;
printing queue elements
System.out.printlnQueue elements:";
queue.print;
printing the value peek returns
int peekitem queue.peek;
System.out.printfnPeek returns: dn peekitem;
printing queue elements after pop
queue.dequeue;
queue.dequeue;
System.out.println
After popping twice:";
queue.print;
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
