来源:互联网 发布:恒生数据 编辑:程序博客网 时间:2024/05/30 19:34
////本程序中的链式栈不带头节点
#include "common.h"


void creat_cstack(cstacklink *cs)
{
*cs = NULL;
}
void clear_cstack(cstacklink *cs)
{
cstacklink p = NULL;
while(*cs)
{
p = *cs;
*cs = (*cs)->next;
free(p);
p = NULL;
}
}
int empty_cstack(cstacklink cs)
{
return cs == NULL ? TRUE : FALSE;
}
void push_cstack(cstacklink *cs, datatype x)
{
cstacklink q = (cstacklink)malloc(sizeof(cstack));
q->data = x;
q->next = NULL;

q->next = *cs;
*cs = q;
}
void pop_cstack(cstacklink *cs)
{
if(empty_cstack(*cs))
{
printf("cstack is empty!\n");
return;
}
cstacklink q = *cs;
*cs = (*cs)->next;
free(q);
q = NULL;
}
int get_top_cstack(cstacklink cs, datatype *ret)
{
if(empty_cstack(cs))
{
printf("cstack is empty!\n");
return FALSE;
}
*ret = cs->data;
return TRUE;
}
void display_cstack(cstacklink cs)
{
cstacklink p = cs;
while(p)
{
printf("%d,",p->data);
p = p->next;
}
putchar('\n');

}


//通过数组实现

#include "common.h"


void creat_sqstack(sqstacklink *sq)
{
*sq = (sqstacklink)malloc(sizeof(sqstack));
(*sq)->top = -1;
}
void clear_sqstack(sqstacklink sq)
{
sq->top = -1;
}
int empty_sqstack(sqstacklink sq)
{
return sq->top == -1 ? TRUE : FALSE;
}
int full_sqstack(sqstacklink sq)
{
return sq->top == MAXSIZE-1 ? TRUE : FALSE;
}
void push_sqstack(sqstacklink sq, datatype x)
{
if(full_sqstack(sq))
{
printf("stack is full!\n");
return;
}
sq->top++;
sq->data[sq->top] = x;
}
void pop_sqstack(sqstacklink sq)
{
if(empty_sqstack(sq))
{
printf("stack is empty!\n");
return;
}
sq->top--;
}
int get_top_sqstack(sqstacklink sq, datatype *ret)//取栈顶
{
if(empty_sqstack(sq))
{
printf("stack is empty!\n");
return FALSE;
}
*ret = sq->data[sq->top];
return TRUE;
}
void display_sqstack(sqstacklink sq)
{
int i=0;
while(i <= sq->top)
{
printf("%d,",sq->data[i]);
i++;
}
putchar('\n');
}

原创粉丝点击