栈的实现代码
栈也是一种线性表,只不过是受限制的线性表
只能从一端读取或者删除元素。先进后出特性(FILO)
栈也分为顺序栈和链表栈
顺序栈不需要额外的空间来指明下个节点的位置,空间利用率高
但是不能动态扩容,而链表栈刚好与之相对。下面是两种栈的代码
顺序栈
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #define OK 1 #define ERROR 0 #define MAX 100 #define DELAY 1 typedef int ElemType; typedef int Status; typedef struct{ ElemType * base; ElemType * top; } Sqstack; Status initStack(Sqstack * stack){ stack->base = (ElemType *)malloc(MAX * sizeof(ElemType)); stack->top = stack->base; return OK; } Status stackEmpty(Sqstack stack){ if(stack.top == stack.base)return OK; else return ERROR; } int stackLength(Sqstack stack){ int count = stack.top - stack.base; return count; } Status getTopValue(Sqstack stack,ElemType * e){ *e = *(stack.top-1); return OK; } Status Push(Sqstack *stack,ElemType e){ if((stack->top - stack->base) >= MAX)return ERROR; *(stack->top) = e; stack->top++; return OK; } Status Pop(Sqstack *stack,ElemType * e){ if(stack->top == stack->base)return ERROR; *e = *(stack->top-1); stack->top--; return OK; } Status printStack(Sqstack stack){ if( stackEmpty(stack) ){ printf("empty\n"); return OK; } printf("stack len is %d \n",stackLength(stack)); while(stack.base < stack.top){ printf("[%d] ",*(stack.base)); stack.base++; } printf("\n"); return OK; } int main(){ Sqstack stack; ElemType value; initStack(&stack); while(1){ system("clear"); printf("p = push\nP = pop\n"); printf("g = get\nValue e = exit\n"); printStack(stack); printf("enter a your choose:"); char sel = getchar(); if( sel == 'p'){ printf("enter push value:"); scanf("%d",&value); Push(&stack,value); sleep(DELAY); } else if(sel == 'P'){ Pop(&stack,&value); printf("value:%d is poped\n",value); sleep(DELAY); } else if(sel == 'g'){ getTopValue(stack,&value); printf("value is %d\n",value); sleep(DELAY); } else if(sel == 'e'){ exit(0); } } }
链表栈
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #define OK 1 #define ERROR 0 typedef int ElemType; typedef int Status; typedef struct Node{ ElemType data; struct Node * next; } Node; Node * initStack(){ Node * s = (Node*)malloc(sizeof(Node)); s->next = NULL; return s; } Status Push(Node *s,ElemType e){ Node *new = (Node*)malloc(sizeof(Node)); new->data = e; new->next = s->next; s->next = new; return OK; } Status Pop(Node *s,ElemType *e){ Node *top = s->next; *e = top->data; s->next = top->next; free(top); return OK; } ElemType getTopvalue(Node *s){ Node *top = s->next; return top->data; } int getLength(Node *s){ int len=0; while(s->next){ s=s->next; len++; } return len; } Status stackEmpty(Node *s){ if(s->next)return ERROR; else return OK; } Status printStack(Node *s){ if(stackEmpty(s)){printf("stack is empty\n");return ERROR;} else printf("stack len is %d\n",getLength(s)); while(s=s->next)printf("[%d]",s->data); printf("\n"); return OK; } int main(){ char sel; ElemType val; Node * s = initStack(); while(1){ system("clear"); printStack(s); printf("enter code:"); sel = getchar(); if(sel == 'p'){ printf("enter value:"); scanf("%d",&val); Push(s,val); } if(sel == 'P'){ Pop(s,&val); printf("%d is poped\n",val); sleep(1); } if(sel == 'g'){ printf("top value is %d\n",getTopvalue(s)); sleep(1); } if(sel == 'e')exit(0); } }
程序员最高境界:静若瘫痪,动若癫痫
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端