Given a singly linked list,write a function to swap elements pairwise.For example,if the linked list is 1->2->3->4->5->6->7 then the function should change it to 2->1->4->3->6->5->7 ,and if the linked list is 1->2->3->4->5->6 then the function should change it to 2->1->4->3->6->5

Note:We don't have to swap the internal data;we have to swap the links.Suppose nodes consist of internal array,then swapping among the links is more beneficial as it avoids multiple swapping between a pair of node.

Solution

/*
Problem description
Pairwise swap elements of a given
linked list by changing links

submitted by
Arjun Mishra
*/

#include<iostream>
using namespace std;

//node structure
struct node
{
       int data;
       struct node* next;
       }*head,*tail,*temp;
//function to enter data into linked list
void push()
{
  temp=(struct node*)malloc(sizeof(struct node));
  if(temp==NULL)
  {
   cout<<"sorry! Cannot Push.Memory Not Available\n";
   return;
   }
   cout<<"Enter the data\n";
   cin>>temp->data;
   temp->next=NULL;
   if(head==NULL)//checking whether it is the very first element or not
   {
    head=temp;
    tail=temp;            
    }
    else
    {
     tail->next=temp;
     tail=temp;
        }  
     }
//function to delete a node from last in a linked list
void pop()
{
 if(head==NULL)
 {
  cout<<"Cannot Delete!List is Empty!\n";
  return;
  }
  if(head->next==NULL)
  {
   temp=head;
   head=NULL;
   tail=NULL;
   free(temp);                 
   return;
   }
   //it deltes from the last
   temp=head;
   while(temp->next!=tail)
   temp=temp->next;
   temp->next=NULL;
   free(tail);
   tail=temp;
   }
//functyion to traverse nodes of a linked list
void traverse()
{
     if(head==NULL)
     {
      cout<<"Cannot traverse!List is Empty!\n";            
      return;
      }
      cout<<"The list is as follows\n";
      temp=head;
      while(temp!=NULL)
      {
       cout<<temp->data<<"->";
       temp=temp->next;                }
       cout<<"NULL"<<endl;
     }
//function to perform pairwise sort
void pair_wise_sort()
{
     if(head==NULL)
     {
      cout<<"Cannot Perform!List is empty\n";
     return;
     }
     if(head->next==NULL)
     {
     cout<<"Action Perfromed!Output is\n";
     traverse();
     return;                   
     }
    struct node *prev = head;
    struct node *curr = (head)->next;
    head = curr;  
    while (true)
    {
        struct node *next = curr->next;
        curr->next = prev;

        if (next == NULL || next->next == NULL)
        {
            prev->next = next;
            break;
        }

                prev->next = next->next;
        prev = next;
        curr = prev->next;
    }
    temp=head;
    while(temp->next!=NULL)
    temp=temp->next;
    tail=temp;
    cout<<"Action Perfromed!Output is\n";
     traverse();
     }

int main()
{  
    int ch;
    x:
    cout<<"Menu:\n\n";

    cout<<"Press 0 to exit\n";
    cout<<"Press 1 to enter data\n";
    cout<<"Press 2 to delete data\n";
    cout<<"Press 3 to traverse the list\n";
    cout<<"Press 4 to pairwise sort the list\n";
    cin>>ch;
    switch(ch)
    {
     case 0:
     return 0;
     case 1:
          push();
          goto x;
     case 2:
          pop();
          goto x;
     case 3:
          traverse();
          goto x;
     case 4:
          pair_wise_sort();
          goto x;
     default:
          cout<<"Wrong Choice!!\n";
          goto x;        
          }
     return 0;    
     }