To delete last from Linked List.
#include<iostream>
#include<conio.h>
using namespace std;
struct node
{
int info;
node *next;
};
class list
{
public:
int count=0;
node *head;
list()
{head=NULL;}
void add_end(int x)
{
if(head==NULL)
{ head=new node;
head->info=x;
head->next=NULL;
count++;
}
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;
count++;
}
}
void delete_from_end()
{
node *ptr, *ctr;
ptr=head;
if(ptr==NULL)
cout<<"There is no node present in list. List is empty.";
else
{
while(ptr->next!=NULL)
{
ctr=ptr;
ptr=ptr->next;
}
ctr->next=NULL;
}
cout<<"\nList after detele last node.\n\n";
disp();
}
void disp()
{
node *ptr;
ptr=head;
while(ptr!=NULL)
{
cout<<ptr->info<<"\t";
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);
}
int l;
cout<<"\n\nElements you entered\n\n";
obj.disp();
obj.delete_from_end();
getch();
return 0;
}
#include<conio.h>
using namespace std;
struct node
{
int info;
node *next;
};
class list
{
public:
int count=0;
node *head;
list()
{head=NULL;}
void add_end(int x)
{
if(head==NULL)
{ head=new node;
head->info=x;
head->next=NULL;
count++;
}
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;
count++;
}
}
void delete_from_end()
{
node *ptr, *ctr;
ptr=head;
if(ptr==NULL)
cout<<"There is no node present in list. List is empty.";
else
{
while(ptr->next!=NULL)
{
ctr=ptr;
ptr=ptr->next;
}
ctr->next=NULL;
}
cout<<"\nList after detele last node.\n\n";
disp();
}
void disp()
{
node *ptr;
ptr=head;
while(ptr!=NULL)
{
cout<<ptr->info<<"\t";
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);
}
int l;
cout<<"\n\nElements you entered\n\n";
obj.disp();
obj.delete_from_end();
getch();
return 0;
}