有序链表

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

Linked List in order

问题

所以我们被给予一个数字数组,我们需要使用链表按顺序打印它们。

数组:19 2 3 13 124 6521 23 1 451 5 3123 412 354 13 13 23 13 1 45

但是我不明白为什么我的代码一直打印出45

我认为我已经正确地做了,但无论如何这是我的代码:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct node_s node_t;
  4. struct node_s
  5. {
  6. int n;
  7. struct node_s *next;
  8. };
  9. node_t *new_element()
  10. {
  11. node_t *new;
  12. new= malloc(sizeof(node_t));
  13. if(new==NULL)
  14. {
  15. printf("Error");
  16. exit(1);
  17. }
  18. return new;
  19. }
  20. node_t *list(node_t *head)
  21. {
  22. FILE *file;
  23. file= fopen("file","r");
  24. if(file==NULL)
  25. {
  26. exit(1);
  27. }
  28. int n;
  29. node_t *new=NULL;
  30. node_t *p=NULL;
  31. new= new_element();
  32. while (fscanf(file,"%d", &n)!=EOF)
  33. {
  34. new->n=n;
  35. new->next=NULL;
  36. if(head==NULL)
  37. {
  38. head=new;
  39. }
  40. else
  41. {
  42. p=head;
  43. while (p->next!=NULL && p->next->n<new->n)
  44. {
  45. p=p->next;
  46. }
  47. new->next=p->next;
  48. p->next=new;
  49. }
  50. }
  51. return head;
  52. }
  53. void printList(node_t *head)
  54. {
  55. node_t *new=head;
  56. while(new != NULL)
  57. {
  58. printf("%d-->",new->n);
  59. new=new->next;
  60. }
  61. printf("NULL");
  62. return;
  63. }
  64. int main() {
  65. node_t *head=NULL;
  66. head= list(head);
  67. printList(head);
  68. return 0;
  69. }

基本上我尝试做的是,我们使用获取的数字创建一个新的节点。首先,我们检查列表是否为空。如果是,我们将新节点设置为头。

否则,我们比较数字,如果下一个节点的数字小于新节点的数字,我们更改指针。newnext指针指向链接的下一个节点,并且指向下一个节点的指针现在指向新节点。

至少我尝试实现了我说的。

英文:

So we have been given an array of numbers and we are required to print them out in order using linked lists

Array: 19 2 3 13 124 6521 23 1 451 5 3123 412 354 13 13 23 13 1 45

But i dont get why but my code keeps printing out 45

I think ive done it correctly but heres my code either way

  1. #include &lt;stdio.h&gt;
  2. #include &lt;stdlib.h&gt;
  3. typedef struct node_s node_t;
  4. struct node_s
  5. {
  6. int n;
  7. struct node_s *next;
  8. };
  9. node_t *new_element()
  10. {
  11. node_t *new;
  12. new= malloc(sizeof(node_t));
  13. if(new==NULL)
  14. {
  15. printf(&quot;Error&quot;);
  16. exit(1);
  17. }
  18. return new;
  19. }
  20. node_t *list(node_t *head)
  21. {
  22. FILE *file;
  23. file= fopen(&quot;file&quot;,&quot;r&quot;);
  24. if(file==NULL)
  25. {
  26. exit(1);
  27. }
  28. int n;
  29. node_t *new=NULL;
  30. node_t *p=NULL;
  31. new= new_element();
  32. while (fscanf(file,&quot;%d&quot;, &amp;n)!=EOF)
  33. {
  34. new-&gt;n=n;
  35. new-&gt;next=NULL;
  36. if(head==NULL)
  37. {
  38. head=new;
  39. }
  40. else
  41. {
  42. p=head;
  43. while (p-&gt;next!=NULL &amp;&amp; p-&gt;next-&gt;n&lt;new-&gt;n)
  44. {
  45. p=p-&gt;next;
  46. }
  47. new-&gt;next=p-&gt;next;
  48. p-&gt;next=new;
  49. }
  50. }
  51. return head;
  52. }
  53. void printList(node_t *head)
  54. {
  55. node_t *new=head;
  56. while(new != NULL)
  57. {
  58. printf(&quot;%d--&gt;&quot;,new-&gt;n);
  59. new=new-&gt;next;
  60. }
  61. printf(&quot;NULL&quot;);
  62. return;
  63. }
  64. int main() {
  65. node_t *head=NULL;
  66. head= list(head);
  67. printList(head);
  68. return 0;
  69. }

Basically what i tried to do is. We create a new node with the acquired number. Firstly we check if list is empty or not. If it is we put the new node as the head.

Else we check the numbers, so if the number of the next node is lower than the number of the new node we change the pointers. The next pointer of new points to the next node of the link and the pointer that pointed to the next node now points to the new node.

At least i tried to implement what i said.

答案1

得分: 0

当我执行new=new_element();时,我必须将它放在while循环中。

英文:

Ok nevermind i found a problem.
When i did the new=new_element(); i had to put that in the while loop.

huangapple
  • 本文由 发表于 2023年6月1日 09:03:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76378082.html
匿名

发表评论

匿名网友

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

确定