主题:各位大侠帮忙注释下 急用!!!!!
程序一
#include "alloc.h"
#include "stdio.h"
#define N0 100
#define infi 32767
int adjmatrix[N0+1][N0+1];
int n;
struct arcnode
{ int v;
int w;
struct arcnode *next;
};
struct node
{ int degree;
struct arcnode *first;
}adjlist[N0+1];
void createAdj()
{ int i,j,w,k;
struct arcnode *p;
printf("n="); scanf("%d",&n);
printf("k="); scanf("%d",&k); /*k=0 No dir */
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
if(i==j) adjmatrix[i][j]=0;
else adjmatrix[i][j]=infi;
while(1)
{ printf("i,j,w:");scanf("%d%d%d",&i,&j,&w);
if(i<1||i>n||j<1||j>n) break;
adjmatrix[i][j]=w;
if(k==0)adjmatrix[j][i]=w;
adjlist[j].degree++;
p=malloc(sizeof(struct arcnode));
p->v=j; p->w=w; p->next=adjlist[i].first;
adjlist[i].first=p;
if(k==0)
{
adjlist[i].degree++;
p=malloc(sizeof(struct arcnode));
p->v=i; p->w=w; p->next=adjlist[j].first;
adjlist[j].first=p;
}
}
}
void prim(int x)
{ int min2tree[N0+1],closest[N0+1],i,j,k,min;
for(i=1; i<=n; i++)
{ min2tree[i]= adjmatrix[x][i];
closest[i]=x;
}
for(i=2; i<=n; i++)
{ min=32767;
for(j=1; j<=n; j++)
if( min2tree[j]!=0 && min2tree[j]<min)
{
min=min2tree[j];
k=j;
}
min2tree[k]=0;
for(j=1; j<=n; j++)
if( min2tree[j]!=0
&& min2tree[j]>adjmatrix[k][j])
{
min2tree[j]=adjmatrix[k][j];
closest[j]=k;
}
}
for(i=1; i<=n; i++)
if(i!=closest[i])
printf("%d--%d:%d,",i,closest[i],
adjmatrix[i][closest[i]]);
}
main()
{ createAdj();
prim(1);
}
程序二
#include "stdio.h"
#include "alloc.h"
typedef struct node
{
int c,e;
struct node *next;
}pn;
/*创建链表*/
pn *create()
{
pn *p,*head;
int n;
printf("enter n:");
scanf("%d",&n);
head=malloc(sizeof(pn));
head->next=NULL;
p=head;
while(n)
{
p->next=malloc(sizeof(pn));
p=p->next;
printf("enter c e:");
scanf("%d%d",&(p->c),&(p->e));
n--;
}
p->next=NULL;
return head;
}
/*输出链表*/
void PL(pn *head)
{
pn *p;
p=head->next;
while(p)
{
printf("(%d,%d)",p->c,p->e);
p=p->next;
}
printf("\n");
}
/*释放链表*/
void freefile(pn *head)
{
pn *p;
p=head;
while(p)
{
head=head->next;
free(p);
p=head;
}
}
/*两个链表相加*/
pn *add(pn *ha,pn *hb)
{
pn *hc,*head,*pa=ha->next,*pb=hb->next;
int x;
head=malloc(sizeof(pn));
head->next=NULL;
hc=head;
while(pa&&pb)
{
if(pa->e==pb->e)
{
x=pa->c+pb->c;
if(x!=0)
{
hc->next=malloc(sizeof(pn));
hc=hc->next;
hc->c=x;
hc->e=pa->e;
}
pa=pa->next;
pb=pb->next;
}
else if(pa->e<pb->e)
{
hc->next=malloc(sizeof(pn));
hc=hc->next;
hc->c=pa->c;
hc->e=pa->e;
pa=pa->next;
}
else
{
hc->next=malloc(sizeof(pn));
hc=hc->next;
hc->c=pb->c;
hc->e=pb->e;
pb=pb->next;
}
}
if(pa==NULL)
{
while(pb)
{
hc->next=malloc(sizeof(pn));
hc=hc->next;
hc->c=pb->c;
hc->e=pb->e;
pb=pb->next;
}
}
if(pb==NULL)
{
while(pa)
{
hc->next=malloc(sizeof(pn));
hc=hc->next;
hc->c=pa->c;
hc->e=pa->e;
pa=pa->next;
}
hc->next=NULL;
return head;
}
}
/*一个单项式和一个多项式相乘*/
pn *onexmul(pn *pa,pn *hb)
{
pn *hc,*pc,*pb=hb->next;
pc=hc=malloc(sizeof(pn));
hc->next=NULL;
while(pb)
{
pc->next=malloc(sizeof(pn));
pc=pc->next;
pc->c=(pa->c)*(pb->c);
pc->e=(pa->e)+(pb->e);
pb=pb->next;
}
pc->next=NULL;
return hc;
}
/*多项式乘多项式*/
pn *mulxmul(pn *ha,pn *hb)
{
pn *hc,*htemp,*pa=ha->next;
hc=malloc(sizeof(pn));
hc->next=NULL;
while(pa)
{
htemp=onexmul(pa,hb);
PL(htemp);
hc=add(hc,htemp);
pa=pa->next;
freefile(htemp);
}
return hc;
}
main()
{
pn *ha,*hb,*hc,*hd;
clrscr();
ha=create();
PL(ha);
hb=create();
PL(hb);
hc=add(ha,hb);
PL(hc);
hd=mulxmul(ha,hb);
PL(hd);
freefile(ha);
freefile(hb);
freefile(hc);
freefile(hd);
}
#include "alloc.h"
#include "stdio.h"
#define N0 100
#define infi 32767
int adjmatrix[N0+1][N0+1];
int n;
struct arcnode
{ int v;
int w;
struct arcnode *next;
};
struct node
{ int degree;
struct arcnode *first;
}adjlist[N0+1];
void createAdj()
{ int i,j,w,k;
struct arcnode *p;
printf("n="); scanf("%d",&n);
printf("k="); scanf("%d",&k); /*k=0 No dir */
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
if(i==j) adjmatrix[i][j]=0;
else adjmatrix[i][j]=infi;
while(1)
{ printf("i,j,w:");scanf("%d%d%d",&i,&j,&w);
if(i<1||i>n||j<1||j>n) break;
adjmatrix[i][j]=w;
if(k==0)adjmatrix[j][i]=w;
adjlist[j].degree++;
p=malloc(sizeof(struct arcnode));
p->v=j; p->w=w; p->next=adjlist[i].first;
adjlist[i].first=p;
if(k==0)
{
adjlist[i].degree++;
p=malloc(sizeof(struct arcnode));
p->v=i; p->w=w; p->next=adjlist[j].first;
adjlist[j].first=p;
}
}
}
void prim(int x)
{ int min2tree[N0+1],closest[N0+1],i,j,k,min;
for(i=1; i<=n; i++)
{ min2tree[i]= adjmatrix[x][i];
closest[i]=x;
}
for(i=2; i<=n; i++)
{ min=32767;
for(j=1; j<=n; j++)
if( min2tree[j]!=0 && min2tree[j]<min)
{
min=min2tree[j];
k=j;
}
min2tree[k]=0;
for(j=1; j<=n; j++)
if( min2tree[j]!=0
&& min2tree[j]>adjmatrix[k][j])
{
min2tree[j]=adjmatrix[k][j];
closest[j]=k;
}
}
for(i=1; i<=n; i++)
if(i!=closest[i])
printf("%d--%d:%d,",i,closest[i],
adjmatrix[i][closest[i]]);
}
main()
{ createAdj();
prim(1);
}
程序二
#include "stdio.h"
#include "alloc.h"
typedef struct node
{
int c,e;
struct node *next;
}pn;
/*创建链表*/
pn *create()
{
pn *p,*head;
int n;
printf("enter n:");
scanf("%d",&n);
head=malloc(sizeof(pn));
head->next=NULL;
p=head;
while(n)
{
p->next=malloc(sizeof(pn));
p=p->next;
printf("enter c e:");
scanf("%d%d",&(p->c),&(p->e));
n--;
}
p->next=NULL;
return head;
}
/*输出链表*/
void PL(pn *head)
{
pn *p;
p=head->next;
while(p)
{
printf("(%d,%d)",p->c,p->e);
p=p->next;
}
printf("\n");
}
/*释放链表*/
void freefile(pn *head)
{
pn *p;
p=head;
while(p)
{
head=head->next;
free(p);
p=head;
}
}
/*两个链表相加*/
pn *add(pn *ha,pn *hb)
{
pn *hc,*head,*pa=ha->next,*pb=hb->next;
int x;
head=malloc(sizeof(pn));
head->next=NULL;
hc=head;
while(pa&&pb)
{
if(pa->e==pb->e)
{
x=pa->c+pb->c;
if(x!=0)
{
hc->next=malloc(sizeof(pn));
hc=hc->next;
hc->c=x;
hc->e=pa->e;
}
pa=pa->next;
pb=pb->next;
}
else if(pa->e<pb->e)
{
hc->next=malloc(sizeof(pn));
hc=hc->next;
hc->c=pa->c;
hc->e=pa->e;
pa=pa->next;
}
else
{
hc->next=malloc(sizeof(pn));
hc=hc->next;
hc->c=pb->c;
hc->e=pb->e;
pb=pb->next;
}
}
if(pa==NULL)
{
while(pb)
{
hc->next=malloc(sizeof(pn));
hc=hc->next;
hc->c=pb->c;
hc->e=pb->e;
pb=pb->next;
}
}
if(pb==NULL)
{
while(pa)
{
hc->next=malloc(sizeof(pn));
hc=hc->next;
hc->c=pa->c;
hc->e=pa->e;
pa=pa->next;
}
hc->next=NULL;
return head;
}
}
/*一个单项式和一个多项式相乘*/
pn *onexmul(pn *pa,pn *hb)
{
pn *hc,*pc,*pb=hb->next;
pc=hc=malloc(sizeof(pn));
hc->next=NULL;
while(pb)
{
pc->next=malloc(sizeof(pn));
pc=pc->next;
pc->c=(pa->c)*(pb->c);
pc->e=(pa->e)+(pb->e);
pb=pb->next;
}
pc->next=NULL;
return hc;
}
/*多项式乘多项式*/
pn *mulxmul(pn *ha,pn *hb)
{
pn *hc,*htemp,*pa=ha->next;
hc=malloc(sizeof(pn));
hc->next=NULL;
while(pa)
{
htemp=onexmul(pa,hb);
PL(htemp);
hc=add(hc,htemp);
pa=pa->next;
freefile(htemp);
}
return hc;
}
main()
{
pn *ha,*hb,*hc,*hd;
clrscr();
ha=create();
PL(ha);
hb=create();
PL(hb);
hc=add(ha,hb);
PL(hc);
hd=mulxmul(ha,hb);
PL(hd);
freefile(ha);
freefile(hb);
freefile(hc);
freefile(hd);
}