最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C語言實(shí)現(xiàn)單鏈表實(shí)現(xiàn)方法

 更新時(shí)間:2017年08月11日 08:31:02   作者:卡哇伊的檸檬  
這篇文章主要介紹了C語言實(shí)現(xiàn)單鏈表實(shí)現(xiàn)方法的相關(guān)資料,鏈表分為單向鏈表、雙向鏈表、循環(huán)鏈表,需要的朋友可以參考下

C語言實(shí)現(xiàn)單鏈表實(shí)現(xiàn)方法

鏈表和我們之前實(shí)現(xiàn)過的順序表一樣,都是簡(jiǎn)單的數(shù)據(jù)結(jié)構(gòu),鏈表分為單向鏈表、雙向鏈表、循環(huán)鏈表。而單向鏈表又分為兩種實(shí)現(xiàn)方法,一種為帶頭節(jié)點(diǎn)的單鏈表,一種為不帶頭節(jié)點(diǎn)的單鏈表。我們來具體看看不帶頭節(jié)點(diǎn)的單鏈表的實(shí)現(xiàn)

單鏈表:它是一種鏈?zhǔn)酱鎯?chǔ)的線性表,用一組地址任意的存儲(chǔ)單元存放線性表的數(shù)據(jù)元素,稱存儲(chǔ)單元為一個(gè)節(jié)點(diǎn)。

今天我們來實(shí)現(xiàn)一些單鏈表的簡(jiǎn)單接口

先看看單鏈表的結(jié)構(gòu): (為了通用性,我們將類型重命名為DataType)

typedef int DataType;

//鏈表
typedef struct Node
{
  DataType *data;
  struct Node *next;
}Node, *pNode, *pList;

接下來看看我們要實(shí)現(xiàn)的接口:

void InitLinkList(pList *pplist);//初始化鏈表
pNode BuyNode(DataType d);//創(chuàng)建鏈表節(jié)點(diǎn)
void PushBack(pList *pplist, DataType d);//尾插
void PopBack(pList *pplist);//尾刪
void PushFront(pList *pplist, DataType d);//頭插
void PopFront(pList *pplist);//頭刪
void PrintList(pList plist);//打印鏈表
pNode Find(pList plist, DataType d);//查找指定元素
void Remove(pList *pplist, DataType d);//刪除指定的一個(gè)元素
void RemoveAll(pList *pplist, DataType d);//刪除指定的所有元素
void Insert(pList *pplist, pNode pos, DataType d);//指定位置的后面插入
void Erase(pList *pplist, pNode pos);//指定位置刪除
void DestroyList(pList *pplist);//銷毀鏈表

來看看每個(gè)接口的具體實(shí)現(xiàn):

pNode BuyNode(DataType d)
{
  pNode newNode = (pNode)malloc(sizeof(Node));
  if (newNode == NULL)
  {
    perror("malloc");
    exit(EXIT_FAILURE);
  }
  newNode->data = d;
  newNode->next = NULL;
  return newNode;
}
void InitLinkList(pList *pplist)
{
  assert(pplist);
  *pplist = NULL;
}
void PushBack(pList *pplist, DataType d)
{
  assert(pplist);
  pNode newNode = BuyNode(d);
  pNode cur = *pplist;
  //鏈表沒有節(jié)點(diǎn)
  if (*pplist == NULL)
  {
    *pplist = newNode;
    return;
  }
  //鏈表有節(jié)點(diǎn)
  while (cur->next != NULL)
  {
    cur = cur->next;
  }
  cur->next = newNode;
}
void PopBack(pList *pplist)
{
  pNode cur = *pplist;
  pNode prev = NULL;
  assert(pplist);
  //鏈表沒有節(jié)點(diǎn)
  if (*pplist == NULL)
  {
    return;
  }
  //鏈表有一個(gè)節(jié)點(diǎn)
  if (cur->next == NULL)
  {
    free(*pplist);
    *pplist = NULL;
    return;
  }
  //鏈表有兩個(gè)及兩個(gè)以上節(jié)點(diǎn)
  while (cur->next != NULL)
  {
    prev = cur;//prev中保存的是cur之前的那個(gè)節(jié)點(diǎn)
    cur = cur->next;
  }
  prev->next = NULL;
  free(cur);
}
void PushFront(pList *pplist, DataType d)
{
  pNode newNode = BuyNode(d);
  //pNode cur = *pplist;
  assert(pplist);
  ////鏈表沒有節(jié)點(diǎn)
  //if (*pplist == NULL)
  //{
  // *pplist = newNode;
  //}
  ////鏈表有節(jié)點(diǎn)
  newNode->next = *pplist;
  *pplist = newNode;

}
void PopFront(pList *pplist)
{
  pNode cur = *pplist;
  assert(pplist);
  //鏈表為空
  if (*pplist == NULL)
  {
    return;
  }
  *pplist = cur->next;
  free(cur);
  cur = NULL;
}
void PrintList(pList plist)
{
  pNode cur = plist;
  while (cur)
  {
    printf("%d-->", cur->data);
    cur = cur->next;
  }
  printf("NULL\n");
}
pNode Find(pList plist, DataType d)
{
  pNode cur = plist;
  while (cur)
  {
    if (cur->data == d)
    {
      return cur;
    }
    cur = cur->next;
  }
  return NULL;
}
void Remove(pList *pplist, DataType d)
{
  pNode cur = *pplist;
  pNode prev = NULL;
  assert(pplist);
  if (cur == NULL)
  {
    return;
  }
  while (cur)
  {
    if (cur->data == d)
    {
      pNode del = cur;
      if (cur == *pplist)
      {
        *pplist = cur->next;
      }
      prev->next = cur->next;
      free(del);
      del = NULL;
      return;
    }
    else
    {
      prev = cur;
      cur = cur->next;
    }
  }
}
void RemoveAll(pList *pplist, DataType d)
{
  pNode cur = *pplist;
  pNode prev = NULL;
  assert(pplist);
  if (*pplist == NULL)
  {
    return;
  }
  while (cur)
  {
    if (cur->data == d)
    {
      pNode del = cur;
      if (cur == *pplist)
      {
        *pplist = cur->next;
      }
      else
      {
        prev->next = cur->next;
      }
      cur = cur->next;
      free(del);
      del = NULL;
    }
    else
    {
      prev = cur;
      cur = cur->next;
    }
  }

}
//在pos后面插入一個(gè)元素
void Insert(pList *pplist, pNode pos, DataType d)
{
  pNode newNode = BuyNode(d);
  assert(pplist);
  assert(pos);
  if (*pplist == NULL)
  {
    PushFront(pplist, d);
    return;
  }
  newNode->next = pos->next;
  pos->next = newNode;
}
void Erase(pList *pplist, pNode pos)
{
  assert(pplist);
  assert(pos);
  //要?jiǎng)h除的是尾節(jié)點(diǎn)
  if (pos->next == NULL)
  {
    PopBack(pplist);
  }
  //刪除的是非尾節(jié)點(diǎn)
  else
  {
    pNode del = pos->next;
    pos->data = pos->next->data;
    pos->next = pos->next->next;
    free(del);
    del = NULL;
  }
}
void DestroyList(pList *pplist)
{
  assert(pplist);
  pNode cur = *pplist;
  while (cur)
  {
    pNode del = cur;
    cur = cur->next;
    printf("del:%d\n", del->data);
    free(del);
    del = NULL;
  }
}

由于這些接口都較為簡(jiǎn)單,所以不進(jìn)行具體的測(cè)試展示,讀者可以自行測(cè)試

以上就是C語言實(shí)現(xiàn)單鏈表實(shí)現(xiàn)方法,如有疑問請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

  • Linux C 獲取進(jìn)程退出值的實(shí)現(xiàn)代碼

    Linux C 獲取進(jìn)程退出值的實(shí)現(xiàn)代碼

    本篇文章是對(duì)在Linux下使用c語言獲取進(jìn)程退出值的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • C語言中的字符型數(shù)據(jù)與ASCII碼表

    C語言中的字符型數(shù)據(jù)與ASCII碼表

    這篇文章主要介紹了C語言中的字符型數(shù)據(jù)與ASCII碼表,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • Opencv繪制最小外接矩形、最小外接圓

    Opencv繪制最小外接矩形、最小外接圓

    這篇文章主要為大家詳細(xì)介紹了Opencv繪制最小外接矩形、最小外接圓的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • C中的volatile使用方法

    C中的volatile使用方法

    volatile 影響編譯器編譯的結(jié)果,指出,volatile 變量是隨時(shí)可能發(fā)生變化的,與volatile變量有關(guān)的運(yùn)算,不要進(jìn)行編譯優(yōu)化,以免出錯(cuò)
    2013-02-02
  • C語言中 type *(0)的具體使用

    C語言中 type *(0)的具體使用

    表達(dá)式?type * (0)?在 C/C++ 編程中是一個(gè)常見的技巧,通常用于內(nèi)核編程和一些系統(tǒng)編程場(chǎng)景中,本文主要介紹了C語言中 type *(0)的具體使用,感興趣的可以了解一下
    2024-08-08
  • Qt多線程實(shí)現(xiàn)網(wǎng)絡(luò)發(fā)送文件功能

    Qt多線程實(shí)現(xiàn)網(wǎng)絡(luò)發(fā)送文件功能

    這篇文章主要為大家詳細(xì)介紹了Qt多線程實(shí)現(xiàn)網(wǎng)絡(luò)發(fā)送文件功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • C語言實(shí)現(xiàn)貪吃蛇游戲(單人版)

    C語言實(shí)現(xiàn)貪吃蛇游戲(單人版)

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)貪吃蛇游戲單人版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • 數(shù)組指針、指針數(shù)組以及二位數(shù)組的深入解析

    數(shù)組指針、指針數(shù)組以及二位數(shù)組的深入解析

    下面來講講多維數(shù)組與指針的關(guān)系。與普通數(shù)組一樣,使用多維數(shù)組時(shí),實(shí)際上將其自動(dòng)轉(zhuǎn)換為指向該數(shù)組第一個(gè)元素的指針
    2013-09-09
  • C++ 中const對(duì)象與const成員函數(shù)的實(shí)例詳解

    C++ 中const對(duì)象與const成員函數(shù)的實(shí)例詳解

    這篇文章主要介紹了C++ 中const對(duì)象與const成員函數(shù)的實(shí)例詳解的相關(guān)資料,希望通過本文能讓大家徹底掌握該如何使用,需要的朋友可以參考下
    2017-08-08
  • Qt的qDebug使用小結(jié)

    Qt的qDebug使用小結(jié)

    使用qDebug()函數(shù)它可以把調(diào)試信息直接輸出到控制臺(tái)上,本文就來介紹一下qDebug的具體使用,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-06-06

最新評(píng)論

江达县| 科尔| 朝阳县| 西藏| 平塘县| 澄城县| 日照市| 英山县| 信宜市| 汝州市| 宁国市| 龙南县| 方正县| 仁化县| 德钦县| 保靖县| 泗洪县| 溧阳市| 灵石县| 息烽县| 乌审旗| 江陵县| 保德县| 镇康县| 嵊泗县| 平远县| 缙云县| 荣昌县| 甘南县| 淮阳县| 佛教| 广饶县| 微博| 阳江市| 三原县| 大石桥市| 南江县| 库尔勒市| 萍乡市| 东乡| 宁夏|