数据结构程序.docx
- 文档编号:11964334
- 上传时间:2023-06-03
- 格式:DOCX
- 页数:31
- 大小:139.02KB
数据结构程序.docx
《数据结构程序.docx》由会员分享,可在线阅读,更多相关《数据结构程序.docx(31页珍藏版)》请在冰点文库上搜索。
数据结构程序
实验一:
#include
#include
#defineLIST_INIT_SIZE100
#defineLISTINCREMENT10
typedefstruct{int*elem;intlength;intlistsize;}SqList;
intInitList_Sq(SqList&L){
L.elem=(int*)malloc(LIST_INIT_SIZE*sizeof(int));
if(!
L.elem)
return0;
L.length=0;
L.listsize=LIST_INIT_SIZE;
return1;}
intListInsert_Sq(SqList&L,inti,inte){
int*p,*q,*newbase;
if(i<1||i>L.length+1)
return0;
if(L.length>=L.listsize){
newbase=(int*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(int));
if(!
newbase)return0;
L.elem=newbase;
L.listsize+=LISTINCREMENT;}
q=&(L.elem[i-1]);
for(p=&(L.elem[L.length-1]);p>=q;--p)
*(p+1)=*p;
*q=e;
++L.length;
return1;}
intListDelete_Sq(SqList&L,inti,int&e){
int*p,*q,*newbase;
if((i<1)||(i>L.length))
return0;
p=&(L.elem[i-1]);
e=*p;
q=L.elem+L.length-1;
for(++p;p<=q;++p)
*(p-1)=*p;
*q=e;
--L.length;
return1;
}voidprint(SqListL)
{for(inti=0;i printf("%d,",L.elem[i]); } voidmain() { SqListL; inte; InitList_Sq(L); ListInsert_Sq(L,135); ListInsert_Sq(L,134); ListInsert_Sq(L,133); ListInsert_Sq(L,132); ListDelete_Sq(L,2,e); print(L); } 实验二: #include #include typedefintStatus; typedefstructLNode{ intdata; structLNode*next; }LNode,*LinkList; StatusListInsert_L(LinkList&L,inti,inte) {LinkListp,s; intj; p=L;j=0; while(p&&j if(! p||j>i-1)return0; s=(LinkList)malloc(sizeof(LNode)); s->data=e;s->next=p->next; p->next=s; return1; } StatusListDelete_L(LinkList&L,inti,int&e) {LinkListp,q; intj; p=L;j=0; while(p->next&&j if(! (p->next)||j>i-1)return0; q=p->next;p->next=q->next; e=q->data;free(q); return1; } voidCreateList_L(LinkList&L,intn) {inti; LinkListp; L=(LinkList)malloc(sizeof(LNode)); L->next=NULL; for(i=n;i>0;--i) {p=(LinkList)malloc(sizeof(LNode)); scanf("%d",&p->data); p->next=L->next;L->next=p;}} StatusGetElem_L(LinkListL,inti,int&e) {LinkListp; intj; p=L->next;j=1; while(p&&j {p=p->next;++j;} if(! p||j>i)return0; e=p->data;return1;} voidmain() {LinkListL,p;inti,n;inte; printf("请输入你想创建多少个元素的链表"); scanf("%d",&n); CreateList_L(L,n); for(p=L->next;p! =NULL;p=p->next) printf("%d",p->data); printf("\n"); printf("请输入想查找第几个元素"); scanf("%d",&i); GetElem_L(L,i,e); printf("e=%d\n",e); printf("请输入想在第几个元素位置前插入元素"); scanf("%d%d",&i,&e); ListInsert_L(L,i,e); for(p=L->next;p! =NULL;p=p->next) printf("%d",p->data); printf("\n"); printf("请输入想删除第几个元素"); scanf("%d",&i); ListDelete_L(L,i,e); for(p=L->next;p! =NULL;p=p->next) printf("%d",p->data); printf("\n"); } 实验三: #include #include #defineMAXNUM20 #defineElemTypeint/*定义顺序栈的存储结构*/ typedefstruct {ElemTypestack[MAXNUM]; inttop; }SqStack;/*初始化顺序表*/ voidInitStack(SqStack*p) {if(! p) printf("内存分配失败! "); p->top=-1; }/*入栈*/ voidPush(SqStack*p,ElemTypex) {if(p->top {p->top=p->top+1; p->stack[p->top]=x;} Else printf("Overflow! \n");}/*出栈*/ ElemTypePop(SqStack*p) {ElemTypex; if(p->top>=0) {x=p->stack[p->top]; printf("以前的栈顶数据元素%d已经被删除! \n",p->stack[p->top]); p->top=p->top-1; return(x);} else {printf("Underflow! ? \n"); return(0);} }/*获取栈顶元素*/ ElemTypeGetTop(SqStack*p) {ElemTypex; if(p->top>=0) {x=p->stack[p->top]; printf("\n栈顶元素为: %d\n",x); return(x);} else {printf("Underflow! \n"); return(0);} }/*遍历顺序表*/ voidOutStack(SqStack*p) {inti; printf("\n");if(p->top<0) printf("这是一个空表! "); for(i=p->top;i>=0;i--) printf("第%d个数据元素是: ? %6d\n",i,p->stack[i]); }/*置空顺序表*/ voidsetEmpty(SqStack*p) {p->top=-1;}/*? 数值转换*/ voidshuzhi(SqStack*p,intn,intx) { while(n){ Push(p,n%x); n=n/x; } }/*判断回文数*/ boolhuiwen(SqStack*p,intn){ inti=0,j=0; inta[MAXNUM]; while(n){ a[i]=n%10; Push(p,n%10); n=n/10; i++; } while(p->stack[p->top-j]==a[j])j++; if(j return0; else return1; }/*主函数*/ voidmain() {SqStack*q; intcord,x,n,y;ElemTypea; printf("第一次使用必须初始! \n"); do{ printf("\n"); printf("\n----------主菜单------------\n"); printf("\n1初始化顺序表\n"); printf("\n2插入一个元素\n"); printf("\n3删除栈顶元素\n"); printf("\n4取栈顶元素\n"); printf("\n5置空顺序栈\n"); printf("\n6数制转换\n"); printf("\n7判断回文数\n"); printf("\n8结束程序运行\n"); printf("\n----------------------------\n"); printf("请输入您的选择(1,2,3,4,5,6,7)"); scanf("%d",&cord); printf("\n"); switch(cord) {case1: {q=(SqStack*)malloc(sizeof(SqStack)); InitStack(q); OutStack(q); }break; case2: {printf("请输入要插入的数据元素: a="); scanf("%d",&a); Push(q,a); OutStack(q); }break; case3: { Pop(q); OutStack(q); }break; case4: { GetTop(q); OutStack(q); }break; case5: { setEmpty(q); printf("\n顺序栈被置空! \n"); OutStack(q); }break; case6: {q=(SqStack*)malloc(sizeof(SqStack)); inti; InitStack(q); printf("请输入要转换的数制: \n"); scanf("%d",&x); printf("请输入要转换的数: N="); scanf("%d",&n); shuzhi(q,n,x); if(q->top<0) printf("! "); for(i=q->top;i>=0;i--) printf("%6d",q->stack[i]); }break; case7: { q=(SqStack*)malloc(sizeof(SqStack)); ints; InitStack(q); printf("请输入要判断的正数: \n"); scanf("%d",&y); s=huiwen(q,y); if(s==1) printf("%d是回文数! ",y); else printf("%d不是回文数! ",y); }break; case8: exit(0);} }while(cord<=8);} 实验四: 矩阵的转置 #include #include usingnamespacestd; #definen12 #definen23 inta[n1][n2],b[n2][n1]; intmain() { inta[n1][n2],b[n2][n1]; inti,j; printf("请输入一个%d行%d列的矩阵: ",n1,n2); for(i=0;i for(j=0;j scanf("%d",*(a+i)+j);//使用元素a[i][j]的地址 printf("转置后的矩阵为: \n"); for(i=0;i { for(j=0;j { *(*(b+i)+j)=*(*(a+j)+i);//b[i][j]=a[j][i] printf("%d\t",b[i][j]); } printf("\n"); } return0; } /*intchushihua() { inti,j; printf("请输入一个%d行%d列的矩阵: ",n1,n2); for(i=0;i for(j=0;j scanf("%d",*(a+i)+j); return0; } intzhuanzhi() { inti,j; printf("转置后的矩阵为: \n"); for(i=0;i { for(j=0;j { *(*(b+i)+j)=*(*(a+j)+i);//b[i][j]=a[j][i] printf("%d\t",b[i][j]); } printf("\n"); } return0; } intmain() { chushihua(); zhuanzhi(); return0; } 实验五: 遍历二叉树 先序二叉树 #include #include typedefstructBiNode{ chardata; structBiNode*lchild,*rchild; }BiNode,*BiTree; intcreatebiTree(BiTree&T) { charch; scanf("%c",&ch); if(ch=='')T=NULL; else{ if(! (T=(BiNode*)malloc(sizeof(BiNode))))return0; T->data=ch; createbiTree(T->lchild); createbiTree(T->rchild); } return1; } intinorder(BiTreeT) { if(T){ inorder(T->lchild); printf("%c",T->data); inorder(T->rchild); } return1; } voidmain() { BiTreeT; createbiTree(T); inorder(T); } 中序遍历 #include #include typedefstructnode { intdata; structnode*lchl,*rchl; }NODE; voidcreate(NODE**t)//建立 {inta; scanf("%d",&a); if(a) { *t=(NODE*)malloc(sizeof(NODE)); (*t)->data=a; create(&((*t)->lchl)); create(&((*t)->rchl)); } Else t=NULL; } voidprint(NODE*T)//中序遍历 { if(T) { print(T->lchl); printf("%4d",T->data); print(T->rchl); }后序二叉树 #include #include structBiTNode//定义结构体 { Chardata; StructBiTNode*lchild,*rchild; } Voidlater(structBiTNode*&p)//前序创建树 { Charch; scanf("%c",&ch); If(ch=='') p=NULL; Else { p=(structBiTNode*)malloc(sizeof(structBiTNode)); p->data=ch; later(p->lchild); later(p->rchild); } } Voidprint(structBiTNode*p)//后序遍历(输出二叉树) { if(p! =NULL) { print(p->lchild); print(p->rchild); printf("%c",p->data); } Else printf(""); } voidmain()//主函数 {/*检测: printf("到了吗");*/ StructBiTNode*p; later(p); print(p); } } intmain() { NODE*root; create(&root); print(root); return0; }. 实验六 顺序查找 #include"stdio.h" #include"malloc.h" #defineMAX100 typedefstructsq{ intkey; //intdata; }sqlist; sqlistst[MAX],s[MAX]; voidcreat(sqlistst[MAX],intn) { inti; for(i=0;i { scanf("%d",&st[i].key); } for(i=0;i { printf("%d",st[i].key); } printf("\n"); } //顺序查找 intshunxu_search(sqlistst[],intn,intkey) {inti=0; for(;i {if((st[i].key==key)&&(i break;} if(i \n"); printf("查找次数为: %d\n",i+1); return (1);} else {printf("查找失败! \n");} } voidmain() {intf=1,s,p,q; inta,no; while(f){ printf("Pleaseinputthenumberwhichyouneed\n"); printf("no.1: 创建数据\nno.2: 按顺序查找\n"); scanf("%d",&no); switch(no){ case1: printf("请输入个数: "); scanf("%d",&a); printf("请输入元素: \n"); creat(st,a);break; case2: printf("请输入要查找的数: "); scanf("%d",&s); q=shunxu_search(st,a,s); break; } printf("doyouwanttocontinue? \nno.1: continue\nno.0: exit\n"); scanf("%d",&f); } } //顺序查找 intshunxu_search(sqlistst[],intn,intkey) {inti=0; for(;i {if((st[i].key==key)&&(i break;} if(i \n"); printf("查找次数为: %d\n",i+1); return (1);} else {printf("查找失败! \n");} } voidmain() {intf=1,s,p,q; inta,no; while(f){ printf("Pleaseinputthenumberwhichyouneed\n"); printf("no.1: 创建数据\nno.2: 按顺序查找\n"); scanf("%d",&no); switch(no){ case1: printf("请输入个数: "); scanf("%d",&a); printf("请输入元素: \n"); creat(st,a);break; case2: printf("请输入要查找的数: "); scanf("%d",&s); q=shunxu_search(st,a,s); break; } printf("doyouwanttocontinue? \nno.1: continue\nno.0: exit\n"); scanf("%d",&f); } } 折半查找 #include #include voidmain() { inta[10]={1,3,4,6,8,9,10,13,15,18}; intn,i,low=0,high=9; intadrress=0,count=0; printf("pelaseinputthesearchnumber: "); scanf("%d",&n); high=high/2; if(n>a[high]) { count++; low=high+1; for(i=low;i<10;i++) { count++; if(a[i]==n) { adrress=i; printf("thenumber%dissearched,theadrressis%d,andsearchnumbersis%d\n",n,adrress,count); } }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 程序
![提示](https://static.bingdoc.com/images/bang_tan.gif)