栈的应用实例
十进制转二进制
十进制通过除而取余数得到的二进制,最后需要倒过来展示。
#include <stdio.h> #include <stdlib.h> #define OK 1 #define ERROR 0 #define MAX 100 typedef int ElemType; typedef int Status; typedef struct{ ElemType * base; ElemType * top; } Stack; Status initStack(Stack *s){ s->base = (ElemType*)malloc(MAX * sizeof(ElemType)); s->top = s->base; return OK; } Status Push(Stack *s,ElemType e){ if( s->top - s->base == MAX )return ERROR; *s->top++ = e; return OK; } Status Pop(Stack *s,ElemType *e){ if( s->top - s->base == 0 )return ERROR; *e = *(--s->top); return OK; } Status convertion(int val){ int popvalue; Stack s; initStack(&s); do{ Push(&s,val%2); }while(val/=2); while(s.top!=s.base){ Pop(&s,&popvalue); printf("%d",popvalue); } printf("\n"); return OK; } int main(){ int value; printf("This is binary convertioner\n"); printf("enter Ctrl + C to stop programe\n"); while(1){ printf("enter a value:"); scanf("%d",&value); convertion(value); } } /* 8 / 2 = 4 ------ 0 push first 4 / 2 = 2 ------ 0 push second 2 / 2 = 1 ------ 0 push third 1 / 2 = 0 ------ 1 push fourth */ /* 1 pop first 0 pop second 0 pop third 0 pop fourth */
判断是否为回文
回文是指无论是正读,还是倒读都是一样的。
#include <stdio.h> #include <stdlib.h> #define OK 1 #define ERROR 0 #define MAX 100 typedef char ElemType; typedef int Status; typedef struct{ ElemType * base; ElemType * top; } Stack; Status initStack(Stack * s){ s->base = (ElemType*)malloc(MAX * sizeof(ElemType)); s->top = s->base; return OK; } Status Push(Stack *s,ElemType e){ if(s->top - s->base == MAX)return ERROR; *(s->top++)=e; return OK; } Status Pop(Stack *s,ElemType *e){ if(s->top - s->base == 0)return ERROR; *e = *(--s->top); return OK; } Status checkPalindrome(char * str){ char c; Stack s; char * p = str; initStack(&s); while( *p != '\n') Push(&s,*p++); while(s.top != s.base){ Pop(&s,&c); if(c != *str++)return ERROR; } return OK; } int main(){ char text[100]; printf("This is check text if it is palindarome\n"); printf("enter the Ctrl + C to exit progrme\n"); while(1){ printf("enter a string:"); fgets(text,100,stdin); if(checkPalindrome(text)) printf("yes it is palindarome\n"); else printf("no it isn't palindarome\n"); } }
判断括号是否匹配
括号有:圆括号,方括号,花括号三种
下面的程序可以判断括号是否一对一对存在且不乱序。
#include <stdio.h> #include <stdlib.h> #define OK 1 #define ERROR 0 typedef char 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; } Status stackEmpty(Node *s){ if(!s->next)return OK; else return ERROR; } Status checkBrackets(char str[]){ Node * s = initStack(); char v; int i = 0; while(str[i] != '\n'){ switch(str[i]){ case '(': Push(s,str[i]); break; case '[': Push(s,str[i]); break; case '{': Push(s,str[i]); break; case ')': if(stackEmpty(s))return ERROR; else{ Pop(s,&v); if(v == '(')break; else return ERROR; } case ']': if(stackEmpty(s))return ERROR; else{ Pop(s,&v); if(v=='[')break; else return ERROR; } case '}': if(stackEmpty(s))return ERROR; else{ Pop(s,&v); if(v == '{')break; else return ERROR; } } i++; } if(stackEmpty(s))return OK; } int main(){ char str[100]; printf("enter a string:"); fgets(str,100,stdin); if(checkBrackets(str))printf("it is matches all\n"); else printf("sorry it didn't matches\n"); return 0; }
程序员最高境界:静若瘫痪,动若癫痫
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!