栈的学习笔记

来源:互联网 发布:反电信网络诈骗宣传片 编辑:程序博客网 时间:2024/06/07 05:25
坚持就是胜利,加油吧自己

前期数据准备:

#define maxsize 100typedef int elemtype;typedef struct stack_type{elemtype stack[maxsize];int top;}stacktype;

使用函数:

//initialvoid initial(stacktype *p){p->top = -1;//Why is top equal to -1?}//Insert elem;bool push(stacktype *p,elemtype a){if (p->top >= maxsize - 1){printf("This stack IS FULL! Please find another stack to finish your option!!!\n");return false;}else{p->top++;p->stack[p->top] = a;return true;}}//Let the top elem get out of the stackelemtype pop(stacktype *p){if (p->top < 0){printf("This stack IS EMPTY!Please find another stack to finish your option!!!\n");return(NULL);}else{p->top--;return(p->stack[p->top + 1]);}}

使用该栈解决的问题:简单的数字进制转换


我的代码:

#include <cstdio>#include <iostream>using namespace std;#define maxsize 100typedef int elemtype;typedef struct stack_type{elemtype stack[maxsize];int top;}stacktype;//void initial(stacktype *p){p->top = -1;//Why is top equal to -1?}//Insert elem;bool push(stacktype *p,elemtype a){if (p->top >= maxsize - 1){printf("This stack IS FULL! Please find another stack to finish your option!!!\n");return false;}else{p->top++;p->stack[p->top] = a;return true;}}//Let the top elem get out of the stackelemtype pop(stacktype *p){if (p->top < 0){printf("This stack IS EMPTY!Please find another stack to finish your option!!!\n");return(NULL);}else{p->top--;return(p->stack[p->top + 1]);}}int main(void){stacktype s,*p;p = &s;//Now here is a simple question ;elemtype num = 12;int choice; bool flag;scanf_s("%d",&choice);initial(p);do{flag = push(p, (num%choice));} while ((num/=choice)&&flag);printf("\nNow the ans is ");while (p->top >= 0){printf("%d",pop(p));}cout << endl;return 0;}

输出结果




0 0
原创粉丝点击