鱼C论坛

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

[学习笔记] 静态数组

[复制链接]
发表于 2020-5-4 16:13:13 | 显示全部楼层 |阅读模式

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

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

x
最近看到数据结构静态数组部分,自认为听懂了,但是一上手发现问题很多,搞了很久好像弄完了,故把代码贴上来,抛砖引玉,希望大家提出不足和改进

  1. #define MAXSIZE 1000

  2. typedef struct component
  3. {
  4.     int data;
  5.     int cur;
  6. }component;

  7. component StaticLinkList[MAXSIZE];

  8. void InitList()
  9. {
  10.     for(int i=0;i<MAXSIZE-1;i++)
  11.     {
  12.         StaticLinkList[i].cur = i+1;
  13.     }
  14.     StaticLinkList[MAXSIZE-1].cur = 0;
  15. }

  16. void Print()
  17. {
  18.     printf("\n--------------------------\n");
  19.     for(int i=StaticLinkList[MAXSIZE-1].cur;i;i = StaticLinkList[i].cur)
  20.     {
  21.         printf("%d\t",StaticLinkList[i].data);
  22.     }
  23.     printf("\n-----------------------------\n");
  24. }

  25. void ListInsert(int cur,int data)
  26. {
  27.     if(cur<1 || cur > MAXSIZE-2)
  28.         return;

  29.     if(StaticLinkList[MAXSIZE-1].cur==0)
  30.     {
  31.         int reserve = StaticLinkList[0].cur;
  32.         StaticLinkList[0].cur = StaticLinkList[reserve].cur;
  33.         StaticLinkList[reserve].data = data;
  34.         StaticLinkList[reserve].cur = 0;
  35.         StaticLinkList[MAXSIZE-1].cur = reserve;
  36.     }

  37.     else if(cur == 1)
  38.     {
  39.         int reserve = StaticLinkList[0].cur;
  40.         StaticLinkList[reserve].data = data;
  41.         StaticLinkList[0].cur = StaticLinkList[reserve].cur;
  42.         StaticLinkList[reserve].cur = StaticLinkList[MAXSIZE-1].cur;
  43.         StaticLinkList[MAXSIZE-1].cur = reserve;
  44.     }
  45.     else
  46.     {
  47.         int tmp = StaticLinkList[MAXSIZE-1].cur;
  48.         for(int i=1;i<cur-1 && StaticLinkList[tmp].cur;i++,tmp = StaticLinkList[tmp].cur);
  49.         int reserve = StaticLinkList[0].cur;
  50.         StaticLinkList[reserve].data = data;
  51.         StaticLinkList[0].cur = StaticLinkList[reserve].cur;
  52.         StaticLinkList[reserve].cur = StaticLinkList[tmp].cur;
  53.         StaticLinkList[tmp].cur = reserve;
  54.     }
  55. }

  56. void ListDelete(int cur)
  57. {
  58.     if(cur < 1 || cur > MAXSIZE-2)
  59.         return;
  60.     int tmp = StaticLinkList[MAXSIZE-1].cur;
  61.     if(tmp == 0)
  62.     {
  63.         printf("\nempty\n\n");
  64.     }

  65.     if(cur == 1)
  66.     {
  67.         StaticLinkList[MAXSIZE-1].cur = StaticLinkList[tmp].cur;
  68.         StaticLinkList[tmp].cur = StaticLinkList[0].cur;
  69.         StaticLinkList[0].cur = tmp;
  70.     }
  71.     else
  72.     {
  73.         for(int i=1;i<cur-1 && StaticLinkList[tmp].cur;i++,tmp = StaticLinkList[tmp].cur);
  74.         if(tmp == 0)
  75.         {
  76.             printf("\n\n\n cur error!\n\n\n");
  77.             return;
  78.         }

  79.         int del = StaticLinkList[tmp].cur;
  80.         StaticLinkList[tmp].cur = StaticLinkList[del].cur;
  81.         StaticLinkList[del].cur = StaticLinkList[0].cur;
  82.         StaticLinkList[0].cur = del;
  83.     }
  84. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-8 21:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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