栈
定义
栈是受限的线性表,插入和删除只能在栈顶进行操作。
代码:
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status;
/*顺序栈*/
#define MAXSIZE 20
typedef int SElemType;
typedef struct _SqStack
{
SElemType data[MAXSIZE];
int top;
}SqStack;
/*进栈*/
Status Push(SqStack *s,SElemType e)
{
if(s->top==MAXSIZE-1)
{
return ERROR;
}
s->top++;
s->data[s->top]=e;
return OK;
}
/*出栈*/
Status Pop(SqStack *s,SElemType *e)
{
if(s->top==-1)
{
return ERROR;
}
*e=s->data[s->top];
s->top--;
return OK;
}
/*共享栈*/
typedef struct _SqDoubleStatck
{
SElemType data[MAXSIZE];
int top1;/*栈1指针*/
int top2;/*栈2指针*/
} SqDoubleStatck;
/*进栈*/
Status Push(SqDoubleStatck *s,SElemType e,int num)
{
if(s->top1+1==s->top2)
{
return ERROR;
}
if(num==1)
{
s->data[++s->top1]=e;
}
if(num==2)
{
s->data[++s->top2]=e;
}
return OK;
}
/*出栈*/
Status Push(SqDoubleStatck *s,SElemType *e,int num)
{
if(num==1)
{
if(s->top1==-1)
{
return ERROR;
}
*e=s->data[s->top1--];
}else if(num==2)
{
if(s->top2==MAXSIZE-1)
{
return ERROR;
}
*e=s->data[s->top2--];
}
return OK;
}
/*链表栈*/
typedef struct _StackNode
{
SElemType data;
struct _StackNode *next;
}StatckNode,*LinkListPtr;
typedef struct _LinkStack
{
LinkListPtr top;
int count;
}LinkStack;
/*进栈*/
Status Push(LinkStack *stack,SElemType e)
{
LinkListPtr p;
p=(LinkListPtr)malloc(sizeof(StatckNode));
p->data=e;
p->next=stack->top;
stack->top=p;
stack->count++;
return OK;
}
/*出栈*/
Status Push(LinkStack *stack,SElemType *e)
{
LinkListPtr p;
p=stack->top;
stack->top=p->next;
free(p);
stack->count--;
return OK;
}
1558

被折叠的 条评论
为什么被折叠?



