Write
a C++ program 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 after creating a list object.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct NOD
{
int info;
struct NOD *next;
};
typedef struct NOD node;
class linklist
{
node *f;
public:
linklist()
{
f=NULL;
}
void insert(int);
void del();
void disp();
};
void linklist::insert(int num)
{
node *p=new node;
p->info=num;
p->next=f;
f=p;
}
void linklist::del()
{
clrscr();
node *temp=f;
if(f==NULL)
cout<<"\n The list is empty";
else
{
cout<<"\n The deleted element is :"<<f->info;
f=f->next;
delete temp;
cout<<"\n Deletion successful";
}
return;
}
void linklist::disp()
{
node *temp=f;
if(f==NULL)
cout<<"\n The list is empty";
else
{
cout<<"\n The element in list are:";
while(temp!=NULL)
{
cout<<" "<<temp->info;
temp=temp->next;
}
}
}
void main()
{
int num,ch=1;
linklist ob;
clrscr();
while(ch)
{
cout<<"\n\n\n\n************** Linked List ************** \n"
<<"\n-------- Menu ---------"
<<"\n Enter 1 to pushed "
<<"\n Enter 2 to popped "
<<"\n Enter 3 to display "
<<"\n Enter 4 to exit "
<<"\n Enter your choice: ";
cin>>ch;
switch(ch)
{
case 1:clrscr();
cout<<"\n Enter the number to be inserted ";
cin>>num;
ob.insert(num);
ob.disp();
break;
case 2:clrscr();
ob.del();
ob.disp();break;
case 3:clrscr();
ob.disp();break;
case 4:exit(0);
default : cout<<"\nInvalid choice";
}
}
getch();
}
OUTPUT
No comments:
Post a Comment