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之和按降序排列。例如: 【输入样例】 【输出样例】 6.4 3 【样例说明】 【评分标准】必须用链表实现 |
---|
#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);
}