データ構造の双チェーン表の基本操作

12542 ワード

////////////////////////////////////////////// , , , , 。//////////////////////////////////////////////  #include <stdio.h>#include <stdlib.h>  typedef  int  ElemType;////////////////////////////////////////////  //   typedef  struct  Node{    ElemType data;    struct  Node *prior;         //     struct  Node *next;          // }Node, *DbList;  ////////////////////////////////////////////  // , DbList DbListCreat(){    Node *L,*p,*r;    L = (Node *)malloc(sizeof(Node));//     L->next = NULL;    r = L;    r->next = NULL;                          //r     ElemType x;    while(scanf("%d",&x) != EOF)    // ,     {        p = (Node *)malloc(sizeof(Node));        p->data = x;        p->next = r->next;        r->next = p;        r = p;    }    r->next = NULL;     return  L;}  ///////////////////////////////////////////   int  getLength(DbList L){    DbList p;               int len=0;
    if(L==NULL) 
        return 0;
    p = L->next;
    while(p!=NULL)
    {
p=p->next;
len++
    }
  return len;  
}
/////////////////////////////////////////// , x   int  DbListFind(DbList L,ElemType x){    DbList p;                //p ,    p = L->next;    int  i = 1;    while(p != NULL && p->data != x )// x **     {                              // , p == NULL p->data         ++i;                       //  for (i = 1, p = L->next; p; p = p->next, i++) {                                    //  if (p->data == x) break;}        p = p->next;    }          if(p == NULL)               // 0        return  0;    else  return  i;             // i}/////////////////////////////////////////  // , i x   DbList DbListInsert(DbList L,int  i,ElemType x){    DbList p,s;                          //s     p = L->next;                         //     int  tempi;    for(tempi = 1;tempi < i-1; tempi++)        p = p->next;    s = (Node *)malloc(sizeof(Node));    s->data = x;                         // x s     s->next = p->next;                        //     p->next->prior = s;    s->prior = p;    p->next = s;          return  L;       }  //////////////////////////////////////////////  // , i   DbList DbListDelete(DLinkList L,int  i){    int  tempi = 1;    DbList p;                        //p 。    p = L->next;    while((tempi++) != i && p != NULL)    {        p = p->next;    }    if(p == NULL)                       //         printf(" 。
"
);    else  if(p->next == NULL)         // , p->next prior     {        p->prior->next = NULL;        free(p);    }    else                                 //     {        p->prior->next = p->next;        p->next->prior = p->prior;        free(p);    }}