Mycodeschool data structure summary

P1: time complexity

https://blog.csdn.net/qq_41523096/article/details/82142747  The thief speaks clearly

P2: linked list

 

 https://blog.csdn.net/Shuffle_Ts/article/details/95055467

https://blog.csdn.net/Endeavor_G/article/details/80552680?depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2&utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2

Insert a set of data in sequence:

 1 #include <iostream>
 2 
 3 struct Node
 4 {
 5     int data;
 6     Node* next;
 7     //struct Node* next;   //C语言中
 8 };
 9 
10 Node* Insert(Node* head, int x)
11 {
12     //struct Node* temp = (Node*)malloc(sizeof(Node));  //C语言写法
13     Node* temp = new Node();
14     temp->data = x;
15     temp->next = NULL;
16      if (head! = NULL)
 17          temp-> next = head;   // Here is because the linked list is inserted backwards, which is equivalent to the stack of books 
18      head = temp;
 19      return head;
 20  }
 21  
22  void Print (Node * head)
 23  {
 24      while (head! = NULL)
 25      {
 26          std :: cout << head-> data << "   " ;
 27          head = head-> next;
 28      }
 29  }
 30  
31  int main ()
32  {
 33      Node * head = NULL;
 34      int n, x;
 35      std :: cout << " How many digits do I need to enter? " << std :: endl;
 36      std :: cin >> n;
 37      for ( int i = 0 ; i <n; i ++ )
 38      {
 39          std :: cout << " Please enter a number " << std :: endl;
 40          std :: cin >> x;
 41          head = Insert (head, x);
 42          std :: cout <<"The linked list is: " ;
 43          Print (head);
 44           std :: cout << " " << std :: endl;
 45      }
 46      std :: cin. Get ();
 47      return  0 ;
 48 }
View Code

 

Guess you like

Origin www.cnblogs.com/xiaodangxiansheng/p/12667941.html