原理图解:
单链表实现代码:
#include<bits/stdc++.h>
using namespace std;
struct Node{
int val;
Node *next;
};
int main(){
// 链表开头
Node* head = new Node;
// 链表结尾
Node* linked_list = head;
// 单链表:1 2 3 4 5 6 7
for (int i=1;i<=7;i++){
Node* now= new Node;
now->val = i;
now->next = nullptr;
// 链表连接新节点
linked_list->next = now;
// 更新链表结尾
linked_list = now;
}
// 遍历链表
Node* now=head->next;
while (now){
cout<<(now->val)<<" ";
now = now->next;
}cout<<"\n";
}