Question

In: Computer Science

Using Java programming, Write a LinkedList method swap() that takes two ints as arguments and swaps...

Using Java programming,

Write a LinkedList method swap() that takes two ints as arguments and swaps the elements at those two positions.

The method should not traverse the list twice to find the elements, and it should not create or destroy any nodes.

Solutions

Expert Solution

import java.io.*;
import java.util.Scanner;
  
public class LinkedList {
  
Node head; // head of list
  
// Linked list Node.
//structure of a node of link list
static class Node {
  
int data;
Node next;
  
// Constructor
Node(int d)
{
data = d;
next = null;
}
}

// Method to insert a new node
public static LinkedList insert(LinkedList list, int data)
{
// Create a new node with given data
Node new_node = new Node(data);
new_node.next = null;
  
// If the Linked List is empty,
// then make the new node as head
if (list.head == null) {
list.head = new_node;
}
else {
// Else traverse till the last node
// and insert the new_node at the end
Node last = list.head;
while (last.next != null) {
last = last.next;
}
  
  
last.next = new_node;
}
  
// Return the list
return list;
}
  
// Method to print the LinkedList.
public static void printList(LinkedList list)
{
Node currNode = list.head;

System.out.print("LinkedList: ");

// Traverse through the LinkedList
while (currNode != null) {
// Print the data at current node
System.out.print(currNode.data + " ");

// Go to next node
currNode = currNode.next;
}
}

// Method to SWAP TWO VALUES
public static void swap(int m,int n,LinkedList list)
{
Node currNode = list.head;
   Node two = list.head;
int temp,opt=0;
  
while (currNode != null) {
//compare the linklist data with the given two numbers
if(m==currNode.data ||n==currNode.data)
       {
           if(opt==1) //second time match
break;

       if(opt==0) //first time match
{
opt=1; //set the two pointer at current node
two=currNode;
}

   }  

// Go to next node
currNode = currNode.next;
  
}
//swap the two data
temp=two.data;
two.data=currNode.data;
currNode.data=temp;

}

// Driver code
public static void main(String[] args)
{
int n,no,a,b,i;
LinkedList list = new LinkedList();
   Scanner scan = new Scanner(System.in);
System.out.println("Enter how many elements to be stored in Linked List");
n=scan.nextInt();
for(i=1;i<=n;i++)
{
System.out.println("Enter a number");

no=scan.nextInt();

list = insert(list, no);
}
  
// Print the LinkedList
printList(list);
//ask for two numbers to swap
System.out.println("Enter a two numbers to swap");
a=scan.nextInt();
b=scan.nextInt();
//swap the two numbers
swap(a,b,list);
//display the list after swap
printList(list);
}
}

OUTPUT


Related Solutions

Write a method weave that takes two arrays of ints, a and b, and that returns...
Write a method weave that takes two arrays of ints, a and b, and that returns an array that contains the elements of a and b in the order a[0], b[0], a[1], b[1], etc. If one of the arrays a or b is longer than the other, just add the extra elements at the end of the array. In your solution, you can use only 3 arrays, namely the two arrays a, and b passed to the method and the...
C Language - Programming Write a function that takes an array of ints, and the size...
C Language - Programming Write a function that takes an array of ints, and the size of the array – another int. It also returns a double. Call this one ‘average.’ Return a double that is the average of the values in the array. Demonstrate that it works by finding the average of an array with these values {78, 90, 56, 99, 88, 68, 92} Write a function that takes one double parameter, and returns a char. The parameter represents...
In java write a method that will take an array and change it into a linkedlist...
In java write a method that will take an array and change it into a linkedlist and then display it in the main method
C programming Write a function called string in() that takes two string pointers as arguments. If...
C programming Write a function called string in() that takes two string pointers as arguments. If the second string is contained in the first string, have the function return the address at which the contained string begins. For instance, string in(“hats”, “at”) would return the address of the a in hats. Otherwise, have the function return the null pointer. Test the function in a complete program that uses a loop to provide input values for feeding to the function.
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
Part A) Java Programming Exercise #2: Write a method, remove, that takes three parameters: an array...
Part A) Java Programming Exercise #2: Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer, say, removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (Note that after deleting the element, the array size is reduced by 1.) Here you assume the array is not sorted. Do not...
art A) Java Programming Exercise #2: Write a method, remove, that takes three parameters: an array...
art A) Java Programming Exercise #2: Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer, say, removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (Note that after deleting the element, the array size is reduced by 1.) Here you assume the array is not sorted. Do not...
In C++ Write a function which takes two parameters: an array of ints and an int...
In C++ Write a function which takes two parameters: an array of ints and an int size of the array and prints every element greater than 5 to the screen. As an example, if the array has the following 10 elements: 2 5 8 9 7 1 0 2 6 3, your function should print out 8 9 7 6. You may assume that the parameters passed to the function are valid. Your function must have the following signature: void...
Write code in java using the LinkedList connecting two different classes. For example, Employee and EmployeeList...
Write code in java using the LinkedList connecting two different classes. For example, Employee and EmployeeList are two different classes that are used to create the assignment.
Write a method public static void minMax(int[] arr) that takes an array of unique ints of...
Write a method public static void minMax(int[] arr) that takes an array of unique ints of length at least two as an argument, and swaps the smallest value of the array into the 0th position and swaps the largest value of the array into the last position. For example, if int[] a = {4, 3, 2, 6, 1, 5}, the method call minMax(a) should modify the array so that it is {1, 3, 2, 5, 4, 6}. The method should...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT