SlideShare a Scribd company logo
1 of 7
Download to read offline
Implement a queue using a linkedlist (java)
Solution
LinkedQueueImplement.java
import java.util.*;
class Node
{
protected int data;
protected Node link;
public Node()
{
link = null;
data = 0;
}
public Node(int d,Node n)
{
data = d;
link = n;
}
public void setLink(Node n)
{
link = n;
}
public void setData(int d)
{
data = d;
}
public Node getLink()
{
return link;
}
public int getData()
{
return data;
}
}
class linkedQueue
{
protected Node front, rear;
public int size;
public linkedQueue()
{
front = null;
rear = null;
size = 0;
}
public boolean isEmpty()
{
return front == null;
}
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');
}
}
Output :
Linked Queue Test
Queue Operations
1. insert
2. remove
3. peek
4. check empty
5. size
1
Enter integer element to insert
10
Queue = 10
Do you want to continue (Type y or n)
y
Queue Operations
1. insert
2. remove
3. peek
4. check empty
5. size
1
Enter integer element to insert
20
Queue = 10 20
Do you want to continue (Type y or n)
y
Queue Operations
1. insert
2. remove
3. peek
4. check empty
5. size
1
Enter integer element to insert
30
Queue = 10 20 30
Do you want to continue (Type y or n)
y
Queue Operations
1. insert
2. remove
3. peek
4. check empty
5. size
5
Size = 3
Queue = 10 20 30
Do you want to continue (Type y or n)
n

More Related Content

Similar to Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf

Exception to indicate that Singly LinkedList is empty. .pdf
  Exception to indicate that Singly LinkedList is empty. .pdf  Exception to indicate that Singly LinkedList is empty. .pdf
Exception to indicate that Singly LinkedList is empty. .pdfaravlitraders2012
 
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdfLabprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdffreddysarabia1
 
33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good TestsTomek Kaczanowski
 
package singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdfpackage singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdfamazing2001
 
OrderTest.javapublic class OrderTest {       Get an arra.pdf
OrderTest.javapublic class OrderTest {         Get an arra.pdfOrderTest.javapublic class OrderTest {         Get an arra.pdf
OrderTest.javapublic class OrderTest {       Get an arra.pdfakkhan101
 
Confitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good TestsConfitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good TestsTomek Kaczanowski
 
GeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good TestsGeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good TestsTomek Kaczanowski
 
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdfHow do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdfmail931892
 
Java programs
Java programsJava programs
Java programsjojeph
 
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.pdfJAVA 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.pdfamrishinda
 
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdfJAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdffantasiatheoutofthef
 
in this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdfin this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdfmichardsonkhaicarr37
 
Basic java, java collection Framework and Date Time API
Basic java, java collection Framework and Date Time APIBasic java, java collection Framework and Date Time API
Basic java, java collection Framework and Date Time APIjagriti srivastava
 
public class TrequeT extends AbstractListT { .pdf
  public class TrequeT extends AbstractListT {  .pdf  public class TrequeT extends AbstractListT {  .pdf
public class TrequeT extends AbstractListT { .pdfinfo30292
 

Similar to Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf (20)

Java practical
Java practicalJava practical
Java practical
 
Exception to indicate that Singly LinkedList is empty. .pdf
  Exception to indicate that Singly LinkedList is empty. .pdf  Exception to indicate that Singly LinkedList is empty. .pdf
Exception to indicate that Singly LinkedList is empty. .pdf
 
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdfLabprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
 
33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests
 
Sam wd programs
Sam wd programsSam wd programs
Sam wd programs
 
package singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdfpackage singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdf
 
5 Rmi Print
5  Rmi Print5  Rmi Print
5 Rmi Print
 
OrderTest.javapublic class OrderTest {       Get an arra.pdf
OrderTest.javapublic class OrderTest {         Get an arra.pdfOrderTest.javapublic class OrderTest {         Get an arra.pdf
OrderTest.javapublic class OrderTest {       Get an arra.pdf
 
Confitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good TestsConfitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good Tests
 
GeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good TestsGeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good Tests
 
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdfHow do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
 
Java programs
Java programsJava programs
Java programs
 
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.pdfJAVA 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
 
Java programs
Java programsJava programs
Java programs
 
Thread
ThreadThread
Thread
 
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdfJAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
 
C# labprograms
C# labprogramsC# labprograms
C# labprograms
 
in this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdfin this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdf
 
Basic java, java collection Framework and Date Time API
Basic java, java collection Framework and Date Time APIBasic java, java collection Framework and Date Time API
Basic java, java collection Framework and Date Time API
 
public class TrequeT extends AbstractListT { .pdf
  public class TrequeT extends AbstractListT {  .pdf  public class TrequeT extends AbstractListT {  .pdf
public class TrequeT extends AbstractListT { .pdf
 

More from kostikjaylonshaewe47

Which of the following organisms are similar to amoeboids in their lo.pdf
Which of the following organisms are similar to amoeboids in their lo.pdfWhich of the following organisms are similar to amoeboids in their lo.pdf
Which of the following organisms are similar to amoeboids in their lo.pdfkostikjaylonshaewe47
 
Which of the following are true More than one answer may be correct.pdf
Which of the following are true More than one answer may be correct.pdfWhich of the following are true More than one answer may be correct.pdf
Which of the following are true More than one answer may be correct.pdfkostikjaylonshaewe47
 
What was the Noble Prize winning advance that saved the magnetic rec.pdf
What was the Noble Prize winning advance that saved the magnetic rec.pdfWhat was the Noble Prize winning advance that saved the magnetic rec.pdf
What was the Noble Prize winning advance that saved the magnetic rec.pdfkostikjaylonshaewe47
 
What is difference between the Tariff and tax SolutionDifferen.pdf
What is difference between the Tariff and tax  SolutionDifferen.pdfWhat is difference between the Tariff and tax  SolutionDifferen.pdf
What is difference between the Tariff and tax SolutionDifferen.pdfkostikjaylonshaewe47
 
What are the most likely point in nutrient media preparation or later.pdf
What are the most likely point in nutrient media preparation or later.pdfWhat are the most likely point in nutrient media preparation or later.pdf
What are the most likely point in nutrient media preparation or later.pdfkostikjaylonshaewe47
 
Toss a fair coin three times. Let X denote the number of heads in fi.pdf
Toss a fair coin three times. Let X denote the number of heads in fi.pdfToss a fair coin three times. Let X denote the number of heads in fi.pdf
Toss a fair coin three times. Let X denote the number of heads in fi.pdfkostikjaylonshaewe47
 
TIA-569 lists six different areas or spaces where cabling is run. Li.pdf
TIA-569 lists six different areas or spaces where cabling is run. Li.pdfTIA-569 lists six different areas or spaces where cabling is run. Li.pdf
TIA-569 lists six different areas or spaces where cabling is run. Li.pdfkostikjaylonshaewe47
 
This is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdfThis is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdfkostikjaylonshaewe47
 
There are two large plants in the authors’ yard a yucca tree and an.pdf
There are two large plants in the authors’ yard a yucca tree and an.pdfThere are two large plants in the authors’ yard a yucca tree and an.pdf
There are two large plants in the authors’ yard a yucca tree and an.pdfkostikjaylonshaewe47
 
The seaport town of New Monopoly has become extremely popular with sh.pdf
The seaport town of New Monopoly has become extremely popular with sh.pdfThe seaport town of New Monopoly has become extremely popular with sh.pdf
The seaport town of New Monopoly has become extremely popular with sh.pdfkostikjaylonshaewe47
 
The figure below shows a series of SNP loci (A-O) loci for two strai.pdf
The figure below shows a series of SNP loci (A-O) loci for two strai.pdfThe figure below shows a series of SNP loci (A-O) loci for two strai.pdf
The figure below shows a series of SNP loci (A-O) loci for two strai.pdfkostikjaylonshaewe47
 
The climatic stability hypothesis for the change in biodiversity with.pdf
The climatic stability hypothesis for the change in biodiversity with.pdfThe climatic stability hypothesis for the change in biodiversity with.pdf
The climatic stability hypothesis for the change in biodiversity with.pdfkostikjaylonshaewe47
 
Table 14.2 Primitive and Advanced Features of Nonvascular Plants as T.pdf
Table 14.2 Primitive and Advanced Features of Nonvascular Plants as T.pdfTable 14.2 Primitive and Advanced Features of Nonvascular Plants as T.pdf
Table 14.2 Primitive and Advanced Features of Nonvascular Plants as T.pdfkostikjaylonshaewe47
 
Solar energy is channeled by molecular vibration from... to the ..pdf
Solar energy is channeled by molecular vibration from... to the ..pdfSolar energy is channeled by molecular vibration from... to the ..pdf
Solar energy is channeled by molecular vibration from... to the ..pdfkostikjaylonshaewe47
 
report “Rabbits and Wolves”Discuss the changes in parameters and how.pdf
report “Rabbits and Wolves”Discuss the changes in parameters and how.pdfreport “Rabbits and Wolves”Discuss the changes in parameters and how.pdf
report “Rabbits and Wolves”Discuss the changes in parameters and how.pdfkostikjaylonshaewe47
 
prove thisSolutionSuppose that A is a product of element.pdf
prove thisSolutionSuppose that A is a product of element.pdfprove thisSolutionSuppose that A is a product of element.pdf
prove thisSolutionSuppose that A is a product of element.pdfkostikjaylonshaewe47
 
Our task this week is to diagnose patients who come in with various .pdf
Our task this week is to diagnose patients who come in with various .pdfOur task this week is to diagnose patients who come in with various .pdf
Our task this week is to diagnose patients who come in with various .pdfkostikjaylonshaewe47
 
number of lines should be included in the last page of the paper aft.pdf
number of lines should be included in the last page of the paper aft.pdfnumber of lines should be included in the last page of the paper aft.pdf
number of lines should be included in the last page of the paper aft.pdfkostikjaylonshaewe47
 
Modify the code below so that two separate listener classes are used .pdf
Modify the code below so that two separate listener classes are used .pdfModify the code below so that two separate listener classes are used .pdf
Modify the code below so that two separate listener classes are used .pdfkostikjaylonshaewe47
 
Lie detector tests are notoriously flawed. Assume that a particular .pdf
Lie detector tests are notoriously flawed.  Assume that a particular .pdfLie detector tests are notoriously flawed.  Assume that a particular .pdf
Lie detector tests are notoriously flawed. Assume that a particular .pdfkostikjaylonshaewe47
 

More from kostikjaylonshaewe47 (20)

Which of the following organisms are similar to amoeboids in their lo.pdf
Which of the following organisms are similar to amoeboids in their lo.pdfWhich of the following organisms are similar to amoeboids in their lo.pdf
Which of the following organisms are similar to amoeboids in their lo.pdf
 
Which of the following are true More than one answer may be correct.pdf
Which of the following are true More than one answer may be correct.pdfWhich of the following are true More than one answer may be correct.pdf
Which of the following are true More than one answer may be correct.pdf
 
What was the Noble Prize winning advance that saved the magnetic rec.pdf
What was the Noble Prize winning advance that saved the magnetic rec.pdfWhat was the Noble Prize winning advance that saved the magnetic rec.pdf
What was the Noble Prize winning advance that saved the magnetic rec.pdf
 
What is difference between the Tariff and tax SolutionDifferen.pdf
What is difference between the Tariff and tax  SolutionDifferen.pdfWhat is difference between the Tariff and tax  SolutionDifferen.pdf
What is difference between the Tariff and tax SolutionDifferen.pdf
 
What are the most likely point in nutrient media preparation or later.pdf
What are the most likely point in nutrient media preparation or later.pdfWhat are the most likely point in nutrient media preparation or later.pdf
What are the most likely point in nutrient media preparation or later.pdf
 
Toss a fair coin three times. Let X denote the number of heads in fi.pdf
Toss a fair coin three times. Let X denote the number of heads in fi.pdfToss a fair coin three times. Let X denote the number of heads in fi.pdf
Toss a fair coin three times. Let X denote the number of heads in fi.pdf
 
TIA-569 lists six different areas or spaces where cabling is run. Li.pdf
TIA-569 lists six different areas or spaces where cabling is run. Li.pdfTIA-569 lists six different areas or spaces where cabling is run. Li.pdf
TIA-569 lists six different areas or spaces where cabling is run. Li.pdf
 
This is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdfThis is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdf
 
There are two large plants in the authors’ yard a yucca tree and an.pdf
There are two large plants in the authors’ yard a yucca tree and an.pdfThere are two large plants in the authors’ yard a yucca tree and an.pdf
There are two large plants in the authors’ yard a yucca tree and an.pdf
 
The seaport town of New Monopoly has become extremely popular with sh.pdf
The seaport town of New Monopoly has become extremely popular with sh.pdfThe seaport town of New Monopoly has become extremely popular with sh.pdf
The seaport town of New Monopoly has become extremely popular with sh.pdf
 
The figure below shows a series of SNP loci (A-O) loci for two strai.pdf
The figure below shows a series of SNP loci (A-O) loci for two strai.pdfThe figure below shows a series of SNP loci (A-O) loci for two strai.pdf
The figure below shows a series of SNP loci (A-O) loci for two strai.pdf
 
The climatic stability hypothesis for the change in biodiversity with.pdf
The climatic stability hypothesis for the change in biodiversity with.pdfThe climatic stability hypothesis for the change in biodiversity with.pdf
The climatic stability hypothesis for the change in biodiversity with.pdf
 
Table 14.2 Primitive and Advanced Features of Nonvascular Plants as T.pdf
Table 14.2 Primitive and Advanced Features of Nonvascular Plants as T.pdfTable 14.2 Primitive and Advanced Features of Nonvascular Plants as T.pdf
Table 14.2 Primitive and Advanced Features of Nonvascular Plants as T.pdf
 
Solar energy is channeled by molecular vibration from... to the ..pdf
Solar energy is channeled by molecular vibration from... to the ..pdfSolar energy is channeled by molecular vibration from... to the ..pdf
Solar energy is channeled by molecular vibration from... to the ..pdf
 
report “Rabbits and Wolves”Discuss the changes in parameters and how.pdf
report “Rabbits and Wolves”Discuss the changes in parameters and how.pdfreport “Rabbits and Wolves”Discuss the changes in parameters and how.pdf
report “Rabbits and Wolves”Discuss the changes in parameters and how.pdf
 
prove thisSolutionSuppose that A is a product of element.pdf
prove thisSolutionSuppose that A is a product of element.pdfprove thisSolutionSuppose that A is a product of element.pdf
prove thisSolutionSuppose that A is a product of element.pdf
 
Our task this week is to diagnose patients who come in with various .pdf
Our task this week is to diagnose patients who come in with various .pdfOur task this week is to diagnose patients who come in with various .pdf
Our task this week is to diagnose patients who come in with various .pdf
 
number of lines should be included in the last page of the paper aft.pdf
number of lines should be included in the last page of the paper aft.pdfnumber of lines should be included in the last page of the paper aft.pdf
number of lines should be included in the last page of the paper aft.pdf
 
Modify the code below so that two separate listener classes are used .pdf
Modify the code below so that two separate listener classes are used .pdfModify the code below so that two separate listener classes are used .pdf
Modify the code below so that two separate listener classes are used .pdf
 
Lie detector tests are notoriously flawed. Assume that a particular .pdf
Lie detector tests are notoriously flawed.  Assume that a particular .pdfLie detector tests are notoriously flawed.  Assume that a particular .pdf
Lie detector tests are notoriously flawed. Assume that a particular .pdf
 

Recently uploaded

Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 

Recently uploaded (20)

Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 

Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf

  • 1. Implement a queue using a linkedlist (java) Solution LinkedQueueImplement.java import java.util.*; class Node { protected int data; protected Node link; public Node() { link = null; data = 0; } public Node(int d,Node n) { data = d; link = n; } public void setLink(Node n) { link = n; } public void setData(int d) { data = d; } public Node getLink() { return link; } public int getData() { return data;
  • 2. } } class linkedQueue { protected Node front, rear; public int size; public linkedQueue() { front = null; rear = null; size = 0; } public boolean isEmpty() { return front == null; } 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++ ; }
  • 3. /* 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(); }
  • 4. } /* 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()); }
  • 5. 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'); } } Output : Linked Queue Test Queue Operations 1. insert 2. remove 3. peek 4. check empty
  • 6. 5. size 1 Enter integer element to insert 10 Queue = 10 Do you want to continue (Type y or n) y Queue Operations 1. insert 2. remove 3. peek 4. check empty 5. size 1 Enter integer element to insert 20 Queue = 10 20 Do you want to continue (Type y or n) y Queue Operations 1. insert 2. remove 3. peek 4. check empty 5. size 1 Enter integer element to insert 30 Queue = 10 20 30 Do you want to continue (Type y or n) y Queue Operations 1. insert 2. remove 3. peek 4. check empty
  • 7. 5. size 5 Size = 3 Queue = 10 20 30 Do you want to continue (Type y or n) n