栈
来源:互联网 发布:java商城小系统 编辑:程序博客网 时间:2024/06/01 13:51
栈是一种后进先出的数据结构,下面给出3种实现:静态数组,动态数组、单链表
1、静态数组实现栈
#include <stdio.h>#define type int#define max 5type array[max];int top=-1;void push(type);type pop(void);type retop();int isempty();int isfull();void static_stackprint(){int i;for(i=0;i<=top;i++)printf("%d%c",array[i],i==top?'\n':' ');}int main(){push(1);push(2);push(3);push(4);push(5);push(6);static_stackprint();printf("pop:%d\n",pop());printf("pop:%d\n",pop());static_stackprint();return 0;}int isfull(){if(top+1==max)return 1;elsereturn 0;}int isempty(){if(top==-1)return 1;elsereturn 0;}void push(type value){if(!isfull()){array[++top]=value;printf("%d insert successfully\n",value);}elseprintf("stack is full\n");}type pop(void){if(!isempty()){return array[top--];}elseperror("stack is empty!\n");}type retop(){if(!isempty())return array[top];elseperror("stack is empty!\n");}
2、动态数组实现
#include <stdio.h>#include <stdlib.h>#include <assert.h>#define type intstatic type *array;int top=-1;void push(type);void pop();type retop();int isempty();void stack_print();void destroy(){assert(top!=-1);free(array);array=NULL;}int main(){push(1);push(6);push(3);push(2);push(5);stack_print();printf("-------------\n");pop();pop();stack_print();printf("-------------\n");printf("%d\n",retop());destroy();return 0;}int isempty(){return top==-1?1:0 ;}void push(type value){array=(type*)realloc(array,(++top+1)*sizeof(type));if(array==NULL){perror("realloc faiture\n");top--;return ;}array[top]=value;return ;}void stack_print(){int i;for(i=0;i<=top;i++)printf("%d%c",array[i],i==top?'\n':' ');}void pop(){if(!isempty()){array=(type*)realloc(array,(--top+1)*sizeof(type));if(array==NULL){perror("realloc failture\n");top++;return ;}return ;}}type retop(){if(!isempty())return array[top];}
3、单链表实现
#include <stdio.h>#include <stdlib.h>#include <assert.h>#define type inttypedef struct stacknode{type data;struct stacknode * next;}stacknode;static struct stacknode * stack;void push(type);void pop();type retop();int isempty();void destroy();void stack_print();int main(){push(5);push(3);push(4);push(2);push(1);stack_print();printf("---------------------\n");pop();pop();stack_print();printf("---------------------\n");printf("%d\n",retop());destroy();return 0;}int isempty(){return stack==NULL?1:0 ;}void push(type value){struct stacknode * newd;newd=(stacknode *)malloc(sizeof(struct stacknode));assert(newd!=NULL);newd->data=value;newd->next=stack;stack=newd;}void stack_print(){struct stacknode *cur=stack;assert(!isempty());while(cur!=NULL){printf("%d%c",cur->data,cur->next==NULL?'\n':' ');cur=cur->next;}}void pop(){struct stacknode * cur;assert(!isempty());cur=stack;stack=cur->next;free(cur);}type retop(){return stack->data;}void destroy(){while(!isempty()){pop();}}
0 0
- 栈
- 栈
- 栈
- 栈
- 栈
- 栈
- 栈
- 栈
- 栈
- 栈
- 栈
- 栈
- 栈
- 栈
- 栈
- 栈
- 栈
- 栈
- java数组的四种排序方法
- xcode如何用代码打开指定的文件夹目录并显示目录内容
- 2011年蓝桥杯初赛第七题
- linux 关闭selinux安全验证和iptables
- 再见,凡尔纳!
- 栈
- 章表示图 --------- UITableView
- 饮料供货
- Stack_Queue 一个数组实现三个栈 @CareerCup
- LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- 自定义Sharepoint的登陆页面
- GridView添加统计(合计)行
- MSSQL文本文件导入导出
- 习近平:凡属重大改革都要于法有据