鱼C论坛

 找回密码
 立即注册
查看: 3302|回复: 1

[已解决]静态链表插入元素,打印结果总是只有第二个位置有数,且后面会覆盖前面

[复制链接]
发表于 2021-3-5 11:29:10 | 显示全部楼层 |阅读模式

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

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

x


  1. //用C++新建一个类,实现静态表的初始化, 获取元素的值,插入,删除操作
  2. //要求:
  3. //1.数据类型可以存储字符串
  4. //2.插入数据,是插入数据后,链表的数据顺序如下:
  5. //"ZHAO"
  6. //"QIAN"
  7. //"SUN"
  8. //"LI"
  9. //"ZHOU"
  10. //"WU"
  11. //"ZHENG"
  12. //"WANG"

  13. #include<winuser.inl>
  14. #include<stdio.h>
  15. #include <cstdlib>
  16. #include<string.h>

  17. #define MAXSIZE 10
  18. #define True 1
  19. void InitialList(struct People* people);
  20. int Getlength(struct People* space);
  21. void InsertElement(struct People* space, int position, int element);
  22. void DeleteElement(struct People* space, int position);
  23. void Free(struct People* space, int position_index);
  24. void printfInfo(struct People* space);


  25. struct People
  26. {
  27.         int data;
  28.         int cur;
  29. }people[MAXSIZE];
  30.        

  31. void InitialList(struct People *people)
  32. {
  33.         int i;
  34.         for(i = 0; i < MAXSIZE - 1 ; i++)
  35.         {
  36.                 people[i].cur = i + 1;
  37.         }
  38.         people[MAXSIZE - 1].cur = 0;
  39. }

  40. int Getlength(struct People* space)
  41. {
  42.         int count = 0;
  43.         //最后一个元素的游标是第一个存有元素的位置下标
  44.         int i = space[MAXSIZE - 1].cur;
  45.         while (i)
  46.         {
  47.                 count++;
  48.                 i = space[i].cur;
  49.         }
  50.         return count;
  51. }

  52. int Get_first_spare_index(struct People* space)
  53. {
  54.         //首先需要判断备用链表是不是空,如果备用链表为空,则说明没有多余的空间可以插入元素
  55.         //当前数组第一个元素的cur,是的备用链表的第一个元素的下标
  56.         int i = space[0].cur;
  57.         if (space[0].cur)  //如果备用链表非空
  58.         {
  59.                 //把第一个备用元素从备用链表中剔除,i即是被剔除的那个备用元素的下标
  60.                 space[0].cur = space[i].cur;
  61.         }
  62.         return i;
  63. }
  64. //插入元素
  65. void InsertElement(struct People* space, int position, int element)
  66. {
  67.         int spare_of_first_e, k ;
  68.         k = MAXSIZE - 1;  //当前k是最后一个元素的下标
  69.         if (position < 1 || position > MAXSIZE)
  70.         {
  71.                 printf("out index\n");
  72.         }
  73.         else
  74.         {
  75.                 spare_of_first_e = Get_first_spare_index(space);
  76.                 if (spare_of_first_e)
  77.                 {
  78.                         space[spare_of_first_e].data = element;
  79.                         //找到要插入位置的前一个位置元素的游标,将游标等于position,
  80.                         //再将当前元素的游标指向原本位置的当前元素的下标
  81.                         //由于游标和下标之间没有连续的对应关系,所以不能通过i++的形式来直接索引
  82.                         for (int z = 1; z <= position - 1; z++) //对下标的索引
  83.                         {
  84.                                 k = space[k].cur;
  85.                         }
  86.                         space[spare_of_first_e].cur = space[k].cur;
  87.                         space[k].cur = spare_of_first_e;
  88.                 }
  89.         }
  90. }
  91.        


  92. //删除元素
  93. void DeleteElement(struct People* space, int position)
  94. {
  95.         if (position < 1 || position > Getlength(space) + 1)
  96.         {
  97.                 printf("out index\n");
  98.         }
  99.         else
  100.         {
  101.                 int k ;
  102.                 k = MAXSIZE - 1;
  103.                 for (int z = 1; z <= position - 1; z++) //对下标的索引
  104.                 {
  105.                         k = space[k].cur;  //获得要删除的元素的前一个元素的下标
  106.                 }
  107.                 space[k].cur = space[position].cur;
  108.                 Free(space, position);
  109.         }
  110. }

  111. void Free(struct People* space, int position_index)
  112. {
  113.         //将指定下标的节点回收到备用链表
  114.         space[position_index].cur = space[0].cur ;
  115.         space[0].cur = position_index;
  116. }

  117. void printfInfo(struct People *space)
  118. {
  119.         printf("all element: \n");
  120.         for (int i = 0; i < MAXSIZE; i++)
  121.         {
  122.                 printf("%d ", space[i].data);
  123.         }
  124.         printf("\n");
  125. }

  126. int main()
  127. {       
  128.         struct People space[MAXSIZE];
  129.         InitialList(space);
  130.         InsertElement(space, 2, 1);
  131.         InsertElement(space, 4, 10);
  132.         printfInfo(space);

  133. }
复制代码
最佳答案
2021-3-6 20:57:43
好多的错误,我认为你这部分没有学好,建议重新学习一下这部分内容

  1. //用C++新建一个类,实现静态表的初始化, 获取元素的值,插入,删除操作
  2. //要求:
  3. //1.数据类型可以存储字符串
  4. //2.插入数据,是插入数据后,链表的数据顺序如下:
  5. //"ZHAO"
  6. //"QIAN"
  7. //"SUN"
  8. //"LI"
  9. //"ZHOU"
  10. //"WU"
  11. //"ZHENG"
  12. //"WANG"

  13. //#include<winuser.inl>
  14. #include<stdio.h>
  15. #include <cstdlib>
  16. #include<string.h>

  17. #define MAXSIZE 10
  18. #define True 1
  19. void InitialList(struct People* people);
  20. int Getlength(struct People* space);
  21. void InsertElement(struct People* space, int position, int element);
  22. void DeleteElement(struct People* space, int position);
  23. void Free(struct People* space, int position_index);
  24. void printfInfo(struct People* space);

  25. /*
  26.    struct People
  27.    {
  28.    int data;
  29.    int cur;
  30.    }people[MAXSIZE];
  31.    */
  32. struct People
  33. {
  34.     int data;
  35.     int cur;
  36. };

  37. void InitialList(struct People *people)
  38. {
  39.     int i;
  40.     for(i = 0; i < MAXSIZE - 1 ; i++)
  41.     {
  42.         people[i].cur = i + 1;
  43.     }



  44.     // 备用链表怎么结束?
  45.     people[MAXSIZE - 2].cur = 0;



  46.     people[MAXSIZE - 1].cur = 0;
  47. }

  48. int Getlength(struct People* space)
  49. {
  50.     int count = 0;
  51.     //最后一个元素的游标是第一个存有元素的位置下标
  52.     int i = space[MAXSIZE - 1].cur;
  53.     while (i)
  54.     {
  55.         count++;
  56.         i = space[i].cur;
  57.     }
  58.     return count;
  59. }

  60. int Get_first_spare_index(struct People* space)
  61. {
  62.     //首先需要判断备用链表是不是空,如果备用链表为空,则说明没有多余的空间可以插入元素
  63.     //当前数组第一个元素的cur,是的备用链表的第一个元素的下标
  64.     int i = space[0].cur;
  65.     if (space[0].cur)  //如果备用链表非空
  66.     {
  67.         //把第一个备用元素从备用链表中剔除,i即是被剔除的那个备用元素的下标
  68.         space[0].cur = space[i].cur;
  69.     }
  70.     return i;
  71. }
  72. //插入元素
  73. void InsertElement(struct People* space, int position, int element)
  74. {
  75.     int spare_of_first_e, k ;
  76.     k = MAXSIZE - 1;  //当前k是最后一个元素的下标



  77.     // position 等于 MAXSIZE 也不行吧?
  78.     //if (position < 1 || position > MAXSIZE)
  79.     if (position < 1 || position >= MAXSIZE)
  80.     {
  81.         printf("out index\n");
  82.     }
  83.     else
  84.     {
  85.         spare_of_first_e = Get_first_spare_index(space);
  86.         if (spare_of_first_e)
  87.         {
  88.             space[spare_of_first_e].data = element;
  89.             //找到要插入位置的前一个位置元素的游标,将游标等于position,
  90.             //再将当前元素的游标指向原本位置的当前元素的下标
  91.             //由于游标和下标之间没有连续的对应关系,所以不能通过i++的形式来直接索引
  92.             for (int z = 1; z <= position - 1; z++) //对下标的索引
  93.             {
  94.                 // 这里难道不需要判断一下吗?
  95.                 // 试想一下,当前链表只有3个元素,而插入的位置是5,那么这里将会是怎么样的一个状态呢?
  96.                 k = space[k].cur;
  97.             }
  98.             space[spare_of_first_e].cur = space[k].cur;
  99.             space[k].cur = spare_of_first_e;
  100.         }
  101.     }
  102. }



  103. //删除元素
  104. void DeleteElement(struct People* space, int position)
  105. {
  106.     // 这里为什么要加1呢?
  107.     // 如果链表只有一个元素我要删除位置2呢?
  108.     //if (position < 1 || position > Getlength(space) + 1)
  109.     if (position < 1 || position > Getlength(space))
  110.     {
  111.         printf("out index\n");
  112.     }
  113.     else
  114.     {
  115.         int k ;
  116.         k = MAXSIZE - 1;
  117.         for (int z = 1; z <= position - 1; z++) //对下标的索引
  118.         {
  119.             k = space[k].cur;  //获得要删除的元素的前一个元素的下标
  120.         }

  121.         int f = space[k].cur;
  122.         space[k].cur = space[f].cur;
  123.         Free(space, f);

  124.         // 下面这波操作是在做什么啊?
  125.         //space[k].cur = space[position].cur;
  126.         //Free(space, position);
  127.     }
  128. }

  129. void Free(struct People* space, int position_index)
  130. {
  131.     //将指定下标的节点回收到备用链表
  132.     space[position_index].cur = space[0].cur ;
  133.     space[0].cur = position_index;
  134. }

  135. // 这个函数是要做什么?输出数组内容?不应该是输出链表内容吗?
  136. //void printfInfo(struct People *space)
  137. void printArray(struct People *space)
  138. {
  139.     printf("all element: \n");
  140.     for (int i = 0; i < MAXSIZE; i++)
  141.     {
  142.         printf("%d ", space[i].data);
  143.     }
  144.     printf("\n");
  145. }

  146. void printElement(struct People *space)
  147. {
  148.     printf("all element: \n");
  149.     int i = space[MAXSIZE - 1].cur;
  150.     while(i)
  151.     {
  152.         printf("%d ", space[i].data);
  153.         i = space[i].cur;
  154.     }
  155.     printf("\n");
  156. }

  157. int main()
  158. {
  159.     /*
  160.     struct People space[MAXSIZE];
  161.     InitialList(space);
  162.     InsertElement(space, 2, 1);     // 在2的位置插入1?位置1怎么办?
  163.     InsertElement(space, 4, 10);    // 位置3怎么办?
  164.     printInfo(space);
  165.     */


  166.     struct People space[MAXSIZE];
  167.     InitialList(space);
  168.     InsertElement(space, 1, 5);
  169.     InsertElement(space, 2, 9);
  170.     InsertElement(space, 3, 7);
  171.     InsertElement(space, 4, 8);
  172.     printElement(space);
  173.     DeleteElement(space, 3);
  174.     DeleteElement(space, 2);
  175.     printElement(space);
  176.     return 0;
  177. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-3-6 20:57:43 | 显示全部楼层    本楼为最佳答案   
好多的错误,我认为你这部分没有学好,建议重新学习一下这部分内容

  1. //用C++新建一个类,实现静态表的初始化, 获取元素的值,插入,删除操作
  2. //要求:
  3. //1.数据类型可以存储字符串
  4. //2.插入数据,是插入数据后,链表的数据顺序如下:
  5. //"ZHAO"
  6. //"QIAN"
  7. //"SUN"
  8. //"LI"
  9. //"ZHOU"
  10. //"WU"
  11. //"ZHENG"
  12. //"WANG"

  13. //#include<winuser.inl>
  14. #include<stdio.h>
  15. #include <cstdlib>
  16. #include<string.h>

  17. #define MAXSIZE 10
  18. #define True 1
  19. void InitialList(struct People* people);
  20. int Getlength(struct People* space);
  21. void InsertElement(struct People* space, int position, int element);
  22. void DeleteElement(struct People* space, int position);
  23. void Free(struct People* space, int position_index);
  24. void printfInfo(struct People* space);

  25. /*
  26.    struct People
  27.    {
  28.    int data;
  29.    int cur;
  30.    }people[MAXSIZE];
  31.    */
  32. struct People
  33. {
  34.     int data;
  35.     int cur;
  36. };

  37. void InitialList(struct People *people)
  38. {
  39.     int i;
  40.     for(i = 0; i < MAXSIZE - 1 ; i++)
  41.     {
  42.         people[i].cur = i + 1;
  43.     }



  44.     // 备用链表怎么结束?
  45.     people[MAXSIZE - 2].cur = 0;



  46.     people[MAXSIZE - 1].cur = 0;
  47. }

  48. int Getlength(struct People* space)
  49. {
  50.     int count = 0;
  51.     //最后一个元素的游标是第一个存有元素的位置下标
  52.     int i = space[MAXSIZE - 1].cur;
  53.     while (i)
  54.     {
  55.         count++;
  56.         i = space[i].cur;
  57.     }
  58.     return count;
  59. }

  60. int Get_first_spare_index(struct People* space)
  61. {
  62.     //首先需要判断备用链表是不是空,如果备用链表为空,则说明没有多余的空间可以插入元素
  63.     //当前数组第一个元素的cur,是的备用链表的第一个元素的下标
  64.     int i = space[0].cur;
  65.     if (space[0].cur)  //如果备用链表非空
  66.     {
  67.         //把第一个备用元素从备用链表中剔除,i即是被剔除的那个备用元素的下标
  68.         space[0].cur = space[i].cur;
  69.     }
  70.     return i;
  71. }
  72. //插入元素
  73. void InsertElement(struct People* space, int position, int element)
  74. {
  75.     int spare_of_first_e, k ;
  76.     k = MAXSIZE - 1;  //当前k是最后一个元素的下标



  77.     // position 等于 MAXSIZE 也不行吧?
  78.     //if (position < 1 || position > MAXSIZE)
  79.     if (position < 1 || position >= MAXSIZE)
  80.     {
  81.         printf("out index\n");
  82.     }
  83.     else
  84.     {
  85.         spare_of_first_e = Get_first_spare_index(space);
  86.         if (spare_of_first_e)
  87.         {
  88.             space[spare_of_first_e].data = element;
  89.             //找到要插入位置的前一个位置元素的游标,将游标等于position,
  90.             //再将当前元素的游标指向原本位置的当前元素的下标
  91.             //由于游标和下标之间没有连续的对应关系,所以不能通过i++的形式来直接索引
  92.             for (int z = 1; z <= position - 1; z++) //对下标的索引
  93.             {
  94.                 // 这里难道不需要判断一下吗?
  95.                 // 试想一下,当前链表只有3个元素,而插入的位置是5,那么这里将会是怎么样的一个状态呢?
  96.                 k = space[k].cur;
  97.             }
  98.             space[spare_of_first_e].cur = space[k].cur;
  99.             space[k].cur = spare_of_first_e;
  100.         }
  101.     }
  102. }



  103. //删除元素
  104. void DeleteElement(struct People* space, int position)
  105. {
  106.     // 这里为什么要加1呢?
  107.     // 如果链表只有一个元素我要删除位置2呢?
  108.     //if (position < 1 || position > Getlength(space) + 1)
  109.     if (position < 1 || position > Getlength(space))
  110.     {
  111.         printf("out index\n");
  112.     }
  113.     else
  114.     {
  115.         int k ;
  116.         k = MAXSIZE - 1;
  117.         for (int z = 1; z <= position - 1; z++) //对下标的索引
  118.         {
  119.             k = space[k].cur;  //获得要删除的元素的前一个元素的下标
  120.         }

  121.         int f = space[k].cur;
  122.         space[k].cur = space[f].cur;
  123.         Free(space, f);

  124.         // 下面这波操作是在做什么啊?
  125.         //space[k].cur = space[position].cur;
  126.         //Free(space, position);
  127.     }
  128. }

  129. void Free(struct People* space, int position_index)
  130. {
  131.     //将指定下标的节点回收到备用链表
  132.     space[position_index].cur = space[0].cur ;
  133.     space[0].cur = position_index;
  134. }

  135. // 这个函数是要做什么?输出数组内容?不应该是输出链表内容吗?
  136. //void printfInfo(struct People *space)
  137. void printArray(struct People *space)
  138. {
  139.     printf("all element: \n");
  140.     for (int i = 0; i < MAXSIZE; i++)
  141.     {
  142.         printf("%d ", space[i].data);
  143.     }
  144.     printf("\n");
  145. }

  146. void printElement(struct People *space)
  147. {
  148.     printf("all element: \n");
  149.     int i = space[MAXSIZE - 1].cur;
  150.     while(i)
  151.     {
  152.         printf("%d ", space[i].data);
  153.         i = space[i].cur;
  154.     }
  155.     printf("\n");
  156. }

  157. int main()
  158. {
  159.     /*
  160.     struct People space[MAXSIZE];
  161.     InitialList(space);
  162.     InsertElement(space, 2, 1);     // 在2的位置插入1?位置1怎么办?
  163.     InsertElement(space, 4, 10);    // 位置3怎么办?
  164.     printInfo(space);
  165.     */


  166.     struct People space[MAXSIZE];
  167.     InitialList(space);
  168.     InsertElement(space, 1, 5);
  169.     InsertElement(space, 2, 9);
  170.     InsertElement(space, 3, 7);
  171.     InsertElement(space, 4, 8);
  172.     printElement(space);
  173.     DeleteElement(space, 3);
  174.     DeleteElement(space, 2);
  175.     printElement(space);
  176.     return 0;
  177. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-30 21:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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