数据结构——顺序栈
这里给出一份数据结构上机实验课的顺序栈代码以及说明。
1 #include<stdio.h> 2 #include<malloc.h> 3 #define ERROR 0 4 #define OK 1 5 #define STACK_INT_SIZE 10 /*存储空间初始分配量*/ 6 #define STACKINCREMENT 5 /*存储空间分配增量*/ 7 typedef int ElemType; /*定义元素的类型*/ 8 typedef struct 9 { 10 ElemType *base;///栈的起始地址 11 ElemType *top;///栈顶指针 12 int stacksize; /*当前已分配的存储空间*/ 13 } SqStack; 14 int InitStack(SqStack *S); /*构造空栈*/ 15 int push(SqStack *S,ElemType e); /*入栈*/ 16 int Pop(SqStack *S,ElemType *e); /*出栈*/ 17 int GetTop(SqStack *S,ElemType *e);///取栈顶元素 18 int CreateStack(SqStack *S); /*创建栈*/ 19 void PrintStack(SqStack *S); /*出栈并输出栈中元素*/ 20 int InitStack(SqStack *S) 21 { 22 S->base=(ElemType *)malloc(STACK_INT_SIZE *sizeof(ElemType)); 23 if(!S->base)///存储分配失败 24 { 25 return ERROR; 26 } 27 S->top=S->base; 28 S->stacksize=STACK_INT_SIZE; 29 return OK; 30 }/*InitStack*/ 31 int Push(SqStack *S,ElemType e) 32 { 33 34 if((S->top)-(S->base)==S->stacksize)///栈已满,追加空间 35 { 36 S->base=(ElemType *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(ElemType)); 37 if(S->base==NULL)///追加空间失败 38 { 39 return 0; 40 } 41 S->top=S->base+S->stacksize;///新空间的栈顶指针 42 S->stacksize=S->stacksize+STACKINCREMENT;///新空间的容量 43 } 44 *S->top=e;///在栈顶插入元素e 45 S->top++;///修改栈顶指针 46 return 1; 47 }/*Push*/ 48 49 int Pop(SqStack *S,ElemType *e) 50 { 51 if(S->top==S->base)///栈为空 52 { 53 return 0; 54 } 55 else 56 { 57 S->top--;///出栈,修改栈顶指针 58 *e=*S->top; 59 return 1; 60 } 61 }/*Pop*/ 62 int GetTop(SqStack *S,ElemType *e) 63 { 64 if(S->top==S->base)///栈为空 65 { 66 return 0; 67 } 68 else 69 { 70 *e=*(S->top-1);///取栈顶元素和出栈的区别是栈顶指针并没有改变 71 return 1; 72 } 73 return 1; 74 } 75 int CreateStack(SqStack *S) 76 { 77 int e; 78 if(InitStack(S)) 79 { 80 printf("Init Success!\n"); 81 } 82 else 83 { 84 printf("Init Fail!\n"); 85 return ERROR; 86 } 87 printf("input data:(Terminated by inputing a character)\n"); 88 while(scanf("%d",&e)) 89 { 90 Push(S,e); 91 } 92 return OK; 93 }/*CreateStack*/ 94 void PrintStack(SqStack *S) 95 { 96 ElemType e; 97 while(Pop(S,&e)) 98 { 99 printf("%3d",e); 100 } 101 102 }/*Pop_and_Print*/ 103 104 int main() 105 { 106 SqStack ss; 107 printf("\n1-createStack\n"); 108 CreateStack(&ss); 109 printf("\n2-Pop&Print\n"); 110 PrintStack(&ss); 111 return 0; 112 }
在第1题的程序中,编写一个十进制转换为二进制的数制转换算法函数(要求利用栈来实现),并验证其正确性。
1 #include<stdio.h> 2 #include<malloc.h> 3 #define ERROR 0 4 #define OK 1 5 #define STACK_INT_SIZE 10 /*存储空间初始分配量*/ 6 #define STACKINCREMENT 5 /*存储空间分配增量*/ 7 typedef int ElemType; /*定义元素的类型*/ 8 typedef struct 9 { 10 ElemType *base; 11 ElemType *top; 12 int stacksize; /*当前已分配的存储空间*/ 13 } SqStack; 14 int InitStack(SqStack *S); /*构造空栈*/ 15 int push(SqStack *S,ElemType e); /*入栈*/ 16 int Pop(SqStack *S,ElemType *e); /*出栈*/ 17 int CreateStack(SqStack *S); /*创建栈*/ 18 void PrintStack(SqStack *S); /*出栈并输出栈中元素*/ 19 int InitStack(SqStack *S) 20 { 21 S->base=(ElemType *)malloc(STACK_INT_SIZE *sizeof(ElemType)); 22 if(!S->base) return ERROR; 23 S->top=S->base; 24 S->stacksize=STACK_INT_SIZE; 25 return OK; 26 }/*InitStack*/ 27 int Push(SqStack *S,ElemType e) 28 { 29 if((S->top)-(S->base)==S->stacksize) 30 { 31 S->base=(ElemType *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(ElemType)); 32 if(S->base==NULL) 33 { 34 return 0; 35 } 36 S->top=S->base+S->stacksize; 37 S->stacksize=S->stacksize+STACKINCREMENT; 38 } 39 *S->top=e; 40 S->top++; 41 return 1; 42 }/*Push*/ 43 int Pop(SqStack *S,ElemType *e) 44 { 45 if(S->top==S->base) 46 { 47 return 0; 48 } 49 else 50 { 51 S->top--; 52 *e=*S->top; 53 return 1; 54 } 55 }/*Pop*/ 56 int IsEmpty(SqStack *S) 57 { 58 if(S->top==S->base) 59 return 1; 60 else 61 return 0; 62 } 63 void Change(int n) 64 { 65 SqStack S; 66 int x; 67 InitStack(&S); 68 while(n>0) 69 { 70 x=n%2; 71 Push(&S,x); 72 n=n/2; 73 } 74 while(!IsEmpty(&S)) 75 { 76 Pop(&S,&x); 77 printf("%d", x); 78 } 79 } 80 int main() 81 { 82 SqStack ss; 83 int n; 84 printf("请输入一个十进制数\n"); 85 scanf("%d",&n); 86 printf("转换后变成的二进制数\n"); 87 Change(n); 88 return 0; 89 }