Adding in the beginning
#include<iostream>
#include<conio.h>
using namespace std;
struct node
{
int info;
node *next;
};
class list
{
public:
node *head;
list()
{head=NULL;}
void add_beginning(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<<"\t"<<ptr->info;
ptr=ptr->next;}
}
};
int main()
{
list obj;
int n,a;
cout<<"How many nodes you want to use ";
cin>>n;
for(int i=1;i<=n;i++)
{cout<<"Enter element ";
cin>>a;
obj.add_beginning(a);
}
cout<<"\nElements you entered :- \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_beginning(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<<"\t"<<ptr->info;
ptr=ptr->next;}
}
};
int main()
{
list obj;
int n,a;
cout<<"How many nodes you want to use ";
cin>>n;
for(int i=1;i<=n;i++)
{cout<<"Enter element ";
cin>>a;
obj.add_beginning(a);
}
cout<<"\nElements you entered :- \n";
obj.disp();
getch();
return 0;
}