链表(一)

单链表

什么是链表?可以理解成高级数组。数组怎么学,无非就是增删改查,链表也一样,本节就讲述链表的增删改查。
数组下标从0开始,链表下标也是从0开始。

软件:VS2022


前言

提示:这里可以添加本文要记录的大概内容:

本人没有那么厉害,不当之处,还请指出。


一、建造链表的基本结构

DataType 就是int 。

typedef int DataType;

struct ListNode {
	DataType data;//数据域
	ListNode* next;//指针域
};

二、链表的实现及其操作

1.节点的初始化

需要用到头文件<malloc.h>

ListNode* ListCreateNode(DataType data) {
	ListNode* node = (ListNode*)malloc(sizeof(ListNode));
	node->data = data;
	node->next = NULL;
	return node;
}

2.把一个数组转化成链表

头插法

ListNode* ListCreateListByHead(int n, int* a) {
		ListNode* head = NULL, * vtx;
		while (n--) {
			vtx = ListCreateNode(a[n]);
			vtx->next = head;
			head = vtx;
		}
		return head;
	}

尾插法

ListNode* ListCreateListByTail(int n, int a[]) {
	ListNode* head, * tail, * vtx;
	int idx=0;
	if (n <= 0) {
		return NULL;
	}
	vtx = ListCreateNode(a[0]);
	head = tail = vtx;
	while (++idx < n) {
		vtx = ListCreateNode(a[idx]);
		tail->next = vtx;
		tail = vtx;
	}
	return head;
}

3.查找节点

1.通过下标查找节点

ListNode* ListGetNode(ListNode* head, int i) {
	ListNode* temp = head;
	int j = 0;
	while (temp&&j<i) {
		j++;
		temp = temp->next;
	}
	if (j > i || !temp) {
		return NULL;
	}
	return temp;
}

2.通过值查找节点

ListNode* ListFindNodeByValue(ListNode* head, DataType v) {
	ListNode* temp = head;
	while (temp) {
		if (temp->data == v) {
			return temp;
		}
		temp = temp->next;
	}
	return NULL;
}

说白点,就是遍历链表,找到就返回,找不到就返回NULL

4.插入节点

ListNode* ListInsertNode(ListNode* head, int i, DataType v) {
	ListNode* pre=head, * aft, * vtx;
	int j = 0;
	while (pre && j < i) {
		pre = pre->next;
		j++;
	}
	if (!pre) {
		return NULL;
	}
	vtx = ListCreateNode(v);
	aft = pre->next;
	pre->next = vtx;
	vtx->next = aft;
	return vtx;
}

5.删除节点

ListNode* ListDeleteNode(ListNode* head, int i) {
	ListNode* aft, * pre, * del;
	int j = 0;
	if (!head) {
		return NULL;
	}
	if (i == 0) {
		del = head;
		head = head->next;
		free(del);
		return head;
	}
	pre = head;
	while (pre && j < i) {
		j++;
		pre = pre->next;
	}
	// 当前pre就是第i+1个节点
	if (!pre && !pre->next) {
		return head;
	}

	del = pre->next;
	aft = del->next;
	pre->next = aft;
	free(del);
	return head;
}

6.删除链表

void ListDestroyList(ListNode ** pHead) {
	ListNode*head = *pHead;
	while(head) {
		head = ListDeleteNode(head, 0);
		// ListPrint(head);
	}
	*pHead = NULL;
}

8.输出函数

void ListPrint(ListNode* head) {
	ListNode* vtx = head;
	while (vtx) {
		cout << vtx->data;
		vtx = vtx->next;
	}
	cout << "NULL" << endl;
}

7.函数测试

int main() {
	int a[5] = { 4,33,9,8,10 };
	ListNode* head = ListCreateListByHead(5, a);
	ListPrint(head);
	ListNode* p = ListGetNode(head, 1);
	cout << "第二个节点是:" <<p->data <<endl;
	cout << "删除第二个节点:" << endl;
	ListNode* Del = ListDeleteNode(head, 1);
	ListPrint(Del);
	cout << "在第二个位置插入88" << endl;
	ListNode* Int = ListInsertNode(head, 1, 88);
	ListPrint(head);
	ListDestroyList(&head);
	return 0;
}

8.测试结果

这就是测试结果

总结

完整代码如下:

#include<stdio.h>
#include<malloc.h>
# include<iostream>
using namespace std;

typedef int DataType;

struct ListNode {
	DataType data;
	ListNode* next;
};

void ListPrint(ListNode* head) {
	ListNode* vtx = head;
	while (vtx) {
		cout << vtx->data<<" ";
		vtx = vtx->next;
	}
	cout << "NULL" << endl;
}


ListNode* ListCreateNode(DataType data) {
	ListNode* node = (ListNode*)malloc(sizeof(ListNode));
	node->data = data;
	node->next = NULL;
	return node;
}

ListNode* ListGetNode(ListNode* head, int i) {
	ListNode* temp = head;
	int j = 0;
	while (temp&&j<i) {
		j++;
		temp = temp->next;
	}
	if (j > i || !temp) {
		return NULL;
	}
	return temp;
}

ListNode* ListFindNodeByValue(ListNode* head, DataType v) {
	ListNode* temp = head;
	while (temp) {
		if (temp->data == v) {
			return temp;
		}
		temp = temp->next;
	}
	return NULL;
}

ListNode* ListInsertNode(ListNode* head, int i, DataType v) {
	ListNode* pre, * aft, * vtx;
	int j = 0;
	pre = head;
	while (pre && j < i) {
		pre = pre->next;
		j++;
	}
	if (!pre) {
		return NULL;
	}
	vtx = ListCreateNode(v);
	aft = pre->next;
	vtx->next = aft;
	pre->next = vtx;
	return vtx;
}

ListNode* ListDeleteNode(ListNode* head, int i) {
	ListNode* aft, * pre, * del;
	int j = 0;
	if (!head) {
		return NULL;
	}
	if (i == 0) {
		del = head;
		head = head->next;
		free(del);
		return head;
	}
	pre = head;
	while (pre && j < i) {
		j++;
		pre = pre->next;
	}
	// 当前pre就是第i+1个节点
	if (!pre && !pre->next) {
		return head;
	}

	del = pre->next;
	aft = del->next;
	pre->next = aft;
	free(del);
	return head;
}

ListNode* ListCreateListByTail(int n, int a[]) {
	ListNode* head, * tail, * vtx;
	int idx=0;
	if (n <= 0) {
		return NULL;
	}
	vtx = ListCreateNode(a[0]);
	head = tail = vtx;
	while (++idx < n) {
		vtx = ListCreateNode(a[idx]);
		tail->next = vtx;
		tail = vtx;
	}
	return head;
}

ListNode* ListCreateListByHead(int n, int* a) {
	
	ListNode* head = NULL, * vtx;
	while (n--) {
		vtx = ListCreateNode(a[n]);
		vtx->next = head;
		head = vtx;
	}
	return head;
}

void ListDestroyList(ListNode ** pHead) {
	ListNode*head = *pHead;
	while(head) {
		head = ListDeleteNode(head, 0);
		// ListPrint(head);
	}
	*pHead = NULL;
	cout << "释放成功!" << endl;
}

int main() {
	int a[5] = { 4,33,9,8,10 };
	ListNode* head = ListCreateListByHead(5, a);
	ListPrint(head);
	ListNode* p = ListGetNode(head, 1);
	cout << "第二个节点是:" <<p->data <<endl;
	cout << "删除第二个节点:" << endl;
	ListNode* Del = ListDeleteNode(head, 1);
	ListPrint(Del);
	cout << "在第二个位置插入88" << endl;
	ListNode* Int = ListInsertNode(head, 1, 88);
	ListPrint(head);
	ListDestroyList(&head);
	return 0;
}

注:这些代码是参考“英雄哪里出来”这个博主的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码有点萌

谢谢

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值