Inserting node with choice (Beginning/End)
#include<iostream>
#include<conio.h>
using namespace std;
struct node
{
int info;
node *next;
};
class list
{
public:
node *head;
list()
{head=NULL;}
void add_begining(int x)
{ if(head==NULL)
{head=new node;
head->info=x;
head->next=NULL;
}
else
{
node *ctr;
ctr=new node;
ctr->info=x;
ctr->next=head;
head=ctr;
}
}
void disp()
{ node *ptr;
ptr=head;
while(ptr!=NULL)
{cout<<endl<<ptr->info;
ptr=ptr->next;}
}
void add_end(int x)
{
node *ptr;
ptr=head;
while(ptr->next!=NULL)
ptr=ptr->next;
node *ctr;
ctr=new node;
ctr->info=x;
ctr->next=NULL;
ptr->next=ctr;
}
};
int main()
{
list obj;
int n,a;
char ag;
yes: {
cout<<"Enter choice: \n 1. Add in bigining. \n 2. Add in end?\n";
cin>>a;
cout<<"Enter element: ";
cin>>n;
switch (a)
{
case 1:
obj.add_begining(n);
break;
case 2:
obj.add_end(n);
break;
default:
cout<<"Invalid choice ";
}
cout<<"Do you want to enter element: (y/n)";
cin>>ag;
if(ag=='y')
goto yes;
}
obj.disp();
getch();
return 0;
}
#include<conio.h>
using namespace std;
struct node
{
int info;
node *next;
};
class list
{
public:
node *head;
list()
{head=NULL;}
void add_begining(int x)
{ if(head==NULL)
{head=new node;
head->info=x;
head->next=NULL;
}
else
{
node *ctr;
ctr=new node;
ctr->info=x;
ctr->next=head;
head=ctr;
}
}
void disp()
{ node *ptr;
ptr=head;
while(ptr!=NULL)
{cout<<endl<<ptr->info;
ptr=ptr->next;}
}
void add_end(int x)
{
node *ptr;
ptr=head;
while(ptr->next!=NULL)
ptr=ptr->next;
node *ctr;
ctr=new node;
ctr->info=x;
ctr->next=NULL;
ptr->next=ctr;
}
};
int main()
{
list obj;
int n,a;
char ag;
yes: {
cout<<"Enter choice: \n 1. Add in bigining. \n 2. Add in end?\n";
cin>>a;
cout<<"Enter element: ";
cin>>n;
switch (a)
{
case 1:
obj.add_begining(n);
break;
case 2:
obj.add_end(n);
break;
default:
cout<<"Invalid choice ";
}
cout<<"Do you want to enter element: (y/n)";
cin>>ag;
if(ag=='y')
goto yes;
}
obj.disp();
getch();
return 0;
}