zoukankan      html  css  js  c++  java
  • c语言实现--双向循环链表操作

    1,双向链表相当于两个单向循环链表。

    2,双向链表的结点定义。

    1 struct DULNode
    2 {
    3     int data;
    4     struct DULNode * prior;
    5     struct DULNode * next;
    6 };
    7 
    8 typedef struct DULNode * linklist;

    3,单循环链表的操作都适用于双循环链表。
    4,双循环链表的操作集合仍在头文件defs.h中。

    5,InitList操作。双循环链表初始化操作示意图

     1 #include"defs.h"
     2 
     3 void InitList(linklist *L) //改变头指针
     4 {
     5     *L = (linklist)malloc(sizeof(struct DULNode));
     6     if (*L == NULL)
     7         exit(0);
     8      (*L)->next = *L;
     9      (*L)->prior = *L; 
    10 }

    6,ClearList操作.

     1 #include"defs.h"
     2 
     3 void ClearList(linklist L)
     4 {
     5     linklist p = L->next;  //p指向链表第一个元素
     6    
     7     while (p != L)
     8      {
     9           p = p->next;  //p指向第二个元素
    10         free(p->prior);
    11      }
    12      (*L)->next = *L;
    13      (*L)->prior = *L;
    14 }
    ClearList.c

    7,DestroyList操作

    1 #include"defs.h"
    2 
    3 void DestroyList(linklist *L)
    4 {
    5     ClearList(*L);
    6     free(*L);
    7     *L = NULL;
    8 }
    DestroyList.c

    8,ListEmpty操作

    1 #include"defs.h"
    2 
    3 int ListEmpty(linklist L)
    4 {
    5     if (L->next == L && L->prior == L) //两个条件均要有
    6         return 0;
    7     else
    8         return 1;
    9 }
    ListEmpty.c

    9,ListLength操作

     1 #include"defs.h"
     2 
     3 int ListLength(linklist L)
     4 {
     5     linklist p = L->next;
     6     int j = 0;
     7     
     8     while (p != L)
     9     {
    10         ++j;
    11         p = p->next;
    12     }
    13     return j;
    14 }
    ListLength.c

    10,GetElem操作

     1 #include"defs.h"
     2 
     3 int GetElem(linklist L, int i, int *e)
     4 {
     5      linklist p = L;
     6      int j = 0;
     7      if (i<1 || i>ListLength(L))
     8          exit(0);
     9      
    10      while (j<i)   //找到第i个结点
    11      {
    12           ++j;
    13           p = p->next;
    14      }
    15      *e = p->data;
    16 }
    GetElem.c

    11,LocateElem操作, 没有找到返回-1

     1 #include"defs.h"
     2 
     3 int LocateElem(linklist L, int e)
     4 {
     5     linklist p = L->next;
     6     int j = 0;
     7 
     8     while (p != L)
     9     {
    10         ++j;
    11         if (p->data == e)
    12              return j;
    13     }
    14     return -1;  
    15 }
    LocateElem.c

    12,PriorElem操作

     1 #include"defs.h"
     2 
     3 int PriorElem(linklist L, int cur_e, int *pri_e)
     4 {
     5     linklist p = L->next->next;  //p指向第二个元素
     6  
     7    while (p != L)
     8     {
     9           if (p->data == cur_e)
    10           {
    11                 *pri_e = p->prior->data;
    12                 return 0;
    13           }
    14     }
    15     return 0;
    16 }
    PriorElem.c

    13,NextElem操作

     1 #include"defs.h"
     2 
     3 int NextElem(linklist L, int cur_e, int *nex_e)
     4 {
     5     linklist p = L->next->next; 
     6     
     7      while (p != L)
     8      {
     9              if (p->prior->data == cur_e)
    10              {
    11                       *nex_e = p->data;
    12                        return 0;
    13              }
    14      }
    15      return 0;
    16 }
    NextElem.c

    14,ListInsert操作

     1 #include"defs.h"
     2 
     3 int ListInsert(linklist L, int i, int e)
     4 {
     5       linklist p = L; //p指向头结点
     6      linklist q, s;
     7       int j = 0;
     8       if (i<1 || i>ListLength(L)+1)
     9          exit(0);
    10   
    11        while (j < i-1) //找到第i-1个结点
    12      {
    13            ++j;
    14            p = p->next;
    15        }
    16        q = p->next;  //q指向第i个结点
    17      
    18      s = (linklist)malloc(sizeof(struct DULNode));
    19       s->data = e;
    20       
    21       s->next = q;  //先改变向右的指针
    22       p->next = s;   
    23       s->prior = p;  //改变向左的指针
    24       q->prior = s;
    25       return 0;
    26 }
    ListInsert.c

    15,ListDelete操作

     1 #include"defs.h"
     2 
     3 int ListDelete(linklist L, int i, int *e)
     4 {
     5     linklist p = L;
     6     linklist q;
     7     int j = 0;
     8     if (i<1 || i>ListLength(L))  //位置是否合理
     9         exit(0);
    10     
    11     while (j < i-1)    //找到第i-1个元素
    12     {
    13          ++j;
    14          p = p->next;
    15     }
    16     q = p->next;  //p指向第i个结点
    17    *e = q->data;
    18     
    19      p->next = q->next;
    20      q->next->prior = p;
    21      free(q);
    22      return 0;
    23 }
    ListDelete.c

    16,TravelList操作

     1 #include"defs.h"
     2 
     3 void TravelList(linklist L)
     4 {
     5     linklist p = L->next;
     6     int j = 0;
     7     while (p != L)
     8     {
     9           ++j;
    10           printf("第%d个元素值为:%d
    ", j, p->data);
    11           p = p->next;
    12     }
    13 }
    TravelList.c

    17,TravelListBack操作,逆序输出表中元素

     1 #include"defs.h"
     2 
     3 void TravelListBack(linklist L)
     4 {
     5      linklist p = L->prior;
     6      while (p != L)
     7      {
     8           printf("%d ", p->data);
     9           p = p->prior;
    10      }
    11      printf("
    ");
    12 }
    TravelListBack.c
  • 相关阅读:
    prepareStatement的用法和解释
    java socket报文通信(一) socket的建立
    java多线程小结
    Java_XML操作
    socket实例2
    socket实例1
    Socket小结
    从源码角度理解android动画Interpolator类的使用
    android使用属性动画代替补间动画
    OKHttp的简单使用
  • 原文地址:https://www.cnblogs.com/rookiefly/p/3452676.html
Copyright © 2011-2022 走看看