Double Link List Code


The Concept of link list and Double link list is clearly explained in Wikipedia and here.I just want to show you the correct code for making doubly link list because sometimes algorithm is explained great but code is not given their.

We first have to make struct and define the structure we want.I just have inserted a character in a node and its next and previous nodes.

So our insert method which would insert a node will be like this. 

void Insert(char a)
{
if(first == NULL)
{ first = new Node();
first->nxt = NULL;
first->prv = NULL;
first->a = a;
last = first;
}
else
{ last->nxt = new Node();
last->nxt->a = a;
last->nxt->prv = last;
last = last->nxt;
last->prv->nxt = last;
}
last->nxt = NULL;
}

And the delete method would be like this.

void Delete(char b)
{
Node * temp = first;
int i = 1;
while(temp!= NULL)
{ if(temp->a == b)
{ temp->prv->nxt = temp->nxt;
temp->nxt->prv = temp->prv;
break;
}
temp = temp->nxt;
}
}

And another method would be which would show All the nodes.

void ShowAll()
{
Node * temp = first;
int i = 1;
while(temp!= NULL)
{
printf("\nAt poistion %d we have %c ",i,temp->a);
temp = temp->nxt;
}
cout << "\nEnd Of List"<< endl;
}

Here is the link from where you can download the CPP file of this code.Download

Continue Reading...
 
|  I Code Help. Blogger Template By Lawnydesignz Powered by Blogger