鱼C论坛

 找回密码
 立即注册
查看: 2281|回复: 0

[学习笔记] 不含头结点的单链表(自用)

[复制链接]
发表于 2021-4-2 11:30:45 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<malloc.h>
  4. #include<windows.h>
  5. typedef int SLDataType;
  6. typedef struct SListNode
  7. {
  8.         struct SListNode*_pNext;
  9.         SLDataType _data;
  10. }SListNode;

  11. void SListInit(SListNode** pHead){        //初始化
  12.         assert(pHead);
  13.         *pHead = NULL;
  14. }
  15. SListNode* SListNewNode(SLDataType data){                //创建新节点
  16.         SListNode*_pNew = (SListNode*)malloc(SListNode));
  17.         if(_pNew == NULL){
  18.                 exit(0);
  19.         }
  20.         else{
  21.                 _pNew->_data = data;
  22.                 _pNew->_pNext = NULL;
  23.                 retuen _pNew;
  24.         }
  25. }
  26. voidSListPrint(SListNode* pHead){                //打印链表
  27.         SListNode* pCur = pHead;
  28.         for(pCur;pCur != NULL;pCur = pCur->_pNext
  29.         {
  30.                 printf("%d--->",pCur->_data);
  31.         }
  32.         printf("NULL\n");
  33. }
  34. void SLlistPushBack(SListNode** pHead,SLDataType data){                //尾插
  35.         assert(pHead);
  36.         SListNode* NewNode = SListNewNOde(data);
  37.         if(*pHead == NULL)
  38.         {
  39.                 *pHead = NewNode;
  40.                 return;
  41.         }
  42.         SListNode* pTail = *pHead;
  43.         while(pTail->_pNext)
  44.         {
  45.                 pTail = pTail->_pNext;
  46.         }
  47.         pTail->_pNext = NewNode;
  48. }

  49. void SListPopBack(SListNode** pHead){                //尾删
  50.         assert(pHead);
  51.         if(NULL == *pHead);
  52.         {
  53.                 return;
  54.         }else if(NULL == (*pHead)->_pNext){
  55.                 free(*pHead);
  56.                 *pHead = NULL;
  57.         }
  58.         else {
  59.                 SListNode* pCur = *pHead;
  60.                 while(pCur->_pNext->_pNext){
  61.         pCur = pCur->_pNext;
  62.                 }
  63.         free(pCur->_pNext->_pNext);
  64.         pCur-> _pNext = NULL;
  65.         }
  66. }

  67. void SListPushFront(SListNode** pHead,SLDataType data)                //头插
  68. {
  69.         assert(pHead);
  70.         SListNode* NewNode = SListNewNode(data);
  71.         NewNode->_pNext = (*pHead);
  72.         *pHead = NewNode;
  73. }

  74. void SListPopFront(SListNode** pHead)                //头删
  75. {
  76.         assert(pHead);
  77.         SListNode* pDel = *pHead;
  78.         if((*pHead) == NULL)
  79.         {
  80.                 printf("链表为空!\n");
  81.                 return;
  82.         }
  83.         eise{
  84.                 *pHead = pDel->_pNext;
  85.                 free(pDel);
  86.         }
  87. }

  88. SListNode* SListFind(SListNode** pHead,SLDataType data){                //查找
  89.         assert(pHead);
  90.         if(*pHead == NULL)
  91.         {
  92.                 return NULL;
  93.         }
  94.         SListNode* pFind = *pHead;
  95.         while(pFind){
  96.                 if(pFind->_data == data){
  97.                         return pFind;
  98.                 }
  99.                 pFind = pFind->_pNext;
  100.         }return NULL;
  101. }

  102. void SListInsert(SListNode** pHead,SListNode* pos,SLDataType data)                //任意位置插入
  103. {
  104.         SListNode*_pNew = (SListNode*)SListNewNode(data);
  105.         if(NULL == pos){
  106.                 return;
  107.         }
  108.         _pNew->_pNext = pos->_pNext;
  109.         pos->_pNext = _pNew;
  110. }

  111. void SListErase(SListNode** pHead,SListNode* pos){                //任意位置删除
  112.         assert(pHead);
  113.         assert(pos);
  114.         if (pos == NULL||*pHead == NULL){
  115.                 return;
  116.         }
  117.         if(pHead == pos){
  118.                 *pHead = pos->_pNext;
  119.         }
  120.         else{
  121.                 SListNode * posPre = *pHead;
  122.                 while(posPre->_pNext != pos){
  123.                         posPre = posPre->_pNext;
  124.                 }
  125.                 posPre->_pNext = pos->_pNext;
  126.                 free(pos);
  127.         }
  128. }

  129. void menu()
  130. {
  131.         printf("***********欢迎使用电脑设备管理系统**************\n");
  132.         printf("***************请选择你的功能********************\n");
  133.         printf("1.初始化*****************************2.创建新节点\n");
  134.         printf("3.头插  *****************************4.头删\n");
  135.         printf("5.尾插  *****************************6.尾删\n");
  136.         printf("7.查找  *****************************8.插入\n");
  137.         printf("9.删除 ******************************10.输出\n");
  138. }

  139. main()
  140. {
  141.        
  142. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-26 09:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表