单链表的各种操作,增删改查以及单链表的翻转等等

#include<iostream>
using namespace std;
typedef struct Node {
	int data;//数据域
	struct Node* pNext;//指针域
}Node,*linkList;
Node* creatList(int length);//创造链表
bool showList(linkList);//打印链表
bool emptyList(linkList);//判断链表是否为空
int length(linkList);//返回链表长度
bool insertList(linkList head, int position, int data);//按位置在链表中插入节点值
bool deleteList(linkList head, int& value, int position);//按位置删除链表中的结点
Node* findList(linkList, int);//按位查找结点
bool tranves(linkList);//翻转链表
int main()
{
	Node* head = nullptr;
	int length;
	int value;
	cout << "***************************************************\n";
	printf("请输入所需要构造链表的长度:");
	scanf_s("%d", &length);
	cout << endl;
	head = creatList(length);
	printf("输出单链表的数据:\n");
	showList(head);
	cout << "***************************************************\n";

	printf("对单链表进行插入操作:\n");
	insertList(head, 2, 3);
	printf("输出单链表的数据:\n");
	showList(head);
	cout << "***************************************************\n";

	Node* node = findList(head, 2);
	if (node != nullptr) {
		printf("打印链表结点值%d", node->data);
	}
	else {
		printf("链表元素查找失败");
	}
	printf("删除结点之后单链表的数据:\n");
	deleteList(head, value, 2);
	showList(head);
	cout << "***************************************************\n";

	tranves(head);
	printf("翻转链表之后单链表的数据:\n");
	showList(head);
	cout << "***************************************************\n";

	return 0;
}
bool tranves(linkList head) {
	if (head == nullptr)
		return false;
	Node* p = head->pNext;
	Node* temp = p->pNext;
	Node* a = temp;
	p->pNext = nullptr;
	while (a != nullptr) {
		a = a->pNext;
		temp->pNext = p;
		head->pNext = temp;
		p = temp;
		temp = a;
	}

	return true;
}
Node* findList(linkList head, int position) {
	if (head == nullptr) {
		printf("链表为空!!!\n");
		return nullptr;//防止链表为空
	}
	Node* node = head->pNext;
	int i = 0;
	if (position < 0) {
		printf("删除位置有误!!!\n");
		return nullptr;
	}
	while (i < position - 1 && node != nullptr) {
		i++;
		node = node->pNext;
	}

	return node;
}
bool deleteList(linkList head, int& value, int position) {
	if (head == nullptr) {
		printf("链表为空!!!\n");
		return false;//防止链表为空
	}
	Node* node = head->pNext;
	int i = 0;
	if (position < 0) {
		printf("删除位置有误!!!\n");
		return false;
	}
	while (i < position -1 && node != nullptr) {
		i++;
		node = node->pNext;
	}

	Node* q = node->pNext;
	value = q->data;
	node->pNext = q->pNext;
	delete q;//事实上删除空结点也不会报错

	return true;
}
bool insertList(linkList head, int position, int data) {
	if (head == nullptr){
		printf("链表为空!!!\n");
		return false;//防止链表为空
	}
	Node* node = head->pNext;
	int i = 0;
	int len = length(head);
	if (position < 0 || position>len) {
		printf("插入位置有误!!!\n");
		return false;
	}
	while (i < position - 1 && node != nullptr) {
		i++;
		node = node->pNext;
	}
	Node* newNode = new Node;
	newNode->data = data;
	newNode->pNext = node->pNext;
	node->pNext = newNode;

	return true;
}
int length(linkList head) {
	Node* node = head->pNext;
	int length = 0;
	while (node != nullptr) {
		length++;
		node = node->pNext;
	}

	return length;
}
bool emptyList(linkList head) {
	return head == nullptr;
}
bool showList(linkList head) {
	if (head == nullptr)
		return false;//防止链表为空
	Node* node = head->pNext;
	int i = 0;
	while (node != nullptr) {
		printf("第%d结点的数据为%d\n", i++, node->data);
		node = node->pNext;
	}

	return true;
}
Node* creatList(int length) {
	Node* head = new Node;
	head->pNext = nullptr;
	if (head == nullptr) {
		printf("结点创建失败!!!");
		exit(-1);
	}
	int data;//结点的数据域
	Node* temp = head;
	for (int i = 0; i < length; i++) {
		Node* node = new Node;
		if (head == nullptr) {
			printf("结点创建失败!!!");
			exit(-1);
		}
		printf("请输入结点的数据:");
		scanf_s("%d", &data);

		node->data = data;//尾插法(也可以用头插法)
		temp->pNext = node;
		temp = node;
		temp->pNext = nullptr;
	}
	
	return head;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值