<sdut-ACM>数据结构实验之链表一:顺序建立链表

太过爱你忘了你带给我的痛 2022-07-16 00:54 347阅读 0赞

Problem Description

输入N个整数,按照输入的顺序建立单链表存储,并遍历所建立的单链表,输出这些数据。

Input

第一行输入整数的个数N;
第二行依次输入每个整数。

Output

输出这组整数。

Example Input

  1. 8
  2. 12 56 4 6 55 15 33 62

Example Output

  1. 12 56 4 6 55 15 33 62

Hint

不得使用数组!

  1. #include
  2. #include
  3. struct node
  4. {
  5. int data;
  6. struct node*next;
  7. };
  8. struct node*creat(int n)
  9. {
  10. int i;
  11. struct node *head, *tail, *p;
  12. head = (struct node*)malloc(sizeof(struct node));
  13. head->next = NULL;
  14. tail = head;
  15. for(i = 1; i <= n; i++)
  16. {
  17. p = (struct node*)malloc(sizeof(struct node));
  18. scanf("%d",&p->data);
  19. p->next = NULL;
  20. tail->next = p;
  21. tail = p;
  22. }
  23. return (head);
  24. };
  25. void display(struct node *head)
  26. {
  27. struct node *p;
  28. p = head->next;
  29. while(p->next!=NULL)
  30. {
  31. printf("%d ",p->data);
  32. p = p->next;
  33. }
  34. printf("%d\n",p->data);
  35. }
  36. int main()
  37. {
  38. int n;
  39. struct node *head;
  40. scanf("%d",&n);
  41. head = creat(n);
  42. display(head);
  43. return 0;
  44. }

发表评论

表情:
评论列表 (有 0 条评论,347人围观)

还没有评论,来说两句吧...

相关阅读