8553
#include <iostream>
#include <string>
#include<malloc.h>
using namespace std;
typedef struct node
{
int data;
struct node *next;
} lnode,*Lnode;
void input(Lnode &pHead)
{
Lnode pNew,pTail;
pTail=pHead;
pNew=new lnode;
pNew->next=NULL;
cin>>pNew->data;
while(pNew->data)
{
pTail->next=pNew;
pTail=pNew;
pNew=new lnode;
cin>>pNew->data;
pNew->next=NULL;
}
delete pNew;
}
void output(Lnode &pHead)
{
pHead=pHead->next;
while(pHead!=NULL)
{
cout << pHead->data<<" ";
pHead=pHead->next;
}
cout << endl;
}
int main()
{
Lnode pHead;
pHead=new lnode;
input(pHead);
output(pHead);
return 0;
}
结果:
8554
#include <iostream>
#include <string>
#include<malloc.h>
using namespace std;
typedef struct node
{
int data;
struct node *next;
} lnode,*Lnode;
void input(Lnode &pHead)
{
Lnode pNew,pTail;
pTail=pHead;
pNew=new lnode;
pNew->next=NULL;
cin>>pNew->data;
while(pNew->data)
{
pTail->next=pNew;
pTail=pNew;
pNew=new lnode;
cin>>pNew->data;
pNew->next=NULL;
}
delete pNew;
}
void output(Lnode &pHead)
{
Lnode p=pHead->next;
while(p!=NULL)
{
cout << p->data<<" ";
p=p->next;
}
cout << endl;
}
void inversion(Lnode &pHead)
{
//创建新指针,指向已存在的节点空间
Lnode p=pHead->next;//指向前移节点的前一个节点,便于改变其的next指向
Lnode q=pHead->next->next;//指向当前要前移的节点
Lnode o=p;//始终指向第一个有效节点
while(p->next)
{
p->next=q->next;//更改指向前移节点q的next
q->next=o;//更改前移节点q指向的next
o=q;//令指针o指向第一个有效节点
q=p->next;//指针q后移
}
pHead->next=o;//将第一个有效节点与pHead连接
}
int main()
{
Lnode pHead;
pHead=new lnode;
input(pHead);
inversion(pHead);
output(pHead);
return 0;
}
结果:
8555
#include <iostream>
#include <string>
#include<malloc.h>
using namespace std;
typedef struct node
{
int data;
struct node *next;
} lnode,*Lnode;
void input(Lnode &pHead,int e)
{
Lnode pNew,p;
pNew=new lnode;
pNew->data=e;
pNew->next=NULL;
p=pHead;
while(p!=NULL&&p->next!=NULL)
{
if(p->next->data>pNew->data) //找到第一个大于e的节点
break;
p=p->next;
}
pNew->next=p->next;//将e插入第一个大于e的节点之前
p->next=pNew;
}
void output(Lnode &pHead)
{
pHead=pHead->next;
while(pHead!=NULL)
{
cout << pHead->data<<" ";
pHead=pHead->next;
}
cout << endl;
}
int main()
{
int e;
Lnode pHead;
pHead=new lnode;
pHead->next=NULL;
cin>>e;
while(e!=0)
{
input(pHead,e);
cin>>e;
}
output(pHead);
return 0;
}
结果:
8556
#include <iostream>
#include <string>
#include<malloc.h>
using namespace std;
typedef struct node
{
int data;
struct node *next;
} lnode,*Lnode;
void input(Lnode &pHead)
{
Lnode pNew,pTail;
pTail=pHead;
pNew=new lnode;
pNew->next=NULL;
cin>>pNew->data;
while(pNew->data)
{
pTail->next=pNew;
pTail=pNew;
pNew=new lnode;
cin>>pNew->data;
pNew->next=NULL;
}
delete pNew;
}
void output(Lnode &pHead)
{
pHead=pHead->next;
while(pHead!=NULL)
{
cout << pHead->data<<" ";
pHead=pHead->next;
}
cout << endl;
}
void del(Lnode &pHead)
{
Lnode p=pHead->next,q=pHead;
while(p!=NULL)
{
if(p->data%2==0)
{
q->next=p->next;
delete(p);
p=q;
}
q=p;
p=q->next;
}
}
int main()
{
Lnode pHead;
pHead=new lnode;
input(pHead);
del(pHead);
output(pHead);
return 0;
}
结果:
8557
#include <iostream>
#include <string>
using namespace std;
#define type int
typedef struct node
{
type data;
struct node *next;
} lnode,*Lnode;
void createLnode(Lnode &pHead)//创建链表
{
Lnode pNew,p;
while(1)
{
pNew=new lnode;
cin>>pNew->data;
pNew->next=NULL;
if(pNew->data==0) break;
p=pHead;
while(p!=NULL && p->next!=NULL)
{
if(p->next->data>=pNew->data) break;
p=p->next;
}
pNew->next=p->next;
p->next=pNew;
}
delete pNew;
}
void printfLnode(Lnode &pHead)//输出链表
{
Lnode p=pHead->next;
while(p!=NULL)
{
cout << p->data<<" ";
p=p->next;
}
cout << endl;
}
void freearray(Lnode &pHead)
{
Lnode p=pHead,q;
while(p!=NULL)
{
q=p->next;
delete p;
p=q;
}
}
void mergeLnode(Lnode &pHead1,Lnode &pHead2)
{
Lnode p1=pHead1->next,p2=pHead2->next,p3=pHead1,q;
pHead1->next=NULL;
while(p1!=NULL && p2!=NULL)
{
if(p1->data<=p2->data)
{
q=p1;
p1=p1->next;
}
else
{
q=p2;
p2=p2->next;
}
q->next=p3->next;
p3->next=q;
}
while(p2!=NULL)
{
q=p2;
p2=p2->next;
q->next=p3->next;
p3->next=q;
}
while(p1!=NULL)
{
q=p1;
p1=p1->next;
q->next=p3->next;
p3->next=q;
}
}
int main()
{
Lnode pHead1,pHead2;
pHead1=new(lnode);
pHead1->next=NULL;
pHead2=new(lnode);
pHead2->next=NULL;
createLnode(pHead1);
createLnode(pHead2);
mergeLnode(pHead1,pHead2);
printfLnode(pHead1);
freearray(pHead2);
return 0;
}
结果:
8558
#include <iostream>
#include <string>
using namespace std;
#define type int
typedef struct node
{
type data;
struct node *next;
} lnode,*Lnode;
void createLnode(Lnode &pHead)//创建链表
{
Lnode pNew,p;
p=pHead;
while(1)
{
pNew=new lnode;
cin>>pNew->data;
pNew->next=NULL;
if(pNew->data==0) break;
p->next=pNew;
p=pNew;
}
delete pNew;
}
void printfLnode(Lnode &pHead)//输出链表
{
Lnode p=pHead->next;
while(p!=NULL)
{
cout << p->data<<" ";
p=p->next;
}
cout << endl;
}
void freearray(Lnode &pHead)
{
Lnode p=pHead,q;
while(p!=NULL)
{
q=p->next;
delete p;
p=q;
}
}
void resolveLnode(Lnode &pHead,Lnode &pHead1,Lnode &pHead2)
{
pHead1=pHead;
//printfLnode(pHead);
pHead2=new lnode;
pHead2->next=NULL;
Lnode p=pHead->next,p1=pHead1,p2=pHead2;
//if(p==NULL) cout<<"********"<<endl;
while(p!=NULL)
{
// cout<<"********"<<endl;
Lnode r=p->next;
if(p->data%2!=0)
{
p1->next=p;
p1=p;
p1->next=NULL;
//cout<<p->data<<endl;
}
else
{
p2->next=p;
p2=p;
p2->next=NULL;
}
p=r;
}
}
int main()
{
Lnode pHead,pHead1,pHead2;
pHead=new lnode;
pHead->next=NULL;
createLnode(pHead);
resolveLnode(pHead,pHead1,pHead2);
printfLnode(pHead1);
printfLnode(pHead2);
return 0;
}
结果:
8559
#include <iostream>
#include <string>
#include<malloc.h>
using namespace std;
#define type int
void input(type *a,int n)
{
int i;
for(i=0; i<n; i++)
cin>>a[i];
}
void output(type *a,int n)
{
int i;
for(i=0; i<n; i++)
cout<<a[i]<<" ";
cout<<endl;
}
void inversion(type *a,int n)
{
int i;
type b;
for(i=0;i<n/2;i++)
{
b=a[i];
a[i]=a[n-i-1];
a[n-i-1]=b;
}
}
int main()
{
int n;
type *a;
cin>>n;
a=(type *)malloc(n*sizeof(int));
input(a,n);
output(a,n);
inversion(a,n);
output(a,n);
return 0;
}
结果:
8560
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int a,b;
struct node *next;
}Lnode;
void createLnode(Lnode *pHead,int n)
{
int i;
Lnode *pNew,*pTail;
pTail=pHead;
for(i=0;i<n;i++)
{
pNew=(Lnode *)malloc(sizeof(Lnode));
pNew->next=NULL;
scanf("%d,%d",&pNew->a,&pNew->b);
pTail->next=pNew;
pTail=pNew;
}
}
void output(Lnode *pHead)
{
Lnode *p=pHead->next;
while(p)
{
printf("%d*x^%d ",p->a,p->b);
p=p->next;
}
printf("\n");
}
void add(Lnode *pHead1,Lnode *pHead2)
{
Lnode *p1,*p2,*p3;
p1=pHead1->next;
p2=pHead2->next;
p3=pHead1;
while(p1&&p2)
{
if(p1->b==p2->b)
{
if(p1->a+p2->a!=0)
{
p3->next=p1;
p3=p1;
p3->a=p1->a+p2->a;
p1=p1->next;
p2=p2->next;
}
else
{
p1=p1->next;
p2=p2->next;
}
}
else if(p1->b<p2->b)
{
if(p1->a!=0)
{
p3->next=p1;
p3=p1;
p1=p1->next;
}
else
p1=p1->next;
}
else
{
if(p1->a!=0)
{
p3->next=p2;
p3=p2;
p2=p2->next;
}
else
p2=p2->next;
}
}
if(p1==NULL) p3->next=p2;
if(p2==NULL) p3->next=p1;
}
int main()
{
int n1,n2;
Lnode *pHead1,*pHead2;
pHead1=(Lnode *)malloc(sizeof(Lnode));
pHead1->next=NULL;
pHead2=(Lnode *)malloc(sizeof(Lnode));
pHead2->next=NULL;
scanf("%d",&n1);
createLnode(pHead1,n1);
scanf("%d",&n2);
createLnode(pHead2,n2);
add(pHead1,pHead2);
output(pHead1);
return 0;
}
结果:
6109
#include <iostream>
#include <string>
using namespace std;
#define type int
typedef struct node
{
type data;
struct node *next;
} lnode,*Lnode;
void createLnode(Lnode &pHead)//创建链表
{
int i;
Lnode pNew,pTail;
pTail=pHead;
pNew=new lnode;
pNew->next=NULL;
cin>>pNew->data;
while(pNew->data)
{
pTail->next=pNew;
pTail=pNew;
pNew=new lnode;
cin>>pNew->data;
pNew->next=NULL;
}
delete pNew;
}
void printfLnode(Lnode &pHead)//输出链表
{
pHead=pHead->next;
while(pHead!=NULL)
{
cout << pHead->data<<" ";
pHead=pHead->next;
}
cout << endl;
}
void freearray(Lnode &pHead)
{
Lnode p=pHead,q;
while(p!=NULL)
{
q=p->next;
delete p;
p=q;
}
}
void mergeLnode(Lnode &pHead1,Lnode &pHead2,Lnode &p)
{
Lnode p1=pHead1->next,p2=pHead2->next,p3;
p=pHead1;
p3=p;
while(p1!=NULL && p2!=NULL)
{
if(p1->data<p2->data)
{
p3->next=p1;
p3=p1;
p1=p1->next;
}
else if(p1->data==p2->data)
{
p3->next=p1;
p3=p1;
p1=p1->next;
p2=p2->next;
}
else
{
p3->next=p2;
p3=p2;
p2=p2->next;
}
}
if(p1==NULL) p3->next=p2;
if(p2==NULL) p3->next=p1;
}
int main()
{
Lnode pHead1,pHead2,p;
pHead1=new(lnode);
pHead1->next=NULL;
pHead2=new(lnode);
pHead2->next=NULL;
createLnode(pHead1);
createLnode(pHead2);
mergeLnode(pHead1,pHead2,p);
printfLnode(pHead1);
freearray(pHead2);
return 0;
}
结果: