#include<iostream>
#include<conio.h>
using namespace std;
struct node
{
int data;
node* link;
}*head=NULL,*ptr,*tptr;
// function to insert elements in the nodes
void create()
{
int item;
cout<<"enter the data to be added"<<endl;
cin>>item;
if(head==NULL)
{
ptr=new node;
ptr->data=item;
ptr->link=NULL;
head=ptr;
}
else
{
ptr=new node;
ptr->data=item;
ptr->link=head;
head=ptr;
}
}
// function to display the elements in the linked-list
void display()
{
ptr=head;
while(ptr!=NULL)
{
cout<<ptr->data<<endl;
ptr=ptr->link;
}
}
// function to reverse the linked-list
void reverse()
{
ptr=head;
tptr=head->link;
node *temp;
if(tptr->link!=NULL)
{
temp=tptr->link;
head->link=NULL;
}
while(temp->link!=NULL)
{
tptr->link=ptr;
ptr=tptr;
tptr=temp;
temp=temp->link;
}
tptr->link=ptr;
head=temp;
temp->link=tptr;
}
int main()
{
int option;
while(option!=4)
{
cout<<"enter 1. to insert in the linked-list"<<endl;
cout<<"enter 2. to display the list"<<endl;
cout<<"enter 3. to reverse the list"<<endl;
cout<<"enter 4. to exit the program"<<endl;
cin>>option;
switch(option)
{
case 1: create();
break;
case 2: display();
break;
case 3: reverse();
break;
}
}
getch();
return 0;
}
#include<conio.h>
using namespace std;
struct node
{
int data;
node* link;
}*head=NULL,*ptr,*tptr;
// function to insert elements in the nodes
void create()
{
int item;
cout<<"enter the data to be added"<<endl;
cin>>item;
if(head==NULL)
{
ptr=new node;
ptr->data=item;
ptr->link=NULL;
head=ptr;
}
else
{
ptr=new node;
ptr->data=item;
ptr->link=head;
head=ptr;
}
}
// function to display the elements in the linked-list
void display()
{
ptr=head;
while(ptr!=NULL)
{
cout<<ptr->data<<endl;
ptr=ptr->link;
}
}
// function to reverse the linked-list
void reverse()
{
ptr=head;
tptr=head->link;
node *temp;
if(tptr->link!=NULL)
{
temp=tptr->link;
head->link=NULL;
}
while(temp->link!=NULL)
{
tptr->link=ptr;
ptr=tptr;
tptr=temp;
temp=temp->link;
}
tptr->link=ptr;
head=temp;
temp->link=tptr;
}
int main()
{
int option;
while(option!=4)
{
cout<<"enter 1. to insert in the linked-list"<<endl;
cout<<"enter 2. to display the list"<<endl;
cout<<"enter 3. to reverse the list"<<endl;
cout<<"enter 4. to exit the program"<<endl;
cin>>option;
switch(option)
{
case 1: create();
break;
case 2: display();
break;
case 3: reverse();
break;
}
}
getch();
return 0;
}
No comments:
Post a Comment