30岁老男人学编程刚写的线性表大部分课程设计程序(运行全部OK)! 下星期开始写栈
线性顺序表总程序(创建,差入,2个并1个)#include <stdio.h>
#include <malloc.h>
#define null 0
#define zongchicun 100
#define meicijia 10
#define error 0
#define ok 1
typedef int zhengshu;
typedef struct {
zhengshu *elem;
int muqiansize;
int zongsize;
}node;
void main()
{int i,n,j,k;
int creat(node &l);
int ascanf(node &l,int i);
void charu(node &l,int j,int k);
void zuhe(node &lc,node la,node lb);
node p,la,lb,lc;
creat(p);
creat(la);
creat(lb);
printf("请输入p几个数字");
scanf("%d",&n);
for(i=0;i<n;i++)
{
ascanf(p,i);}
printf("\n");
for(i=0;i<p.muqiansize;i++)
{printf("%d ",p.elem[i]);}
printf("\n");
printf("请输入要插入的位置\n");
scanf("%d",&j);
printf("要插入的数字\n");
scanf("%d",&k);
charu(p,j,k);
for(i=0;i<p.muqiansize;i++)
{printf("%d ",p.elem[i]);}
printf("\n\n");
printf("请输入la几个数字");
scanf("%d",&n);
printf("请输入la数字\n");
for(i=0;i<n;i++)
{
ascanf(la,i);}
printf("\n\n");
printf("请输入lb几个数字");
scanf("%d",&n);
printf("请输入lb数字\n");
for(i=0;i<n;i++)
{
ascanf(lb,i);}
for(i=0;i<la.muqiansize;i++)
{printf("%d ",la.elem[i]);}
printf("\n\n");
for(i=0;i<lb.muqiansize;i++)
{printf("%d ",lb.elem[i]);}
printf("\n\n");
zuhe(lc,la,lb);
for(i=0;i<lc.muqiansize;i++)
{printf("%d ",lc.elem[i]);}
printf("\n\n");
}
int creat(node &l)
{l.elem=(zhengshu*)malloc(zongchicun*sizeof(zhengshu));
if(!l.elem) return error;
l.muqiansize=0;
l.zongsize=100;
return ok;
}
int ascanf(node &l,int i)
{zhengshu *newbase;
int k;
if(l.muqiansize>=l.zongsize)
{newbase=(zhengshu *)realloc(l.elem,(l.zongsize+meicijia)*sizeof(zhengshu));
l.elem=newbase;
l.zongsize+=meicijia;}
printf("请输入编号");
scanf("%d",&k);
l.elem[i]=k;
l.muqiansize+=1;
return ok;}
void charu(node &l,int j,int k)
{int i,n;
zhengshu *newbase;
if(l.muqiansize>=l.zongsize)
{newbase=(zhengshu *)realloc(l.elem,(l.zongsize+meicijia)*sizeof(zhengshu));
l.elem=newbase;
l.zongsize+=meicijia;}
n=l.muqiansize;
for(i=n;i>=j;i--)
l.elem[i]=l.elem[i-1];
l.elem[i]=k;
l.muqiansize+=1;
}
void zuhe(node &lc,node la,node lb)
{int i=0,j=0,k=0;
lc.muqiansize=la.muqiansize+lb.muqiansize;
lc.elem=(zhengshu*)malloc(lc.muqiansize*sizeof(zhengshu));
while(i<la.muqiansize&&j<lb.muqiansize)
if(la.elem[i]<lb.elem[j]){lc.elem[k]=la.elem[i];i++;k++;}
else if(la.elem[i]==lb.elem[j]) {lc.elem[k]=la.elem[i];i++;j++;k++;}
else {lc.elem[k]=lb.elem[j];j++;k++;}
while(i<la.muqiansize)
{lc.elem[k]=la.elem[i];i++;k++;}
while(j<lb.muqiansize)
{lc.elem[k]=lb.elem[j];j++;k++;}
}
链表总程序
#include <stdio.h>
#include <malloc.h>
#define null 0
#define error 0
#define ok 1
typedef struct lnode
{int m;
lnode *next;
}lnode;
void main()
{void creat(lnode *l);
void shuru(lnode *l,int n);
void shanchu(lnode *l,int n);
void charu(lnode *l,int n,int k);
void hebing(lnode*la,lnode*lb,lnode*lc);
void lianjie(lnode*ld,lnode*le);
void shanchuhebing(lnode*la,lnode *lb,int i,int j,int len);
void dayuxiaoyushanchu(lnode *lv,int mink,int mark);
void nixu(lnode *lx);
void shanxiang(lnode *ly);
void jiaochafang(lnode *lm,lnode *ln);
void dizenghebing(lnode *lg,lnode *lh,lnode *lz);
void jiaoji(lnode *ha,lnode *hb,lnode *hc);
lnode *head,*la,*v,*lb,*lc,*ld,*le,*lv,*lx,*ly,*lm,*ln,*lf,*lz,*lg,*lh,*ha,*hb,*hc;
int n,i,k,l,len,mink,mark,j;
head=(lnode*)malloc(sizeof(lnode));
head->next=null;
la=(lnode*)malloc(sizeof(lnode));
la->next=null;
lb=(lnode*)malloc(sizeof(lnode));
lb->next=null;
le=(lnode*)malloc(sizeof(lnode));
le->next=null;
ld=(lnode*)malloc(sizeof(lnode));
ld->next=null;
lv=(lnode*)malloc(sizeof(lnode));
lv->next=null;
lx=(lnode*)malloc(sizeof(lnode));
lx->next=null;
ly=(lnode*)malloc(sizeof(lnode));
ly->next=null;
lm=(lnode*)malloc(sizeof(lnode));
lm->next=null;
ln=(lnode*)malloc(sizeof(lnode));
ln->next=null;
lg=(lnode*)malloc(sizeof(lnode));
lg->next=null;
lh=(lnode*)malloc(sizeof(lnode));
lh->next=null;
lz=(lnode*)malloc(sizeof(lnode));
lz->next=null;
ha=(lnode*)malloc(sizeof(lnode));
ha->next=null;
hb=(lnode*)malloc(sizeof(lnode));
hb->next=null;
hc=(lnode*)malloc(sizeof(lnode));
hc->next=null;
lc=la;
printf("输入部分:\n\n");
printf("请输入几位数字:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
creat(head); /*创建头列表*/
shuru(head,n); /*列表负值*/
v=head->next;
while(v!=null)
{printf("%d",v->m);
v=v->next;}
printf("\n\n\n\n");
printf("删除部分:\n\n");
printf("输入要删除的位置\n");
scanf("%d",&n);
shanchu(head, n);
v=head->next;
while(v!=null)
{printf("%d",v->m);
v=v->next;}
printf("\n\n\n\n");
printf("插入部分:\n\n");
printf("输入要插入的位置\n");
scanf("%d",&n);
printf("输入要插入的数\n");
scanf("%d",&k);
charu(head,n,k);
v=head->next;
while(v!=null)
{printf("%d",v->m);
v=v->next;}
printf("\n\n\n\n");
printf("将la,lb按非递减放入lc:\n\n"); /*2个表合并部分*/
printf("请输入la几位数字\n");
scanf("%d",&n);
printf("请输入la数字:\n");
for(i=0;i<n;i++)
creat(la); /*创建头列表*/
shuru(la,n); /*列表负值*/
printf("\n\n");
printf("请输入lb几位数字:\n");
scanf("%d",&n);
printf("请输入lb数字\n");
for(i=0;i<n;i++)
creat(lb); /*创建头列表*/
shuru(lb,n); /*列表负值*/
hebing(la,lb,lc);
lc=lc->next;
while(lc!=null)
{printf("%d",lc->m);
lc=lc->next;}
printf("将ld,le连接\n\n"); /*将ld,le连接*/
printf("请输入ld几位数字:\n");
scanf("%d",&n);
printf("请输入ld数字\n");
for(i=0;i<n;i++)
creat(ld); /*创建头列表*/
shuru(ld,n);
printf("\n\n");
printf("请输入ld几位数字:\n");
scanf("%d",&n);
printf("请输入ld数字\n");
for(i=0;i<n;i++)
creat(le); /*创建头列表*/
shuru(le,n);
printf("\n\n");
lianjie(ld,le);
ld=ld->next;
while(ld!=null)
{printf("%d",ld->m);
ld=ld->next;}
printf("\n\n");
printf("从la中删除元素插入到lb\n"); /*从la中删除元素插入到lb*/
printf("请输入la几位数字\n");
scanf("%d",&n);
printf("请输入la数字:\n");
for(i=0;i<n;i++)
creat(la); /*创建头列表*/
shuru(la,n); /*列表负值*/
printf("\n\n");
printf("请输入lb几位数字:\n");
scanf("%d",&n);
printf("请输入lb数字\n");
for(i=0;i<n;i++)
creat(lb); /*创建头列表*/
shuru(lb,n); /*列表负值*/
printf("请输入la要删除的第i个位置:\n");
scanf("%d",&i);
printf("请输入la要删除的len个元素:\n");
scanf("%d",&len);
printf("请输入lb要插入的第j个位置:\n");
scanf("%d",&j);
shanchuhebing(la,lb,i,j,len);
la=la->next;
lb=lb->next;
printf("la:");
while(la!=null)
{printf("%d",la->m);
la=la->next;}
printf("\n");
printf("lb:");
while(lb!=null)
{printf("%d",lb->m);
lb=lb->next;}
printf("\n\n");
printf("删除大于mink小于maxk的元素部分:\n"); /*删除大于mink小于maxk的元素部分*/
printf("请输入lv几位数字:\n");
scanf("%d",&n);
printf("请输入lv数字\n");
for(i=0;i<n;i++)
creat(lv); /*创建头列表*/
shuru(lv,n); /*列表负值*/
printf("请输入大于mink的数:\n");
scanf("%d",&mink);
printf("请输入小于mark的数:\n");
scanf("%d",&mark);
dayuxiaoyushanchu(lv,mink,mark);
lv=lv->next;
printf("lv:");
while(lv!=null)
{printf("%d",lv->m);
lv=lv->next;}
printf("\n\n");
printf("对单链表的逆置部分:\n"); /*对单链表的逆置部分*/
printf("请输入lx几位数字\n");
scanf("%d",&n);
printf("请输入lx数字:\n");
for(i=0;i<n;i++)
creat(lx); /*创建头列表*/
shuru(lx,n);
nixu(lx);
lx=lx->next;
while(lx!=null)
{printf("%d",lx->m);
lx=lx->next;}
printf("\n\n");
printf("删除相同元素部分:\n"); /*删除相同元素部分:*/
printf("请输入ly几位数字\n");
scanf("%d",&n);
printf("请输入ly数字:\n");
for(i=0;i<n;i++)
creat(ly); /*创建头列表*/
shuru(ly,n);
shanxiang(ly);
printf("交叉放入部分:\n\n"); /*交叉放入部分:*/
printf("请输入lm几位数字:\n");
scanf("%d",&n);
printf("请输入lm数字\n");
for(i=0;i<n;i++)
creat(lm); /*创建头列表*/
shuru(lm,n); /*列表负值*/
printf("请输入ln几位数字:\n");
scanf("%d",&n);
printf("请输入ln数字\n");
for(i=0;i<n;i++)
creat(ln); /*创建头列表*/
shuru(ln,n); /*列表负值*/
jiaochafang(lm,ln);
lf=lm;
lf=lf->next;
while(lf!=null)
{printf("%d",lf->m);
lf=lf->next;}
printf("\n\n");
printf("两个递增链表合并成一个递减链表部分:\n"); /*两个递增链表合并成一个递减链表部分:*/
printf("请输入lg几位数字\n");
scanf("%d",&n);
printf("请输入lg数字:\n");
for(i=0;i<n;i++)
creat(lg); /*创建头列表*/
shuru(lg,n); /*列表负值*/
printf("\n\n");
printf("请输入lh几位数字\n");
scanf("%d",&n);
printf("请输入lh数字:\n");
for(i=0;i<n;i++)
creat(lh); /*创建头列表*/
shuru(lh,n); /*列表负值*/
printf("\n\n");
dizenghebing(lg,lh,lz);
lz=lz->next;
while(lz!=null)
{printf("%d",lz->m);
lz=lz->next;}
printf("\n\n");
printf("求2个表ha,hb的交集部分:\n"); /*求2个表ha,hb的交集部分*/
printf("请输入ha几位数字:\n");
scanf("%d",&n);
printf("请输入ha数字\n");
for(i=0;i<n;i++)
creat(ha); /*创建头列表*/
shuru(ha,n);
printf("请输入hb几位数字:\n");
scanf("%d",&n);
printf("请输入hb数字\n");
for(i=0;i<n;i++)
creat(hb); /*创建头列表*/
shuru(hb,n);
jiaoji(ha,hb,hc);
hc=hc->next;
while(hc!=null)
{printf("%d",hc->m);
hc=hc->next;}
printf("\n\n");
}
void creat(lnode *l) /*创建头列表*/
{lnode *k;
k=(lnode*)malloc(sizeof(lnode));
k->next=l->next;
l->next=k;}
void shuru(lnode *l,int n) /*列表负值*/
{int i;
lnode *p;
p=l->next;
for(i=0;i<n;i++)
{
scanf("%d",&p->m);
p=p->next;}}
void shanchu(lnode *l,int n) /*删除列表某个数*/
{lnode *p,*q,*h;
int i;
p=l->next;
q=l;
for(i=1;i<n;i++)
{p=p->next;q=q->next;}
h=p;p=p->next;free(h);
q->next=p;}
void charu(lnode *l,int n,int k)/*插入列表某个数*/
{lnode *p,*v,*q;
int i;
p=l->next;
q=l;
v=(lnode*)malloc(sizeof(lnode));
v->m=k;
for(i=1;i<n;i++)
{p=p->next;q=q->next;}
q->next=v;
v->next=p;}
void hebing(lnode*la,lnode*lb,lnode*lc)/*2个表合并部分*/
{lnode *p1,*p2,*p3;
p1=la->next;
p2=lb->next;
if(p1->m<p2->m) {lc->next=p1;p3=p1;p1=p1->next;}
else {lc->next=p2;p3=p2;p2=p2->next;}
while(p1!=null&&p2!=null)
{if(p1->m<p2->m) {p3->next=p1;p3=p3->next;p1=p1->next;}
else if(p1->m==p2->m) {p3->next=p1;p1=p1->next;p2=p2->next;}
else {p3->next=p2;p3=p3->next;p2=p2->next;}}
if(p1!=null)
p3->next=p1;
else p3->next=p2;}
void lianjie(lnode*ld,lnode*le) /*将ld,le连接*/
{lnode *p,*q;
p=ld->next;
q=ld;
while(p!=null)
{p=p->next;q=q->next;}
q->next=le->next;}
void shanchuhebing(lnode*la,lnode *lb,int i,int j,int len) /*从la中删除元素插入到lb*/
{lnode *p,*q,*l,*m,*v;
int n;
p=la->next;
q=la;
m=lb->next;
v=lb;
for(n=1;n<i;n++)
{p=p->next;q=q->next;}
l=p;
for(n=1;n<len;n++)
p=p->next;
q->next=p->next;
for(n=1;n<j;n++)
{m=m->next;v=v->next;}
v->next=l;
p->next=m;}
void dayuxiaoyushanchu(lnode *lv,int mink,int mark) /*删除大于mink小于maxk的元素部分*/
{lnode *p,*q,*h;
p=lv->next;
q=lv;
while(p->m<=mink)
{p=p->next;q=q->next;}
h=p;
while(p->m<mark)
{p=p->next;free(h);h=p;}
q->next=p;}
void nixu(lnode *lx) /*对单链表的逆置部分*/
{lnode *p,*q,*l;
p=lx->next;
q=lx->next->next;
p->next=null;
while(q!=null)
{l=q;
q=q->next;
l->next=p;
p=l;}
lx->next=p;
}
void shanxiang(lnode *ly) /*删除相同元素部分:*/
{lnode *p,*q,*h;
p=ly->next;
q=ly->next->next;
while(q->next)
{while(p->m==q->m&&q->next) {p->next=q->next;h=q;q=q->next;free(h);}
if(q->next==null) break;
else{p=p->next;q=q->next;}
}
if(p->m==q->m)
p->next=null;
ly=ly->next;
while(ly!=null)
{printf("%d",ly->m);
ly=ly->next;}}
void jiaochafang(lnode *lm,lnode *ln) /*交叉放入部分:*/
{lnode *p,*q,*m,*n;
p=lm->next;
q=ln->next;
while(p!=null&&q!=null)
{m=p;p=p->next;m->next=q;n=q;q=q->next;n->next=p;}
if(p==null)
n->next=q;
else
m->next=p;}
void dizenghebing(lnode *lg,lnode *lh,lnode *lz) /*两个递增链表合并成一个递减链表部分:*/
{lnode *p,*q,*m,*n,*h;
p=lg->next;
q=lh->next;
if(p->m<q->m) {h=p;p=p->next; h->next=null;}
else {h=q;q=q->next; h->next=null;}
while(p!=null&&q!=null)
{if(p->m<q->m) {m=p->next;p->next=h;h=p;p=m;}
else {n=q->next;q->next=h;h=q;q=n;}}
while(p!=null)
{m=p->next;p->next=h;h=p;p=m;}
while(q!=null)
{n=q->next;q->next=h;h=q;q=n;}
lz->next=h;
}
void jiaoji(lnode *ha,lnode *hb,lnode *hc) /*求2个表ha,hb的交集部分*/
{lnode *p,*q,*m,*n;
p=ha->next;
q=hb->next;
n=hc;
while(p!=null&&q!=null)
{if(p->m==q->m)
{m=(lnode*)malloc(sizeof(lnode));
m->next=null;
n->next=m;
n=n->next;
n->m=p->m;
p=p->next;
q=q->next;}
else if(p->m<q->m) p=p->next;
else q=q->next;}
}
运动会分数统计
#include <stdio.h>
#include <malloc.h>
#define null 0
#define error 0
#define ok 1
#define maxsize 100
typedef struct geren
{char xingming[20]; /*姓名*/
int item; /*项目*/
int mingci; /*名次*/
int score; /*分数*/
}ath;
typedef struct xuexiao
{int count; /*学校编号*/
int serial; /*学校共得奖次数*/
int manscore; /*男子总分*/
int womanscore; /*女子总分*/
int totalscore; /*团体总分*/
geren abc[maxsize];
xuexiao *next;} lnode;
int xsm,naz,won;
int zxm;
int i,j;
int item;
int overgame;
int n;
int m;
int xuebian;
lnode *xhead,*xzhong,*xwei;
int *tou=null,*zhong=null,*wei=null;
void main()
{void input();
void creat();
void shuru();
void fenshu();
void shuchu();
int x=1,y=1;
while(x==1)
{printf("请输入男子项目数并<=30:\n");
scanf("%d",&naz);
if(naz>20)
{printf("输入男子项目数>30请重新输入:\n\n");continue;}
x=0;}
while(y==1)
{printf("请输入女子项目数并<=20:\n");
scanf("%d",&won);
if(won>20)
{printf("输入女子项目数>20请重新输入:\n\n");continue;}
y=0;}
zxm=naz+won;
tou=(int*)malloc(zxm*sizeof(int));
zhong=tou;
wei=tou;
input();
fenshu();
shuchu();
}
void input()
{void creat();
void shuru();
xsm=20;
lnode *p;
xhead=(lnode*)malloc(sizeof(lnode));
xhead->next=null;
xzhong=xhead;
int m=1,h=1;
char any;
any='y';
while(any=='y')
{m=1;
while(m==1)
{printf("请输入比赛项目:\n");
scanf("%d",wei);
overgame=*wei;
if(overgame>zxm) {printf("输入项目编号已经超出总项目请重新输入:\n\n");continue;}
for(zhong=tou;zhong<wei;zhong++)
{if(*zhong==overgame) break;}
if(*zhong==overgame&&zhong!=wei) {printf("已经重复输入项目编号请重新输入:\n\n");continue;}
wei++;
m=0;
}
switch(overgame%2)
{case 0: n=3; break;
case 1: n=5; break;
}
for(i=1;i<=n;i++)
{h=1;
printf("第%d项目第%d名部分:\n",overgame,i);
printf("请输入学校编号:\n");
scanf("%d",&xuebian);
if(xuebian>xsm||xuebian<=0)
{i--;printf("请输入学校编号>总学校数重新输入:\n\n");continue;}
if(xhead->next==null)
{creat();shuru();h=0;}
if(h==1)
{for(p=xhead->next;p!=null;p=p->next)
if(p->count==xuebian)
{xzhong=p;shuru();h=0;break;}}
if(h==1)
{creat();shuru();}
}
printf("是否继续输入项目?\n");
scanf("%s",&any);}
}
void creat()
{
xwei=(lnode*)malloc(sizeof(lnode));
xwei->next=null;
xzhong->next=xwei;
xzhong=xzhong->next;
xzhong->serial=0;
xzhong->manscore=0;
xzhong->womanscore=0;
xzhong->totalscore=0;}
void shuru()
{xzhong->count=xuebian;
xzhong->serial+=1;
printf("请输入第%d项目第%d名得奖姓名\n",overgame,i);
scanf("%s",xzhong->abc[xzhong->serial].xingming);
xzhong->abc[xzhong->serial].item=overgame;
xzhong->abc[xzhong->serial].mingci=i;
xzhong=xwei;
}
void fenshu()
{
for(xzhong=xhead->next;xzhong!=null;xzhong=xzhong->next)
{ for(i=1;i<=xzhong->serial;i++)
{if(xzhong->abc[i].item%2==0)
switch(xzhong->abc[i].mingci)
{case 1:xzhong->abc[i].score=5;break;
case 2:xzhong->abc[i].score=3;break;
case 3:xzhong->abc[i].score=1;break;}
else
switch(xzhong->abc[i].mingci)
{case 1:xzhong->abc[i].score=7;break;
case 2:xzhong->abc[i].score=5;break;
case 3:xzhong->abc[i].score=3;break;
case 4:xzhong->abc[i].score=2;break;
case 5:xzhong->abc[i].score=1;break;}
if(xzhong->abc[i].item<=naz)
xzhong->manscore+=xzhong->abc[i].score;
else
xzhong->womanscore+=xzhong->abc[i].score;}
xzhong->totalscore=xzhong->manscore+xzhong->womanscore;}}
void shuchu()
{for(xzhong=xhead->next;xzhong!=null;xzhong=xzhong->next)
{
printf("%d学校共有%d名运动员得奖:\n\n",xzhong->count,xzhong->serial);
for(i=1;i<=xzhong->serial;i++)
printf("%d学校%s同学获得%d项目的第%d名\n\n",xzhong->count,xzhong->abc[i].xingming,xzhong->abc[i].item,xzhong->abc[i].mingci);
printf("%d学校男子项目共获得%d分\n",xzhong->count,xzhong->manscore);
printf("%d学校女子项目共获得%d分\n",xzhong->count,xzhong->womanscore);
printf("%d学校团体项目共获得%d分\n",xzhong->count,xzhong->totalscore);
}
}
约瑟环
#include <stdio.h>
#include <malloc.h>
#define null 0
typedef struct joseph
{int count;
int mima;
joseph *next;
}ath;
int n,m,i,j;
ath *head,*p,*q;
void main()
{void creat();
void shuru();
void mima1();
head=(ath*)malloc(sizeof(ath));
head->next=null;
p=head;
printf("请输入几个人围坐一圈:\n");
scanf("%d",&n);
for(i=1;i<n;i++)
creat();
shuru();
mima1();
}
void creat()
{q=(ath*)malloc(sizeof(ath));
q->next=head;
p->next=q;
p=p->next;}
void shuru()
{p=head;
for(i=1;i<=n;i++)
{p->count=i;
printf("请输入第%d个人的密码:\n",i);
scanf("%d",&p->mima);
p=p->next;}
for(p=head;p!=q;p=p->next)
printf("第%d个人的密码是:%d\n",p->count,p->mima);
printf("第%d个人的密码是:%d\n",p->count,p->mima);
}
void mima1()
{int l;
p=head;
l=6;
while(p->next!=p)
{for(i=1;i<l;i++)
{q=p;p=p->next;}
q->next=p->next;
printf("%d ",p->count);
l=p->mima;
p=p->next;
}
printf("%d ",p->count);}
集合的并、交和差运算
#include <stdio.h>
#include <malloc.h>
#define null 0
typedef struct yunsuan
{char zifu;
yunsuan *next;
}lnode;
lnode *la,*lb,*lc,*p,*q,*m,*n,*l,*x;
char overgame;
void main()
{void shuru(lnode *head);
void bingji(lnode *la,lnode *lb,lnode *lc);
void jiaoji(lnode *la,lnode *lb,lnode *lc);
void chaji(lnode *la,lnode *lb,lnode *lc);
int t;
la=(lnode*)malloc(sizeof(lnode));
la->next=null;
lb=(lnode*)malloc(sizeof(lnode));
lb->next=null;
lc=(lnode*)malloc(sizeof(lnode));
lc->next=null;
printf("请输入la:\n");
shuru(la);
printf("请输入lb:\n");
shuru(lb);
printf("1:并集,2:交集,3:差集");
scanf("%d",&t);
switch(t)
{case 1 :bingji(la,lb,lc); break;
case 2 :jiaoji(la,lb,lc); break;
case 3 :chaji(la,lb,lc);}}
void shuru(lnode *head)
{char a[100],*l;
l=a;
gets(l);
p=head;
for(;*l!='\0';l++)
{q=(lnode*)malloc(sizeof(lnode));
q->next=null;
p->next=q;
p=p->next;
p->zifu=*l;}}
void bingji(lnode *la,lnode *lb,lnode *lc)
{int h=1;
m=la->next;
while(h<3){
p=lc;
while(m->zifu<'a'||m->zifu>'z')
{m=m->next;if(m==null)break;}
if(m==null) {m=lb->next;h++;continue;}
if(p->next==null)
{q=(lnode*)malloc(sizeof(lnode));
q->next=null;
p->next=q;
q->zifu=m->zifu;}
if(h==1) m=m->next;
while(m!=null)
{q=lc;
p=lc->next;
while(m->zifu<'a'||m->zifu>'z')
{m=m->next;if(m==null)break;}
if(m==null) break;
while(m->zifu>p->zifu)
{q=p;p=p->next;if(p==null)break;}
if(p==null) {n=(lnode*)malloc(sizeof(lnode));
q->next=n;
n->next=null;
n->zifu=m->zifu;
m=m->next;
continue;}
if(m->zifu<p->zifu)
{n=(lnode*)malloc(sizeof(lnode));
q->next=n;
n->next=p;
n->zifu=m->zifu;}
m=m->next;
}
if(m==null) {m=lb->next;h++;}}
printf("la,lb的并集是:\n");
p=lc->next;
while(p!=null)
{printf("%c",p->zifu);
p=p->next;}
}
void jiaoji(lnode *la,lnode *lb,lnode *lc)
{m=lc;
p=la->next;
q=lb->next;
for(p=la->next;p!=null;p=p->next)
{while(p->zifu<'a'||p->zifu>'z')
{p=p->next;if(p==null)break;}
if(p==null) break;
for(q=lb->next;q!=null;q=q->next)
{while(q->zifu<'a'||q->zifu>'z')
{q=q->next;if(q==null)break;}
if(q==null)break;
if(p->zifu==q->zifu)
{if(m->next==null)
{l=(lnode*)malloc(sizeof(lnode));
l->next=null;
m->next=l;
l->zifu=p->zifu;
}
else if(p->zifu>lc->next->zifu)
{n=lc->next;
while(p->zifu>n->zifu)
{ m=n;n=n->next;if(n==null) break;}
if(n==null) {l=(lnode*)malloc(sizeof(lnode));
m->next=l;
l->next=null;
l->zifu=p->zifu;
break;
}
if(p->zifu<n->zifu)
{
l=(lnode*)malloc(sizeof(lnode));
m->next=l;
l->next=n;
l->zifu=p->zifu; break;}}
else if(p->zifu<lc->next->zifu)
{l=(lnode*)malloc(sizeof(lnode));
l->next=lc->next;
l->zifu=p->zifu;
lc->next=l;
break;}
}
}}
m=lc->next;
while(m!=null)
{printf("%c ",m->zifu);
m=m->next;
}
}
void chaji(lnode *la,lnode *lb,lnode *lc)
{int y=1;
m=lc;
x=lc;
p=la->next;
q=lb->next;
for(p=la->next;p!=null;p=p->next)
{while(p->zifu<'a'||p->zifu>'z')
{p=p->next;if(p==null)break;}
if(p==null) break;
for(q=lb->next;q!=null;q=q->next)
{while(p->zifu<'a'||p->zifu>'z')
{p=p->next;if(p==null)break;}
if(p==null) break;
if(p->zifu==q->zifu) break;}
if(q==null)
{
if(m->next==null)
{l=(lnode*)malloc(sizeof(lnode));
l->next=null;
m->next=l;
l->zifu=p->zifu;
x=x->next;
}
else if(p->zifu>lc->next->zifu)
{n=lc->next;
while(p->zifu>n->zifu)
{m=n;n=n->next;if(n==null) break;}
if(n==null)
{l=(lnode*)malloc(sizeof(lnode));
m->next=l;
l->next=null;
l->zifu=p->zifu;
}
else if(p->zifu<n->zifu)
{
l=(lnode*)malloc(sizeof(lnode));
l->next=n;
m->next=l;
l->zifu=p->zifu;
}}
else if(p->zifu<lc->next->zifu)
{l=(lnode*)malloc(sizeof(lnode));
l->next=lc->next;
l->zifu=p->zifu;
lc->next=l;
}
}
}
m=lc->next;
while(m!=null)
{printf("%c ",m->zifu);
m=m->next;
}
}
一元稀疏多项式计算
#include <stdio.h>
#include <malloc.h>
#define null 0
typedef struct fangcheng{
float coef;
int expn;
fangcheng *next;
}lnode;
lnode *la,*lb,*lc,*p,*q,*l,*m,*n,*ld,*le;
void main()
{void shuru(lnode *head);
void jiafa(lnode *la,lnode *lb,lnode *lc);
void jianfa(lnode *la,lnode *lb,lnode *ld);
void chengfa(lnode *la,lnode *lb,lnode *le);
la=(lnode*)malloc(sizeof(lnode));
la->next=null;
lb=(lnode*)malloc(sizeof(lnode));
lb->next=null;
lc=(lnode*)malloc(sizeof(lnode)) ;
lc->next=null;
ld=(lnode*)malloc(sizeof(lnode)) ;
ld->next=null;
le=(lnode*)malloc(sizeof(lnode)) ;
le->next=null;
shuru(la);
shuru(lb);
printf("\n");
jiafa(la,lb,lc);
jianfa(la,lb,ld);
chengfa(la,lb,le);}
void shuru(lnode *head)
{float coef=1;
int expn;
p=head;
while(coef!=0)
{printf("请输入一元多项的项数:");
scanf("%f",&coef);
if(coef==0) break;
printf("请输入一元多项的指数:");
scanf("%d",&expn);
if(head->next==null)
{q=(lnode*)malloc(sizeof(lnode));
q->next=null;
p->next=q;
q->expn=expn;
q->coef=coef;}
else
{p=head->next;
q=head;
while(p->expn<expn)
{q=p;p=p->next;if(p==null) break;}
if(p==null)
{l=(lnode*)malloc(sizeof(lnode));
l->next=null;
q->next=l;
l->expn=expn;
l->coef=coef;}
else if(p->expn>expn)
{l=(lnode*)malloc(sizeof(lnode));
l->next=p;
q->next=l;
l->expn=expn;
l->coef=coef;}
else if(p->expn==expn)
{p->coef+=coef;}}}
q=head->next;
while(q!=null)
{printf(":%f %d \n",q->coef,q->expn);q=q->next;}
}
void jiafa(lnode *la,lnode *lb,lnode *lc)
{p=la->next;
q=lb->next;
l=lc;
while(p!=null)
{n=(lnode*)malloc(sizeof(lnode));
n->next=null;
l->next=n;
n->coef=p->coef;
n->expn=p->expn;
p=p->next;
l=l->next;}
while(q!=null)
{m=lc->next;
n=lc;
while(q->expn>m->expn)
{n=m;m=m->next;if(m==null) break;}
if(m==null)
{l=(lnode*)malloc(sizeof(lnode));
l->next=null;
n->next=l;
l->coef=q->coef;
l->expn=q->expn;
q=q->next;}
else if(q->expn<m->expn)
{l=(lnode*)malloc(sizeof(lnode));
n->next=l;
l->next=m;
l->coef=q->coef;
l->expn=q->expn;
q=q->next;}
else
{m->coef+=q->coef;
q=q->next;}}
printf("la+lb:\n");
q=lc->next;
while(q!=null)
{printf(":%f %d \n",q->coef,q->expn);q=q->next;}}
void jianfa(lnode *la,lnode *lb,lnode *ld)
{p=la->next;
q=lb->next;
l=ld;
while(p!=null)
{n=(lnode*)malloc(sizeof(lnode));
n->next=null;
l->next=n;
n->coef=p->coef;
n->expn=p->expn;
p=p->next;
l=l->next;}
while(q!=null)
{m=ld->next;
n=ld;
while(q->expn>m->expn)
{n=m;m=m->next;if(m==null) break;}
if(m==null)
{l=(lnode*)malloc(sizeof(lnode));
l->next=null;
n->next=l;
l->coef=q->coef;
l->expn=q->expn;
q=q->next;}
else if(q->expn<m->expn)
{l=(lnode*)malloc(sizeof(lnode));
n->next=l;
l->next=m;
l->coef=q->coef;
l->expn=q->expn;
q=q->next;}
else
{m->coef-=q->coef;
if(m->coef==0)
{n->next=m->next;
free(m);}
q=q->next;}}
printf("la-lb:\n");
q=ld->next;
if(q==null)
printf("该一元多项为零\n");
while(q!=null)
{printf(":%f %d \n",q->coef,q->expn);q=q->next;}}
void chengfa(lnode *la,lnode *lb,lnode *le)
{p=la->next;
q=lb->next;
l=le;
while(p!=null)
{n=(lnode*)malloc(sizeof(lnode));
n->next=null;
l->next=n;
n->coef=p->coef;
n->expn=p->expn;
p=p->next;
l=l->next;}
while(q!=null)
{l=le->next;
while(l!=null)
{l->coef*=q->coef;
l->expn+=q->expn;
l=l->next;}
q=q->next;}
l=le->next;
while(l!=null)
{printf(":%f %d \n",l->coef,l->expn);l=l->next;}
}