

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
The implementation of a singly linked list queue in java. The code includes a main method for testing the functionality of the queue, an squeue class for the queue itself, and a node class for the nodes in the queue. The squeue class has methods for enqueue (insertion at the end) and dequeue (deletion from the front).
What you will learn
Typology: Exercises
1 / 2
This page cannot be seen from the preview
Don't miss anything!
public static void main(String[] args) { // write your code here SQueue s= new SQueue(); Scanner input = new Scanner(System. in ); int lines = input.nextInt(); for (int i= 0 ;i< lines;i++) { char operation_type = input.next().charAt( 0 ); if (operation_type=='E'){ int school = input.nextInt(); int id= input.nextInt(); s.enQueue(school,id); } else{ Node d = s.deQueue(); if(d!=null) System. out .println(d.school+" "+d.id); public class SQueue { Node front; // reference to front of queue Node rear; // reference to back of queue public SQueue() { //initialize your front and rear front = null; rear = null; } public Node deQueue() //deletion, front { //implement the deQueue function, please keep in mind, it should return the deleted node Node temp = front; if (front == null) { rear = null; } else { front = front.next; temp.next = null; } return temp; } public void enQueue(int s, int id) //tail insertion { // implement the enQueue function, based on the behaviour we described. Node node = new Node(s, id); Node last = front; node.next=null; //Node temp = rear.next; //pointer //isempty if (isEmpty()) { node.prev=null; //rear = node; front = node; } else { while(last != null) { if(node.school == last.school){ node.next = last.next; last.next = node; node.prev=last; if(node.next != null) node.next.prev = node; break; } last = last.next; } if(last == null){ last = front; while(last.next != null) last = last.next; last.next = node; node.prev = last; } } }// other utility functions go here public boolean isEmpty(){ if (front==null && rear==null) return true;
else return false; public class Node { int school; int id; Node next; Node prev; // comment out if you just want to use a singly linked list public Node(int school, int id) { this.school=school; this.id=id; next=null; prev=null; // comment out if you just want to use a singly linked list } }