对好几个月之前看的链表进行了复习。
还是创建头结点,然后依次插入结点。 个人感觉最重要的是对指针/地址的掌握。 只有知道地址的概念,链表的问题也就迎刃而解。
#include <iostream> using namespace std; struct List { int data; List* next; }; void insert(List* node, int dat) { List* newnode = new List(); newnode->next = NULL; newnode->data = dat; List* Last = node; while (Last->next != NULL) { Last = Last->next; } Last->next = newnode; } void print(List* node) { List* newnode = node; while (newnode != NULL) { cout << newnode->data << " "; newnode = newnode->next; } } void add(List* node) { List* head = node; while (head->data != 3) { head = head->next; } List* newnode = new List(); newnode->data = 6; newnode->next = head->next; head->next = newnode; } void del(List* node) { List* head = node; while (head->data != 3) { head = head->next; } head->next = head->next->next; } int main() { List* head = new List(); head->data = 1; head->next = NULL; for (int i = 2; i <= 5; i++) { insert(head, i); //插入2,3,4,5 } print(head); //打印1,2,3,4,5 add(head); //添加6 printf("\n"); print(head); //打印1,2,3,6,4,5 del(head); //删除6 printf("\n"); print(head); //打印1,2,3,4,5 return 0; }
这次重写的过程不是很顺畅,也思路了一会,希望以后能越来越熟练
原文链接: https://www.cnblogs.com/strive-sun/p/13560860.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;
也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/404850
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!