本文介绍 算法终章:遗失的那一部分
算法终章:遗失的那一部分
本文由在当地较为英俊的男子金天大神原创,版权所有,欢迎转载,本文首发地址 https://jinfagang.github.io 。但请保留这段版权信息,多谢合作,有任何疑问欢迎通过微信联系我交流:jintianiloveu
写给自己看的备忘录
数据结构
上一篇博客已经不记得啥时候了. 继续完成我们的终章,基本上所有关于算法的基础部门都会在这里展现,算法是一种思想,一种思维方式,思想是根本,方法就万千。就像少林武功,变招很多,但是终极目标是从来不会改变的。
链表问题继续。首先链表直接创建一个结构体吧,包含插入,删除方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 
  | #include <iostream> #include <string> #include <vector> using namespace std; struct Node {     int data;     Node* p_next; }; void create_list(Node* head) {     auto p = head;     do {         int a;         cin >> a;         if (!head) {             p->data = a;         } else {             auto node = new Node;             node->data = a;             p->p_next = node;             p = node;         }     }while (cin.get() != '\n');     p->p_next = nullptr; } void traverse_list(Node* head) {     auto p = head;     while (p) {         cout << p->data << " ";         p = p->p_next;     }     cout << endl; } int main()  {     auto node = new Node;     create_list(node);     traverse_list(node);     return 0; } 
  | 
 
这里实现一个创建链表,那么插入一个元素到链表中,就是insert_list函数,其实也非常简单,根据插入的位置while循环到该位置,将该位置前面一个的p_next和新插入的Node互换,就ok了。