Thursday, August 26, 2010

LinkLists in C++: Part 2

Second in the series of Link-List funtions.

/***************************************************/
#include< iostream.h >
#include< conio.h >
#include< stdlib.h >

struct node
{
int info;
node* next;
}*start, *end, *t;



void delnode()
{
int ch;
cout <<"\n1.DELETE IN THE BEGINNING.";
cout <<"\n2.DELETE AT THE END.";
cout <<"\n3.DELETE IN THE MIDDLE.";
cout <<"\nENTER CHOICE";
cin >> ch;
switch(ch)
{
case 1:delbeg();
break;
case 2:delend();
break;
case 3:delmid();
break;
default:cout <<"\nWRONG CHOICE";
}
}

void delbeg()
{
t = start->next;
start->next = NULL;
delete start;
start = t;
}

void delend()
{
t = start;
while(t->next->next != NULL)
t=t->next;
t->next = NULL;
delete end;
end = t;
}

void delmid()
{
int n;
cout <<"\nENTER THE INFO TO BE DELETED";
cin >>n;
t = start;
while(t != NULL)
{
if(t->next->info == n)
{
t1 = t->next;
t->next = t1->next;
t1->next = NULL;
delete t1;
break;
}
t = t->next;
}
}

No comments:

Post a Comment