对顺序栈实现如下功能:
void meau(); //菜单函数
void InitStack(Stack *st); //初始化栈
bool IsFull(Stack *st); //判断栈是否已满
bool IsEmpty(Stack *st); //判断栈是否为空
bool Push(Stack *st,ElemType x); //入栈
bool Pop(Stack *st,ElemType *x); //出栈
int lenth(Stack *st); //求栈的长度
bool GetTop(Stack *st,ElemType *x);<span style="white-space:pre"> </span>//获得栈顶元素
void ShowStack(Stack *st); //打印链表内容
void clear(Stack *st); //清空栈
void destory(Stack *st); //销毁栈
stack.h:
#ifndef __STACK_H__
#define __STACK_H__
#include<assert.h>//assert
#include<stdlib.h>//malloc
#include<iostream>//cin cout
using namespace std;
#define STACK_DEFAULT_SIZE 10//栈默认大小
typedef int ElemType;
typedef struct Stack
{
ElemType *base;
int top;
int capacity;
}Stack;
void meau(); //菜单函数
void InitStack(Stack *st); //初始化栈
bool IsFull(Stack *st); //判断栈是否已满
bool IsEmpty(Stack *st); //判断栈是否为空
bool Push(Stack *st,ElemType x); //入栈
bool Pop(Stack *st,ElemType *x); //出栈
int lenth(Stack *st); //求栈的长度
bool GetTop(Stack *st,ElemType *x); //获得栈顶元素
void ShowStack(Stack *st); //打印链表内容
void clear(Stack *st); //清空栈
void destory(Stack *st); //销毁栈
#endif
stack.cpp
#include"stack.h"
/*初始化栈*/
void InitStack(Stack *st)
{
ElemType *s = (ElemType *)malloc(sizeof(ElemType) * STACK_DEFAULT_SIZE);
assert(s != NULL);
st->base = s;
st->capacity = STACK_DEFAULT_SIZE;
st->top = 0;
}
/*判断栈是否已满*/
bool IsFull(Stack *st)
{
return st->top == st->capacity;
}
/*判断栈是否为空*/
bool IsEmpty(Stack *st)
{
return st->top == 0;
}
/*入栈*/
bool Push(Stack *st,ElemType x)
{
if(IsFull(st))
{
cout<<"the stack is full,can't push anymore!"<<endl;
return false;
}
st->base[st->top++] = x;
return true;
}
/*出栈*/
bool Pop(Stack *st,ElemType *x)
{
if(IsEmpty(st))
{
cout<<"the stack is empty!"<<endl;
return false;
}
*x = st->base[--st->top];
return true;
}
/*求栈的长度*/
int lenth(Stack *st)
{
return st->top;
}
/*获得栈顶元素*/
bool GetTop(Stack *st,ElemType *x)
{
if(IsEmpty(st))
{
cout<<"the stack is empty!"<<endl;
return false;
}
*x = st->base[--st->top];
return true;
}
/*打印链表内容*/
void ShowStack(Stack *st)
{
for(int i = st->top-1;i>=0;--i)
{
cout<<st->base[i]<<"--->";
}
cout<<"NULL"<<endl;
}
//清空栈
void clear(Stack *st)
{
st->top = 0;
st->capacity = 0;
}
//销毁栈
void destory(Stack *st)
{
clear(st);
free(st->base);
st->base = NULL;
}
//菜单函数
void meau()
{
cout<<"*********************SeqStack**********************"<<endl;
cout<<"* zyh_helen *"<<endl;
cout<<"* [1]Push [2]Pop [3]lenth [4]GetTop *"<<endl;
cout<<"* [5]ShowStack [6]clear *"<<endl;
}
#include"stack.h"
int main()
{
Stack st;
ElemType item;
int select = 1;
InitStack(&st);
while(select)
{
meau();
cout<<"please choose the num of function:"<<endl;
cin>>select;
switch(select)
{
case 1:
cout<<"input the item you want to input(-1 as a end)"<<endl;
while(cin>>item,item != -1)
{
Push(&st,item);
}
break;
case 2:
Pop(&st,&item);
cout<<item<<endl;
break;
case 3:
cout<<"the length of stack is:"<<lenth(&st)<<endl;
break;
case 4:
GetTop(&st,&item);
cout<<"the top item is:"<<item<<endl;
break;
case 5:
ShowStack(&st);
break;
case 6:
clear(&st);
break;
default:
break;
}
}
destory(&st);
return 0;
}
具体功能:望读者自行测试,如有错误欢迎提出修改意见----->>>zyh_helen