Thursday, 28 December 2017

Design, develop and execute a program in C++ to create a class called LIST (linked list) with member functions to insert an element at the front of the list as well as to delete an element from the front of the list. Demonstrate all the functions of creating a list object.


Implementation of the above problem:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class node
public: int info;
class node*link; 
};
class list
node *head;
public: list() 
{
head=NULL;
}
void insert();
void del();
void display();
~list() {delete head;}
};
void list::insert()
{
node *temp;
temp=new node;
cout<<"\n Enter the element to be inserted:";
cin>>temp->info;
temp->link=head;
head=temp;
}
void list::del()
{
node *temp;
if(head==NULL)
cout<<"\n The list is empty";
else
{
temp=head;
cout<<"\n The deleted element = "<<temp->info;
head=head->link;
temp->link=NULL;
delete(temp);
}
}
void list::display()
{
node *temp;
if(head==NULL)
cout<<"\n The list is empty";
else
{
cout<<"\n The elements of the list are...\n\n";
for(temp=head;temp!=NULL;temp=temp->link)
cout<<temp->info<<"->";
cout<<"NULL";
}
}
void main()
{
int ch;
list l;
clrscr();
while(ch!=4)
{
cout<<"\n\n\n Enter the choice of operation:";
cout<<"\n1.Insert\n2.Delete\n3.Display\n4.Exit:";
cin>>ch;
switch(ch)
{
case 1: l.insert();
break;
case 2: l.del();
break;
case 3: l.display();
break;
case 4: exit(0);
}
}
getch();
}


Thanks
Mukesh Rajput

No comments:

Post a Comment

Thanks
Mukesh Rajput