fork 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.  
  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.  
  23. node *newnode = new node();
  24. newnode->data = fir_node->data - 0.5;
  25. newnode->next = fir_node;
  26. fir_node = newnode;
  27. cout <<"插入在頭端後的串列為"<< endl;
  28. printlist(fir_node);
  29. return 0;
  30. }
  31.  
  32. void printlist(node *list) {
  33. node *cur = new node(); //宣告一個指標,並指向initial list
  34. cur = list;
  35. while (cur != nullptr) {
  36. cout << cur->data << endl;
  37. cur = cur->next;
  38. }
  39. //delete cur;
  40. }
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
創建初始串列為:
1
2
3
插入在頭端後的串列為
0.5
1
2
3