Question

In: Computer Science

Write a program to swap mth and nth elements of a linked list. User should give...

Write a program to swap mth and nth elements of a linked list.

User should give input.

Solutions

Expert Solution

import java.util.Scanner;

public class Main {
  
//Represent a node of the singly linked list
class Node{
int data;
Node next;
  
public Node(int data) {
this.data = data;
this.next = null;
}
}

//Represent the head and tail of the singly linked list
public Node head = null;
public Node tail = null;
  
//push() will add a new node to the list
public void push(int data) {
//Create a new node
Node newNode = new Node(data);
  
//Checks if the list is empty
if(head == null) {
//If list is empty, both head and tail will point to new node
head = newNode;
tail = newNode;
}
else {
//newNode will be added after tail such that tail's next will point to newNode
tail.next = newNode;
//newNode will become new tail of the list
tail = newNode;
}
}
public int getElementAtThatNode(int index)
{
Node current = head;
int count = 0; /* index of Node we are
currently looking at */
while (current != null)
{
if (count == index)
return current.data;
count++;
current = current.next;
}
  
/* if we get to this line, the caller was asking
for a non-existent element so we assert fail */
assert(false);
return 0;
}
//swap() will swap the given two nodes
public void swap(int n1, int n2){
Node prevNode1 = null, prevNode2 = null, node1 = head, node2 = head;
  
//Checks if list is empty
if(head == null) {
return;
}
  
//If n1 and n2 are equal, then list will remain the same
if(n1 == n2)
return;
  
//Search for node1
while(node1 != null && node1.data != n1){
prevNode1 = node1;
node1 = node1.next;
}
  
//Search for node2
while(node2 != null && node2.data != n2){
prevNode2 = node2;
node2 = node2.next;
}
  
if(node1 != null && node2 != null) {
  
//If previous node to node1 is not null then, it will point to node2
if(prevNode1 != null)
prevNode1.next = node2;
else
head = node2;
  
//If previous node to node2 is not null then, it will point to node1
if(prevNode2 != null)
prevNode2.next = node1;
else
head = node1;
  
//Swaps the next nodes of node1 and node2
Node temp = node1.next;   
node1.next = node2.next;   
node2.next = temp;   
}
else {
System.out.println("Swapping is not possible");
}
}
  
//printList() will display all the nodes present in the list
public void printList() {
//Node current will point to head
Node current = head;
  
if(head == null) {
System.out.println("List is empty");
return;
}
while(current != null) {
//Prints each node by incrementing pointer
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
  
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
Main sList = new Main();
System.out.println("How many nodes you want to add in linked list ?:");
int n1 = myObj.nextInt();
//Add nodes to the list
for(int i = 0 ; i< n1; i ++){
System.out.println("Enter your node "+(i+1)+ ":");
int myNode = myObj.nextInt();
sList.push(myNode);
}
System.out.println("\nOriginal list: ");
sList.printList();
System.out.println("Enter node 1 with you want to swap:");
int m = myObj.nextInt();
System.out.println("Enter node 2 with you want to swap:");
int n = myObj.nextInt();
  
m = sList.getElementAtThatNode(m-1);
n = sList.getElementAtThatNode(n-1);
//Swaps the node m with n.
sList.swap(m,n);
  
System.out.println("\nList after swapping nodes: ");
sList.printList();
}
}


Related Solutions

Write a program to swap the first and last elements of a linked list. (i) by...
Write a program to swap the first and last elements of a linked list. (i) by exchanging info part (ii) through pointers Need the program in java with no direct usage of packages use node and link.
In this problem, you will write a program that reverses a linked list. Your program should...
In this problem, you will write a program that reverses a linked list. Your program should take as input a space-separated list of integers representing the original list, and output a space-separated list of integers representing the reversed list. Your algorithm must have a worst-case run- time in O(n) and a worst-case space complexity of O(1) beyond the input. For example, if our input is: 5 7 1 2 3 then we should print: 3 2 1 7 5 Please...
Write a java method to swap between two values in a singly linked list
Write a java method to swap between two values in a singly linked list
Write a c program Write a function to swap two elements of an integer array. Call...
Write a c program Write a function to swap two elements of an integer array. Call the function to swap the first element, i[0] with last element i[n], second element i[1] with the last but one element i[n-1] and so on. Should handle arrays with even and odd number of elements. Call the swap function with the following arrays and print results in each case before and after swapping. i. int arr1[] = {0, 1, 2, 3, 30, 20, 10,...
Write a subroutine named swap in C thatswaps two nodes in a linked list. The first...
Write a subroutine named swap in C thatswaps two nodes in a linked list. The first node should not be able to change places. The nodes are given by: Struct nodeEl { int el; struct nodeEl * next; }; typdef struct nodeEl node; The list header (of type node *) is the first parameter of the subroutine. The second and third parameters consist of integers and are the places in the list where the nodes are to change places. The...
IN JAVA write a program that asks a user for a maximum of 20 user-inputted elements...
IN JAVA write a program that asks a user for a maximum of 20 user-inputted elements and create an array. Then, write a Merge Sort function with recursion (in the main) that takes the user inputted elements in the array, sorts them, and prints them back.
I need a MIPS Assembly program that "Display the elements of the linked list in reverse...
I need a MIPS Assembly program that "Display the elements of the linked list in reverse order." It needs subprogram and those subprogram does not have t registers.
write a recursive method that returns the product of all elements in java linked list
write a recursive method that returns the product of all elements in java linked list
C++ What Should This Program Do? Linked List Class Design your own linked list class (List.h)...
C++ What Should This Program Do? Linked List Class Design your own linked list class (List.h) to hold a series of strings. The linked list node should be implemented as a struct. The class should have member functions for appending, inserting, and deleting nodes. You should also have a display function that will traverse the list & display each node’s value. Don’t forget to add a destructor that destroys the list. DRIVER – driver.cpp Write a driver program (driver.cpp) that...
write a MIPS program to ask user to input the number of elements of array
write a MIPS program to ask user to input the number of elements of array
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT