数据结构 2.1 单链表

1.单链表

线性表:1.有限的序列 2.序列中的每一个元素都有唯一的前驱和后继,除了开头和结尾的两个节点。

顺序表:分配一块连续的内存去存放这些元素,eg、数组

链表:内存是不连续的,元素会各自被分配一块内存,内存和内存之间用指针进行相连。

顺序表和链表的区别是内存的连续与否

data域 | next指针域 ------> data域 | next指针域 ------> data域 | next指针域 ------> NULL

单链表的操作:

1.增加 :1>头插法 2>尾插法

1>插入------> data域 | next指针域 ------> data域 | next指针域 ------> data域 | next指针域 ------> NULL

2>data域 | next指针域 ------> data域 | next指针域 ------> data域 | next指针域 ------> 插入------>NULL

2.删除:用前一个节点的指针直接指向对应节点的后一个节点的前驱,只操作一个指针。

为了使操作方便,在操作中添加一个头节点。头节点并不实际存储,只保存链表中的元素个数。

代码实现:

定义一个链表(结构体):

cpp 复制代码
typedef struct Node {//定义一个结构体
	int data;
	struct Node* next;
}Node;

初始化一个链表:

cpp 复制代码
Node* initList() {//初始化一个链表
	Node* list = (Node*)malloc(sizeof(Node));
	list->data = 0;
	list->next = NULL;
	return list;
}

头插法:

cpp 复制代码
void headInsert(Node* list,int data){//头插法
	Node* node = (Node*)malloc(sizeof(Node));
	node->data = data;
	node->next = list->next;
	list->next = node;
	list->data++;//代表当前链表之中插入元素
}

尾插法:

cpp 复制代码
void tailInsert(Node* list, int data){//尾插法
	Node* head = list;
	Node* node = (Node*)malloc(sizeof(Node));
	node->data = data;
	node->next = NULL;
	list = list->next;
	while (list->next) {
		list = list->next;
	}
	list->next = node;
	head->data++;
}

删除:

cpp 复制代码
void Delete(Node* list, int data){//删除
	Node* head = list;
	Node* pre = list;
	Node* current = list->next;
	list = list->next;
	while (current)
	{
		if (current->data == data)
		{
			pre->next = current->next;
			free(current);
			break;
		}
		pre = current;
		current = current->next;
	}
	list->data--;
}

遍历操作:

cpp 复制代码
void printList(Node* list) {//遍历操作
	list = list->next;
	while (list)
	{
		printf("%d ", list->data);
		list = list->next;
	}
	printf("\n");
}

main函数:

cpp 复制代码
int main()
{
	Node* list = initList();
	headInsert(list, 1);
	headInsert(list, 2);
	headInsert(list, 3);
	headInsert(list, 4);
	headInsert(list, 5);
	tailInsert(list, 6);
	tailInsert(list, 7);
	tailInsert(list, 8);
	tailInsert(list, 9);
	tailInsert(list, 10);
	printList(list);
	Delete(list, 5);
	printList(list);
	Delete(list, 10);
	printList(list);
	Delete(list, 6);
	printList(list);
	return 0;
}

整体函数:

cpp 复制代码
typedef struct Node {//定义一个结构体
	int data;
	struct Node* next;
}Node;

Node* initList() {//初始化一个链表
	Node* list = (Node*)malloc(sizeof(Node));
	list->data = 0;
	list->next = NULL;
	return list;
}

void headInsert(Node* list,int data){//头插法
	Node* node = (Node*)malloc(sizeof(Node));
	node->data = data;
	node->next = list->next;
	list->next = node;
	list->data++;//代表当前链表之中插入元素
}

void tailInsert(Node* list, int data){//尾插法
	Node* head = list;
	Node* node = (Node*)malloc(sizeof(Node));
	node->data = data;
	node->next = NULL;
	list = list->next;
	while (list->next) {
		list = list->next;
	}
	list->next = node;
	head->data++;
}

void Delete(Node* list, int data){//删除
	Node* head = list;
	Node* pre = list;
	Node* current = list->next;
	list = list->next;
	while (current)
	{
		if (current->data == data)
		{
			pre->next = current->next;
			free(current);
			break;
		}
		pre = current;
		current = current->next;
	}
	list->data--;
}

void printList(Node* list) {//遍历操作
	list = list->next;
	while (list)
	{
		printf("%d ", list->data);
		list = list->next;
	}
	printf("\n");
}

int main()
{
	Node* list = initList();
	headInsert(list, 1);
	headInsert(list, 2);
	headInsert(list, 3);
	headInsert(list, 4);
	headInsert(list, 5);
	tailInsert(list, 6);
	tailInsert(list, 7);
	tailInsert(list, 8);
	tailInsert(list, 9);
	tailInsert(list, 10);
	printList(list);
	Delete(list, 5);
	printList(list);
	Delete(list, 10);
	printList(list);
	Delete(list, 6);
	printList(list);
	return 0;
}

运行结果:

相关推荐
Albert Edison1 小时前
【项目设计】C++ 高并发内存池
数据结构·c++·单例模式·哈希算法·高并发
小许学java4 小时前
数据结构-模拟实现顺序表和链表
java·数据结构·链表·arraylist·linkedlist·顺序表模拟实现·链表的模拟实现
稚辉君.MCA_P8_Java5 小时前
Gemini永久会员 C++返回最长有效子串长度
开发语言·数据结构·c++·后端·算法
dragoooon346 小时前
[优选算法专题十.哈希表 ——NO.55~57 两数之和、判定是否互为字符重排、存在重复元素]
数据结构·散列表
稚辉君.MCA_P8_Java7 小时前
Gemini永久会员 go数组中最大异或值
数据结构·后端·算法·golang·哈希算法
会员果汁7 小时前
双向链式队列-C语言
c语言·数据结构
AI科技星8 小时前
张祥前统一场论:引力场与磁矢势的关联,反引力场生成及拉格朗日点解析(网友问题解答)
开发语言·数据结构·经验分享·线性代数·算法
C雨后彩虹8 小时前
最少交换次数
java·数据结构·算法·华为·面试
-森屿安年-8 小时前
二叉平衡树的实现
开发语言·数据结构·c++
稚辉君.MCA_P8_Java9 小时前
Gemini永久会员 Go 返回最长有效子串长度
数据结构·后端·算法·golang