Friday, 2 August 2013

Data Structure Using C++

#include<iostream.h>
#include<conio.h>
class node
{
int data;
node *next;
public:
node()
{
next=NULL;
}
friend class list;
};
class list
{
node *start;
public:
list()
{
start=NULL;
}



void add(int n)
{
node *nw;
nw=new node;
nw->data=n;
nw->next=NULL;
if(start==NULL)
{
start=nw;
}
else
{
nw->next=start;
start=nw;
}
}
void print()
{
for(node *ptr=start;ptr!=NULL;ptr=ptr->next)
cout<<"["<<ptr->data<<"]";
}
};
void main()
{
int n,opt;
list mylist;
do
{
clrscr();
cout<<"\n****************************";
cout<<"\n1. Add\n2. Print\n0. Exit";
cout<<"\n****************************\n";
cout<<"\nEnter your choice>> ";
cin>>opt;
switch(opt)
{
case 1:
cout<<"Enter Number>>";
cin>>n;
mylist.add(n);
break;
case 2:
mylist.print();
getch();
break;
case 0:
break;
default:
cout<<"Invalid Option";
getch();
}


}while(opt!=0);
}