免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 889 | 回复: 1
打印 上一主题 下一主题

问两个数据结构问题,谢谢了 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-05-19 11:51 |只看该作者 |倒序浏览
第一个是线性表顺序链接。编译成功,运行有个小错误。就是删除结点结束后。最后一个结点和前驱结点相同。我找来找去找不出原因。郁闷中。
  1. #include <stdio.h>;
  2. #define maxsize 10
  3. typedef struct sqlist
  4. {
  5.   int data[maxsize];
  6.   int last;
  7. } sqlistTp;
  8. int main()
  9. {
  10.   sqlistTp L;
  11.   int input;
  12.   int count = 1;
  13.   Initsqlist (&L);
  14.   printf("请输入一些数字:");
  15.   scanf("%d", &input);
  16.   while (input != 0)
  17.     {
  18.       insert_sqlist (&L, input, count);
  19.       count++;
  20.       scanf("%d", &input);
  21.     }
  22.   print_sqlist(&L);
  23.   length_sqlist(&L);
  24.   printf("请输入要插入的数据:");
  25.   scanf("%d", &input);
  26.   printf("请输入要插入的位置:");
  27.   scanf("%d", &count);
  28.   insert_sqlist (&L, input, count);
  29.   print_sqlist(&L);
  30.   length_sqlist(&L);
  31.   printf("请输入要读出数字的位置:");
  32.   scanf("%d", &count);
  33.   get_sqlist(&L, count);
  34.   printf("请输入要查找(按值查找)的数字:");
  35.   scanf("%d", &input);
  36.   locate_sqlist(&L, input);
  37.   printf("请输入要删除的位置:");
  38.   scanf("%d", &count);
  39.   delete_sqlist (&L, count);
  40.   print_sqlist(&L);
  41.   length_sqlist(&L);
  42. }
  43. int Initsqlist (sqlistTp *L)
  44. {
  45.   L ->; last = 0;
  46.   return (1);
  47. }
  48. int insert_sqlist (sqlistTp *L, int x, int i)
  49. {
  50.   int j;
  51.   if (L ->; last == maxsize)
  52.     {
  53.       printf("\n表满!\n");
  54.       return 0;
  55.     }
  56.   if ((i < 1) || (i >; L ->; last + 1))
  57.     {
  58.       printf("\n非法位置!\n");
  59.       return 0;
  60.     }
  61.   for (j = L ->; last; j >;= i; j--)
  62.     L ->; data[j] = L ->; data[j - 1];
  63.   L ->; data[i - 1] = x;
  64.   L ->; last = L ->; last + 1;
  65. }
  66. int print_sqlist (sqlistTp *L)
  67. {
  68.   int i;
  69.   printf("当前线性表排列为:\n");
  70.   for (i = 0; i < L ->; last; i++)
  71.     printf("%d ", L ->; data[i]);
  72.   printf("\n");
  73. }
  74. int length_sqlist (sqlistTp *L)
  75. {
  76.   printf("表长为:%d\n", L ->; last);
  77. }
  78. int get_sqlist (sqlistTp *L, int i)
  79. {
  80.   if ((i < 0) || (i >; L ->; last))
  81.    {
  82.      printf("\n非法位置!\n");
  83.      return 0;
  84.    }
  85.   printf("\n第%d个位置的数字是%d\n", i, L ->; data[i - 1]);
  86. }
  87. int locate_sqlist (sqlistTp *L, int x)
  88. {
  89.   int i = 1;
  90.   while ((i <= L ->; last) && (L ->; data[i - 1] != x))
  91.     i++;
  92.   if (i <= L ->; last)
  93.     printf("\n您要找的数在第%d个位置.\n", i, i);
  94.   else
  95.     printf("\n查无此数!\n");
  96. }
  97. int delete_sqlist (sqlistTp *L, int i)
  98. {
  99.   int j;
  100.   if ((i < 1) || (i >; L ->; last))
  101.     {
  102.       printf("\n非法位置!\n");
  103.       return 0;
  104.     }
  105.   for (j = i + 1; j < L ->; last; j++)
  106.     L ->; data[j - 2] = L ->; data[j - 1];
  107.   L ->; last = L ->; last - 1;
  108. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2004-05-19 11:52 |只看该作者

问两个数据结构问题,谢谢了

以下程序编译成功,但运行结果出错。能帮我看看是哪里出错了。好吗


  1. /*栈的链接实现*/

  2. #include <stdio.h>;

  3. typedef struct node
  4. {
  5.   int data;
  6.   struct node *next;
  7. } LStackTp;

  8. int main()
  9. {
  10.   LStackTp ls;
  11.   int input;
  12.   int i = 0;
  13.   int outnum;
  14.   InitStack(&ls);
  15.   printf("请输入一些数字:");
  16.   scanf("%d", &input);
  17.   while (input != 0)
  18.     {
  19.       Push(&ls, input);
  20.       scanf("%d", &input);
  21.     }
  22.   GetTop(&ls, &input);
  23.   printf("\n当前栈顶的数为:%d\n", input);
  24.   printf("请输入要出栈的个数:");
  25.   scanf("%d", &outnum);
  26.   printf("依此出栈的数为:");
  27.   while (i < outnum)
  28.     {
  29.       Pop(&ls, &input);
  30.       printf("%d ", input);
  31.       i ++;
  32.     }
  33.   printf("\n当前栈的排列:");
  34.   while (! EmptyStack(&ls))
  35.     {
  36.       Pop(&ls, &input);
  37.       printf("%d ", input);
  38.     }
  39.   printf("\n\n");
  40. }

  41. int InitStack(LStackTp *ls)
  42. {
  43.   ls = NULL;
  44.   return (1);
  45. }

  46. int Push(LStackTp *ls, int x)
  47. {
  48.   LStackTp *p;
  49.   p = (LStackTp *) malloc (sizeof(LStackTp));
  50.   p ->; data = x;
  51.   p ->; next = ls;
  52.   ls = p;
  53. }

  54. int Pop(LStackTp *ls, int *x)
  55. {
  56.   LStackTp *p;
  57.   if (ls != NULL)
  58.     {
  59.       p = ls;
  60.       *x = p ->; data;
  61.       ls = ls ->; next;
  62.       free(p);
  63.       return 1;
  64.     }
  65.   else
  66.     return 0;
  67. }

  68. int EmptyStack(LStackTp *ls)
  69. {
  70.   if (ls == NULL)
  71.     return (1);
  72.   else
  73.     return (0);
  74. }

  75. int GetTop(LStackTp *ls , int *x)
  76. {
  77.   if (ls != NULL)
  78.     {
  79.       *x = ls ->; data;
  80.       return 1;
  81.     }
  82.   else
  83.     return 0;
  84. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP