The question is to reverse a Linked List.Here ,the reversing is done by detaching the node and putting it at the expected position.
for example:
Suppose we have a Linked List as follows:
Now,detach the node marked '1' and make it the end node.The Linked List will look like:
2->3->4->1
Detach node marked '2' and put it between the node marked '4' and '1'.The Linked List will look like:
3->4->2->1
Further,detach '3' and put it between node marked '4' and '2'.Now.the Linked List becomes
4->3->2->1
Algorithm:
structure definition:
struct node
{
int data;
struct node* next;
}*head,*tail,*temp;
if( head equals NULL )
{
//The list is empty
return;
}
if(head->next==NULL)
{
//Only one node ,hence, reverse will be the same
return;
}
while(head not equals tail)
{
temp=head;
head=head->next;//move head to the next adjacent node
temp->next=tail->next;//detach the former head node
tail->next=temp;//put the former head node at the right index
}
//after the execution of the above steps....tail will be pointing the same location as that of the head.
temp=head;
//making tail to point at the last node
while(temp->next not equals NULL)
temp=temp->next;
tail=temp;
Solution:
/*
Problem Description
Reversing a Linked List
submitted by
Arjun Mishra
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
struct node
{
int data;
struct node* next;
}*head,*tail,*temp;
void push()
{
temp=(struct node*)malloc(sizeof(struct node));
if(!(temp))
{
cout<<"Memory not available";
return;
}
cout<<"Enter the data\n";
cin>>temp->data;
temp->next=NULL;
if(head==NULL)
{
head=temp;
tail=temp;
}
else
{
tail->next=temp;
tail=temp;
}
}
void pop()
{
if(head==NULL)
{
cout<<"Cannot delete!Empty List\n";
return;
}
if(head->next==NULL)
{
temp=head;
head=NULL;
tail=NULL ;
free(temp);
return ;
}
temp=head;
while(temp->next!=tail)
temp=temp->next;
tail=temp;
temp=temp->next;
tail->next=NULL;
free(temp);
}
void traverse()
{
if(head==NULL)
{
cout<<"List is empty!\n";
return;
}
temp=head;
while(temp!=NULL)
{
cout<<temp->data<<"->";
temp=temp->next;
}
cout<<"NULL"<<endl;
}
void reverse()
{
if(head==NULL)
{
cout<<"List is Empty!Cannot reverse\n";
return;
}
if(head->next==NULL)
{
return;
}
while(head!=tail)
{
temp=head;
head=head->next;
temp->next=tail->next;
tail->next=temp;
}
temp=head;
while(temp->next!=NULL)
temp=temp->next;
tail=temp;
}
int main()
{
while(1)
{
system("cls");
int ch;
cout<<"Menu:\n";
cout<<"Press 0 to exit\n";
cout<<"Press 1 to push\n";
cout<<"Press 2 to pop\n";
cout<<"Press 3 to traverse\n";
cout<<"Press 4 to reverse\n";
cout<<endl<<"Enter Your Choice\n";
cin>>ch;
switch(ch)
{
case 0:
return 0;
break;
case 1:
push();
break;
case 2:
pop();
break;
case 3:
traverse();
break;
case 4:
reverse();
break;
default:
cout<<"Wrong Choice\n";
}
cout<<"Press any key to continue...";
getchar();
getchar();
}
}
for example:
Suppose we have a Linked List as follows:
2->3->4->1
Detach node marked '2' and put it between the node marked '4' and '1'.The Linked List will look like:
3->4->2->1
Further,detach '3' and put it between node marked '4' and '2'.Now.the Linked List becomes
4->3->2->1
Algorithm:
structure definition:
struct node
{
int data;
struct node* next;
}*head,*tail,*temp;
if( head equals NULL )
{
//The list is empty
return;
}
if(head->next==NULL)
{
//Only one node ,hence, reverse will be the same
return;
}
while(head not equals tail)
{
temp=head;
head=head->next;//move head to the next adjacent node
temp->next=tail->next;//detach the former head node
tail->next=temp;//put the former head node at the right index
}
//after the execution of the above steps....tail will be pointing the same location as that of the head.
temp=head;
//making tail to point at the last node
while(temp->next not equals NULL)
temp=temp->next;
tail=temp;
Solution:
/*
Problem Description
Reversing a Linked List
submitted by
Arjun Mishra
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
struct node
{
int data;
struct node* next;
}*head,*tail,*temp;
void push()
{
temp=(struct node*)malloc(sizeof(struct node));
if(!(temp))
{
cout<<"Memory not available";
return;
}
cout<<"Enter the data\n";
cin>>temp->data;
temp->next=NULL;
if(head==NULL)
{
head=temp;
tail=temp;
}
else
{
tail->next=temp;
tail=temp;
}
}
void pop()
{
if(head==NULL)
{
cout<<"Cannot delete!Empty List\n";
return;
}
if(head->next==NULL)
{
temp=head;
head=NULL;
tail=NULL ;
free(temp);
return ;
}
temp=head;
while(temp->next!=tail)
temp=temp->next;
tail=temp;
temp=temp->next;
tail->next=NULL;
free(temp);
}
void traverse()
{
if(head==NULL)
{
cout<<"List is empty!\n";
return;
}
temp=head;
while(temp!=NULL)
{
cout<<temp->data<<"->";
temp=temp->next;
}
cout<<"NULL"<<endl;
}
void reverse()
{
if(head==NULL)
{
cout<<"List is Empty!Cannot reverse\n";
return;
}
if(head->next==NULL)
{
return;
}
while(head!=tail)
{
temp=head;
head=head->next;
temp->next=tail->next;
tail->next=temp;
}
temp=head;
while(temp->next!=NULL)
temp=temp->next;
tail=temp;
}
int main()
{
while(1)
{
system("cls");
int ch;
cout<<"Menu:\n";
cout<<"Press 0 to exit\n";
cout<<"Press 1 to push\n";
cout<<"Press 2 to pop\n";
cout<<"Press 3 to traverse\n";
cout<<"Press 4 to reverse\n";
cout<<endl<<"Enter Your Choice\n";
cin>>ch;
switch(ch)
{
case 0:
return 0;
break;
case 1:
push();
break;
case 2:
pop();
break;
case 3:
traverse();
break;
case 4:
reverse();
break;
default:
cout<<"Wrong Choice\n";
}
cout<<"Press any key to continue...";
getchar();
getchar();
}
}
No comments:
Post a Comment