#include
class linklist
{
private:
struct node
{
int data;
node *link;
}*p;
public:
linklist();
void append( int num );
void add( int num );
void addafter( int c, int num );
void del( int num );
void display();
int count();
~linklist();
};
linklist::linklist()
{
p=NULL;
}
void linklist::append(int num)
{
node *q,*t;
if( p == NULL )
{
p = new node;
p->data = num;
p->link = NULL;
}
else
{
q = p;
while( q->link != NULL )
q = q->link;
t = new node;
t->data = num;
t->link = NULL;
q->link = t;
}
}
void linklist::add(int num)
{
node *q;
q = new node;
q->data = num;
q->link = p;
p = q;
}
void linklist::addafter( int c, int num)
{
node *q,*t;
int i;
for(i=0,q=p;i
q = q->link;
if( q == NULL )
{
cout<<"\nThere are less than "<
}
}
t = new node;
t->data = num;
t->link = q->link;
q->link = t;
}
void linklist::del( int num )
{
node *q,*r;
q = p;
if( q->data == num )
{
p = q->link;
delete q;
return;
}
r = q;
while( q!=NULL )
{
if( q->data == num )
{
r->link = q->link;
delete q;
return;
}
r = q;
q = q->link;
}
cout<<"\nElement "<
void linklist::display()
{
node *q;
cout<
for( q = p ; q != NULL ; q = q->link )
cout<
}
int linklist::count()
{
node *q;
int c=0;
for( q=p ; q != NULL ; q = q->link )
c++;
return c;
}
linklist::~linklist()
{
node *q;
if( p == NULL )
return;
while( p != NULL )
{
q = p->link;
delete p;
p = q;
}
}
int main()
{
linklist ll;
cout<<"No. of elements = "<
ll.append(134);
ll.append(235);
ll.append(434);
ll.append(441);
ll.append(500);
ll.add(2);
ll.add(1);
ll.addafter(3,63);
ll.addafter(6,16);
ll.display();
cout<<"\nNo. of elements = "<
ll.del(63);
ll.del(16);
ll.del(500);
cout<<"\nNo. of elements = "<
}
No comments:
Post a Comment