Question

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

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

Code needed in java.

Homework Answers

Answer #1

Code in java programming language:

 
class Node 
{ 
    int Data; 
    Node next; 
    Node(int data) 
    { 
        Data = data; 
        next = null; 
    } 
} 
  
class Test 
{ 
    Node head; 
    public void swap(int x, int y) 
    { 
        if (x == y) return; 
  
       Node preX = null, currentX = head; 
        while (currentX != null && currentX.Data != x) 
        { 
            preX = currentX; 
            currentX = currentX.next; 
        } 
  
        Node preY = null, currentY = head; 
        while (currentY != null && currentY.Data != y) 
        { 
            preY = currentY; 
            currentY = currentY.next; 
        } 
  
        if (currentX == null || currentY == null) 
            return; 
  
        if (preX != null) 
            preX.next = currentY; 
        else 
            head = currentY; 
  
        if (preY != null) 
            preY.next = currentX; 
        else 
        head = currentX; 
  
        Node temp = currentX.next; 
        currentX.next = currentY.next; 
        currentY.next = temp; 
    } 
  
    public void push(int new_data) 
    { 
        Node new_Node = new Node(new_data); 
  
        new_Node.next = head; 
  
        head = new_Node; 
    } 
  
    public void print() 
    { 
        Node tNode = head; 
        while (tNode != null) 
        { 
            System.out.print(tNode.Data+" "); 
            tNode = tNode.next; 
        } 
    } 
  
    public static void main(String[] args) 
    { 
        Test linkList = new Test(); 
        linkList.push(70); 
        linkList.push(60); 
        linkList.push(50); 
        linkList.push(40); 
        linkList.push(30); 
        linkList.push(20); 
        linkList.push(10);
  
        System.out.print("\n Linked list :"); 
        linkList.print(); 
  
        linkList.swap(40, 30); 
  
        System.out.print("\n Sorted Linked list : "); 
        linkList.print(); 
    } 
}

Output Screen Shot:

Thank you................

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
program in java 1- Write a code to remove continuous repeatitive elements of a Linked List....
program in java 1- Write a code to remove continuous repeatitive elements of a Linked List. Example: Given: -10 -> 3 -> 3 -> 3 -> 5 -> 6 -> 6 -> 3 -> 2 -> 2 -> NULL The answer: -10 -> 3 -> 5 -> 6 -> 3 -> 2 -> NULL
Write a java program using java.util package to add these elements to linked list {KKU, KSU,...
Write a java program using java.util package to add these elements to linked list {KKU, KSU, KAU, NU} then add -KAUSTI to index 2 and remove the KKU element.
Machine Problem 3 - Linked List C++ For this assignment you will write a program that...
Machine Problem 3 - Linked List C++ For this assignment you will write a program that inserts 20 random integers from 0 to 100 in order in a linked list object. The program will create another linked list, but with 15 random integers from 0 – 100 in order. The program then will merge those two ordered linked list into a single ordered list. The function merge should receive references to each of the list objects to be merged and...
Given this definition of a generic Linked List node: public class LLNode {     private T...
Given this definition of a generic Linked List node: public class LLNode {     private T data;     private LLNode next;     public LLNode(T data, LLNode next) {           this.data = data;           this.next = next;     }     public void setNext(LLNode newNext){ next = newNext; }     public LLNode getNext(){ return next; }     public T getData() {return data;} } Write the findMinimumNode method body. This method returns the linked list node that contains the minimum value in the...
Write python code for the queue class buuilt with a linked list using: First- returns first...
Write python code for the queue class buuilt with a linked list using: First- returns first value in queue, PrintQ-returns whole queue as list, RemoveValue-removes specific value from queue, RemoveIndex-removes value from queue by index
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings....
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings. You must base your code on the doubly linked list implementation given in my Week 8 slides. Change the code so that instead of an ‘int’ each node stores a string (choose a suitable size). Each node should also have a next node pointer, and previous node pointer. Then write functions to implement the following linked list operations: • A printList function that prints...
How do you delete the tail node of a singly linked list if the link has...
How do you delete the tail node of a singly linked list if the link has the head and does no have tail? Write the code. How much time does it take to do it? (java)
In JAVA: Write a program that reads an integer, a list of words, and a character....
In JAVA: Write a program that reads an integer, a list of words, and a character. The integer signifies how many words are in the list. The output of the program is every word in the list that contains the character at least once. Assume at least one word in the list will contain the given character. Assume that the list of words will always contain fewer than 20 words. Ex: If the input is: 4 hello zoo sleep drizzle...
Write an iterative algorithm in Java-like pseudocode for printing a singly linked list in reverse in...
Write an iterative algorithm in Java-like pseudocode for printing a singly linked list in reverse in O(N) time. You can use as much extra space as you need. The original list pointers CAN NOT BE MODIFIED. State in big-O notation how much extra space is used by this algorithm. Write another iterative algorithm in Java-like pseudocode for printing a singly linked list in reverse using O(1) extra space. The original list pointers CAN NOT BE MODIFIED. This algorithm can have...
Use MIPS assembly language program to swap two of the integers in an integer array. The...
Use MIPS assembly language program to swap two of the integers in an integer array. The program should include the Swap function to swap the integers and the main function to call the Swap function. The main function should: • Pass the starting address of the array in $a0. • Pass the indices of the two elements to swap in $a1 and $a2. • Preserve (i.e. push onto the stack) any T registers that it uses. • Call the Swap...