栈
程序员文章站
2022-05-20 13:15:51
...
链栈代码
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
typedef char SElemType;
typedef struct SqStack {
SElemType data;
SqStack *next;
}SqStack, *StackPre;
typedef struct {
StackPre base;
StackPre top;
}StackPtr;
int Initstack(StackPtr &s){//初始化栈
s.base = new SqStack;
if( s.base == NULL )
{
printf("初始化失败,内存空间不足\n");
exit(0);
}
s.top = s.base;
s.top->next = 0;
s.base->next = NULL;
return 1;
}
int DestoryStack(StackPtr &s)//销毁
{
StackPre p;
p = s.top;
if( s.top != s.base )
{
s.top = s.top->next;
free(p);
p = s.top;
}
free(s.top);
s.top = s.base = p = NULL;
return 1;
}
int ClearStack(StackPtr &s)//清空
{
if( s.top == s.base )
{
return 0;
}
StackPre p;
p = s.top;
while( s.top != s.base )
{
s.top = s.top->next;
free(p);
p = s.top;
}
p = NULL;
return 1;
}
int LenStack(StackPtr s)//栈长
{
int i=0;
while(s.base != s.top)
{
i++;
s.top = s.top->next;
}
return i;
}
int StackEmpty(StackPtr s)//判断栈是否为空
{
if(s.base == s.top)
return 1;
else
return 0;
}
int GetTop(StackPtr s, SElemType &e)//栈顶元素
{
if(s.base == s.top)
return 0;
e = s.top->data;
return 1;
}
int Push(StackPtr &s, SElemType e)//入栈
{
StackPre p;
p = new SqStack;
if( p == NULL )
{
printf("入栈失败,内存空间不足\n");
exit(0);
}
p->data = e;
p->next = s.top;
s.top = p;
return 1;
}
int Pop(StackPtr &s, SElemType &e)//出栈,弹出栈顶
{
if(s.base == s.top)
return 0;
StackPre p;
e = s.top->data;
p = s.top;
s.top = s.top->next;
free(p);
p = NULL;
return 1;
}
int StackShow(StackPtr s)//显示栈
{
if(s.top == s.base)
{
printf("栈为空,无元素\n");
return 0;
}
StackPre p;
p = s.top;
printf("栈内元素为:");
while( p != s.base)
{
printf("%c ",p->data);
p = p->next;
}
printf("\n");
}
void help()
{
cout<<"1~~~~~~初始化栈"<<endl;
cout<<"2~~~~~~销毁栈"<<endl;
cout<<"3~~~~~~清空栈"<<endl;
cout<<"4~~~~~~栈的长度"<<endl;
cout<<"5~~~~~~栈是否为空"<<endl;
cout<<"6~~~~~~获取栈顶元素"<<endl;
cout<<"7~~~~~~入栈"<<endl;
cout<<"8~~~~~~出栈"<<endl;
cout<<"9~~~~~~显示栈"<<endl;
cout<<" ~~~~~输入0或负数退出!"<<endl;
}
int main(){
int operator_code;
StackPtr s;
help();
SElemType c;
while(1)
{
cin>>operator_code;
if(operator_code == 1)
{
if(Initstack(s))
printf("初始化成功\n");
}
if(operator_code == 2)
{
if(DestoryStack(s))
printf("销毁成功\n");
}
if(operator_code == 3)
{
if(ClearStack(s))
printf("清空成功");
else
printf("栈是空的\n");
}
if(operator_code == 4)
{
int len = LenStack(s);
printf("栈长为:%d\n", len);
}
if(operator_code == 5)
{
if(StackEmpty(s))
printf("栈空\n");
else
printf("栈非空\n");
}
if(operator_code == 6)
{
if(GetTop(s, c))
printf("栈顶元素为:%c\n", c);
else
printf("栈空,无栈顶元素\n");
}
if(operator_code == 7)
{
cout << "请输入你要入栈的元素: ";
cin >> c;
if(Push(s, c))
printf("入栈成功\n");
}
if(operator_code == 8)
{
if(Pop(s, c))
cout<<"弹出成功,栈顶元素为:"<<c<<endl;
else
cout<<"栈为空,无元素"<<endl;
}
if(operator_code == 9)
{
StackShow(s);
}
if(operator_code <=0)
break;
}
return 0;
}
顺序栈代码
#include <iostream>
#include<stdlib.h>
#include<math.h>
#include<stdio.h>
using namespace std;
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OK 1
#define STACK_INIT_SIZE 20
#define STACKINCREMENT 10
typedef char ElemType;
typedef char ElemType;
typedef struct{
ElemType *base;
ElemType *top;
int sizeStack;
}sqStack;
int InitStack(sqStack *s)//初始化一个栈
{
s->base = (ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType));
if( !s->base)
{
exit(0);
return 0;
}
s->top = s->base;
s->sizeStack = STACK_INIT_SIZE;
return 1;
}
void DestoryStack(sqStack &s)//销毁栈
{
free(s.base);
s.base = NULL;
s.top = NULL;
s.sizeStack = 0;
printf("栈已销毁\n");
}
void ClearStack(sqStack &s)//清空栈
{
s.top = s.base;
}
int Push(sqStack *s,ElemType e)//压入栈中一个元素
{
if(s->top-s->base == s->sizeStack)
{
s->base = (ElemType *)realloc(s->base,(s->sizeStack+STACKINCREMENT)*sizeof(ElemType));
if(!s->base)
exit(0);
return 0;
}
*(s->top) = e;
s->top++;
return 1;
}
int StackEmpty(sqStack s)//判断栈是否为空
{
if(s.top == s.base)
return 1;
else
return 0;
}
int GetTop(sqStack s, ElemType &e)//获得栈顶元素
{
if(s.top == s.base)
return ERROR;
else
e = *(s.top-1);
return 1;
}
int Pop(sqStack *s, ElemType *e)//弹出栈顶元素并取得这个值
{
if(s->top == s->base)
return 0;
*e = *--(s->top);
}
int Stacklen(sqStack s)//栈的长度
{
return (s.top - s.base);
}
int StackVisit(sqStack s)//显示栈
{
if( s.base == s.top )
{
printf("栈为空,无元素\n");
return 0;
}
printf("栈内元素有: ");
while(s.base != s.top)
{
printf("%c ",*s.base);
s.base++;
}
printf("\n");
return 1;
}
void help(){
cout<<"1~~~~~~~初始化栈"<<endl;
cout<<"2~~~~~~~清空栈"<<endl;
cout<<"3~~~~~~~销毁栈"<<endl;
cout<<"4~~~~~~~栈的长度"<<endl;
cout<<"5~~~~~~~判断栈是否为空栈"<<endl;
cout<<"6~~~~~~~压入栈中一个元素"<<endl;
cout<<"7~~~~~~~获得栈顶元素"<<endl;
cout<<"8~~~~~~~弹出栈顶元素"<<endl;
cout<<"9~~~~~~~显示栈"<<endl;
cout<<" 输入小于等于0的数退出"<<endl;
}
int main()
{
sqStack s;
help();
int operate_code,len;
char c;
while(1)
{
printf("请输入操作: \n");
cin >> operate_code;
if(operate_code == 1)
{
if( InitStack(&s) )
{
printf("初始化栈成功\n");
}
else
printf("内存不够,初始化失败\n");
}
if(operate_code == 2)
{
ClearStack(s);
printf("已清空栈\n");
}
if(operate_code == 3)
{
DestoryStack(s);
}
if(operate_code == 4)
{
len = Stacklen(s);
printf("栈的长度为:%d\n",len);
}
if(operate_code == 5)
{
if(StackEmpty(s))
printf("栈为空\n");
else
printf("栈不为空\n");
}
if(operate_code == 6)
{
printf("请输入你想压入栈的字符:");
cin >> c;
if(Push(&s,c))
printf("插入成功\n");
else
printf("插入失败,内存分配失败\n");
}
if(operate_code == 7)
{
if( GetTop(s,c) )
printf("栈顶元素为:%c\n",c);
else
printf("这个栈为空\n");
}
if(operate_code == 8)
{
if( Pop(&s, &c) )
printf("弹出成功,栈顶元素为:%c\n",c);
else
printf("这个栈为空\n");
}
if(operate_code == 9)
{
StackVisit(s);
}
if(operate_code <= 0)
{
break;
}
}
return 0;
}