|
发表于 2021-9-30 20:00:11
|
显示全部楼层
本帖最后由 jhq999 于 2021-10-1 07:01 编辑
- #include <stdio.h>
- #include <stdlib.h>
- //#include <E:\Study Source\Ccode\DataStructure_Ex1\third\c1.h>
- enum Status{ERROR,OK};
- typedef struct Link{
- int coe;
- int elem;
- struct Link *next;
- }link,*linklist;
- Status initLink(linklist &L){
- L=(linklist)malloc(sizeof(link));
- L->next=NULL;
- L->coe=0;
- return OK;
- }
- Status GetElem(linklist &L,int i,int &coe,int &elem){
- int j=1;
- linklist p=L->next;
- while(p&&(j<i)){
- p=p->next;
- ++j;
- }
- if(!p||(j>i))return ERROR;
- coe=p->coe;
- elem=p->elem;
- return OK;
- }
- link *LocateElem(linklist &L,int coe,int elem){
- linklist p=(linklist)malloc(sizeof(link));
- p=L->next;
- while(p&&(p->coe!=coe)&&p->elem!=elem){
- p=p->next;
- }
- return p;
- }
- Status LinkInsert(linklist &L,int i,int coe, int elem){
- linklist p=L;
- int j=0;
- while(p&&(j<i-1)){
- p=p->next;
- ++j;
- }
- if(!p||(j>i-1))
- return ERROR;
- linklist s=(linklist)malloc(sizeof(link));
- s->coe=coe;
- s->elem=elem;
- s->next=p->next;
- p->next=s;
- L->coe++;
- return OK;
- }
- Status ListDel(linklist &L,int i){
- linklist p=L;
- int j=0;
- while((p->next)&&(j<i-1)){
- p=p->next;
- ++j;
- }
- if(!(p->next)||(j>i-1))return ERROR;
- linklist q=p->next;
- p->next=q->next;
- free(q);
- L->coe--;
- return OK;
- }
- void addNode(linklist &L,int n){
- linklist p,tail;
- tail=L;
- for(int i=0;i<n;i++){
- p=(linklist)malloc(sizeof(link));
- scanf("%d %d",&p->coe,&p->elem);
- tail->next=p;
- tail=p;
- }
- L->coe+=n;
- tail->next=NULL;
- }
- void addNode(linklist &L,int coe,int elem){
- linklist p,tail;
- tail=L;
- while(tail->next){
- tail=tail->next;
- }
- p=(linklist)malloc(sizeof(link));
- p->coe=coe;p->elem=elem;
- tail->next=p;
- tail=p;
- L->coe++;
- tail->next=NULL;
- }
- void display(linklist &L){
- linklist temp=L;
- while (temp->next) {
- temp=temp->next;
- printf("%d的%d次方 ",temp->coe,temp->elem);
- }
- printf("\n");
- }
- linklist addEveryOne(linklist &a,linklist &b){
- linklist p,q,r,temp;
- p=a->next;
- q=b->next;
- r=(linklist)malloc(sizeof(link));
- r->coe=r->elem=0;r->next=NULL;///////////////////////////////////////初始化为0,主要是r->next=NULL,要不天知道这个指针指向哪里?
- temp=r;
- while(p&&q){
- if(p->elem>q->elem){
- addNode(r,q->coe,q->elem);
- temp=temp->next;
- q=q->next;
- }
- else if(p->elem<q->elem){
- addNode(r,p->coe,p->elem);
- temp=temp->next;
- p=p->next;
- }
- else if(p->elem==q->elem){
- int sum = p->coe+q->coe;
- if(sum == 0){
- p = p->next;
- q = q->next;
- }
- else{
- addNode(r,sum,p->elem);
- temp=temp->next;
- p = p->next;
- q = q->next;
- }
- }
- }
- if(p!=NULL)
- temp->next = p;
- if(q!=NULL)
- temp->next = q;
- if(p==NULL&&q==NULL)
- temp->next=NULL;
- return r;
- }
- //#include"E:\Study Source\Ccode\DataStructure_Ex2\second\SecondLinkList.h"
- int main() {
- linklist p;
- initLink(p);
- addNode(p,3);
- display(p);
- printf("%d\n",p->coe);
-
- linklist q;
- initLink(q);
- addNode(q,4);
- display(q);
- printf("%d\n",q->coe);
-
-
- linklist L=addEveryOne(p,q);
- display(L);
- return 0;
- }
复制代码 |
|