数据结构之双向链表实现

来源:互联网 发布:js中有标量类型吗 编辑:程序博客网 时间:2024/05/01 19:58

       与单向链表不同,双向链表的每一个结点除了包含数据外,还包含一头一尾2个指针,分别指向他的前驱结点和后继结点,这样在已知某一结点时,查找他的前驱结点和后继结点就很方便。但是因为有2个指针,存储时消耗的空间大,并且操作起来比单向链表复杂。总之一句话,单向链表能做的双向链表都能做,但双向链表能做的单向链表不一定能做。

typedef struct DuLNode{  int data;  struct DuLNode *pre,*next;}DuLNode,*DuLinklist;//1.创建双向循环链表int initlist(DuLinklist L){  L = (DuLinklist)malloc(sizeof(struct DuLNode));  if(L == NULL)  {    printf("over flow!\n");    return -1;  }  L->pre = L->next = NULL;  return 0;}//2.销毁双向循环链表int destorylist(DuLinklist L){  DuLinklist p,q;  p = L->next;  while(p != L)  {    q = p->next;    free(p);    p = q;  }  free(L);  L = NULL;  return 0;}//3.置空表int clearlist(DuLinklist L){  DuLinklist p,q;  p = L->next;  while(p != L)  {    q = p->next;    free(p);    q = p;  }  L->next = L->pre = L;  return 0;}//4.判断是否为空表int isempty(DuLinklist L){  if((L->next == L) && (L->pre == L))    return 0;  else    return -1;}//5.获取表中元素的个数int getlength(DuLinklist L){  int len = 1;  DuLinklist p = L->next;  while(p != L)  {    len++;    p = p->next;  }  return len;}//6.获取位置为location的元素int getvalue(DuLinklist L,int location){  int i = 0,value;  DuLinklist p;  p = L->next;  while((p != L) && (i < location))  {    i++;    p = p->next;  }  if((p == L) || (i > location))    return -1;  value = p->data;  return value;}//7.定位元素value的位置int getlocation(DuLinklist L,int value){  int location = 1;  DuLinklist p;  p = L->next;  while(p != L)  {    location++;    p = p->next;    if(value == p->data)      break;  }  if(p == L)  {    printf("is not here!\n");    return -1;  }  return location;}//8.取当前元素cur_e的前驱元素int prevalue(DuLinklist L,int cur_e,int pre_e){  DuLinklist p,q;  p = L->next;  while(p != L)  {    q = p->next;    if(cur_e == q->data)    {      pre_e = p->data;      return 0;    }    p = q;  }  return -1;}//9.取当前元素cur_e的后继元素int nextvalue(DuLinklist L,int cur_e,int next_e){  DuLinklist p,q;  p = L->next;  while(p != L)  {    q = p->next;    if(cur_e == p->data)    {      next_e = q->data;      return 0;    }    p = q;  }  return -1;}//10.在链表的位置location之后插入元素valueint insertlist(DuLinklist L,int location,int value){  DuLinklist p,q,new;  int i = 0;  p = L->next;  while((p != NULL) && (i < location))  {    p = p->next;    i++;  }  new = (DuLinklist)malloc(sizeof(DuLNode));  if(new == NULL)  {    printf("over flow!\n");    return -1;  }  q = p->next;  p->next = new;  new->pre = p;  new->next = q;  q->pre = new;  new->data = value;  return 0;}//11.删除链表中位置location处的元素int deletelist(DuLinklist L,int location){  DuLinklist p;  p = L->next;  int i = 0;  while( (p != NULL) && (i < location))  {    i++;    p = p->next;  }  p->pre->next = p->next;  p->next->pre = p->pre;  free(p);  return 0; }//12.顺序打印链表中的元素int travellist(DuLinklist L){  DuLinklist p;  p = L->next;  int i = 0;  if(!isempty(L))    return -1;  while(p != L)  {    printf("p->data[i]=%d\n",p->data);    p = p->next;    i++;  }  return 0;}//13.逆序打印链表中的元素int backlist(DuLinklist L){  DuLinklist q;  q = L->pre;  int j = 0;  if(!isempty(L))    return -1;  while(q != L)  {    printf("q->data[j]=%d\n",q->data);    q = q->pre;    j++;  }  return 0;}


原创粉丝点击