SlideShare a Scribd company logo
1 of 11
Download to read offline
Write a JAVA LinkedListRec class that has the following methods: size, empty, insertBefore,
insertAfter, addAtHead, addAtEnd, remove, replace, peekFront, peekEnd, removeFront,
removeEnd, toString. Use recursion to implement most of these methods. Write a driver to test
your implemented LinkedListRec class methods.
Solution
import java.util.Scanner;
public class LinkedListRec {
int data;
LinkedListRec prev=null,next=null;
int size(LinkedListRec l)//method which returns the length of the list
{
if(l==null)return 0;
return 1+size(l.next);
}
boolean empty(LinkedListRec l)//method checks whether the list is empty or not...
{
if(l==null)return true;
else return false;
}
LinkedListRec insertBefore(LinkedListRec l,int value,int num_insertbefore)//method which
adds a new number before the given number..
{
if(l==null)return l;
if(l.data == num_insertbefore)
{
LinkedListRec n = new LinkedListRec();
n.data = value;
if(l.prev==null)
{
n.next = l;
l=n;
}
else
{
n.prev = l.prev;
n.next= l;
l.prev = n;
}
return l;
}
return insertBefore(l.next,value,num_insertbefore);
}
LinkedListRec insertAfter(LinkedListRec l,int value,int num_insertafter)//method which adds
the new number after the given number
{
if(l==null)return l;
if(l.data == num_insertafter)
{
LinkedListRec n = new LinkedListRec();
n.data = value;
if(l.next==null)
{
l.next = n;
n.prev = l;
}
else
{
n.next = l.next;
l.next.prev = n;
n.prev = l;
}
return l;
}
return insertAfter(l.next,value,num_insertafter);
}
LinkedListRec addAtHead(LinkedListRec l,int value)//method which adds a new number at
the head position of the list...
{
LinkedListRec n = new LinkedListRec();
n.data=value;
n.next = l;
l=n;
return l;
}
LinkedListRec addAtEnd(LinkedListRec l,int value)//method which adds a new number at
the end position of the list...
{
if(l==null){ l = new LinkedListRec();l.data = value;return l;}
if(l.next==null)
{
LinkedListRec n = new LinkedListRec();
n.data=value;
n.prev = l;
l.next = n;
return l;
}
return addAtEnd(l.next,value);
}
LinkedListRec remove(LinkedListRec l,int value)//method which removes given numbet
from the list....
{
if(l==null)return l;
if(l.data == value)
{
if(l.prev!=null){
l.prev = l.next;
}
else
l=l.next;
return l;
}
return remove(l.next,value);
}
boolean replace(LinkedListRec l,int value,int newvalue)//method which replaces current
number with new number,...
{
if(l==null)return false;
if(l.data == value)
{
l.data = newvalue;
return true;
}
return replace(l.next,value,newvalue);
}
int peekFront(LinkedListRec l)//method which returns the first value of the list...
{
if(l!=null)
return l.data;
else return -1;
}
int peekEnd(LinkedListRec l)//method which returns the last value of the list........
{
if(l==null)return -1;
if(l.next == null)return l.data;
return peekEnd(l.next);
}
LinkedListRec removeFront(LinkedListRec l)//method which removes the first element of the
list..
{
if(l==null)return l;
l=l.next;
return l;
}
boolean removeEnd(LinkedListRec l)//method which removes the last element of the list//
{
if(l.next==null)
{
if(l.prev!=null){l.prev.next=null;return true;}
l.data = -1;
return true;
}
return removeEnd(l.next);
}
String tostring(LinkedListRec l)//displaying list
{
if(l==null)return "";
System.out.print(l.data+"->");
return l.data+"->"+tostring(l.next);
}
public static void main(String argv[])
{
//driver testing code...
LinkedListRec ll = new LinkedListRec();
ll.data = 2;
Scanner sc = new Scanner(System.in);
int c=-2;
while(true)
{
System.out.println("Select on option(-1 to exit) 1: size of list 2: is list empty 3: insert
Before a number 4:insert After a number 5: add At head 6: add at End 7: remove from list 8:
Replace a value 9: peekfrontvalue 10:peekendvalue 11:removefrontvalue 12:removeendvalue
13:display");
System.out.print("Enter ur choice");
c = sc.nextInt();
if(c==-1)break;
if(c==1){
System.out.println(" Size of List:"+ll.size(ll));
}
else if(c==2){
if(ll.empty(ll))
System.out.println(" List is Empty ");
else System.out.println(" List is not Empty ");
}
else if(c==3){
int v,p;
System.out.println(" Enter number to insert:");
v = sc.nextInt();
System.out.println(" Enter number to insertBefore it:");
p = sc.nextInt();
ll=ll.insertBefore(ll, v, p);
}
else if(c==4){
int v,p;
System.out.println(" Enter number to insert:");
v = sc.nextInt();
System.out.println(" Enter number to insertAfter it:");
p = sc.nextInt();
ll.insertAfter(ll, v, p);
}
else if(c==5){
int v;
System.out.println(" Enter number to insert:");
v = sc.nextInt();
ll= ll.addAtHead(ll, v);
}
else if(c==6){
int v;
System.out.println(" Enter number to insert:");
v = sc.nextInt();
ll=ll.addAtEnd(ll, v);
}
else if(c==7){
int v;
System.out.println(" Enter number to remove:");
v = sc.nextInt();
ll=ll.remove(ll, v);
}
else if(c==8){
int v,p;
System.out.println(" Enter number to replace:");
v = sc.nextInt();
System.out.println(" Enter which number to replace:");
p = sc.nextInt();
ll.replace(ll, v, p);
}
else if(c==9){
System.out.println(" Front value of the list:"+ll.peekFront(ll));
}
else if(c==10){
System.out.println(" End value of the list:"+ll.peekEnd(ll));
}
else if(c==11){
ll=ll.removeFront(ll);
System.out.println(" Front value of the list is removed");
}
else if(c==12){
if(ll.removeEnd(ll))
{
System.out.println(" Front value of the list is removed");
}
}
else if(c==13){
System.out.println(" The list:");
ll.tostring(ll);
System.out.println();
}
}
}
}
ouput:
run:
Select on option(-1 to exit)
1: size of list
2: is list empty
3: insert Before a number
4:insert After a number
5: add At head
6: add at End
7: remove from list
8: Replace a value
9: peekfrontvalue
10:peekendvalue
11:removefrontvalue
12:removeendvalue
13:display
Enter ur choice8
Enter number to replace:
2
Enter which number to replace:
7
Select on option(-1 to exit)
1: size of list
2: is list empty
3: insert Before a number
4:insert After a number
5: add At head
6: add at End
7: remove from list
8: Replace a value
9: peekfrontvalue
10:peekendvalue
11:removefrontvalue
12:removeendvalue
13:display
Enter ur choice13
The list:
7->
Select on option(-1 to exit)
1: size of list
2: is list empty
3: insert Before a number
4:insert After a number
5: add At head
6: add at End
7: remove from list
8: Replace a value
9: peekfrontvalue
10:peekendvalue
11:removefrontvalue
12:removeendvalue
13:display
Enter ur choice9
Front value of the list:7
Select on option(-1 to exit)
1: size of list
2: is list empty
3: insert Before a number
4:insert After a number
5: add At head
6: add at End
7: remove from list
8: Replace a value
9: peekfrontvalue
10:peekendvalue
11:removefrontvalue
12:removeendvalue
13:display
Enter ur choice6
Enter number to insert:
9
Select on option(-1 to exit)
1: size of list
2: is list empty
3: insert Before a number
4:insert After a number
5: add At head
6: add at End
7: remove from list
8: Replace a value
9: peekfrontvalue
10:peekendvalue
11:removefrontvalue
12:removeendvalue
13:display
Enter ur choice13
The list:
7->9->
Select on option(-1 to exit)
1: size of list
2: is list empty
3: insert Before a number
4:insert After a number
5: add At head
6: add at End
7: remove from list
8: Replace a value
9: peekfrontvalue
10:peekendvalue
11:removefrontvalue
12:removeendvalue
13:display
Enter ur choice1
Size of List:2
Select on option(-1 to exit)
1: size of list
2: is list empty
3: insert Before a number
4:insert After a number
5: add At head
6: add at End
7: remove from list
8: Replace a value
9: peekfrontvalue
10:peekendvalue
11:removefrontvalue
12:removeendvalue
13:display
Enter ur choice-1

More Related Content

Similar to Write a JAVA LinkedListRec class that has the following methods siz.pdf

Implementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdfImplementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdfmaheshkumar12354
 
Please help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdfPlease help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdfankit11134
 
Write a java class LIST that outputsmainpublic class Ass.pdf
Write a java class LIST that outputsmainpublic class Ass.pdfWrite a java class LIST that outputsmainpublic class Ass.pdf
Write a java class LIST that outputsmainpublic class Ass.pdfebrahimbadushata00
 
Write a function to merge two doubly linked lists. The input lists ha.pdf
Write a function to merge two doubly linked lists. The input lists ha.pdfWrite a function to merge two doubly linked lists. The input lists ha.pdf
Write a function to merge two doubly linked lists. The input lists ha.pdfinfo706022
 
import java-util-Iterator- import java-util-NoSuchElementException- im.pdf
import java-util-Iterator- import java-util-NoSuchElementException- im.pdfimport java-util-Iterator- import java-util-NoSuchElementException- im.pdf
import java-util-Iterator- import java-util-NoSuchElementException- im.pdfStewart29UReesa
 
For this homework, you will write a program to create and manipulate.pdf
For this homework, you will write a program to create and manipulate.pdfFor this homework, you will write a program to create and manipulate.pdf
For this homework, you will write a program to create and manipulate.pdfherminaherman
 
NumberList.java (implements the linked list)public class NumberLis.pdf
NumberList.java (implements the linked list)public class NumberLis.pdfNumberList.java (implements the linked list)public class NumberLis.pdf
NumberList.java (implements the linked list)public class NumberLis.pdfanjanacottonmills
 
Design, implement, test(In Java ) a doubly linked list ADT, using DL.pdf
Design, implement, test(In Java ) a doubly linked list ADT, using DL.pdfDesign, implement, test(In Java ) a doubly linked list ADT, using DL.pdf
Design, implement, test(In Java ) a doubly linked list ADT, using DL.pdftrishacolsyn25353
 
The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
 The MyLinkedList class used in Listing 24.6 is a one-way directional .docx The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
The MyLinkedList class used in Listing 24.6 is a one-way directional .docxKomlin1
 
The LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdfThe LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdfmalavshah9013
 
In C++Write a recursive function to determine whether or not a Lin.pdf
In C++Write a recursive function to determine whether or not a Lin.pdfIn C++Write a recursive function to determine whether or not a Lin.pdf
In C++Write a recursive function to determine whether or not a Lin.pdfflashfashioncasualwe
 
please help me in C++Objective Create a singly linked list of num.pdf
please help me in C++Objective Create a singly linked list of num.pdfplease help me in C++Objective Create a singly linked list of num.pdf
please help me in C++Objective Create a singly linked list of num.pdfaminbijal86
 
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdfHere is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdfarrowmobile
 
Csphtp1 23
Csphtp1 23Csphtp1 23
Csphtp1 23HUST
 
Csphtp1 23
Csphtp1 23Csphtp1 23
Csphtp1 23HUST
 
Please solve the following problem using C++- Thank you Instructions-.docx
Please solve the following problem using C++- Thank you Instructions-.docxPlease solve the following problem using C++- Thank you Instructions-.docx
Please solve the following problem using C++- Thank you Instructions-.docxPeterlqELawrenceb
 
Please add-modify the following to the original code using C# 1- Delet.docx
Please add-modify the following to the original code using C# 1- Delet.docxPlease add-modify the following to the original code using C# 1- Delet.docx
Please add-modify the following to the original code using C# 1- Delet.docxStewartt0kJohnstonh
 

Similar to Write a JAVA LinkedListRec class that has the following methods siz.pdf (20)

Implementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdfImplementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdf
 
Please help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdfPlease help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdf
 
Write a java class LIST that outputsmainpublic class Ass.pdf
Write a java class LIST that outputsmainpublic class Ass.pdfWrite a java class LIST that outputsmainpublic class Ass.pdf
Write a java class LIST that outputsmainpublic class Ass.pdf
 
Write a function to merge two doubly linked lists. The input lists ha.pdf
Write a function to merge two doubly linked lists. The input lists ha.pdfWrite a function to merge two doubly linked lists. The input lists ha.pdf
Write a function to merge two doubly linked lists. The input lists ha.pdf
 
import java-util-Iterator- import java-util-NoSuchElementException- im.pdf
import java-util-Iterator- import java-util-NoSuchElementException- im.pdfimport java-util-Iterator- import java-util-NoSuchElementException- im.pdf
import java-util-Iterator- import java-util-NoSuchElementException- im.pdf
 
For this homework, you will write a program to create and manipulate.pdf
For this homework, you will write a program to create and manipulate.pdfFor this homework, you will write a program to create and manipulate.pdf
For this homework, you will write a program to create and manipulate.pdf
 
NumberList.java (implements the linked list)public class NumberLis.pdf
NumberList.java (implements the linked list)public class NumberLis.pdfNumberList.java (implements the linked list)public class NumberLis.pdf
NumberList.java (implements the linked list)public class NumberLis.pdf
 
Design, implement, test(In Java ) a doubly linked list ADT, using DL.pdf
Design, implement, test(In Java ) a doubly linked list ADT, using DL.pdfDesign, implement, test(In Java ) a doubly linked list ADT, using DL.pdf
Design, implement, test(In Java ) a doubly linked list ADT, using DL.pdf
 
The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
 The MyLinkedList class used in Listing 24.6 is a one-way directional .docx The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
 
The LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdfThe LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdf
 
Unit - 2.pdf
Unit - 2.pdfUnit - 2.pdf
Unit - 2.pdf
 
2.ppt
2.ppt2.ppt
2.ppt
 
In C++Write a recursive function to determine whether or not a Lin.pdf
In C++Write a recursive function to determine whether or not a Lin.pdfIn C++Write a recursive function to determine whether or not a Lin.pdf
In C++Write a recursive function to determine whether or not a Lin.pdf
 
please help me in C++Objective Create a singly linked list of num.pdf
please help me in C++Objective Create a singly linked list of num.pdfplease help me in C++Objective Create a singly linked list of num.pdf
please help me in C++Objective Create a singly linked list of num.pdf
 
Chapter14
Chapter14Chapter14
Chapter14
 
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdfHere is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
 
Csphtp1 23
Csphtp1 23Csphtp1 23
Csphtp1 23
 
Csphtp1 23
Csphtp1 23Csphtp1 23
Csphtp1 23
 
Please solve the following problem using C++- Thank you Instructions-.docx
Please solve the following problem using C++- Thank you Instructions-.docxPlease solve the following problem using C++- Thank you Instructions-.docx
Please solve the following problem using C++- Thank you Instructions-.docx
 
Please add-modify the following to the original code using C# 1- Delet.docx
Please add-modify the following to the original code using C# 1- Delet.docxPlease add-modify the following to the original code using C# 1- Delet.docx
Please add-modify the following to the original code using C# 1- Delet.docx
 

More from info785431

Carbon dioxide (CO_2) is a non-polar molecule. Is this consistent wit.pdf
Carbon dioxide (CO_2) is a non-polar molecule. Is this consistent wit.pdfCarbon dioxide (CO_2) is a non-polar molecule. Is this consistent wit.pdf
Carbon dioxide (CO_2) is a non-polar molecule. Is this consistent wit.pdfinfo785431
 
Compare and contrast the messages about science and integrity in Fra.pdf
Compare and contrast the messages about science and integrity in Fra.pdfCompare and contrast the messages about science and integrity in Fra.pdf
Compare and contrast the messages about science and integrity in Fra.pdfinfo785431
 
Briely, what was Darwins explanation for the appearance of design.pdf
Briely, what was Darwins explanation for the appearance of design.pdfBriely, what was Darwins explanation for the appearance of design.pdf
Briely, what was Darwins explanation for the appearance of design.pdfinfo785431
 
A router receives a message addressed 172.16.15.75. The relevant rou.pdf
A router receives a message addressed 172.16.15.75. The relevant rou.pdfA router receives a message addressed 172.16.15.75. The relevant rou.pdf
A router receives a message addressed 172.16.15.75. The relevant rou.pdfinfo785431
 
A study of sandflies in Panama classified flies caught in light traps.pdf
A study of sandflies in Panama classified flies caught in light traps.pdfA study of sandflies in Panama classified flies caught in light traps.pdf
A study of sandflies in Panama classified flies caught in light traps.pdfinfo785431
 
___Tissue found in the brain, spinal cord, and peripheral nerves. The.pdf
___Tissue found in the brain, spinal cord, and peripheral nerves. The.pdf___Tissue found in the brain, spinal cord, and peripheral nerves. The.pdf
___Tissue found in the brain, spinal cord, and peripheral nerves. The.pdfinfo785431
 
You have been exposed to each of the 8 microbes below. One of them h.pdf
You have been exposed to each of the 8 microbes below. One of them h.pdfYou have been exposed to each of the 8 microbes below. One of them h.pdf
You have been exposed to each of the 8 microbes below. One of them h.pdfinfo785431
 
Write a snippet of C code that will enable the ADC to continuously r.pdf
Write a snippet of C code that will enable the ADC to continuously r.pdfWrite a snippet of C code that will enable the ADC to continuously r.pdf
Write a snippet of C code that will enable the ADC to continuously r.pdfinfo785431
 
Write a BFS algorithm using only arrays and no other data structure..pdf
Write a BFS algorithm using only arrays and no other data structure..pdfWrite a BFS algorithm using only arrays and no other data structure..pdf
Write a BFS algorithm using only arrays and no other data structure..pdfinfo785431
 
Why has one prominent textbook author described IO management as the.pdf
Why has one prominent textbook author described IO management as the.pdfWhy has one prominent textbook author described IO management as the.pdf
Why has one prominent textbook author described IO management as the.pdfinfo785431
 
which is true of the data shown in the histogram Which is true of th.pdf
which is true of the data shown in the histogram Which is true of th.pdfwhich is true of the data shown in the histogram Which is true of th.pdf
which is true of the data shown in the histogram Which is true of th.pdfinfo785431
 
What is the value of studying humanities in a business or technical .pdf
What is the value of studying humanities in a business or technical .pdfWhat is the value of studying humanities in a business or technical .pdf
What is the value of studying humanities in a business or technical .pdfinfo785431
 
What are the two components of dynamic pressureVelocity and densi.pdf
What are the two components of dynamic pressureVelocity and densi.pdfWhat are the two components of dynamic pressureVelocity and densi.pdf
What are the two components of dynamic pressureVelocity and densi.pdfinfo785431
 
USING JAVAImplement the quicksort optimization median-of-three, i.pdf
USING JAVAImplement the quicksort optimization median-of-three, i.pdfUSING JAVAImplement the quicksort optimization median-of-three, i.pdf
USING JAVAImplement the quicksort optimization median-of-three, i.pdfinfo785431
 
There are 40 students in our class. How many ways they can be lined .pdf
There are 40 students in our class. How many ways they can be lined .pdfThere are 40 students in our class. How many ways they can be lined .pdf
There are 40 students in our class. How many ways they can be lined .pdfinfo785431
 
The Task For this assignment you will write a rudimentary text edi.pdf
The Task For this assignment you will write a rudimentary text edi.pdfThe Task For this assignment you will write a rudimentary text edi.pdf
The Task For this assignment you will write a rudimentary text edi.pdfinfo785431
 
The SIP handles what functionsA.) establishes a call through the .pdf
The SIP handles what functionsA.) establishes a call through the .pdfThe SIP handles what functionsA.) establishes a call through the .pdf
The SIP handles what functionsA.) establishes a call through the .pdfinfo785431
 
The NBA decides to look into the use of meldonium in the league foll.pdf
The NBA decides to look into the use of meldonium in the league foll.pdfThe NBA decides to look into the use of meldonium in the league foll.pdf
The NBA decides to look into the use of meldonium in the league foll.pdfinfo785431
 
The organelle that serves as the digestive system in the cell is the .pdf
The organelle that serves as the digestive system in the cell is the .pdfThe organelle that serves as the digestive system in the cell is the .pdf
The organelle that serves as the digestive system in the cell is the .pdfinfo785431
 
The major type of interactive forces between molecules of NH_3 are .pdf
The major type of interactive forces between molecules of NH_3 are  .pdfThe major type of interactive forces between molecules of NH_3 are  .pdf
The major type of interactive forces between molecules of NH_3 are .pdfinfo785431
 

More from info785431 (20)

Carbon dioxide (CO_2) is a non-polar molecule. Is this consistent wit.pdf
Carbon dioxide (CO_2) is a non-polar molecule. Is this consistent wit.pdfCarbon dioxide (CO_2) is a non-polar molecule. Is this consistent wit.pdf
Carbon dioxide (CO_2) is a non-polar molecule. Is this consistent wit.pdf
 
Compare and contrast the messages about science and integrity in Fra.pdf
Compare and contrast the messages about science and integrity in Fra.pdfCompare and contrast the messages about science and integrity in Fra.pdf
Compare and contrast the messages about science and integrity in Fra.pdf
 
Briely, what was Darwins explanation for the appearance of design.pdf
Briely, what was Darwins explanation for the appearance of design.pdfBriely, what was Darwins explanation for the appearance of design.pdf
Briely, what was Darwins explanation for the appearance of design.pdf
 
A router receives a message addressed 172.16.15.75. The relevant rou.pdf
A router receives a message addressed 172.16.15.75. The relevant rou.pdfA router receives a message addressed 172.16.15.75. The relevant rou.pdf
A router receives a message addressed 172.16.15.75. The relevant rou.pdf
 
A study of sandflies in Panama classified flies caught in light traps.pdf
A study of sandflies in Panama classified flies caught in light traps.pdfA study of sandflies in Panama classified flies caught in light traps.pdf
A study of sandflies in Panama classified flies caught in light traps.pdf
 
___Tissue found in the brain, spinal cord, and peripheral nerves. The.pdf
___Tissue found in the brain, spinal cord, and peripheral nerves. The.pdf___Tissue found in the brain, spinal cord, and peripheral nerves. The.pdf
___Tissue found in the brain, spinal cord, and peripheral nerves. The.pdf
 
You have been exposed to each of the 8 microbes below. One of them h.pdf
You have been exposed to each of the 8 microbes below. One of them h.pdfYou have been exposed to each of the 8 microbes below. One of them h.pdf
You have been exposed to each of the 8 microbes below. One of them h.pdf
 
Write a snippet of C code that will enable the ADC to continuously r.pdf
Write a snippet of C code that will enable the ADC to continuously r.pdfWrite a snippet of C code that will enable the ADC to continuously r.pdf
Write a snippet of C code that will enable the ADC to continuously r.pdf
 
Write a BFS algorithm using only arrays and no other data structure..pdf
Write a BFS algorithm using only arrays and no other data structure..pdfWrite a BFS algorithm using only arrays and no other data structure..pdf
Write a BFS algorithm using only arrays and no other data structure..pdf
 
Why has one prominent textbook author described IO management as the.pdf
Why has one prominent textbook author described IO management as the.pdfWhy has one prominent textbook author described IO management as the.pdf
Why has one prominent textbook author described IO management as the.pdf
 
which is true of the data shown in the histogram Which is true of th.pdf
which is true of the data shown in the histogram Which is true of th.pdfwhich is true of the data shown in the histogram Which is true of th.pdf
which is true of the data shown in the histogram Which is true of th.pdf
 
What is the value of studying humanities in a business or technical .pdf
What is the value of studying humanities in a business or technical .pdfWhat is the value of studying humanities in a business or technical .pdf
What is the value of studying humanities in a business or technical .pdf
 
What are the two components of dynamic pressureVelocity and densi.pdf
What are the two components of dynamic pressureVelocity and densi.pdfWhat are the two components of dynamic pressureVelocity and densi.pdf
What are the two components of dynamic pressureVelocity and densi.pdf
 
USING JAVAImplement the quicksort optimization median-of-three, i.pdf
USING JAVAImplement the quicksort optimization median-of-three, i.pdfUSING JAVAImplement the quicksort optimization median-of-three, i.pdf
USING JAVAImplement the quicksort optimization median-of-three, i.pdf
 
There are 40 students in our class. How many ways they can be lined .pdf
There are 40 students in our class. How many ways they can be lined .pdfThere are 40 students in our class. How many ways they can be lined .pdf
There are 40 students in our class. How many ways they can be lined .pdf
 
The Task For this assignment you will write a rudimentary text edi.pdf
The Task For this assignment you will write a rudimentary text edi.pdfThe Task For this assignment you will write a rudimentary text edi.pdf
The Task For this assignment you will write a rudimentary text edi.pdf
 
The SIP handles what functionsA.) establishes a call through the .pdf
The SIP handles what functionsA.) establishes a call through the .pdfThe SIP handles what functionsA.) establishes a call through the .pdf
The SIP handles what functionsA.) establishes a call through the .pdf
 
The NBA decides to look into the use of meldonium in the league foll.pdf
The NBA decides to look into the use of meldonium in the league foll.pdfThe NBA decides to look into the use of meldonium in the league foll.pdf
The NBA decides to look into the use of meldonium in the league foll.pdf
 
The organelle that serves as the digestive system in the cell is the .pdf
The organelle that serves as the digestive system in the cell is the .pdfThe organelle that serves as the digestive system in the cell is the .pdf
The organelle that serves as the digestive system in the cell is the .pdf
 
The major type of interactive forces between molecules of NH_3 are .pdf
The major type of interactive forces between molecules of NH_3 are  .pdfThe major type of interactive forces between molecules of NH_3 are  .pdf
The major type of interactive forces between molecules of NH_3 are .pdf
 

Recently uploaded

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
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
 
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
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
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
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...PsychoTech Services
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
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
 

Recently uploaded (20)

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.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 ...
 
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
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
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
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
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
 

Write a JAVA LinkedListRec class that has the following methods siz.pdf

  • 1. Write a JAVA LinkedListRec class that has the following methods: size, empty, insertBefore, insertAfter, addAtHead, addAtEnd, remove, replace, peekFront, peekEnd, removeFront, removeEnd, toString. Use recursion to implement most of these methods. Write a driver to test your implemented LinkedListRec class methods. Solution import java.util.Scanner; public class LinkedListRec { int data; LinkedListRec prev=null,next=null; int size(LinkedListRec l)//method which returns the length of the list { if(l==null)return 0; return 1+size(l.next); } boolean empty(LinkedListRec l)//method checks whether the list is empty or not... { if(l==null)return true; else return false; } LinkedListRec insertBefore(LinkedListRec l,int value,int num_insertbefore)//method which adds a new number before the given number.. { if(l==null)return l; if(l.data == num_insertbefore) { LinkedListRec n = new LinkedListRec(); n.data = value; if(l.prev==null) { n.next = l; l=n;
  • 2. } else { n.prev = l.prev; n.next= l; l.prev = n; } return l; } return insertBefore(l.next,value,num_insertbefore); } LinkedListRec insertAfter(LinkedListRec l,int value,int num_insertafter)//method which adds the new number after the given number { if(l==null)return l; if(l.data == num_insertafter) { LinkedListRec n = new LinkedListRec(); n.data = value; if(l.next==null) { l.next = n; n.prev = l; } else { n.next = l.next; l.next.prev = n; n.prev = l; } return l; } return insertAfter(l.next,value,num_insertafter);
  • 3. } LinkedListRec addAtHead(LinkedListRec l,int value)//method which adds a new number at the head position of the list... { LinkedListRec n = new LinkedListRec(); n.data=value; n.next = l; l=n; return l; } LinkedListRec addAtEnd(LinkedListRec l,int value)//method which adds a new number at the end position of the list... { if(l==null){ l = new LinkedListRec();l.data = value;return l;} if(l.next==null) { LinkedListRec n = new LinkedListRec(); n.data=value; n.prev = l; l.next = n; return l; } return addAtEnd(l.next,value); } LinkedListRec remove(LinkedListRec l,int value)//method which removes given numbet from the list.... { if(l==null)return l; if(l.data == value) { if(l.prev!=null){ l.prev = l.next; } else l=l.next; return l;
  • 4. } return remove(l.next,value); } boolean replace(LinkedListRec l,int value,int newvalue)//method which replaces current number with new number,... { if(l==null)return false; if(l.data == value) { l.data = newvalue; return true; } return replace(l.next,value,newvalue); } int peekFront(LinkedListRec l)//method which returns the first value of the list... { if(l!=null) return l.data; else return -1; } int peekEnd(LinkedListRec l)//method which returns the last value of the list........ { if(l==null)return -1; if(l.next == null)return l.data; return peekEnd(l.next); } LinkedListRec removeFront(LinkedListRec l)//method which removes the first element of the list.. { if(l==null)return l; l=l.next; return l; } boolean removeEnd(LinkedListRec l)//method which removes the last element of the list// {
  • 5. if(l.next==null) { if(l.prev!=null){l.prev.next=null;return true;} l.data = -1; return true; } return removeEnd(l.next); } String tostring(LinkedListRec l)//displaying list { if(l==null)return ""; System.out.print(l.data+"->"); return l.data+"->"+tostring(l.next); } public static void main(String argv[]) { //driver testing code... LinkedListRec ll = new LinkedListRec(); ll.data = 2; Scanner sc = new Scanner(System.in); int c=-2; while(true) { System.out.println("Select on option(-1 to exit) 1: size of list 2: is list empty 3: insert Before a number 4:insert After a number 5: add At head 6: add at End 7: remove from list 8: Replace a value 9: peekfrontvalue 10:peekendvalue 11:removefrontvalue 12:removeendvalue 13:display"); System.out.print("Enter ur choice"); c = sc.nextInt(); if(c==-1)break; if(c==1){ System.out.println(" Size of List:"+ll.size(ll)); }
  • 6. else if(c==2){ if(ll.empty(ll)) System.out.println(" List is Empty "); else System.out.println(" List is not Empty "); } else if(c==3){ int v,p; System.out.println(" Enter number to insert:"); v = sc.nextInt(); System.out.println(" Enter number to insertBefore it:"); p = sc.nextInt(); ll=ll.insertBefore(ll, v, p); } else if(c==4){ int v,p; System.out.println(" Enter number to insert:"); v = sc.nextInt(); System.out.println(" Enter number to insertAfter it:"); p = sc.nextInt(); ll.insertAfter(ll, v, p); } else if(c==5){ int v; System.out.println(" Enter number to insert:"); v = sc.nextInt(); ll= ll.addAtHead(ll, v); } else if(c==6){ int v; System.out.println(" Enter number to insert:"); v = sc.nextInt(); ll=ll.addAtEnd(ll, v); } else if(c==7){ int v;
  • 7. System.out.println(" Enter number to remove:"); v = sc.nextInt(); ll=ll.remove(ll, v); } else if(c==8){ int v,p; System.out.println(" Enter number to replace:"); v = sc.nextInt(); System.out.println(" Enter which number to replace:"); p = sc.nextInt(); ll.replace(ll, v, p); } else if(c==9){ System.out.println(" Front value of the list:"+ll.peekFront(ll)); } else if(c==10){ System.out.println(" End value of the list:"+ll.peekEnd(ll)); } else if(c==11){ ll=ll.removeFront(ll); System.out.println(" Front value of the list is removed"); } else if(c==12){ if(ll.removeEnd(ll)) { System.out.println(" Front value of the list is removed"); } } else if(c==13){ System.out.println(" The list:"); ll.tostring(ll); System.out.println(); }
  • 8. } } } ouput: run: Select on option(-1 to exit) 1: size of list 2: is list empty 3: insert Before a number 4:insert After a number 5: add At head 6: add at End 7: remove from list 8: Replace a value 9: peekfrontvalue 10:peekendvalue 11:removefrontvalue 12:removeendvalue 13:display Enter ur choice8 Enter number to replace: 2 Enter which number to replace: 7 Select on option(-1 to exit) 1: size of list 2: is list empty 3: insert Before a number 4:insert After a number 5: add At head 6: add at End 7: remove from list 8: Replace a value
  • 9. 9: peekfrontvalue 10:peekendvalue 11:removefrontvalue 12:removeendvalue 13:display Enter ur choice13 The list: 7-> Select on option(-1 to exit) 1: size of list 2: is list empty 3: insert Before a number 4:insert After a number 5: add At head 6: add at End 7: remove from list 8: Replace a value 9: peekfrontvalue 10:peekendvalue 11:removefrontvalue 12:removeendvalue 13:display Enter ur choice9 Front value of the list:7 Select on option(-1 to exit) 1: size of list 2: is list empty 3: insert Before a number 4:insert After a number 5: add At head 6: add at End 7: remove from list 8: Replace a value 9: peekfrontvalue 10:peekendvalue 11:removefrontvalue
  • 10. 12:removeendvalue 13:display Enter ur choice6 Enter number to insert: 9 Select on option(-1 to exit) 1: size of list 2: is list empty 3: insert Before a number 4:insert After a number 5: add At head 6: add at End 7: remove from list 8: Replace a value 9: peekfrontvalue 10:peekendvalue 11:removefrontvalue 12:removeendvalue 13:display Enter ur choice13 The list: 7->9-> Select on option(-1 to exit) 1: size of list 2: is list empty 3: insert Before a number 4:insert After a number 5: add At head 6: add at End 7: remove from list 8: Replace a value 9: peekfrontvalue 10:peekendvalue 11:removefrontvalue 12:removeendvalue 13:display
  • 11. Enter ur choice1 Size of List:2 Select on option(-1 to exit) 1: size of list 2: is list empty 3: insert Before a number 4:insert After a number 5: add At head 6: add at End 7: remove from list 8: Replace a value 9: peekfrontvalue 10:peekendvalue 11:removefrontvalue 12:removeendvalue 13:display Enter ur choice-1