某农业大学数据结构A-第3周作业

1.两个有序单链表的合并

【问题描述】两个不带头结点的有序单链表LA和LB的合并,请合并到LA表中再输出,LB表的空间全部释放。

【样例输入】

1 3 5 7 9 0

1 2 3 4 15 20 0

【样例输出】

1 2 3 4 5 7 9 15 20

【注意】0代表输入结束

#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
	int data;
	struct node*next;
 }node,*List;
 
void Initlist(List r)
{
	r->next=NULL;
}

node*creatlist(List r)
{
    node *s;
    node *p;
    p=r;
	int c,flag=1;
	while(flag==1)
	{
		scanf("%d",&c);
		if(c!=0)
		{
			s=new node;
			s->data=c;
			p->next=s;
			p=s;
		}
		else
		{
			flag=0;
			p->next=NULL;
		}
	}
}
/*
void Merge(node* heada,node* headb,node* headc){
	node* a = heada->next;
	node* b = headb->next;
	node* c = headc;
	while(a != NULL && b != NULL){
		node* tmp = new node;
		tmp->next = NULL;
		if(a->data < b->data){
			tmp->data = a->data;
			a = a->next;
		}else if(a->data > b->data){
			tmp->data = b->data;
			b = b->next;
		}else{
			tmp->data = a->data;
			a = a->next;
			b = b->next;
		}
		c->next = tmp;
		c = tmp ;
	}
	while(a!=NULL){
		node* tmp = new node;
		tmp->data = a->data;
		tmp->next = NULL;
		c->next = tmp;
		c = tmp;
		a = a->next;
	}
	while(b!=NULL){
		node* tmp = new node;
		tmp->data = b->data;
		tmp->next = NULL;
		c->next = tmp;
		c = tmp;
		b = b->next;
	} 
}
*/

void Merge(List la,List lb,List lc)
{
	List a,b,c;
	a=la->next;
	b=lb->next;
	c=lc;
	while(a!=NULL && b!=NULL)
	{
		node*temp=new node;
		temp->next=NULL;
		if(a->data>b->data)
		{
			temp->data=b->data;
			b=b->next;
		}
		else if(a->data<b->data)
		{
			temp->data=a->data;
			a=a->next;
		}
		else
		{
			temp->data=a->data;
			a=a->next;
			b=b->next;
		}
		c->next=temp;
		c=temp;
	}
	if(a!=NULL)
	{
		c->next=a;
		c=a;
	}
	if(b!=NULL)
	{
		c->next=b;
		c=b;
	}
}

void Print(List r)
{
	node*p;
	p=r;
	while(r!=NULL)
	{
	    p=p->next;
		printf("%d ",p->data);
	}
}

int main()
{
	node*la=new node;
	node*lb=new node;
	node*lc=new node;
	Initlist(la);
	Initlist(lb);
	Initlist(lc);
	creatlist(la);
	creatlist(lb);
	Merge(la,lb,lc);
	Print(lc);
}
2.单链表数据排序

【问题描述】用单链表存储数据,实现对结点整体的从小到大的排序。

【注意】可采用翻转课堂上讲解的插入、冒泡排序中的一种,或者采用选择排序。每组成员一共需要实现其中的两种排序:插入和选择排序、或者冒泡和选择排序。

【输入形式】待排序的数据,以0作为结束。

【输出形式】排序后的数据

【样例输入】2 4 3 1 5 0

【样例输出】1 2 3 4 5

 
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define LM LLONG_MAX
#define IM INT_MAX
#define _for(i,a,b) for(int i=a;i<=b;i++)
struct node{
	int val;
	node* next;
};
inline int read(){
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch^48);
        ch=getchar();
    }
    return x*f;
}
node* head=NULL;
void Insert(int x){
	 if(head==NULL){
	 	node* tmp=new node;
	 	head=tmp;
	 	tmp->next=NULL;
	 	tmp->val=x;
	 	return;
	 }
	 node* tmp=head;
	 while(tmp->next!=NULL) tmp=tmp->next;
	 node* tmp1=new node;
	 tmp1->val=x;
	 tmp->next=tmp1;
	 tmp1->next=NULL;
	 return;	
} 
void Sort(){
	node* pre;
	node* cur;
	for(pre=head;pre!=NULL;pre=pre->next)
	for(cur=pre->next;cur!=NULL;cur=cur->next){
		if(pre->val>cur->val){
			int tmp=cur->val;
			cur->val=pre->val;
			pre->val=tmp;
			
		}
	}
	
}
void print(){
	node* temp=head;
	while(temp!=NULL){
		cout<<temp->val<<" ";
		temp=temp->next;
	}
}
int main(){
	int tmp;
	cin>>tmp;
	while(tmp){
		Insert(tmp);
		cin>>tmp;
	}
	Sort();
	print();
	
} 
3.多项式相加

【问题描述】编写一个程序用单链表存储多项式,并实现两个一元多项式A与B相加的函数。A,B刚开始是无序的,A与B之和按降序排列。例如:
                        多项式A:  1.2X^0  2.5X^1  3.2X^3  -2.5X^5
                        多项式B:  -1.2X^0  2.5X^1  3.2X^3   2.5X^5   5.4X^10
                        多项式A与B之和:5.4X^10  6.4X^3  5X^1
【输入形式】第一行第一个数据n代表多项式的总项数,后面的2*n个数据,每一对代表对应的某一项的系数和指数,第二行类似,第三行的数据x要查询第几项
【输出形式】多项式中某一项的系数与指数,系数保留一位小数

【输入样例】
4 1.2 0 2.5 1 3.2 3 -2.5 5
5 -1.2 0 2.5 1 3.2 3 2.5 5 5.4 10
2

【输出样例】

6.4 3

【样例说明】
第一个多项式的系数与指数对,以空格隔开
第二个多项式的系数与指数对,以空格隔开
输出第2项的系数与指数,系数与指数间用空格隔开,系数保留一位小数

【评分标准】必须用链表实现

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define LM LLONG_MAX
#define IM INT_MAX
#define _for(i,a,b) for(int i=a;i<=b;i++)
struct node{
	int x;
	double val;
	node* next;
};
inline int read(){
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch^48);
        ch=getchar();
    }
    return x*f;
}
node* head=NULL;
void Insert(double x,int y){
	 if(head==NULL){
	 	node* tmp=new node;
	 	head=tmp;
	 	tmp->next=NULL;
	 	tmp->x=y;
	 	tmp->val=x;
	 	return;
	 }
	 node* tmp=head;
	 while(tmp->next!=NULL) tmp=tmp->next;
	 node* tmp1=new node;
	 tmp1->x=y;
	 tmp1->val=x;
	 tmp->next=tmp1;
	 tmp1->next=NULL;
	 return;	
} 
void Sort(){
	node* pre;
	node* cur;
	if(head->next==NULL) return;
	for(pre=head;pre!=NULL;pre=pre->next)
	for(cur=pre->next;cur!=NULL;cur=cur->next){
		if(pre->x<cur->x){
			double tmp=cur->val;
			int tmp1=cur->x;
			cur->val=pre->val;
			cur->x=pre->x;
			pre->x=tmp1;
			pre->val=tmp;
		}
	}
	
}
 
void print(){
	node* temp=head;
	while(temp!=NULL){
		cout<<temp->val<<" "<<temp->x<<" ";
		temp=temp->next;
	}
	cout<<endl;
}
void solve(double x,int y){

	node* tmp=head;
	bool flag=0;
	if(tmp->x<y){
		node* tmp1=new node;
		tmp1->next=tmp;
		tmp1->x=y;
		tmp1->val=x;
		head=tmp1;
		return;
	}
	if(tmp->next!=NULL)
	while(tmp->next!=NULL){
		if(tmp->x==y) {
		tmp->val=tmp->val+x;
		return;
	}
		if(tmp->next->x<y){
			node* tmp1=new node;
			tmp1->next=tmp->next;
			tmp->next=tmp1;
			tmp1->val=x;
			tmp1->x=y;
			return;
		}
		tmp=tmp->next;
	}
	if(tmp->next!=NULL)tmp=tmp->next;
	if(tmp->x>y){
		node* tmp1=new node;
		tmp1->next=tmp->next;
		tmp->next=tmp1;
		tmp1->val=x;
		tmp1->x=y;
		return;
	}
}
void ans(int x){
	node* tmp=head;
	int pos=0;
	while(tmp!=NULL){
		if(tmp->val!=0) pos++;
		if(pos==x) break;
		tmp=tmp->next;
	} 
	printf("%.1f %d",tmp->val,tmp->x);
}
int main(){
	int n,m;
	cin>>n;
	_for(i,1,n){
		double x;int y;
		cin>>x>>y;
		Insert(x,y);
	}

	Sort();

	cin>>m;
	_for(i,1,m){
		double x;int y;
		cin>>x>>y;
		solve(x,y);
	}
//	print();
	int tmp;
	cin>>tmp;
	ans(tmp);
}
 
 
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值