计算机软件技术C语言.docx
- 文档编号:1992662
- 上传时间:2023-05-02
- 格式:DOCX
- 页数:28
- 大小:20.09KB
计算机软件技术C语言.docx
《计算机软件技术C语言.docx》由会员分享,可在线阅读,更多相关《计算机软件技术C语言.docx(28页珍藏版)》请在冰点文库上搜索。
计算机软件技术C语言
稀疏矩阵
#include
#include
#defineLENsizeof(structspmatrix)
#defineM16
structnode
{inti,j;/*行、列号*/
intv;/*元素值*/
};
structspmatrix/*稀疏矩阵类型*/
{intm,n,t;/*行数,列数,非零元素个数*/
structnodedata[M];/*三元组表*/
};
structspmatrixA={6,4,6,0,1,5,0,4,8,1,0,1,1,2,3,2,1,-2,3,0,6};
/*转置运算*/
structspmatrix*TRANSMAT(a)
structspmatrix*a;
{
intano,bno,col;/*ano指A中的结点序号,bno指B中的结点序号,col指A中的列号*/
structspmatrix*b;
b=(structspmatrix*)malloc(LEN);
b->m=a->n;
b->n=a->m;
b->t=a->t;
if(b->t>0)
{bno=0;
for(col=0;col<=a->n;col++)
for(ano=0;ano
if(a->data[ano].j==col)
{b->data[bno].i=a->data[ano].j;
b->data[bno].j=a->data[ano].i;
b->data[bno].v=a->data[ano].v;
bno++;
}
}
return(b);
}
voidmain()
{intnum;
structspmatrix*a=&A;
structspmatrix*c;
c=(structspmatrix*)malloc(LEN);
printf("转置前的三元组:
\n");
for(num=0;num<6;num++)
printf("\n%d行,%d列,值:
%d\n",a->data[num].i,a->data[num].j,a->data[num].v);
c=TRANSMAT(a);
printf("\n转置后的三元组:
\n");
for(num=0;num<6;num++)
printf("\n%d行,%d列,值:
%d\n",c->data[num].i,c->data[num].j,c->data[num].v);
}
栈操作
#defineM50
#include
#include
typedefstruct
{intdata[M];
inttop;
}seqstack;
/*判断栈空*/
intEMPTY(seqstack*s)
{
if(s->top>=0)return(0);
elsereturn
(1);
}
/*进栈*/
seqstack*PUSH(seqstack*s,intx)
{
if(s->top==M-1)
{printf("\n上溢\n");return;}
else
{s->top++;
s->data[s->top]=x;
}
return(s);
}
/*出栈*/
intPOP(seqstack*s)
{
if(EMPTY(s))
{printf("\n下溢\n");return;}
else
{s->top--;
printf("\n出栈元素:
%d",s->data[s->top+1]);
return(s);
}
}
/*取栈顶元素*/
intTOP(seqstack*s)
{
if(EMPTY(s))
{printf("\n空栈\n");return;}
else
return(s->data[s->top]);
}
voidmain()
{
seqstack*L;
intx,p,q;
L=(seqstack*)malloc(sizeof(seqstack));
L->top=-1;
printf("是否进栈,1=是、0=否:
");
scanf("%d",&p);
if(p==1)
{
printf("输入进栈元素,以输入0结束:
\n");
scanf("%d",&x);
while(x!
=0)
{
L=PUSH(L,x);
scanf("%d",&x);
}
}
printf("是否出栈,1=是、0=否:
");
scanf("%d",&q);
if(q==1)
while(!
EMPTY(L))
POP(L);
}
建立单链表
#include
#include
#defineLENsizeof(structstudent)
structstudent
{longnum;
floatscore;
structstudent*next;
};
intn;
structstudent*head;
/*建立单链表creat(void)*/
structstudent*creat(void)
{structstudent*p1,*p2;
n=0;
p1=p2=(structstudent*)malloc(LEN);
printf("请输入学生的学号和成绩(以输入‘0’结束):
");
scanf("%ld%f",&p1->num,&p1->score);
head=NULL;
while(p1->num!
=0)
{n=n+1;
if(n==1)head=p1;
elsep2->next=p1;
p2=p1;
p1=(structstudent*)malloc(LEN);
scanf("%ld%f",&p1->num,&p1->score);
}
p2->next=NULL;
return(head);
}
/*单链表插入(在已知学号结点前插入一个新的结点)函数*/
insert()
{structstudent*h,*p;
inta,x1;floatx2;
printf("\n请输入已知学号:
");
scanf("%d",&a);
printf("\n请输入新插入的学号和成绩:
");
scanf("%d%f",&x1,&x2);
p=(structstudent*)malloc(LEN);
p->num=x1;p->score=x2;
h=head;
if(head==NULL)
{head=p;p->next=NULL;return;}
elseif(head->num==a)
{p->next=head;head=p;return;}
else
{while((h->next!
=NULL)&&(h->next->num!
=a))
h=h->next;
p->next=h->next;h->next=p;return;
}
}
/*删除操作(根据学号进行删除)*/
del()
{structstudent*h,*p,*q;
inta;
h=head;
printf("\n请输入要删除的学号:
");
scanf("%d",&a);
if(head==NULL)
{printf("\n空表,无法进行删除操作。
");return;}
elseif(head->num==a)
{p=head->next;head=head->next;free(p);return;}
else
{while((h->next!
=NULL)&&(h->next->num!
=a))
h=h->next;
if(h->next==NULL)printf("\n无此结点");
q=h->next;h->next=q->next;
free(q);
}
}
/*在屏幕上输出链表中的数据(学号和成绩)*/
putlist()
{structstudent*q;
printf("\n建立的链表数据如下:
\n\n");
q=head;
if(head!
=NULL)
do
{printf("学号:
%ld号成绩:
%5.1f\n",q->num,q->score);
q=q->next;
}while(q!
=NULL);
}
voidmain()
{intt;
creat();
putlist();
printf("\n是否进行插入操作[1=是,2=否]:
");
scanf("%d",&t);
if(t==1)
{insert();
putlist();
}
elseif(t==2)
printf("\n不进行插入操作.\n");
printf("\n是否进行删除操作[1=是,2=否]:
");
scanf("%d",&t);
if(t==1)
{del();
printf("\n进行删除操作后的数据:
");
putlist();
}
elseif(t==2)
printf("\n不进行删除操作.");
}
二叉树建立
#include
#include
#definemaxsize10
typedefstructnode
{intdata;
structnode*lchild,*rchild;
}bitree;
bitree*Q[maxsize];
bitree*CREATREE()
{charch;
intfront,rear;
bitree*root,*s;
root=NULL;
front=1;rear=0;
ch=getchar();
while(ch!
='#')
{s=NULL;
if(ch!
='@')
{s=(bitree*)malloc(sizeof(bitree));
s->data=ch;
s->lchild=NULL;
s->rchild=NULL;
}
rear++;
Q[rear]=s;
if(rear==1)root=s;
else
{if(s&&Q[front])
if(rear%2==0)Q[front]->lchild=s;
else
Q[front]->rchild=s;
if(rear%2==1)front++;
}
ch=getchar();
}
return(root);
}
voidinorder(t)
bitree*t;
{if(t)
{inorder(t->lchild);
printf("%c",t->data);
inorder(t->rchild);
}
return;
}
voidmain()
{bitree*root;
root=CREATREE();
printf("\n根节点是:
%c\n",root->data);
printf("建立的二叉树中的节点是:
\n");
inorder(root);
}
#include
#include
structpnode
{floatcoef;/*coef是多项式中的系数*/
intexp;/*exp是多项式中的指数*/
structpnode*next;
};
/*建立单链表带头结点creat(void)*/
structpnode*creat(charx)
{intn;
structpnode*head;
structpnode*p1,*p2;
n=0;
p1=(structpnode*)malloc(sizeof(structpnode));
p1->coef=0;p1->exp=-1;
head=p1;p2=p1;
p1=(structpnode*)malloc(sizeof(structpnode));
printf("请输入%c多项式的系数和指数(以输入系数,指数都是‘0’结束):
",x);
scanf("%f%d",&p1->coef,&p1->exp);
while((p1->coef!
=0)&&(p1->exp!
=0))
{
p2->next=p1;
p2=p1;
p1=(structpnode*)malloc(sizeof(structpnode));
scanf("%f%d",&p1->coef,&p1->exp);
}
p2->next=head;
return(head);
}
/*在屏幕上输出链表中的数据(多项式的系数和指数)*/
putlist(structpnode*head,charx)
{structpnode*q;
printf("\n\n建立的%c链表数据如下:
\n",x);
q=head;
if(head!
=NULL)
do
{printf("系数:
%f指数:
%d\n",q->coef,q->exp);
q=q->next;
}while(q!
=head);
}
/*多项式相加函数POLYADD*/
structpnode*POLYADD(structpnode*A,structpnode*B)
{
structpnode*ptr,*q,*q1,*q2;
floatx;
q1=A;q2=B;
q=(structpnode*)malloc(sizeof(structpnode));/*生成和多项式的头结点*/
q->coef=0;
q->exp=-1;
q->next=q;
ptr=q;/*ptr指向和多项式的头结点*/
q1=q1->next;/*q1指向A多项式的第一个结点*/
q2=q2->next;/*q2指向B多项式的第一个结点*/
while((q1!
=A)&&(q2!
=B))
{
if(q1->exp==q2->exp)/*指数相同的项的操作*/
{x=q1->coef+q2->coef;
if(x!
=0)
{
q->next=(structpnode*)malloc(sizeof(structpnode));
q=q->next;q->coef=x;q->exp=q1->exp;
}
q1=q1->next;
q2=q2->next;
}
else/*指数不相同的项的操作*/
{
q->next=(structpnode*)malloc(sizeof(structpnode));
q=q->next;
if(q1->exp>q2->exp)/*A指数大于B指数的操作*/
{
q->coef=q2->coef;
q->exp=q2->exp;
q2=q2->next;
}
else/*B指数大于A指数的操作*/
{
q->coef=q1->coef;
q->exp=q1->exp;
q1=q1->next;
}
}
}
while(q1!
=A)/*如果A多项式未处理完,将剩余项直接复制到和多项式*/
{
q->next=(structpnode*)malloc(sizeof(structpnode));
q=q->next;q->coef=q1->coef;
q->exp=q1->exp;q1=q1->next;
}
while(q2!
=B)/*如果B多项式未处理完,将剩余项直接复制到和多项式C*/
{
q->next=(structpnode*)malloc(sizeof(structpnode));
q=q->next;q->coef=q2->coef;
q->exp=q2->exp;q2=q2->next;
}
q->next=ptr;
return(ptr);
}
/*主函数*/
voidmain()
{
structpnode*a,*b,*c;
a=creat('A');
b=creat('B');
putlist(a,'A');
putlist(b,'B');
c=POLYADD(a,b);
putlist(c,'C');
}
#include
#defineN6
/*插入(根据位置插入)*/
insertlist(inta[N],intn,inti,intx)
{intj;
if((i<1)||(i>N))
{printf("i输入错");
return;
}
for(j=N-1;j>=i-1;j--)
a[j+1]=a[j];
a[j+1]=x;
return;
}
/*删除(根据位置删除)*/
DELETELIST(inta[N],intn)
{inti,j;
printf("\n输入要删除的位置序号:
");
scanf("%d",&i);
if((i<1)||(i>n))
{printf("参数错");return;}
for(j=i-1;j<=N-1;j++)
a[j]=a[j+1];
return;
}
voidmain()
{inta[N],i,n,x;
n=3;
printf("输入数组数据:
\n");
for(i=0;i<=n;i++)
{printf("a[%d]=",i);
scanf("%d",&a[i]);
}
printf("输入要插入的位置和数据:
");
scanf("%d%d",&i,&x);
insertlist(a,n,i,x);
printf("输出插入后的数据:
\n");
for(i=0;i printf("a[%d]=%d\n",i,a[i]); DELETELIST(a,n); printf("\n输出删除后的数据: \n"); for(i=0;i printf("a[%d]=%d\n",i,a[i]); } 二叉排序树 #include #include typedefstructnode {intdata; structnode*lchild,*rchild; }bitree; /*插入操作函数*/ bitree*insertbitree(bitree*p,intb) { if(p==NULL) { p=(bitree*)malloc(sizeof(bitree)); p->data=b; p->lchild=NULL; p->rchild=NULL; } elseif(b p->lchild=insertbitree(p->lchild,b); elsep->rchild=insertbitree(p->rchild,b); return(p); } /*建立二叉排序树函数*/ bitree*creatbitree(bitree*t) { intkey; t=NULL; printf("输入二叉排序树的数值(以输入0结束): "); scanf("%d",&key); while(key! =0) { t=insertbitree(t,key); scanf("%d",&key); } return(t); } /*删除二叉排序树上的结点函数*/ bitree*del(bitree*t) { intfag,k; bitree*p,*s,*q,*f; p=t;f=NULL; printf("输入要删除的结点值: "); scanf("%d",&k); while(p! =NULL)/*查找关键字为K的待删除结点*/ { if(p->data==k)break;/*找到,跳出查找循环*/ f=p; if(p->data>k)p=p->lchild; elsep=p->rchild; } if(p==NULL)printf("要删除的结点不在此二叉排序树中。 "); if(p->lchild==NULL)/**p无左子树*/ { if(f==NULL)t=p->rchild; elseif(f->lchild==p) f->lchild=p->rchild; else f->rchild=p->rchild; free(p); } else/**p有左子树*/ { q=p; s=p->lchild; while(s->rchild! =NULL) { q=s; s=s->rchild; } if(q==p) q->lchild=s->lchild; else q->rchild=s->lchild; p->data=s->data; free(s); fag=1; } return(t); } /*中序遍历在屏幕上输出二叉排序树函数*/ voidinorder(bitree*t) {if(t) { inorder(t->lchild); printf("%d",t->data); inorder(t->rchild); } } voidmain() { bitree*root; intt; root=creatbitree(root); printf("\n根节点是: %d\n",root->data); printf("建立的二叉树中的节点是: \n"); inorder(root); printf("\n是否进行删除操作[1=是,2=否]: "); scanf("%d",&t); if(t==1) { root=del(root); printf("\n进行删除操作后的数据: "); inorder(root); } elseif(t==2) printf("\n不进行删除操作."); } 哈夫曼树建立 #definen4 #definem2*n-1 #definemax9998 #include typedefstructtree {intdata; intlchild,rchild,prnt; }hufmtree; /*建立哈夫曼树函数*/
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 计算机软件 技术 语言