[C++]数据结构---链表

0.环境

[Windows8.1 + Dev-cpp5.11]

1.链表存储结构与准备工作

1.1.原理

每次存储下当前这一项的值和下一项的地址。
在这里插入图片描述
在内存中链表像这样存储:
list-pic2
而在内存中数组像这样存储:
在这里插入图片描述

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.原理

connect

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次操作):

链表数组
23ms12ms

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;
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值