Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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

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

  • How is a node removed from the queue?
  • What is the time complexity of adding and removing nodes from the queue?
  • How is a node added to the queue?

Typology: Exercises

2019/2020

Uploaded on 02/21/2020

dan-jo
dan-jo 🇺🇸

4 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

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