badge

C++ Program to imlpement SWAPPING LINK LIST

Program in c++ for SWAPPING THE LINK LIST...

DESCRIPTION:- Swapping of the link list  , in this program basically happen that when u enter the no that which no u want to swap then it takes the number and then it swap it with its another number(which is pointed to next).


OUTPUT OF THE PROGRAM:-




SOURCE CODE:-

#include <iostream >

#include <conio .h>
using namespace std;
struct node
{
int data;
node *link;
};
node *p=NULL;
void append(int num)
{
if(p==NULL)
{
node *temp=new node;
temp->data=num;
temp->link=NULL;
p=temp;
}
else
{
node *temp,*new1=new node;
temp=p;
while(temp->link!=NULL)
{
temp=temp->link;
}
new1->data=num;
new1->link=NULL;
temp->link=new1;
}

}

void swap(int k)
{
int i;
node *temp,*locp=NULL,*temp2;
temp=p;
for(i=1;i {
locp=temp;
temp=temp->link;
}
//temp2=temp->link;
node *temp1=temp->link->link;

temp->link->link=temp;
locp->link=temp->link;
temp->link=temp1;
}
void display()
{
node *x;
x=p;
while(x!=NULL)
{
cout<data<<"\n";
x=x->link;
}
}



int main()
{
int k;
append(2);
    append(3);
    append(4);
    append(5);
    append(6);

 
    display();
 cout<<"Enter no";
 cin>>k;
 swap(k);

 display();

}




/* IF U FOUND ANY PROBLEM IN THIS PROGRAM  OR IF YOU HAVE ANY QUERY RELATED THIS PROGRAM THEN PLEASE COMMENT IN THIS POST OR YOU CAN DIRECTLY MAIL ME ON piyushkhandelwal029@gmail.com . */

THANKU.
SHARE

Admin

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments: