栈的建立&操作(C语言)
来源:互联网 发布:mac开机黑屏logo不亮 编辑:程序博客网 时间:2024/06/07 19:30
#include "Stdio.h"
#define StackSize 100 /*定义储存空间大小*/
typedef char DataType;
/****************定义栈储存空间结构************/
typedef struct
{
DataType data[StackSize]; /**/
int top; /**/
}SqlStack;
/*****************生成栈*******************/
void Ininital(SqlStack* S)
{
S->top=-1;
}
/*****************判空****************/
int IsEmpty(SqlStack* S)
{
return S->top==-1;
}
/***************判满*****************/
int IsFull(SqlStack* S)
{
return S->top==StackSize-1;
}
/**************压栈*****************/
void Push(SqlStack* S,DataType ch)
{
if(IsFull(S))
{
printf("full");
exit(1);
}
S->data[++S->top]=ch;
}
/***************弹桟**************/
DataType Pop(SqlStack* S)
{
if(IsEmpty(S))
{
printf("empty");
exit(1);
}
return S->data[S->top--];
}
/****************取栈顶元素*****************/
DataType Top(SqlStack* S)
{
if(IsEmpty(S))
{
printf("empty");
}
return S->data[S->top];
}
/*************主函数******************/
int main(void)
{
DataType x,y;
SqlStack stack;
Ininital(&stack);
Push(&stack,'p');
Push(&stack,'u');
x=Top(&stack);
Pop(&stack);
y=Top(&stack);
Pop(&stack);
printf("%c",x);
printf("%c",y);
getch();
return 0;
}
/*************另一种做法(类表现形式)*********************/
#include "Stdio.h"
#define StackSize 100
typedef char DataType;
typedef struct
{ DataType data[StackSize];
int top;
void (*Initial)();
int (*IsEmpty)();
int (*IsFull)();
void (*Push)();
char (*Pop)();
char (*Top)();
}SqlStack;
void Initial(SqlStack *S)
{
S->top=-1;
}
int IsEmpty(SqlStack *S)
{
return S->top==-1;
}
int IsFull(SqlStack *S)
{
return S->top==StackSize-1;
}
void Push(SqlStack *S,DataType x)
{
if(IsFull(S))
{
printf("stack is full ,can't input it");
exit(1);
}
S->data[++S->top]=x;
}
DataType Pop(SqlStack *S)
{
if(IsEmpty(S))
{
printf("the stack is empty ,can't output anything");
}
return S->data[S->top--];
}
DataType Top(SqlStack *S)
{
if(IsEmpty(S))
{
printf("stack is null");
exit(1);
}
return S->data[S->top];
}
void start(SqlStack* number)
{
(*number).Initial=Initial;
(*number).IsEmpty=IsEmpty;
(*number).IsFull =IsFull ;
(*number).Pop=Pop ;
(*number).Top=Top ;
(*number).Push=Push ;
}
int main(void)
{
SqlStack S;
start(&S);
S.Initial(&S);
S.Push(&S,'p');
S.Push(&S,'u');
printf("%c",S.Top(&S));
S.Pop(&S);
printf("%c",S.Top(&S));
getch();
return 0;
}
- 栈的建立&操作(C语言)
- 二叉排序树的建立以及相关操作 C语言
- c语言建立单链表及基于单链表的操作
- c语言(逆序)建立链栈与栈的基本操作
- C语言链表操作(新增单向链表的逆序建立)
- c语言-单链表的建立
- C语言 堆栈的建立
- C语言树的建立
- 图的邻接矩阵的建立(C语言实现)
- C语言中栈的建立和应用
- 二叉树的建立与输出以及其他一些相关操作(递归算法实现) C语言
- 单向链表的建立(C语言)
- 数据结构之:常用数据结构的建立(C语言)
- 数据结构 栈的建立及入栈出栈操作 C语言版
- 对栈的各种操作的实现(C语言)
- 数据结构栈的基本的操作(C语言)
- 在Linux下用C语言操作数据库sqlite3(建立数据库)
- 在Linux下用C语言操作数据库sqlite3(建立数据库)
- 一个例子学懂搜索引擎(lucene)
- MyEclipse注册码生成
- 这是一个开发实例
- 每天一小步 iphone应用开发 -- 分离float后面无效的0
- HDU 2971(数论,构造矩阵+矩阵乘法优化)
- 栈的建立&操作(C语言)
- 关于使用UTF-8开发ASP网站
- Qt网络(二)HTTP编程
- 012 测试菜单1
- 矩阵乘法算法
- Qt网络(三)FTP(一)
- Qt网络(四)FTP(二)
- QScrollBar设置setStyleSheet方法
- Qt网络(五)获取本机网络信息