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
Write a routine to interchange the mth and nth elements of a singly-linked list. You may...
Write a routine to interchange the mth and nth elements of a singly-linked list. You may assume that the ranks m and n are passed in as parameters. Allow for all the ways that m and n can occur. You must rearrange the pointers, not simply swap the contents. Please use psuedocode
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
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
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...
JAVA Implement a θ(n) time non-recursive program that reverses a singly linked list L of n...
JAVA Implement a θ(n) time non-recursive program that reverses a singly linked list L of n elements. The program gets the head of the list (L.head) as input and cannot use any storage other than the given list. Don't not use recursive~~~~~~~ thanks
Use MARS to write and simulate a MIPS assembly language program to swap two of the...
Use MARS to write and simulate a 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. Download the template file “P4_template.asm” provided on Blackboard. Add your code to this file. Do not modify any of the code provided in the file. The main function should: • Pass the starting address of the array in $a0....
Write C language code for a function that takes a doubly linked list as a parameter...
Write C language code for a function that takes a doubly linked list as a parameter and deletes all the nodes in even positions from the first to the last after displaying the content of each node to the console.
) IN JAVA: Write a recursive method lowestPaidEmployeeRec that is placed in the linked list after...
) IN JAVA: Write a recursive method lowestPaidEmployeeRec that is placed in the linked list after the method countHighEarners. Method returns employee with lowest salary in entire linked lists of employees. Assume the same LinkedList class as is given as in question 10 above. // PRECONDITION: Linked list is not empty. public Employee lowestPaidEmployeeRec(Node first) // first is reference to the beginning of linked list. { }
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT