fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3. struct node{
  4. int data;///函数的返回值类型 函数名(参数类型 参数名)
  5. node *next;///是下一个朋友的房间号
  6. };
  7. node *createList(int n)
  8. {
  9. node *head,*p;
  10. head = p = new node;///
  11. cin >> p->data;
  12. n--;
  13. while(n)
  14. {
  15. node *q = new node;
  16. cin >> q->data;
  17. p->next = q;
  18. p = p->next;
  19. n--;
  20. }
  21. p->next = NULL;
  22. return head;
  23. }
  24. void print(node *p)
  25. {
  26. while(p!=NULL)
  27. {
  28. cout << p->data << " ";
  29. p = p->next;
  30. }
  31. }
  32. int main() {
  33. int n;
  34. cin >> n;
  35. node *head = createList(n);
  36. print(head)
  37. /*
  38. 把构建链表和输出链表写成函数
  39. 构建链表的函数需要返回值?需要头指针作为返回值
  40. 输出链表的函数需要返回值?不需要返回值,用void
  41.  
  42. int n;
  43. cin >> n;
  44. node *head,*p;
  45. head = p = new node;
  46. cin >> p->data;
  47. n--;
  48. while(n)
  49. {
  50. node *q = new node;
  51. cin >> q->data;
  52. p->next = q;
  53. p = p->next;
  54. n--;
  55. }
  56. p->next = NULL;
  57.  
  58. p = head;
  59. while(p!=NULL)
  60. {
  61. cout << p->data << " ";
  62. p = p->next;
  63. }
  64. */
  65. return 0;
  66. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
5
1 2 3 4 5
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:36:13: error: expected ‘;’ before ‘return’
  print(head)
             ^
             ;
prog.cpp:65:2:
  return 0;
  ~~~~~~      
stdout
Standard output is empty