链表
0.环境
[Windows8.1 + Dev-cpp5.11]
1.链表存储结构与准备工作
1.1.原理
每次存储下当前这一项的值和下一项的地址。
在内存中链表像这样存储:
而在内存中数组像这样存储:
1.2.代码实现存储
struct list_node {
int value;
list_node *next;
};
typedef list_node Lnode;
typedef list_node * link;
1.3.一些准备函数
#include <memory.h>
void* malloc( std::size_t size );
void free( void* ptr );
2.初始化
用malloc
不停的获取空间,这里不使用head
存储数据
link make(link Head, int len) {
Head = (link)malloc(sizeof(Lnode));
link last_l = Head, next_l;
while(len--) {
next_l = (link)malloc(sizeof(Lnode));
last_l->next = next_l;
last_l = next_l;
}
last_l->next = NULL;
return Head;
}
3.连接链表
3.1.原理
3.2.代码
void connect(link head1, link head2) {
link v_head2 = head2->next, now = head1;
while (true) {
if (now->next == NULL) {
now->next = v_head2;
break;
}
now = now->next;
}
free(head2);
return;
}
4.打印链表
写了这么多,却无法确认对不对–打印
void display(link head) {
link now = head;
if (now->next == NULL) {
return;
}
while (true) {
now = now->next;
printf("%d ", now->value);
if (now->next == NULL) {
break;
}
}
printf("\n");
return;
}
5.插入节点
5.1.原理
5.2.代码
bool insert(link list, link value, int place = 0) {
link now = list, next_v;
for (int i = 0; i < place; i++) {
if (now->next == NULL) return false;
now = now->next;
}
next_v = now->next;
value->next = next_v;
now->next = value;
return true;
}
6.删除节点
6.1.原理
6.2.代码
bool del(link list, int place = 0) {
link now = list;
for (int i = 0; i < place; i++) {
if (now->next == NULL) return true;
now = now->next;
}
link need_del = now->next;
now->next = need_del->next;
free(need_del);
return true;
}
7. 链表VS数组
7.1. 链表缺点
速度较慢(对于1000次操作):
链表 | 数组 |
---|---|
23ms | 12ms |
7.2. 链表优点
链表可以无限延长 | 数组有固定长度 |
---|
8.测试与完整代码
#include <Cstdio>
//#include <Windows.h>
//#include <iostream>
#include <stdlib.h>
#include <memory.h>
#define LENGTH 3
using namespace std;
struct list_node {
int value;
list_node *next;
};
typedef list_node Lnode;
typedef list_node * link;
link make(link Head, int len) {
Head = (link)malloc(sizeof(Lnode));
link last_l = Head, next_l;
while(len--) {
next_l = (link)malloc(sizeof(Lnode));
last_l->next = next_l;
last_l = next_l;
}
last_l->next = NULL;
return Head;
}
void connect(link head1, link head2) {
link v_head2 = head2->next, now = head1;
while (true) {
if (now->next == NULL) {
now->next = v_head2;
break;
}
now = now->next;
}
free(head2);
return;
}
bool insert(link list, link value, int place = 0) {
link now = list, next_v;
for (int i = 0; i < place; i++) {
if (now->next == NULL) return false;
now = now->next;
}
next_v = now->next;
value->next = next_v;
now->next = value;
return true;
}
bool del(link list, int place = 0) {
link now = list;
for (int i = 0; i < place; i++) {
if (now->next == NULL) return true;
now = now->next;
}
link need_del = now->next;
now->next = need_del->next;
free(need_del);
return true;
}
void display(link head) {
link now = head;
if (now->next == NULL) {
return;
}
while (true) {
now = now->next;
printf("%d ", now->value);
if (now->next == NULL) {
break;
}
}
printf("\n");
return;
}
int main() {
link list1, list2;
list1 = make(list1, LENGTH);
list2 = make(list2, LENGTH);
link now = list1;
for (int i = 0; i < LENGTH; i++) {
now = now->next;
printf("data1: ");
scanf("%d", &now->value);
}
printf("\n");
now = list2;
for (int i = 0; i < LENGTH; i++) {
now = now->next;
printf("data2: ");
scanf("%d", &now->value);
}
printf("\n");
connect(list1, list2);
display(list1);
link in_value = (link)malloc(sizeof(Lnode));
int place;
printf("\ninsert data: ");
scanf("%d", &(in_value->value));
//scanf("%d", &in_value->value);
//printf("+ok1\n");
//Sleep(1000);
printf("insert place: ");
scanf("%d", &place);
insert(list1, in_value, 1);
//printf("+ok2\n");
display(list1);
printf("del ")
display(list1);
return 0;
}