Docsity

Implementation of a Singly Linked List Queue in Java, Exercises of Computer Science

Toggle sidebar
bg1
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;
pf2

Partial preview of the text

Download Implementation of a Singly Linked List Queue in Java and more Exercises Computer Science in PDF only on Docsity!

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 } }