Implementation of Linked List with C++
Implementation of Linked List with C++
#include<iostream>
using namespace std;
class node
{
public:
int x;
node *next;
void add_no()
{
cout<<"enter a no";
cin>>x;
}
void display_no()
{
cout<<"the no is"<<x<<endl;
}
};
class list
{
private:
node *start;
public:
void add()
{
node *fresh;
fresh=new node;
fresh->add_no();
fresh->next= start;
start=fresh;
}
void traverse()
{
for(node *t=start; t!=NULL; t=t->next){
t->display_no();
}
}
void query(){
int num;
cout<<"enter num: "<<endl;
cin>>num;
node *t;
t=start;
while(t!=NULL)
{
if(t->x==num)
{
cout<<"ELEMENT FOUND IN THE LINK LIST"<<endl;
return;
}
t=t->next;
}
cout<<"ELEMENT "<<num<<" NOT FOUND IN THE LIST"<<endl;
}
void del()
{
node *t;
t=start;
start=start->next;
delete t;
}
};
int main(void)
{
int i;char c='y';
list *l=new list;
do
{
cout<<"1.add"<<endl;
cout<<"2.delete"<<endl;
cout<<"3.traverse"<<endl;
cout<<"4.query"<<endl;
cout<<"enter choice"<<endl;
cin>>i;
switch(i)
{
case 1:
l->add();
break;
case 2:
l->del();
break;
case 3:
l->traverse();
break;
case 4:
l->query();
break;
default:
cout<<"invalid input";
}
cout<<"want to continue?";
cin>>c;
}while(c=='y');
}
0 comments:
Post a Comment