fork(2) download
  1. #include <iostream>
  2. using namespace std;
  3. struct node {
  4. double data;
  5. struct node *next;
  6. };
  7. void printlist(node *list);
  8. void insert_head(node *list);
  9. int main() {
  10. // your code goes here
  11. node *fir_node = new node();
  12. node *sec_node = new node();
  13. node *thir_node = new node();
  14. fir_node->data = 1;
  15. sec_node->data = 2;
  16. thir_node->data = 3;
  17. fir_node->next = sec_node;
  18. sec_node->next = thir_node;
  19. thir_node->next = nullptr;
  20. cout << "創建初始串列為:" << endl;
  21. printlist(fir_node);
  22. cout <<"插入在頭端後的串列為"<< endl;
  23. insert_head(fir_node);
  24. printlist(fir_node);
  25. return 0;
  26. }
  27.  
  28. void printlist(node *list) {
  29. node *cur = new node(); //宣告一個指標,並指向initial list
  30. cur = list;
  31. while (cur != nullptr) {
  32. cout << cur->data << endl;
  33. cur = cur->next;
  34. }
  35. //delete cur;
  36. }
  37. void insert_head(node *list){
  38. node *newnode = new node();
  39. newnode->data = list->data - 0.5;
  40. newnode->next = list;
  41. list = newnode;
  42. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
創建初始串列為:
1
2
3
插入在頭端後的串列為
1
2
3