[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
    评论
链表的反转可以通过修改节点的前驱和后继指针来实现。以下是一个示例代码实现: ```c #include <stdio.h> #include <stdlib.h> // 定义双链表节点 struct Node { int data; struct Node* prev; struct Node* next; }; // 反转双链表 struct Node* reverse(struct Node* head) { struct Node* current = head; struct Node* temp = NULL; while (current != NULL) { // 交换当前节点的前驱和后继指针 temp = current->prev; current->prev = current->next; current->next = temp; // 向后移动 current = current->prev; } // 更新头节点指针 if (temp != NULL) { head = temp->prev; } return head; } // 打印双链表 void printList(struct Node* node) { while (node != NULL) { printf("%d ", node->data); node = node->next; } } int main() { // 创建双链表 struct Node* head = (struct Node*)malloc(sizeof(struct Node)); struct Node* second = (struct Node*)malloc(sizeof(struct Node)); struct Node* third = (struct Node*)malloc(sizeof(struct Node)); head->data = 1; head->prev = NULL; head->next = second; second->data = 2; second->prev = head; second->next = third; third->data = 3; third->prev = second; third->next = NULL; printf("原始双链表:"); printList(head); // 反转双链表 head = reverse(head); printf("\n反转后的双链表:"); printList(head); return 0; } ``` 这段代码首先定义了一个 `Node` 结构体来表示双链表的节点,其中包括一个数据域 `data`、一个指向前驱节点的指针 `prev`,以及一个指向后继节点的指针 `next`。 在 `reverse` 函数中,我们使用一个临时变量 `temp` 来交换当前节点的前驱和后继指针,然后将当前节点指针向后移动。反转完成后,更新头节点指针,确保它指向反转后的链表的头部。 最后,在 `main` 函数中创建一个简单的双链表,并调用 `reverse` 函数进行反转操作。通过调用 `printList` 函数来打印原始链表和反转后的链表。 以上代码的输出结果应为: ``` 原始双链表:1 2 3 反转后的双链表:3 2 1 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值