数据结构程序代码(C语言版)
- 格式:pdf
- 大小:121.33 KB
- 文档页数:7
2
if((a=='{')&&(b=='}')) return 1; return 0; } int empty(stack *s){ // 若 栈 为 空 , 则 返 回 true,否则 false return s->top; } main(){ stack *s; //构造栈 S char *string,c; //输入字符串数组 string 和数 据元素 c int i=0; scanf("%s",string); init(s); //构造一个空栈 S push(s,string[i]); //将字符串中的第 i 个元 素,插入到栈 S 中 do{ i++; if(cmp(gettop(s),string[i])) pop(s,&c); //删除 S 的栈顶元素,并用 C 返回其值 else push(s,string[i]); //将字符串中的第 i 个元 素,插入到栈 S 中 } while(string[i+1]); if(empty(s)==0) //若栈为空,返回 ok,否则 返回 no printf("ok"); else printf("no"); }
void AddPolyn(polyn pa,polyn pb){ //完成 多项式的相加,即:Pa=Pa+Pb,并销毁 Pb polyn ha,hb,qa,qb,qt; float sum; ElemType a,b; ha=pa;hb=pb; qa=ha->next;qb=hb->next; while(qa&&qb){ a=qa->data;b=qb->data; switch(cmp(a,b)){ case -1: //Pa 中当前结点的指数值小
P96 稀疏矩阵 #include<stdio.h> #include<stdlib.h> #define M 100 #define ROW 6 #define COL 5 typedef struct{ int i,j; int e; }Triple; typedef struct{ Triple data[M+1];
q=1; for(col=1;col<=m.nu;col++)
for(p=1;p<=m.tu;p++) if(m.data[p].j==col){
t->data[q].i=m.data[p].j;t->data[q].j=m.d ata[p].i;
t->data[q].e=m.data[p].e;q++; } }
3
return 1; } main(){ TSMatrix a,b; DataIn(&a); DataOut(a); Trans(a,&b); printf("\n"); DataOut(b); } P128//遍历二叉树 #include <stdlib.h> #define M 40 typedef struct btnode{ char data; struct btnode *lchild,*rchild; }BtNode; BtNode *p[M+1]; BtNode *Creat_Bt(void){ /*建立二叉树*/ int i,j;char ch; BtNode *s,*t; scanf("%d%c",&i,&ch); while(i!=0&&ch!='#')
P39 一元多项式的表示 #include<stdio.h> #include<stdlib.h> #include<math.h> typedef struct{ float coef; int expn; }ElemType; typedef struct LNode{ ElemType data; struct LNode *next; }LNode,*LinkList; typedef LinkList polyn; void CreatP(polyn p,int m){ int x; float y; int i; polyn q,s; q=p; for(i=1;i<=m;i++){ s=(polyn)malloc(sizeof(LNode)); scanf("%f%d",&y,&x); s->data.coef=y; s->data.expn=x; q->next=s; q=s; } s->next=NULL; } void PrintP(polyn p){ polyn q; q=p->next; printf("\n"); while(q){ printf("%4.1fx%d+",q->data.coef,q->data.expn) ; q=q->next; } } int cmp(ElemType a,ElemType b){ if(a.expn>b.expn) return -1; else if(a.expn==b.expn) return 0; else return 1; }
{ s=(BtNode*)malloc(sizeof(BtNode)); s->data=ch; s->lchild=s->rchild=NULL; p[i]=s; if(i==1) t=s; else { j=i/2; if(i%2==0) p[j]->lchild=s; else p[j]->rchild=s; } scanf("%d%c",&i,&ch); } return t; } void Preorder(BtNode *bt) /*先根遍历*/ { if(bt!=0) { printf("%c",bt->data);
m->data[n].i=i; m->data[n].j=j; m->data[n].e=x; n++; } i=(j%COL)?i:++i; j=j%COL+1; } m->mu=ROW;m->nu=COL; m->tu=n-1; } void DataOut(TSMatrix m){ int i,j,n=1; for(i=1;i<=m.mu;i++){ printf("\n"); for(j=1;j<=m.nu;j++) if((i==m.data[n].i)&&(j==m.data[n].j))
ha=qa;qa=ha->next; break; case 0: //两者指数值相等 sum=a.coef+b.coef; if(fabs(sum)>1e-4){// 修 改 多 项 式 Pa 中 当前结点的系数 qa->data.coef=sum;ha=qa; } else{ ha->next=qa->next;free(qa); } hb=qb;qb=hb->next; qa=ha->next; break; case 1: //Pb 中当前结点的指数值小 hb->next=qb->next;qt=qb;qb=hb->next; qt->next=qa;ha->next=qt; ha=ha->next; break; } }
{ push(a,x%h); x=x/h; } printf("\n"); while(empty(a)!=0) //若栈 a 不为空,则返 回 y 的值 { pop(a,y); if(*y<10) printf("%d",*y); else printf("%c",*y-10+97); } }
P48 //括号配对检验(栈) #include <stdio.h> #include <stdlib.h> typedef struct{ char *elem; int top; }stack; void init(stack *s){ //构造一个空栈,并为其 分配存储空间 s->elem=(char*)malloc(100*sizeof(char)); s->top=0; } void push(stack *s,char c){ //插入 c 为新的栈 顶元素 s->elem[s->top++]=c; } void pop(stack *s,char *c){ //删除栈 S 的栈顶 元素,并用 c 返回其值,并返回 1,否则返 回0 *c=s->elem[--s->top]; } char gettop(stack *s){ //栈存在且非空,返回 栈顶元素 return s->elem[s->top-1]; } int cmp(char a,char b){ if((a=='(')&&(b==')')) return 1; if((a=='[')&&(b==']')) return 1;
int mu,nu,tu; }TSMatrix; void DataIn(TSMatrix *m){ FILE *fp; int i=1,j=1,n=1,x; fp=fopen("m.txt","r"); while(!feof(fp)){ fscanf(fp,"%d",&x); if(x!=0) {