Sunday, June 05, 2005

double linked list implementation

 

#include "stdafx.h"

#include <conio.h>

 

struct dnode

{

      struct dnode *prev;

      int data;

      struct dnode *next;

};

void append(struct dnode **, int );

int display(struct dnode *);

void addatbeg(struct dnode **, int );

void addafter(struct dnode **, int , int );

void deleteq(struct dnode **, int );

int _tmain(int argc, _TCHAR* argv[])

{

      struct dnode *p;

      p=NULL;

      int count=0;

      append(&p, 11);

      append(&p, 12);

      append(&p, 13);

      append(&p, 14);

      append(&p, 15);

      append(&p, 16);

      append(&p, 17);

      count = display(p);

      addatbeg(&p, 10);

      printf("\n");

      printf("the size of the linked list is: %d", display(p));

      addafter(&p, 2, 32);

      addafter(&p, 4, 36);

      addafter(&p, 8, 23);

      addafter(&p, 5, 78);

      addafter(&p, 7, 89);

      addafter(&p, 3, 54);

      addatbeg(&p, 9);

      printf("\nthe size of the linked list is: %d", display(p));

      deleteq(&p, 23);

      printf("\nthe size of the linked list is: %d", display(p));

      getch();

      return 0;

}

void append(struct dnode **q, int num)

{

      struct dnode *s, *r=*q;

      /*if(*q==NULL)

      {

            printf("\ntest");

            r=(struct dnode*)malloc(sizeof(struct dnode));

            r->data = num;

            r->prev = NULL;

            r->next = NULL;

      }*/

      if(*q==NULL)

      {

            //printf("\ntest");

            *q=(struct dnode*)malloc(sizeof(struct dnode));

            (*q)->data = num;

            (*q)->prev = NULL;

            (*q)->next = NULL;

      }

      else

      {

            printf("test\n");

            while(r->next!=NULL)

                  r=r->next;

            s = (struct dnode*)malloc(sizeof(struct dnode));

            s->data = num;

            s->prev = r;

            s->next = NULL;

            r->next  =s;

      }

}

 

void addatbeg(struct dnode **s, int num)

{

      struct dnode *q;

      q=(struct dnode*)malloc(sizeof(struct dnode));

      q->data = num;

      q->next=*s;

      q->prev=NULL;

      (*s)->prev=q;

      *s=q;

}

 

void addafter(struct dnode **q, int loc, int num)

{

      struct dnode *temp, *r=*q;

      int c=0;

      for (c=0; c<loc; c++)

      {

            r=r->next;

            if(r==NULL)

            {

                  printf("There are less than %d elements in the linked list", loc);

                  return;

            }

      }

      r=r->prev;

      temp = (struct dnode*)malloc(sizeof(struct dnode));

      temp->data = num;

      temp->prev = r;

      temp->next = r->next;

      temp->next->prev=temp;

      r->next=temp;

}

int display(struct dnode *p)

{

      int c=0;

      while(p!=NULL)

      {    

            printf("\n%d", p->data);

            p = p->next;

            c++;

      }

      return c;

}

 

void deleteq(struct dnode **s, int num)

{

      struct dnode *q=*s;

      while(q!=NULL)

      {

            if(q->data==num)

            {

                  if(q==*s)

                  {

                        q = q->next;

                        q->prev=NULL;

                  }

                  else

                  {

                        if(q->next==NULL)

                              q->prev->next=NULL;

                        else

                        {

                              q->prev->next = q->next;

                              q->next->prev=q->prev;

                        }

                              free(q);

                  }

                  return;

            }

            q=q->next;

      }

      printf("\n%d not found", num);

}

 

Pradyut
http://pradyut.tk
http://groups.yahoo.com/group/d_dom/
http://groups-beta.google.com/group/oop_programming
India

 

No comments: