#include <stdio.h>
#define SIZE sizeof(LINKSTACK)
#define PRINT_ITEM "%d " //定义输出项的宏
#define SCANF_ITEM "%d" //定义输入项的宏
typedef int bool; //定义bool类型
const bool true = 1; //定义bool类型true常量
const bool false = 0; //定义bool类型false常量
typedef int ELEMTYPE; //定义元素类型
typedef struct snode //定义链栈
{
ELEMTYPE data;
struct snode *next;
} LINKSTACK;
const ELEMTYPE STACK_NULL = -9999;//定义常量栈为NULL的值
LINKSTACK * INITSTACK(); //初始化栈
LINKSTACK * CREATESTACK(ELEMTYPE); //创建链栈
LINKSTACK * PUSH(LINKSTACK *,ELEMTYPE);//进栈操作
ELEMTYPE GETTOP(LINKSTACK *);//获取栈顶元素
LINKSTACK * POP(LINKSTACK *);//出栈操作
bool EMPTY(LINKSTACK *);//判断栈是否为NULL
LINKSTACK * CLEAR(LINKSTACK *); //清空链栈
int CURRENT_SIZE(LINKSTACK *); //当前链栈中的元素个数
void PRINT_STACK(LINKSTACK *);//打印链栈元素
void menu(); //菜单
void print_enter(); //打印回车
void print_tab(); //打印tab
void print_menu_item(char *,char *); //打印菜单中的每一项
void print_str(char *);
void print_info(char *,bool); //打印信息,bool意思是是否打印回车
ELEMTYPE get_input();//获取压栈的数据
LINKSTACK * stack_push(LINKSTACK *);//压入链栈
LINKSTACK * stack_pop(LINKSTACK *);//出栈
LINKSTACK * stack_get_top(LINKSTACK *); //获取栈顶元素
LINKSTACK * stack_empty(LINKSTACK *); //判断栈是否为空
LINKSTACK * stack_cur_size(LINKSTACK *);//获取栈中的元素个数
LINKSTACK * stack_clear(LINKSTACK *);//清空栈中元素
LINKSTACK * link_stack_method(LINKSTACK *,LINKSTACK * (* fun)(LINKSTACK *)); //定义函数指针
int main(int argc,char *argv[])
{
LINKSTACK * top = NULL;
int select_menu_value;
menu();
do
{
print_info("select menu:",false);
scanf("%d",&select_menu_value);
switch(select_menu_value)
{
case 0:
menu();
break;
case -1:
break;
case 1:
top = link_stack_method(top,stack_push);
break;
case 2:
top = link_stack_method(top,stack_pop);
break;
case 3:
top = link_stack_method(top,stack_get_top);
break;
case 4:
top = link_stack_method(top,stack_empty);
break;
case 5:
top = link_stack_method(top,stack_cur_size);
break;
case 6:
top = link_stack_method(top,stack_clear);
break;
default:
menu();
break;
}
}while(-1 != select_menu_value);
return 0;
}
LINKSTACK * INITSTACK()
{
LINKSTACK * stack = malloc(SIZE);
return stack;
}
LINKSTACK * CREATESTACK(ELEMTYPE data)
{
LINKSTACK * head = INITSTACK();
head->data = data;
return head;
}
LINKSTACK * PUSH(LINKSTACK * top,ELEMTYPE data)
{
LINKSTACK *p = CREATESTACK(data);
if (NULL == top) //当栈为NULL,则将当前的数据存放在栈底
{
p->next = NULL;
}
else
{
p->next = top;
}
top = p;
return top;
}
ELEMTYPE GETTOP(LINKSTACK * top)
{
ELEMTYPE result = STACK_NULL;
if (top != NULL)
{
result = top->data;
}
return result;
}
LINKSTACK * POP(LINKSTACK * top)
{
LINKSTACK * del;
if (! EMPTY(top))
{
del = top;
top = top->next;
free(del); //释放内存
}
return top;
}
bool EMPTY(LINKSTACK * top)
{
bool result = false;
if (top == NULL)
{
result = true;
}
return result;
}
LINKSTACK * CLEAR(LINKSTACK * top)
{
LINKSTACK * del;
if (! EMPTY(top))
{
del = top;
top = top->next;
free(del);
top = CLEAR(top); //使用递归进行将栈置空操作
}
return top;
}
int CURRENT_SIZE(LINKSTACK * top)
{
int result = 0;
while (top != NULL)
{
result ++;
top = top->next;
}
return result;
}
void PRINT_STACK(LINKSTACK * top)
{
while (top != NULL)
{
printf(PRINT_ITEM,top->data);
top = top->next;
}
}
void menu()
{
print_enter();
print_tab();print_info("link stack manage system",true);print_enter();
print_menu_item("1","push a element");
print_menu_item("2","pop top element");
print_menu_item("3","get top element");
print_menu_item("4","judge stack is empty");
print_menu_item("5","stack element count");
print_menu_item("6","clear stack");
print_menu_item("0","return menu");
print_menu_item("-1","exit system");
}
void print_enter()
{
printf("\n");
}
void print_tab()
{
printf("\t");
}
void print_menu_item(char * item,char * desc)
{
print_tab();
print_str(item);
print_tab();
print_str(desc);
print_enter();
}
void print_str(char *str)
{
while(*str)
{
printf("%c",*str++);
}
}
void print_info(char * str,bool enter)
{
print_tab();
print_str(str);
if (enter)
{
print_enter();
}
}
ELEMTYPE get_input()
{
ELEMTYPE result;
print_info("input a value:",false);
scanf(SCANF_ITEM,&result);
return result;
}
LINKSTACK * stack_push(LINKSTACK * top)
{
ELEMTYPE data = get_input();
top = PUSH(top,data);
print_info("push success.",true);
return top;
}
LINKSTACK * stack_pop(LINKSTACK * top)
{
top = POP(top);
print_info("pop success.",true);
return top;
}
LINKSTACK * stack_get_top(LINKSTACK * top)
{
ELEMTYPE result = GETTOP(top);
print_tab();
if (result != STACK_NULL)
{
printf("top element is ");
printf(PRINT_ITEM,result);
}
else
{
printf("error:the stack is null.");
}
print_enter();
return top;
}
LINKSTACK * stack_empty(LINKSTACK * top)
{
bool result = EMPTY(top);
if (! result)
{
print_info("stack is not empty.",true);
}
else
{
print_info("stack is empty.",true);
}
return top;
}
LINKSTACK * stack_cur_size(LINKSTACK * top)
{
int result = CURRENT_SIZE(top);
print_tab();
printf("all count is %d ",result);
print_enter();
return top;
}
LINKSTACK * stack_clear(LINKSTACK * top)
{
top = CLEAR(top);
print_info("clear stack is success.",true);
return top;
}
LINKSTACK * link_stack_method(LINKSTACK * top,LINKSTACK * (* fun)(LINKSTACK * src_top))
{
return (* fun)(top);
}
|