问题关于我的链表代码,奇怪的错误

huangapple go评论98阅读模式
英文:

Question for my linked list code, odd bug

问题

这段代码有一个问题,造成了在第三次循环case 2之后返回了值3221226356。这个问题可能与内存分配和指针操作有关。如果要持续循环输入数据,您需要在每次循环之后重新初始化一些变量,以确保它们不会影响下一个循环。

以下是一些可能的修复:

  1. case 2的循环内,将totalitemcode和其他可能影响下一个循环的变量重置为初始状态。
  2. 确保循环中使用的缓冲区或指针不会超出其范围,以防止内存访问错误。

请注意,修复代码可能需要更多的调试和测试以确保它按预期工作。如果您需要详细的代码更改建议,请告诉我。

英文:

I have a problem with this code, I don't know why it stops looping the second switch(input) after inputing the datas 3 times.

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. struct Node{
  5. char Name[50];
  6. int value;
  7. Node *next, *prev;
  8. }*head, *tail;
  9. Node* createNode(int value, char name[]){
  10. Node* newNode = (Node*)malloc(sizeof(Node*));
  11. strcpy(newNode->Name, name);
  12. newNode->value = value;
  13. return newNode;
  14. }
  15. void pushTail(int value, char name[]){
  16. Node *newNode = createNode(value, name);
  17. if(head == NULL){
  18. head = tail = newNode;
  19. return;
  20. }
  21. else{
  22. tail->next = newNode;
  23. newNode->prev = tail;
  24. tail = newNode;
  25. }
  26. }
  27. void makelongList(){
  28. for(int i = 0; i < 25; i++){
  29. printf("\n");
  30. }
  31. }
  32. void orderHistory(int val){
  33. Node* curr = head;
  34. makelongList();
  35. printf("=== Order History ===\n");
  36. int i = 0;
  37. while(curr != NULL){
  38. printf("Order %d : %s, %d pcs\n", i+1, curr->Name, curr->value);
  39. i++;
  40. curr = curr->next;
  41. }
  42. printf("Total Spent = Rp. %d \n", val);
  43. printf("=====================\n"); getchar();
  44. }
  45. void itemDatabase(){
  46. makelongList();
  47. printf("=========================================\n");
  48. printf("| Item Code | Item Name | Price |\n");
  49. printf("=========================================\n");
  50. printf("| A0 | Milk Tea | Rp. 3000 |\n");
  51. printf("| A1 | Mineral Water | Rp. 2000 |\n");
  52. printf("| A2 | Hot Dog | Rp. 7000 |\n");
  53. printf("| A3 | Cheeseburger | Rp. 9000 |\n");
  54. printf("| A4 | Cold/Hot Tea | Rp. 4000 |\n");
  55. printf("=========================================\n");
  56. printf("\n Press enter to continue\n"); getchar();
  57. }
  58. void printMenu(){
  59. printf("=== suniB Supermarket ===\n");
  60. printf("1. View Available Items\n");
  61. printf("2. Buy Items\n");
  62. printf("3. Order History\n");
  63. printf("4. Exit\n");
  64. printf("=========================\n");
  65. printf(">> ");
  66. }
  67. void printReciept(int val){
  68. printf("=== suniB Supermarket ===\n");
  69. printf("= - R E C I E P T - ===\n ");
  70. printf(" [Name]-[QTY] \n");
  71. Node *curr = head;
  72. while(curr != NULL){
  73. printf(" %s %d \n", curr->Name, curr->value);
  74. curr = curr->next;
  75. }
  76. printf("Total Spent = Rp. %d \n", val);
  77. printf("-Thank You For Shopping!-\n");
  78. printf("=========================\n");
  79. }
  80. void classicprintList(){
  81. Node *curr = head;
  82. printf("NULL");
  83. while(curr != NULL){
  84. printf("<-[ %d ]->", curr->value);
  85. curr = curr->next;
  86. }
  87. printf("NULL\n");
  88. }
  89. int main(){
  90. int input, qty, pricelist[10] = {3000, 2000, 7000, 9000, 4000}, mult = 0, total = 0, temps = 0;
  91. char itemcode[2], temp[2] = {0}, optionstring[2];
  92. do{
  93. makelongList();
  94. printMenu();
  95. scanf("%d", &input); getchar();
  96. switch(input){
  97. case 1:
  98. itemDatabase();
  99. break;
  100. case 2:
  101. do{
  102. do{
  103. printf("Insert the item code you want to buy : "); scanf("%s", itemcode); getchar();
  104. }while((itemcode[0] != 'A') && (itemcode[1] != '0') && (itemcode[1] != '1') && (itemcode[1] != '2') && (itemcode[1] != '3') && (itemcode[1] != '4'));
  105. do{
  106. printf("Insert the amount : "); scanf("%d", &qty); getchar();
  107. }while(qty < 0);
  108. temp[0] = itemcode[1];
  109. temps = atoi(temp);
  110. switch(temps){
  111. case 0:
  112. mult = qty*pricelist[0];
  113. total += mult;
  114. printf("\nItem name : Milk Tea\n");
  115. break;
  116. case 1:
  117. mult = qty*pricelist[1];
  118. total += mult;
  119. printf("\nItem name : Mineral Water\n");
  120. break;
  121. case 2:
  122. mult = qty*pricelist[2];
  123. total += mult;
  124. printf("\nItem name : Hot Dog\n");
  125. break;
  126. case 3:
  127. mult = qty*pricelist[3];
  128. total += mult;
  129. printf("\nItem name : Cheeseburger\n");
  130. break;
  131. case 4:
  132. mult = qty*pricelist[4];
  133. total += mult;
  134. printf("\nItem name : Cold | Hot Tea\n");
  135. break;
  136. }
  137. printf("Your total amount will be : %d\n", total);
  138. printf("Do you want to continue? [Y | N]? : "); scanf("%c", optionstring); getchar();
  139. pushTail(qty, itemcode);
  140. }while(strcmp(optionstring, "Y") == 0);
  141. break;
  142. case 3:
  143. orderHistory(total);
  144. break;
  145. case 4:
  146. break;
  147. }
  148. }while(input >= 1 && input <= 4);
  149. return 0;
  150. }

the program returns value 3221226356 after the third loop of case 2. If anyone could help it would be amazing.

I expect the loop to keep going and input as many datas possible. Would say at least it could loop 10 times, but the more the better.

答案1

得分: 1

你的链表实现存在未定义行为,因为在添加到链表中的节点中,数据成员 nextprev 没有被初始化。

  1. Node* createNode(int value, char name[]){
  2. Node* newNode = (Node*)malloc(sizeof(Node*));
  3. strcpy(newNode->Name, name);
  4. newNode->value = value;
  5. return newNode;
  6. }
  7. void pushTail(int value, char name[]){
  8. Node *newNode = createNode(value, name);
  9. if(head == NULL){
  10. head = tail = newNode;
  11. return;
  12. }
  13. else{
  14. tail->next = newNode;
  15. newNode->prev = tail;
  16. tail = newNode;
  17. }
  18. }

此外,还存在其他问题。例如,在这个带有非常冗长条件的 do while 语句中:

  1. do{
  2. printf("插入你想购买的物品代码:"); scanf("%s", itemcode); getchar();
  3. }while((itemcode[0] != 'A') && (itemcode[1] != '0') && (itemcode[1] != '1') && (itemcode[1] != '2') && (itemcode[1] != '3') && (itemcode[1] != '4'));

由于数组 itemcode 声明为 2 个元素,除了终止的零字符 '\0' 外,只能存储一个字符,因此在调用 scanf 时尝试访问超出数组的内存。因此,诸如

  1. (itemcode[1] != '1')

这样的表达式是没有意义的。

英文:

Your implementation of the list has undefined behavior because data members next and prev are not initialized in nodes added to the list

  1. Node* createNode(int value, char name[]){
  2. Node* newNode = (Node*)malloc(sizeof(Node*));
  3. strcpy(newNode->Name, name);
  4. newNode->value = value;
  5. return newNode;
  6. }
  7. void pushTail(int value, char name[]){
  8. Node *newNode = createNode(value, name);
  9. if(head == NULL){
  10. head = tail = newNode;
  11. return;
  12. }
  13. else{
  14. tail->next = newNode;
  15. newNode->prev = tail;
  16. tail = newNode;
  17. }
  18. }

Also there are other problems.

For example in this do while statement with an awful long condition

  1. do{
  2. printf("Insert the item code you want to buy : "); scanf("%s", itemcode); getchar();
  3. }while((itemcode[0] != 'A') && (itemcode[1] != '0') && (itemcode[1] != '1') && (itemcode[1] != '2') && (itemcode[1] != '3') && (itemcode[1] != '4'));

there is an attempt to access memory beyond the array itemcode in call of scanf because the array declared with 2 elements can store only one character apart from the terminating zero character '\0'. So such expressions like for example that

  1. (itemcode[1] != '1')

do not make sense.

huangapple
  • 本文由 发表于 2023年4月1日 00:21:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75900724.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定