#include <iostream>
using namespace std;
struct node {
int data;
node *next;
};
void create(node *&t,int a)
{
if(t==NULL)
{
t=new node;
t->data=a;
t->next=NULL;
}
else
{
create(t->next,a);
}
}
void rev(node *&t)
{
node *temp=NULL;
node *next;
node *cur=t;
while(t!=NULL)
{
next=t->next;
t->next=temp;
temp=t;
t=next;
}
t=temp;
}
void print(node *t)
{
while(t!=NULL)
{
cout<<t->data<<" ";
t=t->next;
}
cout<<endl;
}
node *blockrev(node *t,int a)
{
node *next,*cur=t;
node *head;
int flag=0;
node *temp=NULL;
node *curhead=t;
for(int i=0;i<a&&t!=NULL;i++)
{
next=t->next;
t->next=temp;
temp=t;
t=next;
}
if(next!=NULL)
curhead->next=blockrev(next,a);
return temp;
}
int main()
{
int a[]={2,3,4,5,6,7,8,'\0'};
int n=7;
node *t=NULL;
for(int i=0;i<n;i++)
{
create(t,a[i]);
}
print(t);
cout<<"reversing the list\n";
rev(t);
print(t);
rev(t);
cout<<"\nblock wise reverse of list\n";
t=blockrev(t,2);
print(t);
rev(t);
print(t);
return 0;
}
using namespace std;
struct node {
int data;
node *next;
};
void create(node *&t,int a)
{
if(t==NULL)
{
t=new node;
t->data=a;
t->next=NULL;
}
else
{
create(t->next,a);
}
}
void rev(node *&t)
{
node *temp=NULL;
node *next;
node *cur=t;
while(t!=NULL)
{
next=t->next;
t->next=temp;
temp=t;
t=next;
}
t=temp;
}
void print(node *t)
{
while(t!=NULL)
{
cout<<t->data<<" ";
t=t->next;
}
cout<<endl;
}
node *blockrev(node *t,int a)
{
node *next,*cur=t;
node *head;
int flag=0;
node *temp=NULL;
node *curhead=t;
for(int i=0;i<a&&t!=NULL;i++)
{
next=t->next;
t->next=temp;
temp=t;
t=next;
}
if(next!=NULL)
curhead->next=blockrev(next,a);
return temp;
}
int main()
{
int a[]={2,3,4,5,6,7,8,'\0'};
int n=7;
node *t=NULL;
for(int i=0;i<n;i++)
{
create(t,a[i]);
}
print(t);
cout<<"reversing the list\n";
rev(t);
print(t);
rev(t);
cout<<"\nblock wise reverse of list\n";
t=blockrev(t,2);
print(t);
rev(t);
print(t);
return 0;
}
No comments:
Post a Comment