SDUT ACM PTA 数据结构 实验二 链表

1 单链表的创建及遍历

#include <bits/stdc++.h>
using namespace std;

int main() {
	list<int> l;
	int n, x;
	cin >> n;
	//cin
	while (n--) {
		cin >> x;
		l.push_back(x);
	}
	//cout
	list<int>::iterator it;
	for (it = l.begin(); it != l.end(); it++) {
		if (it == l.begin())
			cout << *it;
		else
			cout << " " << *it;
	}
	return 0;
}

2 两个有序链表序列的合并

#include <bits/stdc++.h>
using namespace std;

int main() {
	list<int> l1, l2;
	int x;
	//cin
	while (cin >> x) 
	{
		if (x == -1)	break;
		else	l1.push_back(x);
	}
	while (cin >> x) 
	{
		if (x == -1)	break;
		else	l2.push_back(x);
	}
	//合并函数合并
	l1.merge(l2);
	//cout
	list<int>::iterator it;
	if (l1.empty())cout << "NULL" << endl;//存在都为空的情况
	else 
	{
		for (it = l1.begin(); it != l1.end(); it++) 
		{
			if (it == l1.begin())
				cout << *it;
			else
				cout << " " << *it;
		}
	}
	return 0;
}

3 单链表的创建,遍历与销毁

#include <bits/stdc++.h>
using namespace std;

int main() 
{
	list<int> l;
	int x;
	//cin
	while (cin >> x) 
	{
		if (x == -1)	break;
		else	l.push_back(x);
	}
	//逆序
	l.reverse();//也可以直接逆序输出
	//cout
	list<int>::iterator it;
	for (it = l.begin(); it != l.end(); it++)
		cout << *it << " " ;
	return 0;
}

4 程序设计综合实践 1.3

#include <bits/stdc++.h>
using namespace std;

int main() {
	list<int> l1, l2;
	int x;
	//cin
	while (cin >> x) 
	{
		if (x > 0)		l1.push_back(x);
		else if (x < 0)		l2.push_back(x);
	}
	//sort
	l1.sort();
	l2.sort();
	//cout
	list<int>::iterator it;
	for (it = l1.begin(); it != l1.end(); it++) 
	{
		if (it == l1.begin())	cout << *it;
		else	cout << "->" << *it ;
	}
	cout << "\n";
	for (it = l2.begin(); it != l2.end(); it++) 
	{
		if (it == l2.begin())	cout << *it;
		else	cout << "->" << *it ;
	}
	cout << "\n";
	//reverse
	l1.reverse();
	l2.reverse();
	for (it = l1.begin(); it != l1.end(); it++) 
	{
		if (it == l1.begin())	cout << *it;
		else	cout << "->" << *it ;
	}
	cout << "\n";
	for (it = l2.begin(); it != l2.end(); it++) 
	{
		if (it == l2.begin())	cout << *it;
		else	cout << "->" << *it ;
	}
	cout << "\n";
	return 0;
}

5 程序设计综合实践 1.4

#include <bits/stdc++.h>
using namespace std;

int main() {
	list<int> l;
	int x,t;
	//cin
	while (cin >> x)l.push_back(x);
	//sort
	l.sort();
	//cout
	list<int>::iterator it;
	for (it = l.begin(); it != l.end(); it++) 
	{
		if (it == l.begin())	cout << *it;
		else	cout <<"->"<<*it ;
	}
	cout<<"\n";
	t=0;
	for (it = l.begin(); it != l.end(); it++) 
	{
		if (*it % 2 == 1)
			if (t==0){cout << *it;t=1;}
		else
			cout <<"->"<<*it ;
	}
	cout << "\n";
	t=0;
	for (it = l.begin(); it != l.end(); it++) 
	{
		if (*it % 2 == 0)
			if (t==0){cout<<*it;t=1;}
		else
			cout <<"->"<<*it;
	}
	cout << "\n";
	return 0;
}

6 删除重复字符

#include <bits/stdc++.h>
using namespace std;

int main() {
	list<char> l;
	char x;
	//cin
	while (x != EOF) 
	{
		if ((x = getchar()) == '\n')break;
		else	l.push_back(x);
	}
	//去重函数要用在排序之后
	l.sort();
	l.unique();
	//cout
	list<char>::iterator it;
	for (it = l.begin(); it != l.end(); it++)
		cout << *it;
	return 0;
}

7 约瑟夫环

#include <bits/stdc++.h>
using namespace std;

int main() {
	list<int> l1;
	int n, m, i;
	cin >> n >> m;
	for (i = 1; i <= n; i++) 
	{
		l1.emplace_back(i);
	}
	list<int>::iterator it1 = l1.begin();
	while (l1.size() != 1) 
	{ //链表元素个数如果为1,说明只剩下了一个人,那么删除操作就结束
		for (i = 0; i < m - 1; i++) 
		{
			if (it1 == --l1.end()) 
			{ //如果迭代器指向了链表最后一个元素,就是l1.end()的前一位,则返回到链表头部
				it1 = l1.begin();
			}
			else
				it1++;//如果迭代器没有指向最后一个元素,那么就正常自增
		}
		cout << *it1 << " ";
		if (it1 != --l1.end()) 
		{ //如果不是最后一个元素,正常删除就行
			it1 = l1.erase(it1); //erase()函数返回的是删除元素下一位,这里是防止下一位是l1.end()的情况
		}
		else
		{
			l1.pop_back();//如果是最后一个元素的话,直接调用pop_back函数,并且迭代器直接返回到链表头部,这一步很关键
			it1 = l1.begin();
		}
	}
	cout << *it1;
	return 0;
}

8 一元多项式的乘法与加法运算

#include<bits/stdc++.h>
using namespace std;
#define N 10100
int a[N],b[N],c[N],d[N];//a:第一个多项式,b:第二个多项式,c:a,b乘积多项式,d:a,b和多项式  
int main()
{
	int m,n,tmp1,tmp2,count=0;//tmp1:系数,tmp2:指数 ,,count用于最后的输出格式控制
	cin>>m;
	for(int i=0;i<m;i++){
		cin>>tmp1>>tmp2;
		a[tmp2]=tmp1;//指数为数组的下标,系数为数组的值
	}
	cin>>n;
	for(int j=0;j<n;j++){
		cin>>tmp1>>tmp2;
		b[tmp2]=tmp1;
	}
	for(int i=0;i<N;i++){
		for(int j=0;j<N;j++){
			if(a[i]&&b[j]){
				c[i+j]+=a[i]*b[j];//c数组是新的空数组,算法是指数相加,系数相乘,注意是+=,
				//因为有可能出现指数相同的情况 
			}	
		}
	}
	for(int i=0;i<N;i++){
		if(a[i]||b[i])
			d[i]+=b[i];//d数组是新的空数组,算法是系数相加,注意是+=
		d[i]+=a[i];
	}
	for(int k=N;k>=0;k--){
		if(c[k]){
			if(count) cout<<' '; 
			cout<<c[k]<<' '<<k;
			count++;
		}
	}
	if(count==0)
		cout<<"0 0";
	cout<<endl;
	count=0;
	for(int k=N;k>=0;k--){
		if(d[k]){
			if(count) cout<<' ';
			cout<<d[k]<<' '<<k;
			count++;
		}
	}
	if(count==0)
		cout<<"0 0";
	return 0;
}

9 带头节点的双向循环链表操作

#include <bits/stdc++.h>
using namespace std;

int main() {
	list<int> l;//list本身就是双向链表
	int x, f = 1;
	//cin
	while (cin >> x) 
		if (x == -1)break;
		else 
		{
			if (f == 1) 
			{
				l.push_front(x);
				f = 2;
			}
			else if (f == 2) 
			{
				l.push_back(x);
				f = 1;
			}
		}
	//cout
	list<int>::iterator it;
	for (it = l.begin(); it != l.end(); it++) 
	{
		if (it == l.begin())	cout << *it;
		else	cout << " " << *it;
	}
	cout << "\n";
	l.reverse();
	for (it = l.begin(); it != l.end(); it++) 
	{
		if (it == l.begin())	cout << *it;
		else	cout << " " << *it;
	}
	return 0;
}

10 链表去重

#include<bits/stdc++.h>
using namespace std;
struct Node{
	int data;
	int next;
}a[100001];
int main()
{
	int first,n;
	cin>>first>>n;
	while(n--)
	{
		int key;
		cin>>key;
		cin>>a[key].data>>a[key].next;
	}
	int vis[100001]={0};
	int pre[100001],del[100001];
	int k1=0,k2=0;
	int i=first;
	while(a[i].next!=-1){
		int num=abs(a[i].data);
		if(vis[num]==0)
		{
			vis[num]=1;
			pre[k1++]=i;
		}
		else del[k2++]=i;
		i=a[i].next;
	}
	int num=abs(a[i].data);
	if(vis[num]==0) pre[k1++]=i;
	else del[k2++]=i;
	printf("%05d %d ",pre[0],a[pre[0]].data);
	for(int j=1;j<k1;j++)
	{
		printf("%05d\n",pre[j]);
		printf("%05d %d ",pre[j],a[pre[j]].data);
	}
	cout<<"-1"<<endl;
	if(k2)
	{
		printf("%05d %d ",del[0],a[del[0]].data);
		for(int j=1;j<k2;j++)
		{
			printf("%05d\n",del[j]);
			printf("%05d %d ",del[j],a[del[j]].data);
		}
		cout<<"-1"<<endl;
	}
	return 0;
}

11 单链表就地逆置

#include <bits/stdc++.h>
using namespace std;

int main() {
	list<int> l;
	int x, n;
	cin >> n;
	while (n--) 
	{
		while (cin >> x) 
		{	
			if (x == -1)	break;
			else	l.push_back(x);
		}
		l.reverse();
		list<int>::iterator it;
		for (it = l.begin(); it != l.end(); it++) 
		{
			if (it == l.begin())	cout << *it;
			else	cout << " " << *it;
		}
		cout << "\n";
		l.clear();
	}
	return 0;
}

不懂得可以私信我,随时解答。
程序有错误请私信我,以及时改正。感谢!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值