单链表(c++)
一,定义
单链表每个结点储存一个数据和一个指向下一结点的指针,末尾结点指向nullptr。
二,特点
- 在内存中可以不连续
- 相比顺序表,插入删除操作效率高,O(1)
- 查找元素复杂度O(n)
三,操作
1. 定义结点类型
typedef struct node{
int value;
node* next {nullptr};
}node, *list;
2. 初始化链表
list init(){
node* n = (node*)malloc(sizeof(node));
n->next = nullptr;
return n;
}
创建链表方法:
list my_list = init();
(头节点不存放数据)
3. 尾插法
void append(list& li, int value){
//find tail node
node* target = li;
for(;target->next != nullptr; target = target->next);
// create node
target->next = (node*)malloc(sizeof(node));
target->next->next = nullptr;
target->next->value = value;
}
4. 获取链表长度
unsigned len(list& li){
unsigned length=0;
for(node* n=li->next; n!=nullptr; n=n->next)
length++;
return length;
}
5. 插入
void insert(list& li, unsigned index, int value){
node* target = li;
node* new_node = (node*)malloc(sizeof(node));
//检测输入是否有效
if(index>len(li)){
std::cerr << "index out of range" << std::endl;
return;
}
//找到对应位置
for(unsigned i=0; i<index; i++)
target = target->next;
// 插入
new_node->next = target->next;
new_node->value = value;
target->next = new_node;
}
6. 删除结点
void del(list& li, unsigned index){
node* target = li;
node* temp=nullptr;
//检测输入是否有效
if(index>=len(li)){
std::cerr << "index out of range" << std::endl;
return;
}
//找到对应位置
for(unsigned i=0; i<index; i++)
target = target->next;
//删除
temp = target->next;
target->next = temp->next;
free(temp);
}
7. 打印链表
void print(list& li){
node* target = li->next;
while(target != nullptr){
std::cout << target->value << ' ';
target = target->next;
}
std::cout << std::endl;
}
四,完整代码
#include <iostream>
typedef struct node{
int value;
node* next {nullptr};
}node, *list;
list init(){
node* n = (node*)malloc(sizeof(node));
n->next = nullptr;
return n;
}
unsigned len(list& li){
unsigned length=0;
for(node* n=li->next; n!=nullptr; n=n->next)
length++;
return length;
}
void print(list& li){
node* target = li->next; // 方便管理,头节点不存放数据
while(target != nullptr){
std::cout << target->value << ' ';
target = target->next;
}
std::cout << std::endl;
}
void append(list& li, int value){
//find tail node
node* target = li;
for(;target->next != nullptr; target = target->next);
// create node
target->next = (node*)malloc(sizeof(node));
target->next->next = nullptr;
target->next->value = value;
}
void insert(list& li, unsigned index, int value){
node* target = li;
node* new_node = (node*)malloc(sizeof(node));
if(index>len(li)){
std::cerr << "index out of range" << std::endl;
return;
}
for(unsigned i=0; i<index; i++)
target = target->next;
new_node->next = target->next;
new_node->value = value;
target->next = new_node;
}
void del(list& li, unsigned index){
node* target = li;
node* temp=nullptr;
if(index>=len(li)){
std::cerr << "index out of range" << std::endl;
return;
}
for(unsigned i=0; i<index; i++)
target = target->next;
temp = target->next;
target->next = temp->next;
free(temp);
}
int main(){
list my_list = init();
int command, index, value;
std::cout << "single list" << std::endl
<< "0. quit" << std::endl
<< "1. append" << std::endl
<< "2. insert" << std::endl
<< "3. delete" << std::endl
<< "4. print" << std::endl
<< "5. len" << std::endl;
while(1){
std::cout << "input command:";
std::cin >> command;
if (command==0){
return 0;
} else if(command==1) {
std::cout << "value:";
std::cin >> value;
append(my_list, value);
} else if(command==2) {
std::cout << "index:";
std::cin >> index;
std::cout << "value:";
std::cin >> value;
insert(my_list, index, value);
} else if(command==3) {
std::cout << "index:";
std::cin >> index;
del(my_list, index);
} else if(command==4) {
print(my_list);
} else if(command==5) {
std::cout << "length:" << len(my_list) << std::endl;
} else {
std::cerr << "input error";
}
}
return 0;
}
记录学习过程,如有问题欢迎指出。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本