• 带头结点的链表实现线性表的基本操作


    带头结点的链表和不带头结点的链表之间存在一定的区别

    1.phead是从第一个开始循环(第一个数据元素), 带头结点的phead从第二个结点开始循环(第一个数据元素)

    2.不带头结点的链表在插入和删除操作的时候需要把插入的第一个位置和其他位置分开讨论,带头结点的链表在插入和删除的时候操作一致

    主要原因是不带头结点的链表phead和phead->next赋值的时候不一致的情况造成的。

    所以带头结点的链表比不带头结点的链表方便

    所有的操作都是针对元素本身,所以需要找到或者给出元素所在的位置i,才能对其进行操作,插入和删除找到的是i-1

    若是对整个结构操作,则需要利用循环,同样利用到位置i或者指针p对数据元素的操作。

      1 //线性表链式存储
      2 
      3 //链表结点
      4 struct LNode
      5 {
      6     elemtype data;
      7     struct LNode *next;
      8 };
      9 
     10 //1.初始化带有头结点的链表
     11 int InitList_L(LNode *pHead)
     12 {
     13     pHead = (LNode*)malloc(sizeof(LNode));
     14     if (!pHead)
     15     {
     16         return ERROR;
     17     }
     18     pHead->next = NULL;
     19     return OK;
     20 }
     21 
     22 //2.建立链表的方式之一,通过循环调用插入链表结点函数
     23 
     24 int CreatList_L(LNode *pHead)
     25 {
     26     
     27     if (!pHead)
     28     {
     29         return ERROR;
     30     }
     31     
     32      
     33     for (int i = 3;i > 0;--i)
     34     {
     35         LNode *p = (LNode*)malloc(sizeof(LNode));
     36         scanf("%d",&(p->data));
     37         p->next = pHead->next;
     38         pHead->next = p;
     39     }
     40     return OK;
     41 }
     42 
     43 //3.插入结点
     44 int ListInsert_L(LNode *pHead,int i,elemtype e)
     45 {
     46     LNode *p = pHead;
     47     int j = 0;
     48     while (p && j < i-1)
     49     {
     50         p = p->next;
     51         j++;
     52     }
     53     LNode *s = (LNode *)malloc(sizeof(LNode));
     54     s->data = e;
     55     s->next = p->next;
     56     p->next = s;
     57     return OK;
     58 }
     59 int CreatList_L2(LNode *pHead)
     60 {
     61 
     62     LNode *p = pHead ;
     63     int e = 0;
     64     for (int i = 2;i > 0;--i)
     65     {
     66         scanf("%d",&e);
     67         ListInsert_L(p,1,e);
     68         p = p->next;
     69     }
     70     return OK;
     71 }
     72 
     73 
     74 
     75 //4.删除链表结点
     76 int DeleteList_L(LNode *pHead,int i)
     77 {
     78     LNode *p = pHead;
     79     int j = 0;
     80     while (p && j < i-1)
     81     {
     82         p = p->next;
     83         ++j;
     84     }
     85     LNode *q = p->next;
     86     p->next = q->next;
     87     free(q);
     88     return OK;
     89 }
     90 //5.清空或者摧毁链表(包括头结点)
     91 //直接清除所有节点
     92 int ClearList(LNode *pHead)
     93 {
     94     LNode *pTemp= NULL;
     95     while(pHead)
     96     {
     97         pTemp = pHead ->next;
     98         free(pHead);
     99         pHead = pTemp;
    100     }
    101     return  OK;
    102 }
    103 //间接清空所有结点
    104 int ClearList_L(LNode *pHead)
    105 {
    106     //删除数据元素结点
    107     while (pHead->next)
    108     {
    109         DeleteList_L(pHead,1);
    110     }
    111     free(pHead);//最后删除头结点
    112     return OK;
    113 }
    114 
    115 //6.取得线性表的第i个数据元素
    116 int GetList_L(LNode *pHead,int i)
    117 {
    118     int j = 1;
    119     LNode *p = pHead->next;
    120     while(p && j < i)
    121     {
    122         p->next;
    123     }
    124     //return p;//返回位置指针
    125     return p->data;//返回数据元素
    126     
    127 }
    128 //7.后继
    129 
    130 int NextElem_L(LNode *pHead,int i)
    131 {
    132     int j = 1;
    133     LNode *p = pHead->next;
    134     while(p && j < i)
    135     {
    136         p->next;
    137     }
    138     
    139     return p->next->data;//返回后继指针
    140 }
    141 
    142 //8.前驱
    143 int PriorElem_L(LNode *pHead,int i)
    144 {
    145     int j = 0;
    146     LNode *p = pHead;
    147     while(p && j < i -1)
    148     {
    149         p->next;
    150     }
    151 
    152     return p->next->data;//返回后继指针
    153 }
    154 
    155 //9.长度
    156 int ListLength(LNode *pHead)
    157 {
    158     int length = 0;
    159     for (LNode * p = pHead->next;p != NULL;)
    160     {
    161         length++;
    162         p=p->next;
    163     }
    164     return length;
    165 }
    166 //10.输出
    167 void ListDisplay(LNode *pHead)
    168 {
    169     
    170     for (LNode * p = pHead->next;p != NULL;)
    171     {
    172         printf("%d ",p->data);
    173         p=p->next;
    174     }
    175     
    176 }
    177 
    178 int main()
    179 {
    180     LNode *pHead =NULL;
    181     //InitList_L(pHead);
    182     pHead = (LNode*)malloc(sizeof(LNode));
    183     pHead->next = NULL;
    184     if (!pHead)
    185     {
    186         return ERROR;
    187     }
    188     CreatList_L(pHead);
    189     ListDisplay(pHead);
    190     ListInsert_L(pHead,2,190);
    191     printf("
    ");
    192     ListDisplay(pHead);
    193     printf("
    ");
    194     DeleteList_L(pHead,3);
    195 ListDisplay(pHead);
    196     return OK;
    197 }
    View Code
  • 相关阅读:
    iOS NSUserDefaults 存储可变数组问题
    iOS之[文件下载 / 大文件下载 / 断点下载]
    macOS 新手开发:第 2 部分
    iOS 游戏素材
    iOS 动画
    macOS 开发
    iOS 之访问权限以及跳转到系统界面
    蓝桥杯—ALGO-18 单词接龙(DFS)
    蓝桥杯—ALGO-12 幂方分解(递归递推)
    蓝桥杯—ALGO-131 Beaver's Calculator
  • 原文地址:https://www.cnblogs.com/liuweilinlin/p/3149096.html
Copyright © 2020-2023  润新知