Friday, January 24, 2020

Posted on Leave a Comment

Swap mth and nth element of singly linked list

A ‘C’ program to swap mth and nth element of singly linked list.

Swap Mth And Nth Element Of Singly Linked List


Source Code



#include<stdio.h>

#include<conio.h>

struct link {

  int data;

  struct link * next;

}; /*End of struct link*/

struct link * start = NULL;

void create();

void display();

void swap();

void main() {

  int ch;

  clrscr();

  do {

    printf(“\nPress 1 To Create Node.”);

    printf(“\nPress 2 To Display”);

    printf(“\nPress 3 To Swap”);

    printf(“\nPress 4 To Exit”);

    printf(“\nEnter Your Choice: “);

    scanf(“ % d”, & ch);

    switch (ch) {

    case 1:

      create();

      break;

    case 2:

      display();

      break;

    case 3:

      swap();

      break;

    case 4:

      exit(0);

    default:

      printf(“\nInvalid Choice… Try Again”);

      getch();

    } /*End of switch*/

  } while (ch != 4); /*End of while loop*/

} /*End of void main()*/

void create() {

  struct link * p, * node;

  printf(“\nEnter Data: “);

  p = (struct link * ) malloc(sizeof(struct link));

  scanf(“ % d”, & p - > data);

  p - > next = NULL;

  if (start == NULL)

    start = p;

  else {

    node = start;

    while (node - > next != NULL)

      node = node - > next;

    node - > next = p;

  } /*End of else*/

} /*End of void create()*/

void display() {

  struct link * node;

  if (start == NULL) {

    printf(“\nList Is Empty”);

    getch();



  } /*End of if*/

  node = start;

  while (node != NULL) {

    printf(“ % d“, node - > data);

    node = node - > next;

  } /*End of while loop*/

  getch();

} /*End of void display()*/

void swap() {

  int m, n, i;

  struct link * node1, * node2;

  if (start == NULL) {

    printf(“\nList Is Empty”);

  } /*End of if*/

  node1 = start;

  printf(“\nEnter Mth Position: “);

  scanf(“ % d”, & m);

  for (i = 0; i < m; i++) {

    node1 = node1 - > next;

    if (node1 == NULL) {

      printf(“\nWrong Location Enterred, , ”);

    } /*End of if*/

  } /*End of i for loop*/

  printf(“\nEnter Nth Position: “);

  scanf(“ % d”, & n);

  node2 = start;

  for (i = 0; i < n; i++) {

    node2 = node2 - > next;

    if (node2 == NULL) {

      printf(“\nWrong Location Enterred..”);

    } /*End of if*/

  } /*End of i for loop*/

  node1 - > data = (node1 - > data) + (node2 - > data) - ((node2 - > data) = (node1 - > data));

  printf(“\nElement Swapped”);

}

OUTPUT:



Press 1 To Create Node.
Press 2 To Display
Press 3 To Swap
Press 4 To Exit
Enter Your Choice: 1
Enter Data: 3
Press 1 To Create Node.
Press 2 To Display
Press 3 To Swap
Press 4 To Exit
Enter Your Choice: 1
Enter Data: 5
Press 1 To Create Node.
Press 2 To Display
Press 3 To Swap
Press 4 To Exit
Enter Your Choice: 1
Enter Data: 9
Press 1 To Create Node.
Press 2 To Display
Press 3 To Swap
Press 4 To Exit
Enter Your Choice: 2
3 5 9
Press 1 To Create Node.
Press 2 To Display
Press 3 To Swap
Press 4 To Exit
Enter Your Choice: 3
Enter Mth Position: 0
Enter Nth Position: 1
Element Swapped
Press 1 To Create Node.
Press 2 To Display
Press 3 To Swap
Press 4 To Exit
Enter Your Choice: 2
5 3 9

  Download Source Code
Download Source code

0 comments:

Post a Comment

Codearea.in is featured by projectsgeek.com. Powered by Blogger.