Saturday 16 September 2017

Program to make last node as first node in singly Linklist in C++/C

#include<bits/stdc++.h>
#include<conio.h>
using namespace std;

struct node
{
int info;
node *link;
};

void insertFirst(node **start,int item)
{

node *temp = new node;
temp->info=item;
temp->link=*start;
*start=temp;
}
void insertAfter(node *start,int item)
{
while(start->link!=NULL)
{
start=start->link;
}
node *temp=new node;
temp->info=item;
temp->link=start->link;
start->link=temp;
}
void disp(node *start)
{

while(start!=NULL)
{
cout<<start->info<<" ";
start=start->link;
}
}
void swap(node** start)
{
if (*start == NULL || (*start)->link == NULL)
        return;

    
    struct node *prev = NULL;
    struct node *next = *start;

    
    while (next->link != NULL)
    {
        prev = next;
        next = next->link;
    }
    prev->link = NULL;
next->link = *start;
    *start = next;
}





main()
{
node *start=NULL;
insertFirst(&start,1);
insertAfter(start,2);
insertAfter(start,3);
insertAfter(start,4);
insertAfter(start,5);
insertAfter(start,6);
insertAfter(start,7);
cout<<"Before Swapping:\n";
disp(start);
swap(&start);
cout<<"\nAfter Swapping:";
disp(start);




}

No comments:

Post a Comment

Amazon1Ads