Insert node at end of linked list
#include<iostream>
#include<conio.h>
using namespace std;
struct node
{
int info;
node *next;
};
class list
{
public:
node *head;
list()
{head=NULL;}
void add_end(int x)
{
if(head==NULL)
{head=new node;
head->info=x;
head->next=NULL;
}
else
{
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;
}
}
void disp()
{ node *ptr;
ptr=head;
cout<<"\n\nElements you entered\n";
while(ptr!=NULL)
{
cout<<endl<<"\t\t"<<ptr->info;
ptr=ptr->next;}
}
};
int main()
{
list obj;
int n,a;
cout<<"How many elements you want to enter?\nAns. ";
cin>>a;
for(int i=0;i<a;i++)
{
cout<<"Enter element : ";
cin>>n;
obj.add_end(n);
}
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_end(int x)
{
if(head==NULL)
{head=new node;
head->info=x;
head->next=NULL;
}
else
{
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;
}
}
void disp()
{ node *ptr;
ptr=head;
cout<<"\n\nElements you entered\n";
while(ptr!=NULL)
{
cout<<endl<<"\t\t"<<ptr->info;
ptr=ptr->next;}
}
};
int main()
{
list obj;
int n,a;
cout<<"How many elements you want to enter?\nAns. ";
cin>>a;
for(int i=0;i<a;i++)
{
cout<<"Enter element : ";
cin>>n;
obj.add_end(n);
}
obj.disp();
getch();
return 0;
}