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');
}
}
Ad

Recommended

Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
kostikjaylonshaewe47
 
JAVA A double-ended queue is a list that allows the addition and.pdf
JAVA A double-ended queue is a list that allows the addition and.pdf
amrishinda
 
Description (Part A) In this lab you will write a Queue implementati.pdf
Description (Part A) In this lab you will write a Queue implementati.pdf
rishabjain5053
 
Queue Data Structure
Queue Data Structure
Zidny Nafan
 
Queue Data Structure
Queue Data Structure
Sriram Raj
 
Linked stack-and-linked-queue
Linked stack-and-linked-queue
soniasharmafdp
 
stack and queue array implementation, java.
stack and queue array implementation, java.
CIIT Atd.
 
stack and queue array implementation in java.
stack and queue array implementation in java.
CIIT Atd.
 
Data Structures Lab 8.pptx
Data Structures Lab 8.pptx
mervat32
 
A linked stack is implemented using a standard Node class as follows.pdf
A linked stack is implemented using a standard Node class as follows.pdf
kisgstin23
 
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
RAtna29
 
Queues in C++ detailed explanation and examples .ppt
Queues in C++ detailed explanation and examples .ppt
Jamiluddin39
 
05 queues
05 queues
Rajan Gautam
 
[10 points]Add the.pdf
[10 points]Add the.pdf
PRATIKSINHA7304
 
queueDATA STRUCTURES AND ITS OPERATIONS IMPLEMETED WITH EXAMPLES
queueDATA STRUCTURES AND ITS OPERATIONS IMPLEMETED WITH EXAMPLES
KusumaS36
 
chapter10-queue-161018120329.pdf
chapter10-queue-161018120329.pdf
ssuserff72e4
 
Bsc cs ii dfs u-2 linklist,stack,queue
Bsc cs ii dfs u-2 linklist,stack,queue
Rai University
 
package algs13;import stdlib.;import java.util.Iterator;im.docx
package algs13;import stdlib.;import java.util.Iterator;im.docx
gerardkortney
 
Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queue
Rai University
 
Data Structure (Queue)
Data Structure (Queue)
Adam Mukharil Bachtiar
 
Bca ii dfs u-2 linklist,stack,queue
Bca ii dfs u-2 linklist,stack,queue
Rai University
 
Data Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptx
RashidFaridChishti
 
Queue
Queue
Swarup Kumar Boro
 
Ds stack & queue
Ds stack & queue
Sunipa Bera
 
Data Structures and Algorithms-DSA_Linkedlist_class 7.pdf
Data Structures and Algorithms-DSA_Linkedlist_class 7.pdf
Chethan Raddi
 
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
VictorXUQGloverl
 
"Odisha's Living Legacy: Culture & Art".
"Odisha's Living Legacy: Culture & Art".
1983puspanjali
 
Queue Data Structure
Queue Data Structure
Poulami Das Akuli
 
SKIPPED hich level of biological orga.pdf
SKIPPED hich level of biological orga.pdf
siennatimbok52331
 
please explain transcription and translationSolutionAnsTran.pdf
please explain transcription and translationSolutionAnsTran.pdf
siennatimbok52331
 

More Related Content

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

Data Structures Lab 8.pptx
Data Structures Lab 8.pptx
mervat32
 
A linked stack is implemented using a standard Node class as follows.pdf
A linked stack is implemented using a standard Node class as follows.pdf
kisgstin23
 
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
RAtna29
 
Queues in C++ detailed explanation and examples .ppt
Queues in C++ detailed explanation and examples .ppt
Jamiluddin39
 
05 queues
05 queues
Rajan Gautam
 
[10 points]Add the.pdf
[10 points]Add the.pdf
PRATIKSINHA7304
 
queueDATA STRUCTURES AND ITS OPERATIONS IMPLEMETED WITH EXAMPLES
queueDATA STRUCTURES AND ITS OPERATIONS IMPLEMETED WITH EXAMPLES
KusumaS36
 
chapter10-queue-161018120329.pdf
chapter10-queue-161018120329.pdf
ssuserff72e4
 
Bsc cs ii dfs u-2 linklist,stack,queue
Bsc cs ii dfs u-2 linklist,stack,queue
Rai University
 
package algs13;import stdlib.;import java.util.Iterator;im.docx
package algs13;import stdlib.;import java.util.Iterator;im.docx
gerardkortney
 
Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queue
Rai University
 
Data Structure (Queue)
Data Structure (Queue)
Adam Mukharil Bachtiar
 
Bca ii dfs u-2 linklist,stack,queue
Bca ii dfs u-2 linklist,stack,queue
Rai University
 
Data Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptx
RashidFaridChishti
 
Queue
Queue
Swarup Kumar Boro
 
Ds stack & queue
Ds stack & queue
Sunipa Bera
 
Data Structures and Algorithms-DSA_Linkedlist_class 7.pdf
Data Structures and Algorithms-DSA_Linkedlist_class 7.pdf
Chethan Raddi
 
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
VictorXUQGloverl
 
"Odisha's Living Legacy: Culture & Art".
"Odisha's Living Legacy: Culture & Art".
1983puspanjali
 
Queue Data Structure
Queue Data Structure
Poulami Das Akuli
 
Data Structures Lab 8.pptx
Data Structures Lab 8.pptx
mervat32
 
A linked stack is implemented using a standard Node class as follows.pdf
A linked stack is implemented using a standard Node class as follows.pdf
kisgstin23
 
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
RAtna29
 
Queues in C++ detailed explanation and examples .ppt
Queues in C++ detailed explanation and examples .ppt
Jamiluddin39
 
queueDATA STRUCTURES AND ITS OPERATIONS IMPLEMETED WITH EXAMPLES
queueDATA STRUCTURES AND ITS OPERATIONS IMPLEMETED WITH EXAMPLES
KusumaS36
 
chapter10-queue-161018120329.pdf
chapter10-queue-161018120329.pdf
ssuserff72e4
 
Bsc cs ii dfs u-2 linklist,stack,queue
Bsc cs ii dfs u-2 linklist,stack,queue
Rai University
 
package algs13;import stdlib.;import java.util.Iterator;im.docx
package algs13;import stdlib.;import java.util.Iterator;im.docx
gerardkortney
 
Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queue
Rai University
 
Bca ii dfs u-2 linklist,stack,queue
Bca ii dfs u-2 linklist,stack,queue
Rai University
 
Data Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptx
RashidFaridChishti
 
Ds stack & queue
Ds stack & queue
Sunipa Bera
 
Data Structures and Algorithms-DSA_Linkedlist_class 7.pdf
Data Structures and Algorithms-DSA_Linkedlist_class 7.pdf
Chethan Raddi
 
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
VictorXUQGloverl
 
"Odisha's Living Legacy: Culture & Art".
"Odisha's Living Legacy: Culture & Art".
1983puspanjali
 

More from siennatimbok52331 (20)

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

Recently uploaded (20)

Publishing Your Memoir with Brooke Warner
Publishing Your Memoir with Brooke Warner
Brooke Warner
 
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
Arshad Shaikh
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
2025 June Year 9 Presentation: Subject selection.pptx
2025 June Year 9 Presentation: Subject selection.pptx
mansk2
 
Measuring, learning and applying multiplication facts.
Measuring, learning and applying multiplication facts.
cgilmore6
 
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
Quiz Club of PSG College of Arts & Science
 
How to Manage Multi Language for Invoice in Odoo 18
How to Manage Multi Language for Invoice in Odoo 18
Celine George
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
Chalukyas of Gujrat, Solanki Dynasty NEP.pptx
Chalukyas of Gujrat, Solanki Dynasty NEP.pptx
Dr. Ravi Shankar Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
FIRST DAY HIGH orientation for mapeh subject in grade 10.pptx
FIRST DAY HIGH orientation for mapeh subject in grade 10.pptx
GlysdiEelesor1
 
Revista digital preescolar en transformación
Revista digital preescolar en transformación
guerragallardo26
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
Assisting Individuals and Families to Promote and Maintain Health – Unit 7 | ...
Assisting Individuals and Families to Promote and Maintain Health – Unit 7 | ...
RAKESH SAJJAN
 
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Restu Bias Primandhika
 
“THE BEST CLASS IN SCHOOL”. _
“THE BEST CLASS IN SCHOOL”. _
Colégio Santa Teresinha
 
How to Implement Least Package Removal Strategy in Odoo 18 Inventory
How to Implement Least Package Removal Strategy in Odoo 18 Inventory
Celine George
 
Publishing Your Memoir with Brooke Warner
Publishing Your Memoir with Brooke Warner
Brooke Warner
 
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
Arshad Shaikh
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
2025 June Year 9 Presentation: Subject selection.pptx
2025 June Year 9 Presentation: Subject selection.pptx
mansk2
 
Measuring, learning and applying multiplication facts.
Measuring, learning and applying multiplication facts.
cgilmore6
 
How to Manage Multi Language for Invoice in Odoo 18
How to Manage Multi Language for Invoice in Odoo 18
Celine George
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
FIRST DAY HIGH orientation for mapeh subject in grade 10.pptx
FIRST DAY HIGH orientation for mapeh subject in grade 10.pptx
GlysdiEelesor1
 
Revista digital preescolar en transformación
Revista digital preescolar en transformación
guerragallardo26
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
Assisting Individuals and Families to Promote and Maintain Health – Unit 7 | ...
Assisting Individuals and Families to Promote and Maintain Health – Unit 7 | ...
RAKESH SAJJAN
 
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Restu Bias Primandhika
 
How to Implement Least Package Removal Strategy in Odoo 18 Inventory
How to Implement Least Package Removal Strategy in Odoo 18 Inventory
Celine George
 
Ad

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'); } }