In: Computer Science
This is a JAVA assignment and i dont have the SinglyLinkedList class
Exercise 1
In this exercise, you will add a method swapNodes to
SinglyLinkedList class. This method
should swap two nodes node1 and node2 (and not just their contents)
given references only to
node1 and node2. The new method should check if node1 and node2 are
the same node, etc.
Write the main method to test the swapNodes method. Hint: You may
need to traverse the list.
Exercise 2
In this exercise, you will use the DoublyLinkedList implementation
of the textbook. Write a
method for concatenating two doubly linked lists L and M, with
header and trailer sentinel
nodes, into a single list L′. Write a main method to test the new
method. Hint: Connect the end
of L into the beginning of M.
Thanks
Exercise 1 Solution:
import java.util.Scanner;
class Node
{
int data;
Node next;
Node(int t)
{
data = t;
next = null;
}
}
public class LinkedList
{
Node head;
public void swapNodes(int x, int y)
{
//There is nothing to do if x and y are same
if (x == y) return;
Node previous_X = null, current_X = head;
while (current_X != null && current_X.data != x)
{
previous_X = current_X;
current_X = current_X.next;
}
Node previous_Y = null, current_Y = head;
while (current_Y != null && current_Y.data != y)
{
previous_Y = current_Y;
current_Y = current_Y.next;
}
// If either x or y is not present, nothing to do
if (current_X == null || current_Y == null)
return;
if (previous_X != null)
previous_X.next = current_Y;
else
head = current_Y;
if (previous_Y != null)
previous_Y.next = current_X;
else
head = current_X;
//Swapping the next pointers
Node temp = current_X.next;
current_X.next = current_Y.next;
current_Y.next = temp;
}
public void push(int new_data)
{
Node new_Node = new Node(new_data);
new_Node.next = head;
head = new_Node;
}
public void printList()
{
Node tNode = head;
while (tNode != null)
{
System.out.print(tNode.data+" ");
tNode = tNode.next;
}
}
//Main function to test the code
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
LinkedList llist = new LinkedList();
System.out.println("Enter the values that are need to pushed into list(by space separated values)");
String str=sc.nextLine();
String s[]=str.split(" ");
for(int i=s.length-1;i>=0;i--){
llist.push(Integer.parseInt(s[i]));
}
System.out.println("Enter any two node values that you want to swap");
System.out.print("Node 1 value:");
int N1=sc.nextInt();
System.out.print("Node 2 value:");
int N2=sc.nextInt();
System.out.print("\n Linked list before calling swapNodes() (before swapping): ");
llist.printList();
llist.swapNodes(N1, N2);
System.out.print("\n Linked list after calling swapNodes() (after swapping): ");
llist.printList();
}
}
Exercise 2 solution:
import java.util.*;
class Node
{
int data;
Node next;
Node(int t) {data = t;
next = null;}
}
public class MergeLists
{
Node head;
public void addToTheLast(Node node)
{
if (head == null)
{
head = node;
}
else
{
Node temp = head;
while (temp.next != null)
temp = temp.next;
temp.next = node;
}
}
void printList()
{
Node temp = head;
while (temp != null)
{
System.out.print(temp.data + " ");
temp = temp.next;
}
System.out.println();
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
MergeLists llist1 = new MergeLists();
MergeLists llist2 = new MergeLists();
System.out.println("Enter two lists that are to be merged(by space separated vales)");
System.out.print("Enter List 1:");
String str1=sc.nextLine();
String s1[]=str1.split(" ");
System.out.print("Enter List 2:");
String str2=sc.nextLine();
String s2[]=str2.split(" ");
for(int i=0,j=0;i<s1.length&&j<s2.length;i++,j++){
llist1.addToTheLast(new Node(Integer.parseInt(s1[i])));
llist2.addToTheLast(new Node(Integer.parseInt(s2[j])));
}
llist1.head = new Gfg().sortedMerge(llist1.head,
llist2.head);
llist1.printList();
}
}
class Gfg
{
Node sortedMerge(Node headA, Node headB)
{
Node dummyNode = new Node(0);
Node tail = dummyNode;
while(true)
{
if(headA == null)
{
tail.next = headB;
break;
}
if(headB == null)
{
tail.next = headA;
break;
}
if(headA.data <= headB.data)
{
tail.next = headA;
headA = headA.next;
}
else
{
tail.next = headB;
headB = headB.next;
}
tail = tail.next;
}
return dummyNode.next;
}
}
Thank you!, if you have any queries post it below in the comment section i will try my best to resolve your queries and if required i will add them to my code.