SlideShare a Scribd company logo
Using NetBeans
Implement a queue named QueueLL using a Linked List (same as we did for the stack). This
implementation must be used in all the following problems.
Implement a queue QueueST using a stack (use StackLL).
Test your implementations in the main with examples.
Solution
Answer:-
import java.util.*;
/* Class Node */
class Node
{
protected int data;
protected Node link;
/* Constructor */
public Node()
{
link = null;
data = 0;
}
/* Constructor */
public Node(int d,Node n)
{
data = d;
link = n;
}
/* Function to set link to next Node */
public void setLink(Node n)
{
link = n;
}
/* Function to set data to current Node */
public void setData(int d)
{
data = d;
}
/* Function to get link to next node */
public Node getLink()
{
return link;
}
/* Function to get data from current Node */
public int getData()
{
return data;
}
}
/* Class linkedQueue */
class linkedQueue
{
protected Node front, rear;
public int size;
/* Constructor */
public linkedQueue()
{
front = null;
rear = null;
size = 0;
}
/* Function to check if queue is empty */
public boolean isEmpty()
{
return front == null;
}
/* Function to get the size of the queue */
public int getSize()
{
return size;
}
/* Function to insert an element to the queue */
public void insert(int data)
{
Node nptr = new Node(data, null);
if (rear == null)
{
front = nptr;
rear = nptr;
}
else
{
rear.setLink(nptr);
rear = rear.getLink();
}
size++ ;
}
/* Function to remove front element from the queue */
public int remove()
{
if (isEmpty() )
throw new NoSuchElementException("Underflow Exception");
Node ptr = front;
front = ptr.getLink();
if (front == null)
rear = null;
size-- ;
return ptr.getData();
}
/* Function to check the front element of the queue */
public int peek()
{
if (isEmpty() )
throw new NoSuchElementException("Underflow Exception");
return front.getData();
}
/* Function to display the status of the queue */
public void display()
{
System.out.print(" Queue = ");
if (size == 0)
{
System.out.print("Empty ");
return ;
}
Node ptr = front;
while (ptr != rear.getLink() )
{
System.out.print(ptr.getData()+" ");
ptr = ptr.getLink();
}
System.out.println();
}
}
/* Class LinkedQueueImplement */
public class LinkedQueueImplement
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
/* Creating object of class linkedQueue */
linkedQueue lq = new linkedQueue();
/* Perform Queue Operations */
System.out.println("Linked Queue Test ");
char ch;
do
{
System.out.println(" Queue Operations");
System.out.println("1. insert");
System.out.println("2. remove");
System.out.println("3. peek");
System.out.println("4. check empty");
System.out.println("5. size");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter integer element to insert");
lq.insert( scan.nextInt() );
break;
case 2 :
try
{
System.out.println("Removed Element = "+ lq.remove());
}
catch (Exception e)
{
System.out.println("Error : " + e.getMessage());
}
break;
case 3 :
try
{
System.out.println("Peek Element = "+ lq.peek());
}
catch (Exception e)
{
System.out.println("Error : " + e.getMessage());
}
break;
case 4 :
System.out.println("Empty status = "+ lq.isEmpty());
break;
case 5 :
System.out.println("Size = "+ lq.getSize());
break;
default :
System.out.println("Wrong Entry  ");
break;
}
/* display queue */
lq.display();
System.out.println(" Do you want to continue (Type y or n)  ");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
}

More Related Content

PDF
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
PDF
JAVA A double-ended queue is a list that allows the addition and.pdf
PDF
Description (Part A) In this lab you will write a Queue implementati.pdf
PPT
Queue Data Structure
PPT
Queue Data Structure
PDF
Linked stack-and-linked-queue
PPT
stack and queue array implementation, java.
PPT
stack and queue array implementation in java.
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
JAVA A double-ended queue is a list that allows the addition and.pdf
Description (Part A) In this lab you will write a Queue implementati.pdf
Queue Data Structure
Queue Data Structure
Linked stack-and-linked-queue
stack and queue array implementation, java.
stack and queue array implementation in java.

Similar to Using NetBeansImplement a queue named QueueLL using a Linked List .pdf (20)

PPTX
Data Structures Lab 8.pptx
PDF
A linked stack is implemented using a standard Node class as follows.pdf
PPT
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
PPT
Queues in C++ detailed explanation and examples .ppt
PDF
05 queues
PDF
[10 points]Add the.pdf
PPT
queueDATA STRUCTURES AND ITS OPERATIONS IMPLEMETED WITH EXAMPLES
PDF
chapter10-queue-161018120329.pdf
PPTX
Bsc cs ii dfs u-2 linklist,stack,queue
DOCX
package algs13;import stdlib.;import java.util.Iterator;im.docx
PPTX
Mca ii dfs u-3 linklist,stack,queue
PPSX
Data Structure (Queue)
PPTX
Bca ii dfs u-2 linklist,stack,queue
PPTX
Data Structures and Agorithm: DS 09 Queue.pptx
PPTX
Ds stack & queue
PDF
Data Structures and Algorithms-DSA_Linkedlist_class 7.pdf
DOCX
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
PDF
"Odisha's Living Legacy: Culture & Art".
PPTX
Queue Data Structure
Data Structures Lab 8.pptx
A linked stack is implemented using a standard Node class as follows.pdf
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
Queues in C++ detailed explanation and examples .ppt
05 queues
[10 points]Add the.pdf
queueDATA STRUCTURES AND ITS OPERATIONS IMPLEMETED WITH EXAMPLES
chapter10-queue-161018120329.pdf
Bsc cs ii dfs u-2 linklist,stack,queue
package algs13;import stdlib.;import java.util.Iterator;im.docx
Mca ii dfs u-3 linklist,stack,queue
Data Structure (Queue)
Bca ii dfs u-2 linklist,stack,queue
Data Structures and Agorithm: DS 09 Queue.pptx
Ds stack & queue
Data Structures and Algorithms-DSA_Linkedlist_class 7.pdf
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
"Odisha's Living Legacy: Culture & Art".
Queue Data Structure
Ad

More from siennatimbok52331 (20)

PDF
SKIPPED hich level of biological orga.pdf
PDF
please explain transcription and translationSolutionAnsTran.pdf
PDF
Please I want a detailed complete answers for each part.Then make.pdf
PDF
Please answer all of #5 After the completion of S phase, F.2F functi.pdf
PDF
Consider the security of a computer belonging to a criminal organiza.pdf
PDF
Match the bones in column A with the characteristics in column B. Pla.pdf
PDF
Is the mean age at which American children learn to walk less than 15.pdf
PDF
Introduction to material science What kind of analyses methods will .pdf
PDF
Is this a Cohort Study What would be an example of a Cohort study.pdf
PDF
Function of medium veins Function of medium veinsSolution.pdf
PDF
Explain the four factors of production. In what way are they rela.pdf
PDF
Discrete Math problem37 students use a variety of forms of transp.pdf
PDF
Discuss the involvement of interest groups in the political circumst.pdf
PDF
Determine which of these statements describing the cytochrome P450 f.pdf
PDF
Describe the difference between the lytic cycle and lysogeny when bac.pdf
PDF
Describe some of the different ways members of Congress can represen.pdf
PDF
Company B Company C None of them satisfy the requirements. Question9 .pdf
PDF
Case 2S [symptoms] 10 month male presents to pediatrician’s offic.pdf
PDF
A Correlation of zero means that (what)SolutionA value of zer.pdf
PDF
A bank had 5024 depositers, with an avevrage account of $512. Assume.pdf
SKIPPED hich level of biological orga.pdf
please explain transcription and translationSolutionAnsTran.pdf
Please I want a detailed complete answers for each part.Then make.pdf
Please answer all of #5 After the completion of S phase, F.2F functi.pdf
Consider the security of a computer belonging to a criminal organiza.pdf
Match the bones in column A with the characteristics in column B. Pla.pdf
Is the mean age at which American children learn to walk less than 15.pdf
Introduction to material science What kind of analyses methods will .pdf
Is this a Cohort Study What would be an example of a Cohort study.pdf
Function of medium veins Function of medium veinsSolution.pdf
Explain the four factors of production. In what way are they rela.pdf
Discrete Math problem37 students use a variety of forms of transp.pdf
Discuss the involvement of interest groups in the political circumst.pdf
Determine which of these statements describing the cytochrome P450 f.pdf
Describe the difference between the lytic cycle and lysogeny when bac.pdf
Describe some of the different ways members of Congress can represen.pdf
Company B Company C None of them satisfy the requirements. Question9 .pdf
Case 2S [symptoms] 10 month male presents to pediatrician’s offic.pdf
A Correlation of zero means that (what)SolutionA value of zer.pdf
A bank had 5024 depositers, with an avevrage account of $512. Assume.pdf
Ad

Recently uploaded (20)

PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
Trump Administration's workforce development strategy
PPTX
master seminar digital applications in india
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Lesson notes of climatology university.
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Microbial diseases, their pathogenesis and prophylaxis
2.FourierTransform-ShortQuestionswithAnswers.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
O5-L3 Freight Transport Ops (International) V1.pdf
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Trump Administration's workforce development strategy
master seminar digital applications in india
Final Presentation General Medicine 03-08-2024.pptx
Final Presentation General Medicine 03-08-2024.pptx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Pharma ospi slides which help in ospi learning
Lesson notes of climatology university.
Anesthesia in Laparoscopic Surgery in India
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
human mycosis Human fungal infections are called human mycosis..pptx
Supply Chain Operations Speaking Notes -ICLT Program
FourierSeries-QuestionsWithAnswers(Part-A).pdf

Using NetBeansImplement a queue named QueueLL using a Linked List .pdf

  • 1. Using NetBeans Implement a queue named QueueLL using a Linked List (same as we did for the stack). This implementation must be used in all the following problems. Implement a queue QueueST using a stack (use StackLL). Test your implementations in the main with examples. Solution Answer:- import java.util.*; /* Class Node */ class Node { protected int data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(int d,Node n) { data = d; link = n; } /* Function to set link to next Node */ public void setLink(Node n) { link = n; } /* Function to set data to current Node */
  • 2. public void setData(int d) { data = d; } /* Function to get link to next node */ public Node getLink() { return link; } /* Function to get data from current Node */ public int getData() { return data; } } /* Class linkedQueue */ class linkedQueue { protected Node front, rear; public int size; /* Constructor */ public linkedQueue() { front = null; rear = null; size = 0; } /* Function to check if queue is empty */ public boolean isEmpty() { return front == null; } /* Function to get the size of the queue */ public int getSize()
  • 3. { return size; } /* Function to insert an element to the queue */ public void insert(int data) { Node nptr = new Node(data, null); if (rear == null) { front = nptr; rear = nptr; } else { rear.setLink(nptr); rear = rear.getLink(); } size++ ; } /* Function to remove front element from the queue */ public int remove() { if (isEmpty() ) throw new NoSuchElementException("Underflow Exception"); Node ptr = front; front = ptr.getLink(); if (front == null) rear = null; size-- ; return ptr.getData(); } /* Function to check the front element of the queue */ public int peek() { if (isEmpty() ) throw new NoSuchElementException("Underflow Exception");
  • 4. return front.getData(); } /* Function to display the status of the queue */ public void display() { System.out.print(" Queue = "); if (size == 0) { System.out.print("Empty "); return ; } Node ptr = front; while (ptr != rear.getLink() ) { System.out.print(ptr.getData()+" "); ptr = ptr.getLink(); } System.out.println(); } } /* Class LinkedQueueImplement */ public class LinkedQueueImplement { public static void main(String[] args) { Scanner scan = new Scanner(System.in); /* Creating object of class linkedQueue */ linkedQueue lq = new linkedQueue(); /* Perform Queue Operations */ System.out.println("Linked Queue Test "); char ch; do { System.out.println(" Queue Operations"); System.out.println("1. insert");
  • 5. System.out.println("2. remove"); System.out.println("3. peek"); System.out.println("4. check empty"); System.out.println("5. size"); int choice = scan.nextInt(); switch (choice) { case 1 : System.out.println("Enter integer element to insert"); lq.insert( scan.nextInt() ); break; case 2 : try { System.out.println("Removed Element = "+ lq.remove()); } catch (Exception e) { System.out.println("Error : " + e.getMessage()); } break; case 3 : try { System.out.println("Peek Element = "+ lq.peek()); } catch (Exception e) { System.out.println("Error : " + e.getMessage()); } break; case 4 : System.out.println("Empty status = "+ lq.isEmpty()); break; case 5 :
  • 6. System.out.println("Size = "+ lq.getSize()); break; default : System.out.println("Wrong Entry "); break; } /* display queue */ lq.display(); System.out.println(" Do you want to continue (Type y or n) "); ch = scan.next().charAt(0); } while (ch == 'Y'|| ch == 'y'); } }