首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

用数组兑现栈的基本操作

2012-10-08 
用数组实现栈的基本操作/*用数组(顺序表)实现栈的常用操作*/#includestdio.h#include stdlib.h//定义

用数组实现栈的基本操作

/*  用数组(顺序表)实现栈的常用操作*/#include<stdio.h>#include <stdlib.h>//定义结构体typedef struct Node{      int data[20]; int top;} SeqStack;//函数操作声明void SeqStackInit(SeqStack);//栈的初始化bool SeqStackInitEmpty(SeqStack);//判断栈是否为空void SeqStackPush(SeqStack,int);//入栈操作int SeqStackPop(SeqStack);//出栈操作int SeqStackGetPop(SeqStack);//取栈顶元素int main(void){SeqStack s;//声明出结构体变量 SeqStackInit( s);   return 0;}//初始化实现void SeqStackInit(SeqStack s){    s.top=-1;//如果空栈 声明-1然后++}//判断栈是否为空bool SeqStackInitEmpty(SeqStack s){      if(s.top==-1)  return true;  else  return false;}//入栈操作void SeqStackPush(SeqStack s ,int val){if(20-1==s.top){   printf("栈已满");   exit(-1);}else {  s.data[++s.top]=val;}}//出栈操作int SeqStackPop(SeqStack s){if(SeqStackInitEmpty(s)){   printf("栈为空");   exit(-1);}   int val=s.data[s.top];   s.top--;   return val;  }//取栈顶元素int SeqStackGetPop(SeqStack s){   if(SeqStackInitEmpty(s))   {     printf("空栈"); exit(-1);   }   return s.data[s.top];}
?

热点排行