链表操作总结

本文介绍了动态链表的创建、节点插入、删除以及链表的反转、清空、升序插入等操作。通过示例代码详细展示了如何实现这些基本的链表操作,包括在特定节点前插入新节点、删除指定节点以及保持链表升序的插入方法。

声明

一年前闲着无聊时,研究研究了链表,当时写的时候也花了不少时间,当时我的思路如下

动态链表带头结点

#include <iostream>
using namespace std;
class node {
public:
	int id;
	node* next;
};

node* ListCreat() {
	node* head = NULL;

	head = (node*)malloc(sizeof(node));
	if (head == NULL)
	{
		return NULL;
}

	head->id =-1;
	head->next = NULL;

	node* pCur = head;
	node* pNew = NULL;

	int data;
	while (1) {
		cout <<"请输入数据";
		cin>>data;
		if (data == -1) {
			break;
		}


		pNew = (node*)malloc(sizeof(node));
			if (pNew == NULL) {
				continue;
			}

		pNew->id = data;
		pNew->next = NULL;
		//链表建立关系
		pCur->next = pNew;

		pNew->next = NULL;

		pCur = pNew;
	}
	return head;
}
int  ListPrint(node* head) {

	if (head == NULL)
	{
		return -1;
	}
	//取出第一个有效结点,head的下一次结点
	node* pCur = head->next;
	cout << "head->";
	while (pCur != NULL)
	{
		cout <<pCur->id<<"->";
		pCur = pCur->next;
	}
	cout << "NULL\n";
	return 0;
}
int  main() {
	node* head = NULL;
	head = ListCreat();
	ListPrint(head);
	cout << " ";
	system("pause");
	return 0;
}

结点的插入

#include <iostream>
using namespace std;
class node {
public:
	int id;
	node* next;
};
//在值为x的结点前,插入值为y的结点,若x结点不存在,则插在表尾
int increasenode(node* head, int x, int y)
{
	if (head == NULL)
	{
		return -1;
	}
	node* Pre = head;
	node* Cur = head->next;
	while (Cur != NULL)
	{
		if (Cur->id == x)
		{
			break;
		}
		Pre = Cur;
		Cur = Cur->next;
	}
	node* pNew = (node*)malloc(sizeof(node));
	if (pNew == NULL) {
		return -2;
	}
	pNew->id = y;
	pNew->next = NULL;

	Pre->next = pNew;
	pNew->next = Cur;
}

node* ListCreat() {
	node* head = NULL;

	head = (node*)malloc(sizeof(node));
	if (head == NULL)
	{
		return NULL;
	}

	head->id = -1;
	head->next = NULL;

	node* pCur = head;
	node* pNew = NULL;

	int data;
	while (1) {
		cout << "请输入数据";
		cin >> data;
		if (data == -1) {
			break;
		}


		pNew = (node*)malloc(sizeof(node));
		if (pNew == NULL) {
			continue;
		}

		pNew->id = data;
		pNew->next = NULL;
		//链表建立关系
		pCur->next = pNew;

		pNew->next = NULL;

		pCur = pNew;
	}
	return head;
}
int  ListPrint(node* head) {

	if (head == NULL)
	{
		return -1;
	}
	//取出第一个有效结点,head的下一次结点
	node* pCur = head->next;
	cout << "head->";
	while (pCur != NULL)
	{
		cout << pCur->id << "->";
		pCur = pCur->next;
	}
	cout << "NULL\n";
	return 0;
}
int  main() {
	node* head = NULL;
	head = ListCreat();
	ListPrint(head);
	int x; int y;
	cout << "分别输入x和y,在x的前面插入y" << endl;
	cin >> x >> y;
	increasenode(head, x, y);
	cout << "在x的前面插入y:";
	ListPrint(head);
	cout << " ";
	system("pause");
	return 0;

}

静态链表

#include <iostream>
using namespace std;
class node {
public:
	int id;
	char name[100];
	node* next;

};
int  main() {

	node s1 = { 1,"biaobiao",NULL };
	node s2 = { 2,"aha",NULL };
	node s3 = { 3,"leilei",NULL };
	s1.next = &s2;
	s2.next = &s3;
	s3.next = NULL;
	node* p = &s1;
	while (p != NULL) {
		cout << p->id << "   " << p->name << "   " << endl;
		p = p->next;
	}
}

链表反转

#include<iostream>
using namespace std;
 class node {
public:
	int id;
	node* next;
};
int noderexerse(node* head) {
	if (head == NULL || head->next == NULL || head->next->next == NULL)
	{
		return -1;
	}
	node* pre = head->next;
	node* cur = pre->next;
	node* temp=NULL;
	while (cur != NULL)
	{
		temp=cur->next;
		cur->next = pre;

		pre = cur;
		cur = temp;
	}
	head->next->next = NULL;
	head->next = pre;
	return 0;
}

node* ListCreat() {
	node* head = NULL;

	head = (node*)malloc(sizeof(node));
	if (head == NULL)
	{
		return NULL;
	}

	head->id = -1;
	head->next = NULL;

	node* pCur = head;
	node* pNew = NULL;

	int data;
	while (1) {
		cout << "请输入数据";
		cin >> data;
		if (data == -1) {
			break;
		}

		pNew = (node*)malloc(sizeof(node));
		if (pNew == NULL) {
			continue;
		}

		pNew->id = data;
		pNew->next = NULL;
		//链表建立关系
		pCur->next = pNew;

		pNew->next = NULL;

		pCur = pNew;
	}
	return head;
}
int  ListPrint(node* head) {

	if (head == NULL)
	{
		return -1;
	}
	//取出第一个有效结点,head的下一次结点
	node* pCur = head->next;
	cout << "head->";
	while (pCur != NULL)
	{
		cout << pCur->id << "->";
		pCur = pCur->next;
	}
	cout << "NULL\n";
	return 0;
}
int  main() {
	node* head = NULL;
	head = ListCreat();
	ListPrint(head);
	noderexerse(head);
	cout << "反转后为:" << endl;
	ListPrint(head);
}

清空动态建立的链表

#include <iostream>
using namespace std;
class node {
public:
	int id;
	node* next;
};
//清空链表
int destroynode(node* head) {
	if (head == NULL) {
		return -1;
	}
	node* temp = NULL;
	int i = 0;
	while (head != NULL)
	{
		temp = head->next;
		free(head);
		head = NULL;
		head = temp;
		i++;
	}
	cout << "释放次数为:" << i<<endl;
	return 0;
}
node* ListCreat() {
	node* head = NULL;

	head = (node*)malloc(sizeof(node));
	if (head == NULL)
	{
		return NULL;
	}

	head->id = -1;
	head->next = NULL;

	node* pCur = head;
	node* pNew = NULL;

	int data;
	while (1) {
		cout << "请输入数据";
		cin >> data;
		if (data == -1) {
			break;
		}


		pNew = (node*)malloc(sizeof(node));
		if (pNew == NULL) {
			continue;
		}

		pNew->id = data;
		pNew->next = NULL;
		//链表建立关系
		pCur->next = pNew;

		pNew->next = NULL;

		pCur = pNew;
	}
	return head;
}
int  ListPrint(node* head) {

	if (head == NULL)
	{
		return -1;
	}
	//取出第一个有效结点,head的下一次结点
	node* pCur = head->next;
	cout << "head->";
	while (pCur != NULL)
	{
		cout << pCur->id << "->";
		pCur = pCur->next;
	}
	cout << "NULL\n";
	return 0;
}
int  main() {
	node* head = NULL;
	head = ListCreat();
	ListPrint(head);
	destroynode(head);
	ListPrint(head);
	cout << " ";
	system("pause");
	return 0;
}

删除第一个为x的结点

#include <iostream>
using namespace std;
class node {
public:
	int id;
	node* next;
};
int freecode(node*head,int x)
{
	if (head == NULL)
	{
		return -1;
	}
	node* Pre = head;
	node* Cur = head->next;
	int flag = 0;//0没有找到,1找到了
	while (Cur != NULL)
	{
		if (Cur->id == x)
		{
			Pre->next = Cur->next;
			free(Cur);
			Cur = NULL;
			flag = 1;
			break;
		}
		Pre = Cur;
		Cur = Cur->next;
	}
	if (flag == 0)
	{
		cout << "没有值为" << x << "的结点" << endl;
		return -2;
	}


}
node* ListCreat() {
	node* head = NULL;

	head = (node*)malloc(sizeof(node));
	if (head == NULL)
	{
		return NULL;
	}

	head->id = -1;
	head->next = NULL;

	node* pCur = head;
	node* pNew = NULL;

	int data;
	while (1) {
		cout << "请输入数据";
		cin >> data;
		if (data == -1) {
			break;
		}


		pNew = (node*)malloc(sizeof(node));
		if (pNew == NULL) {
			continue;
		}

		pNew->id = data;
		pNew->next = NULL;
		//链表建立关系
		pCur->next = pNew;

		pNew->next = NULL;

		pCur = pNew;
	}
	return head;
}
int  ListPrint(node* head) {

	if (head == NULL)
	{
		return -1;
	}
	//取出第一个有效结点,head的下一次结点
	node* pCur = head->next;
	cout << "head->";
	while (pCur != NULL)
	{
		cout << pCur->id << "->";
		pCur = pCur->next;
	}
	cout << "NULL\n";
	return 0;
}
int  main() {
	node* head = NULL;
	head = ListCreat();
	ListPrint(head);
	int x;
	cout << "请输入需要删除的结点" << endl;
	cin >> x;
	freecode(head, x);
	cout << "删除后结果为:" << endl;
	ListPrint(head);
	cout << " ";
	system("pause");
	return 0;

}

删除所有为x的结点

#include <iostream>
using namespace std;
class node {
public:
	int id;
	node* next;
};
int freecode(node* head, int x)
{
	if (head == NULL)
	{
		return -1;
	}
	node* Pre = head;
	node* Cur = head->next;
	int flag = 0;//0没有找到,1找到了
	while (Cur != NULL)
	{
		if (Cur->id == x)
		{
			Pre->next = Cur->next;
			free(Cur);
			Cur = NULL;
			flag = 1;
			Cur = Pre->next;
			continue;
			//break;
		}
		Pre = Cur;
		Cur = Cur->next;
	}
	if (flag == 0)
	{
		cout << "没有值为" << x << "的结点" << endl;
		return -2;
	}


}
node* ListCreat() {
	node* head = NULL;

	head = (node*)malloc(sizeof(node));
	if (head == NULL)
	{
		return NULL;
	}

	head->id = -1;
	head->next = NULL;

	node* pCur = head;
	node* pNew = NULL;

	int data;
	while (1) {
		cout << "请输入数据";
		cin >> data;
		if (data == -1) {
			break;
		}


		pNew = (node*)malloc(sizeof(node));
		if (pNew == NULL) {
			continue;
		}

		pNew->id = data;
		pNew->next = NULL;
		//链表建立关系
		pCur->next = pNew;

		pNew->next = NULL;

		pCur = pNew;
	}
	return head;
}
int  ListPrint(node* head) {

	if (head == NULL)
	{
		return -1;
	}
	//取出第一个有效结点,head的下一次结点
	node* pCur = head->next;
	cout << "head->";
	while (pCur != NULL)
	{
		cout << pCur->id << "->";
		pCur = pCur->next;
	}
	cout << "NULL\n";
	return 0;
}
int  main() {
	node* head = NULL;
	head = ListCreat();
	ListPrint(head);
	int x;
	cout << "请输入需要删除的结点" << endl;
	cin >> x;
	freecode(head, x);
	cout << "删除后结果为:" << endl;
	ListPrint(head);
	cout << " ";
	system("pause");
	return 0;

}

升序链表插入

#include<iostream>
using namespace std;
 struct node {

	int id;
	node* next;
};
//假如原来链表是升序 的,升序后插入新节点
//不能插入结点后再排序,是升序插入新节点x
int  insertnode(node* head, int x) {
	
	 if (head == NULL)
	 {
		 return -1;
	 }
	 node* Pre = head;
	 node* Cur = head->next;
	 while (Cur != NULL)
	 {
		 if (Cur->id > x)
		 {
			 break;
		 }
		 Pre = Cur;
		 Cur = Cur->next;
	 }
	 node* pNew = (node*)malloc(sizeof(node));
	 if (pNew == NULL) {
		 return -2;
	 }
	 pNew->id = x;
	 pNew->next = NULL;

	 Pre->next = pNew;
	 pNew->next = Cur;

  }
int  nodesort(node* head)
{
	if (head == NULL || head->next == NULL)
	{
		return 0;
	}
	node* pre = NULL;
	node* cur = NULL;
	node temp;
	for (pre = head->next; pre->next != NULL; pre = pre->next)
	{
		for (cur = pre->next; cur != NULL; cur = cur->next)
		{
			if (pre->id > cur->id)
			{
				temp.id = cur->id;
				cur->id = pre->id;
				pre->id = temp.id;
				/*
				temp = *cur;
				*cur = *pre;
				*pre = temp;
				temp.next = cur->next;
				cur->next = pre->next;
				pre->next = temp.next;*/
			}
		}
	}
	return 0;
}

node* ListCreat() {
	node* head = NULL;

	head = (node*)malloc(sizeof(node));
	if (head == NULL)
	{
		return NULL;
	}

	head->id = -1;
	head->next = NULL;

	node* pCur = head;
	node* pNew = NULL;

	int data;
	while (1) {
		cout << "请输入数据";
		cin >> data;
		if (data == -1) {
			break;
		}


		pNew = (node*)malloc(sizeof(node));
		if (pNew == NULL) {
			continue;
		}

		pNew->id = data;
		pNew->next = NULL;
		//链表建立关系
		pCur->next = pNew;

		pNew->next = NULL;

		pCur = pNew;
	}
	return head;
}
int  ListPrint(node* head) {

	if (head == NULL)
	{
		return -1;
	}
	//取出第一个有效结点,head的下一次结点
	node* pCur = head->next;
	cout << "head->";
	while (pCur != NULL)
	{
		cout << pCur->id << "->";
		pCur = pCur->next;
	}
	cout << "NULL\n";
	return 0;
}
int  main() {
	node* head = NULL;
	head = ListCreat();
	ListPrint(head);
	nodesort(head);
	int x;
	cout << "输入想要升序插入的数:" << endl;
	cin >> x;
	cout << "升序插入" << x << "后:" << endl;
	insertnode(head, x);
	ListPrint(head);
}

用链表进行排序

#include<iostream>
using namespace std;
class node {
public:
	int id;
	node* next;
};
int  nodesort(node* head) {
	if (head == NULL || head->next == NULL)
	{
		return 0;
	}
	node* pre = NULL;
	node* cur = NULL;
	node temp;
	for (pre = head->next; pre->next != NULL; pre = pre->next)
	{
		for (cur = pre->next; cur != NULL; cur = cur->next)
		{
			if (pre->id > cur->id)
			{
				temp.id = cur->id;
				cur->id = pre->id;
				pre->id = temp.id;
				/*
				temp = *cur;
				*cur = *pre;
				*pre = temp;
				temp.next = cur->next;
				cur->next = pre->next;
				pre->next = temp.next;*/
			}
		}
	}
	return 0;
}

node* ListCreat() {
	node* head = NULL;

	head = (node*)malloc(sizeof(node));
	if (head == NULL)
	{
		return NULL;
	}

	head->id = -1;
	head->next = NULL;

	node* pCur = head;
	node* pNew = NULL;

	int data;
	while (1) {
		cout << "请输入数据";
		cin >> data;
		if (data == -1) {
			break;
		}


		pNew = (node*)malloc(sizeof(node));
		if (pNew == NULL) {
			continue;
		}

		pNew->id = data;
		pNew->next = NULL;
		//链表建立关系
		pCur->next = pNew;

		pNew->next = NULL;

		pCur = pNew;
	}
	return head;
}
int  ListPrint(node* head) {

	if (head == NULL)
	{
		return -1;
	}
	//取出第一个有效结点,head的下一次结点
	node* pCur = head->next;
	cout << "head->";
	while (pCur != NULL)
	{
		cout << pCur->id << "->";
		pCur = pCur->next;
	}
	cout << "NULL\n";
	return 0;
}
int  main() {
	node* head = NULL;
	head = ListCreat();
	ListPrint(head);
	nodesort(head);
	cout << "排序后为:";
	ListPrint(head);
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

寻梦&之璐

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值