1) Based on the classes that we created in lecture named Node and LinkedList (wh
ID: 3564350 • Letter: 1
Question
1) Based on the classes that we created in lecture named Node and LinkedList (which we used to make a one-directional linked list) create a method called find which will return the 1st node object whose data value matches the parameter passed to the method
2) Also in class LinkedList, create a method called replace which will:
a) find a node whose value matches the first value passed as a parameter
b) remove that node
c) create a new node, with the second value that was passed as a parameter
d) insert that new node in that same place in the linked list where the other node was removed
***NOTE****
you MAY NOT make any changes to the Node class. you MAY NOT add a setData method.
**** To think about****
This replace method inserts the new node in the same place that the other node was deleted, however that might not be the correct place for this node (if we are keeping the list sorted). As a result you cannot call methods delete or insert here.
**********************
3) Create a method called sum that will traverse (travel through) all of the nodes in a linked list and output (JOptionPane or to the console) the sum of the integers off all of those nodes.
**********************
4) Create the methods find, delete and replace in class LinkedList2
You can submit all 3 classes in one file, or you can place the methods in the LinkedList and LinkedList2 classes and submit those entire classes
Explanation / Answer
1)
public Node find(int data)
{
if(first == null)
{
System.out.println("Linked List is Empty");
}
else
{
temp = first;
prev = temp;
while(temp.data != data && temp != null)
{
prev = temp;
temp = temp.next;
}
if(temp.data == data)
{
return temp;
}
}
return null;
}
------------------------------------------------------------------------------------------------------------------------------------------
2)
public void replace(int data1,int data2)
{
if(first == null)
{
System.out.println("Linked List is Empty");
}
else
{
temp = first;
prev = temp;
while(temp.data != data1 && temp != null)
{
prev = temp;
temp = temp.next;
}
if(temp.data == data1)
{
Node newNode = new Node(data2);
prev.next = newNode;
newNode.next = temp.next;
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------
3)
public int sum()
{
int sum = 0;
temp = first;
while(temp != null)
{
sum += temp.data;
temp = temp.next;
}
return sum;
}
-------------------------------------------------------------------------------------------------------------------------------------------------
4)
class LinkedList2
{
Node first,temp,prev;
public void replace(int data)
{
if(first == null)
{
System.out.println("Linked List is Empty");
}
else
{
temp = first;
prev = temp;
while(temp.data != data && temp != null)
{
prev = temp;
temp = temp.next;
}
if(temp.data == data)
{
Node newNode = new Node(data);
prev.next = newNode;
newNode.next = temp;
}
}
}
public Node find(int data)
{
if(first == null)
{
System.out.println("Linked List is Empty");
}
else
{
temp = first;
prev = temp;
while(temp.data != data && temp != null)
{
prev = temp;
temp = temp.next;
}
if(temp.data == data)
{
return temp;
}
}
return null;
}
public void delete(int data)
{
if(first == null)
{
System.out.println("Linked List is Empty");
}
else
{
temp = first;
prev = temp;
while(temp.data != data && temp != null)
{
prev = temp;
temp = temp.next;
}
if(temp.data == data)
{
prev.next = temp.next;
}
else
{
System.out.println("Element is not found in List");
}
}
}
}
---------------------------------------------------------------------------------------------------------------------------------------------
Complete Implementation and output:
public class LinkedList {
public static void main(String arg[])
{
LinkedListOperation ob = new LinkedListOperation();
ob.insert(12);
ob.insert(32);
ob.insert(52);
ob.insert(82);
ob.insert(32);
ob.show();
System.out.println(" Sum is :"+ob.sum());
System.out.println("finding :"+(ob.find(52).data));
ob.replace(32,65);
ob.show();
}
}
class Node
{
int data;
Node next;
public Node(int data)
{
this.data = data;
this.next = null;
}
}
class LinkedListOperation
{
Node first = null,temp,prev,last;
public void replace(int data1,int data2)
{
if(first == null)
{
System.out.println("Linked List is Empty");
}
else
{
temp = first;
prev = temp;
while(temp.data != data1 && temp != null)
{
prev = temp;
temp = temp.next;
}
if(temp.data == data1)
{
Node newNode = new Node(data2);
prev.next = newNode;
newNode.next = temp.next;
}
}
}
public void insert(int data)
{
if(first==null)
{
first = new Node(data);
last = first;
}
else
{
temp = new Node(data);
last.next = temp;
last = temp;
}
}
public void show()
{
temp = first;
while(temp.next != null)
{
System.out.print(" "+temp.data);
temp = temp.next;
}
}
public Node find(int data)
{
if(first == null)
{
System.out.println("Linked List is Empty");
}
else
{
temp = first;
prev = temp;
while(temp.data != data && temp != null)
{
prev = temp;
temp = temp.next;
}
if(temp.data == data)
{
return temp;
}
}
return null;
}
public int sum()
{
int sum = 0;
temp = first;
while(temp != null)
{
sum += temp.data;
temp = temp.next;
}
return sum;
}
}
Sample Output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.