栈(一)——栈的基本操作
来源:互联网 发布:小米 数据分析师 编辑:程序博客网 时间:2024/05/16 10:46
1.栈的简介
栈是一种后入先出的数据结构,一般包含两种最基本的操作:入栈(push)和出栈(pop)。
入栈操作:top指针上移,元素入栈。
出栈操作:top指针下移。
栈空的条件:top == bottom
栈满的条件:top == maxsize-1
2.有数据序列1 2 3一次存入一个栈stack中,则出栈顺序可以为以下四种:
1,2,3; 2,1,3; 3,2,1; 1,3,2.
栈是一种后入先出的数据结构,一般包含两种最基本的操作:入栈(push)和出栈(pop)。
入栈操作:top指针上移,元素入栈。
出栈操作:top指针下移。
栈空的条件:top == bottom
栈满的条件:top == maxsize-1
2.有数据序列1 2 3一次存入一个栈stack中,则出栈顺序可以为以下四种:
1,2,3; 2,1,3; 3,2,1; 1,3,2.
3.任意输入一个十进制整数x(x<32768),输出x的二进制值。
#include <stdio.h>#define MAXSIZE15int main(){int a[MAXSIZE];int bottom, top;int x;bottom = top = -1;printf("Please input x(0<=x<=32767):");scanf("%d", &x);while(x){a[++top] = x%2;x /= 2;}while(-1 != top){printf("%d ", a[top--]);}printf("\n");return 0;}4.判断一个C语言表达式的左右括号是否匹配,提示:将要判断的表达式用字符串输入。
#include <stdio.h>#define MAXSIZE100int main(){char a[MAXSIZE];int top = -1;int i;printf("Please input a expression:");gets(a);i = 0;while(a[i]){if('(' == a[i])top ++;else if(')' == a[i]){if(-1 == top)break;elsetop --;}i ++;}if('\0' == a[i] && -1 == top)printf("Match.\n");elseprintf("Not match.\n");return 0;}
5.栈的基本操作——出栈和入栈
#include <stdio.h>#include <malloc.h>typedef struct st{int maxsize;int top;int *pstack;}stack;void create_stack(stack *s, int ms);void push(stack *s, int x);int pop(stack *s);void clear_stack(stack *s);int main(){stack s;int ms, i;int a[] = {13, 17, 15, 25};printf("Please input stack size:");scanf("%d", &ms);create_stack(&s, ms);for(i = 0; i < 4; i++)push(&s, a[i]);while(s.top != -1)printf("%d ", pop(&s));printf("\n");clear_stack(&s);return 0;}void create_stack(stack *s, int ms){s->maxsize = ms;s->top = -1;s->pstack = (int *)malloc(ms*sizeof(int));}void push(stack *s, int x){if(s->top < s->maxsize-1)s->pstack[++s->top] = x;}int pop(stack *s){if(s->top != -1)return s->pstack[s->top--];}void clear_stack(stack *s){s->maxsize = 0;s->top = -1;free(s->pstack);s->pstack = 0;}
0 0
- 栈(一)——栈的基本操作
- 栈的基本操作(一)
- Linux命令(一)——目录的基本操作
- 栈—顺序栈的基本操作
- 数据结构——栈的基本操作
- 数据结构——栈的基本操作
- 数据结构——栈的基本操作
- 栈和队列算法一之栈的基本操作
- XML文档的基本操作(一)
- 树形控件的基本操作(一)
- 线程的基本操作(一)
- 命令行的基本操作(一)
- 数据表的基本操作(一)
- mongoose的基本操作(一)
- 线性表的基本操作(一)
- MySQL(一)--数据库的基本操作
- Swift字符串的基本操作(一)
- rails应用程序的基本操作(一)
- hdu4883 & BestCoder Round #2 TIANKENG’s restaurant(暴力)
- java容器
- MyBatis(3.2.3)的配置文件详解
- Android异步处理
- java关键字
- 栈(一)——栈的基本操作
- UITableView重用机制
- 年轻,才叫创业
- 101个硬币100真、1假,真假区别在于重量。请用无砝码天平称两次给出真币重还是假币重的结论。
- MFC 下调用Qt编写的含界面的dll
- 时间复杂度为O(n)的排序算法,你会吗?——其实很简单的
- Python中实现远程调用(RPC、RMI)简单例子
- jpa对单个实体bean的crud操作
- linux 软件安装的通行方式