Question

In: Computer Science

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 subroutine should be written in such a way that the nodes change places (and not the elements in the nodes). A suitable precondition for calling the subroutine is that the two places must be greater than 1 and less than or equal to the number of list elements. The subroutine must be written without using library functions (the exceptions are malloc and free). It is allowed to define help functions. Also write an explanation of how the subroutine works

Solutions

Expert Solution

Program

#include<stdio.h>
#include<stdlib.h>

/* A linked list node */
struct nodeEl
{
   int el;
   struct nodeEl *next;
};

typedef struct nodeEl node;

/* Function to swap nodes x and y in linked list by
changing links */
void swap(node **head_ref, int x, int y)
{
   // Nothing to do if x and y are same
   if (x == y)
   {
       printf("Sorry , swap can't occur as you have entered same indexes to be swapped, try again !!\n");
       return;  
   }
  
   if(x==1 || y==1)
   {
       printf("Sorry , swap can't occur as you have entered index 1 that can't be swapped, try again !!\n");
       return;  
   }
  
   // Search for x (keep track of prevX and CurrX )
   node *prevX = NULL, *currX = *head_ref;
   int cntx=1,cnty=1;
   while(cntx!=x)
   {
       prevX=currX;
       currX = currX->next;
       cntx++;
   }
  
   // Search for y (keep track of prevY and CurrY )
   node *prevY = NULL, *currY = *head_ref;
   while(cnty!=y)
   {
       prevY = currY;
       currY = currY->next;
       cnty++;
   }
  
   // If x is not head of linked list
   if (prevX != NULL)
       prevX->next = currY;
   else // Else make y as new head
       *head_ref = currY;
  
   // If y is not head of linked list
   if (prevY != NULL)
       prevY->next = currX;
   else // Else make x as new head
       *head_ref = currX;
  
   // Swap next pointers
   node *temp = currY->next;
   currY->next = currX->next;
   currX->next = temp;
}

/* Function to add a node at the beginning of List */
void push(node** head_ref, int new_data)
{
   /* allocate node */
   node * new_node = (node *) malloc(sizeof(node));

   /* put in the data */
   new_node->el = new_data;

   /* link the old list off the new node */
   new_node->next = (*head_ref);

   /* move the head to point to the new node */
   (*head_ref) = new_node;
}

/* Function to print nodes in a given linked list */
void printList(node *head)
{
   while(head != NULL)
   {
       printf("%d ", head->el);
       head = head->next;
   }
}

/* Driver program to test above function */
int main()
{
   node *start = NULL;

   /* The constructed linked list is:
   1->2->3->4->5->6->7
   push(&start, 77);
   push(&start, 63);
   push(&start, 51);
   push(&start, 44);
   push(&start, 39);
   push(&start, 22);
   push(&start, 10);
   */
  
   int i,n,value,index1,index2;
   printf("Enter the total no. of nodes to inserted = ");
   scanf("%d",&n);
  
   /* The constructed linked list */
   for(i=0;i<n;i++)
   {
       scanf("%d",&value);
       push(&start, value);
   }

   printf("\nLinked list before calling swap() ");
   printList(start);
  
   printf("\nEnter the two indexes that are to be swapped (index starts from 1) :-\n");
   scanf("%d",&index1);
   scanf("%d",&index2);
   swap(&start, index1, index2);

   printf("\nLinked list after calling swap() ");
   printList(start);

   return 0;
}

Outputs :-

Code Explanation :-
1. Firstly I have created a start node that is the head of the linked list and is NULL in the beginning.Then geeting the input from user for how many nodes he/she wants to have in a linked list (it is variable n).
2.Then I have taken the value for each node from the user using the loop.
3.Then taking two indexes as input from the user in variables namely index1 and index2 which he/she wants to swap.
4.Then calling swap function/subroutine and passing the start/head of linked list and both the indexes.
5.Then in swap() , I have checked that both the indexes entered by user are same or different as if he/she had entered same value then swap can't be done.
6.If both indexes are distinct then the program checks that the index1 and index2 variable must not equal to 1.
7.If it satisfies both the above conditions then it does the swapping.
8.At last , the program prints the list again to show to changes.


Related Solutions

1.Please write a C++ program that counts the nodes in a linked list with the first...
1.Please write a C++ program that counts the nodes in a linked list with the first node pointed to by first. Also please explain. 2. Write a program to determine the average of a linked list of real numbers with the first node pointed to by first. 3. Determine the computing times of the algorithms in question 1 and 4. Write a program to insert a new node into a linked list with the first node pointed to by first...
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 C++, Write a function to reverse the nodes in a linked list. You should not...
In C++, Write a function to reverse the nodes in a linked list. You should not create new nodes when you reverse the the linked list. The function prototype:          void reverse(Node*& head); Use the following Node definition: struct Node {    int data;    Node *next; }
Write down a C program which will create a list (simple linear linked list) of nodes....
Write down a C program which will create a list (simple linear linked list) of nodes. Each node consists of two fields. The first field is a pointer to a structure that contains a student id (integer) and a grade-point average (float). The second field is a link. The data are to be read from a text file. Your program should read a file of 10 students (with student id and grade point average) and test the function you wrote...
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
/* *fix the below C Program to Display the Nodes of a Linked List in Reverse...
/* *fix the below C Program to Display the Nodes of a Linked List in Reverse */ #include <stdio.h> #include <stdlib.h> struct node { int visited; int a; struct node *next; }; int main() { struct node *head = NULL; generate(head); printf("\nPrinting the list in linear order\n"); linear(head); printf("\nPrinting the list in reverse order\n"); display(head); delete(head); return 0; } void display(struct node *head) { struct node *temp = head, *prev = head; while (temp->visited == 0) { while (temp->next !=...
Write a Java program to implement a double-linked list with addition of new nodes at the...
Write a Java program to implement a double-linked list with addition of new nodes at the end of the list. Add hard coded nodes 10, 20, 30, 40 and 50 in the program. Print the nodes of the doubly linked list.
Suppose a linked list of 20 nodes. The middle node has a data –250. Write the...
Suppose a linked list of 20 nodes. The middle node has a data –250. Write the pseudocode to replace the middle node of the linked list with a new node and new data. Assume that the list's head pointer is called head_ptr and the data for the new node is called entry
Suppose a linked list of 20 nodes. The middle node has a data –250. Write the...
Suppose a linked list of 20 nodes. The middle node has a data –250. Write the pseudocode to replace the middle node of the linked list with a new node and new data. Assume that the list's head pointer is called head_ptr and the data for the new node is called entry
Given a linked list of integers, remove any nodes from the linked list that have values...
Given a linked list of integers, remove any nodes from the linked list that have values that have previously occurred in the linked list. Your function should return a reference to the head of the updated linked list. (In Python)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT