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 Java program/method that takes a LinkedList and returns a new LinkedList with the integer...
Write a Java program/method that takes a LinkedList and returns a new LinkedList with the integer values squared and reversed. Example: if the LinkedList has (9, 5,4,6), then the returned list will have (36, 16,25,81). What is the time-complexity of your code? You must use listIterator for full credit. public LinkedList getReverseSquaredList (LinkedList list) { }
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
For a LinkedList in Java how do I - Add a method named replace() that takes...
For a LinkedList in Java how do I - Add a method named replace() that takes in two parameters (the data object to replaced, followed by the one to be inserted) that will remove the first occurrence of the item to be replaced and add the second parameter to the list. The method returns a boolean - true if the item to be replaced was found, false if not - Add an instance method named showList() (no parameters or return...
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...
Write a Java function to swap two integers.
Write a Java function to swap two integers.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT