单链表结合

单链表就是包含着数据域和指针域,它不像数组那样连续分配空间的,它是连续的,所以保存一个连接下一个数据的指针域,也就是struct node*next,。

//尾插法
#include<iostream>
using namespace std;
typedef struct node
{
	int data;
	struct node*next;
}node;
void showlist(node*head)
{
	
		head = head->next;
		while (head != NULL)
		{
			cout << head->data <<" ";
			head = head->next;
		}
	

}
node* creatlist()
{
	node* head = new node[sizeof(node)];
	node*t = head;
	if (head == NULL)
	{
		cout << "创建失败";
	}//创建 一个空链表
	int data;
	cin >> data;
	while (data)
	{
		node*cur = new node[sizeof(node)];
		cur->data = data;
		t->next = cur;
		t = cur;
		cin >> data;
	}
	t->next = NULL;
	return head;
}
int main()
{
	node* head = creatlist();
	showlist(head);
	~system("pause");
	return 0;

}

 

 

//头插法
#if 1
#include<iostream>
using namespace std;
typedef struct node
{
	int data;
	struct node*next;
}node;
void showlist(node*head)
{
	while (head != NULL)
	{
		head = head->next;
		while (head != NULL)
		{
			cout << head->data << endl;
			head = head->next;
		}
	}

}
node* creatlist()
{
	node* head = (node*)malloc(sizeof(node));
	head->next = NULL;//不能少啊!!
	if (head == NULL)
	{
		exit(-1);
	}//创建 一个空链表
	int data;
	cin >> data;
	while (data)
	{
		node*cur = (node*)malloc(sizeof(node));
		cur->data = data;//要填入数据的啊
		cur->next = head->next;
		head->next = cur;
		cin >> data;
	}
	return head;
}
int main()
{
	node* head = creatlist();
	showlist(head);
	~system("pause");
	return 0;

}
#endif

 

 

//数据的删除

//尾插法
#include<iostream>
using namespace std;
typedef struct node
{
	int data;
	struct node*next;
}node;
void showlist(node*head)
{

	node*phead = head->next;
	while (phead != NULL)
	{
		cout << phead->data << " ";
		phead = phead->next;
	}


}
node* creatlist()
{
	node* head = new node[sizeof(node)];
	node*t = head;
	if (head == NULL)
	{
		cout << "创建失败";
	}//创建 一个空链表
	int data;
	cin >> data;
	while (data)
	{
		node*cur = new node[sizeof(node)];
		cur->data = data;
		t->next = cur;
		t = cur;
		cin >> data;
	}
	t->next = NULL;
	return head;
}
void deleteData(node*head)
{
	cout << "请输入你要删除的数据";
	int data;
	cin >> data;
	node* p = head->next;
	node*q, *ph;
	if (p->data == data)
	{
		head->next = p->next;
		ph = p;
		p = ph->next;
		free(ph);
	}
	while (p != NULL)
	{
		ph = p;
		if (p->data == data)
		{
			q->next = p->next;//1
			p = ph->next;//2
			free(ph);//3
			continue;
		}
		q = p;//4
		p = p->next;//5

	}

}
int main()
{
	node* head = creatlist();
	showlist(head);

	deleteData(head);
	cout << "删除后";
	showlist(head);
	~system("pause");
	return 0;

}

 在删除的时候,要注意如果删除的数据是第一个数据的时候,要另外讨论,在写这个代码的时候,我就忽略了,然后就想了一下,删除第一数据怎么办。还要注意的一点就是head不保存数据。

补充还是有问题,如果1 1 1 2 3 54,那么就出错!

 

数据的反转

//数据的反转
#include<iostream>
using namespace std;
typedef struct node
{
	int data;
	struct node*next;
}node;
void showlist(node*head)
{

	node*phead = head->next;
	while (phead != NULL)
	{
		cout << phead->data << " ";
		phead = phead->next;
	}


}
node* creatlist()
{
	node* head = new node[sizeof(node)];
	node*t = head;
	if (head == NULL)
	{
		cout << "创建失败";
	}//创建 一个空链表
	int data;
	cin >> data;
	while (data)
	{
		node*cur = new node[sizeof(node)];
		cur->data = data;
		t->next = cur;
		t = cur;
		cin >> data;
	}
	t->next = NULL;
	return head;
}
void reverList(node*head)
{
	node*p = head->next;
	head->next = NULL;
	node*cur=p;
	node*t;
	while (cur != NULL)
	{
		t = cur;
		cur = cur->next;
		t->next = head->next;
		head->next = t;
	}
}
int main()
{
	node* head = creatlist();
	showlist(head);
	reverList(head);
	cout << "逆置后" << endl;
	showlist(head);
	~system("pause");
	return 0;

}

 

数据的插入(本质:头插法)

//尾插法
#include<iostream>
using namespace std;
typedef struct node
{
	int data;
	struct node*next;
}node;

node* creatlist()
{
	node* head = new node[sizeof(node)];
	node*t = head;
	if (head == NULL)
	{
		cout << "创建失败";
	}//创建 一个空链表
	int data;
	cin >> data;
	while (data)
	{
		node*cur = new node[sizeof(node)];
		cur->data = data;
		t->next = cur;
		t = cur;
		cin >> data;
	}
	t->next = NULL;
	return head;
}
void showlist(node*head)
{

	head = head->next;
	while (head != NULL)
	{
		cout << head->data << " ";
		head = head->next;
	}
	cout << endl;


}
void insertData(node*head)
{
	cout << "请输入你要插入的数据:";
	int data;
	cin >> data;
	node*cur = new node[sizeof(node)];
	cur->data = data;
	cur->next = head->next;
	head->next = cur;

}
int main()
{
	node* head = creatlist();
	showlist(head);
	insertData(head);
	showlist(head);
	~system("pause");
	return 0;

}

 链表的冒泡排序

//尾插法
#include<iostream>
using namespace std;
typedef struct node
{
	int data;
	struct node*next;
}node;

node* creatlist()
{
	node* head = new node[sizeof(node)];
	node*t = head;
	if (head == NULL)
	{
		cout << "创建失败";
	}//创建 一个空链表
	int data;
	cin >> data;
	while (data)
	{
		node*cur = new node[sizeof(node)];
		cur->data = data;
		t->next = cur;
		t = cur;
		cin >> data;
	}
	t->next = NULL;
	return head;
}
void showlist(node*head)
{

	head = head->next;
	while (head != NULL)
	{
		cout << head->data << " ";
		head = head->next;
	}
	cout << endl;


}
void listDataConpare(node*head)
{
	node*ph = head->next;
	head = head->next;
	node*p,*q;
	int len=0;
	while (ph != NULL)
	{
		len++;
		ph = ph->next;
	}

	int temp;
	for (int i=0; i<len-1;i++)
	{
		p= head;
		q = p->next;
		for (int j=0; j<len-1-i; j++)
		{
			if (p->data>q->data)
			{
				temp = p->data;
				p->data = q->data;
				q->data = temp;//积极交换的是数据,指针域未改变
			}
			p = p->next;
			q = p->next;
		}
	}
}
int main()
{
	node* head = creatlist();
	showlist(head);
	listDataConpare(head);
	showlist(head);
	~system("pause");
	return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值