鱼C论坛

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

[技术交流] c链表

[复制链接]
发表于 2018-8-27 21:20:05 | 显示全部楼层 |阅读模式

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

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

x
刚刚用c写了一个数据链表,,希望大佬可以指导指导

  1. /******************/
  2. /*一个小的链表程序*/
  3. /******************/
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. //结点的结构体
  7. typedef struct node
  8. {
  9.     char data;
  10.     struct node*next;
  11. }Node;
  12. //链表的结构体
  13. typedef struct
  14. {
  15.     Node *head;
  16.     Node *list1;
  17.     int num;
  18. }NList;
  19. //生成一个链表
  20. void CreateList(NList *list)
  21. {
  22.     list->head = NULL;
  23.     list->list1 = NULL;
  24.     list->num = 0;
  25. }
  26. //向链表中添加数据
  27. void AddList(NList *list)
  28. {
  29.     char data;
  30.     scanf("%c",&data);
  31.     while(data != '#')
  32.     {
  33.         if(list->head == NULL)
  34.         {
  35.             list->head = (Node *)malloc(sizeof(Node));
  36.             list->head->data = data;
  37.             list->head->next = NULL;
  38.             list->list1 = list->head;
  39.             list->num = 1;
  40.         }
  41.         else
  42.         {
  43.             Node *p = (Node *)malloc(sizeof(Node));
  44.             list->list1->next = p;
  45.             p->data = data;
  46.             list->list1 = p;
  47.             list->list1->next = NULL;
  48.             list->num++;
  49.         }
  50.         scanf("%c",&data);
  51.     }
  52. }
  53. //显示链表中的数据
  54. void ShowList(NList list)
  55. {
  56.     Node *P1;
  57.     P1=list.head;
  58.     while(P1 != NULL)
  59.     {
  60.         printf("%c",P1->data);
  61.         P1 = P1->next;
  62.     }
  63. }
  64. //删除链表头的数据
  65. void DelList_head(NList *list)
  66. {
  67.     if(list->head != NULL)
  68.     {
  69.         Node *p2 = list->head;
  70.         list->head = p2->next;
  71.         list->num--;
  72.         free(p2);
  73.     }
  74.     else
  75.     {
  76.         printf("链表中不存在数据\n");
  77.     }
  78. }
  79. int main()
  80. {
  81.     NList list2;
  82.     CreateList(&list2);
  83.     AddList(&list2);
  84.     ShowList(list2);
  85.     printf("\n链表中的数据个数是:%d个\n",list2.num);
  86.     DelList_head(&list2);
  87.     ShowList(list2);
  88.     printf("\n链表中的数据个数是:%d个\n",list2.num);
  89.     return 0;
  90. }
复制代码

程序执行的结构如下:


sh.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-30 18:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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