#include <iostream>
#include <malloc.h>
#include <stdlib.h>
#define OK 0
#define OVER -1
#define ERROR -2
using namespace std;
typedef int elem_type;
typedef int function_type;
typedef struct stack
{
elem_type data;
struct stack *back;
}Stack;
/*********************************************
功能:构造一个栈并将栈初始化
*********************************************/
Stack* Init_Stack(void)
{
Stack *top = (Stack *)malloc(sizeof(Stack));
if( top == NULL )
exit(OVER);
top->back = NULL;
return top;
}
/*********************************************
功能:判断栈是否为空
*********************************************/
bool Empty_Stack(Stack *top)
{
if( top->back == NULL)
return true;
else
return false;
}
/*********************************************
功能:获取栈的长度
*********************************************/
function_type Len_Stack(Stack *top)
{
int len = 0;
top = top->back;
while(top)
{
++len;
top = top->back;
}
return len;
}
/*********************************************
功能:将栈销毁
*********************************************/
function_type Clear_Stack(Stack *top)
{
Stack *temp = NULL;
while(top)
{
temp = top->back;
free(top);
top = temp;
}
return OK;
}
/*********************************************
功能:获取栈顶元素
*********************************************/
function_type Get_Stack(Stack *top,elem_type *e)
{
if( Empty_Stack(top) )
exit(OVER);
*e = top->back->data;
return OK;
}
/*********************************************
功能:删除栈顶元素
*********************************************/
function_type Pop_Stack(Stack *top,elem_type *e)
{
if( Empty_Stack(top) )
exit(OVER);
Stack *temp = top->back;
*e = temp->data;
top->back = temp->back;
free(temp);
return OK;
}
/*********************************************
功能:增加栈顶元素
*********************************************/
function_type Push_Stack(Stack *top,elem_type e)
{
Stack *temp = (Stack *)malloc(sizeof(Stack));
if( temp == NULL )
exit(OVER);
temp->data = e;
temp->back = top->back;
top->back = temp;
return OK;
}
/*********************************************
功能:输出栈中的所有数据
*********************************************/
function_type Printf_Stack(Stack *top)
{
if( Empty_type(top) )
exit(OVER);
top = top->back;
while(top)
{
cout<<setw(5)<<top->data;
top = top->back;
}
cout<<endl;
return OK;
}
int main(int,char **)
{
return OK;
}
数据结构_链栈
最新推荐文章于 2020-10-14 18:25:00 发布