数据结构之栈

来源:互联网 发布:linux elf文件 编辑:程序博客网 时间:2024/05/16 11:03
/*****************************\*数据结构之栈*IDE:VS2010\*****************************//** 栈的操作:*初始化、进栈、出栈(取栈顶元素)、栈是否非空*/#include <iostream>using namespace std;//定义顺序栈#define MAX_LEN 100typedef struct stack{int data[MAX_LEN];int top;}stack;//初始化,建个空栈svoid initStack(stack* s){s->top = -1;}//是否非空bool isEmpty(stack* s){return s->top==-1 ? true : false;}//进栈bool push(stack* s, int x){if(s->top == MAX_LEN-1)return false;s->top++;s->data[s->top] = x;return true;}//出栈(取栈顶元素)bool pop(stack* s, int* x){if(!isEmpty(s)){*x=s->data[s->top--];return true;}return false;}

0 0