鱼C论坛

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

[技术交流] 单链表可以理解为翻书吗?但好像有不恰当(节点与节点的位置不连续)!

[复制链接]
发表于 2020-3-3 17:49:17 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 Cool_Breeze 于 2020-3-4 10:18 编辑

原文作者链接!
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #define SIZE sizeof(struct test)
  4. typedef struct test
  5. {
  6.         int n;
  7.         struct test *next;
  8. }*P;//定义P为(struct test *)的别名
  9. P initLink();//初始化链表
  10. void Display(P p);//打印链表元素
  11. P InsertElem(P p,int elem,int add); //插入节点
  12. P DeleteElem(P p,int position);//删除节点
  13. int FindElem(P p,int elem);//查找元素
  14. P change(P p,int data);//更改元素值
  15. //简单搜索指定元素的位置
  16. int location(P p,int element);
  17. int main()
  18. {
  19.         //初始化链表
  20.         P p=initLink();
  21.         Display(p);
  22.        
  23.         printf("在8的位置插入元素88!\n");
  24.         p=InsertElem(p,88,8);
  25.         Display(p);
  26.        
  27.         printf("删除元素88\n");
  28.         p=DeleteElem(p,8);
  29.         Display(p);
  30.        
  31.         printf("查找元素6的位置\n");
  32.         int position=FindElem(p,6);
  33.         if (position < 0) printf("没有找到该元素\n");
  34.         else printf("该元素的位置为:%d\n",position);
  35.        
  36.         printf("更该第四个元素的值为666!\n");
  37.         p=change(p,666);
  38.         Display(p);
  39.        
  40.         //简单搜索指定元素的位置
  41.         int element=1;
  42.         int locat=location(p,element);
  43.         if (locat < 0) printf("没有找到该元素\n");
  44.         else printf("元素值:%d的位置为:%d\n",element,locat);
  45.        
  46.         return 0;
  47. }

  48. P initLink()
  49. {
  50.         P p=(P)malloc(SIZE);//头指针
  51.         P temp = p;//指向头
  52.         int i;
  53.         for (i=1;i<=10;i++)
  54.         {
  55.                 P a=(P)malloc(SIZE);//(首元节点)
  56.                 a->n=i;
  57.                 a->next=NULL;
  58.                 temp->next=a;
  59.                 temp=temp->next;
  60.         }
  61.         return p;
  62. }
  63. void Display(P p)
  64. {
  65.         P temp = p;
  66.         while (temp->next)//指向NULL时,代表结束
  67.         {
  68.                 temp=temp->next;
  69.                 printf("%d ",temp->n);
  70.         }
  71.         putchar('\n');
  72. }
  73. P InsertElem(P p,int elem,int add)
  74. {
  75.         P temp=p;
  76.         //找到插入节点的上一个节点
  77.         int i;
  78.         for (i=1;i<add;i++)
  79.         {
  80.                 if (! temp)//temp非空!
  81.                 {
  82.                         printf("插入位置ERROR!\n");
  83.                         return p;
  84.                 }
  85.                 temp=temp->next;
  86.         }
  87.         //创建插入节点A;
  88.         P A=(P)malloc(SIZE);
  89.         A->n=elem;//赋值
  90.         A->next=temp->next;//指向下一个节点
  91.         temp->next=A;//上一个节点指向A
  92.         return p;
  93. }
  94. P DeleteElem(P p,int position)//删除节点
  95. {
  96.         P temp=p;
  97.         int i;
  98.         for (i=1;i<position;i++)
  99.         {
  100.                 temp=temp->next;
  101.         }
  102.         P pause=temp->next;//暂时储存被删除节点
  103.         temp->next=temp->next->next;
  104.         free(pause);//释放内存
  105.         return p;
  106. }
  107. int FindElem(P p,int elem)
  108. {
  109.         P temp=p;
  110.         int i=1;
  111.         while (temp->next)
  112.         {
  113.                 temp=temp->next;
  114.                 if (temp->n == elem) return i;
  115.                 i++;
  116.         }
  117.         return -1;
  118. }
  119. P change(P p,int data)//更改元素值
  120. {
  121.         P temp = p;//头指针(不是首元指针)
  122.         int i=1;
  123.         for (;i<=4;i++)
  124.         {
  125.                 temp=temp->next;
  126.         }
  127.         temp->n=data;
  128.         return p;
  129. }
  130. //简单搜索指定元素的位置
  131. int location(P p,int element)
  132. {
  133.         P temp=p;
  134.         int locat=1;
  135.         while (temp->next)
  136.         {
  137.                 temp=temp->next;
  138.                 if (temp->n == element) return locat;
  139.                 locat ++;
  140.         }
  141.         return -1;
  142. }
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-5 00:16

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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