栈
来源:互联网 发布:杜克大学商学院知乎 编辑:程序博客网 时间:2024/05/20 19:31
栈:是一种只允许在一端进行插入与删除的特殊线性表,链式存储和顺序存储
特点:后进先出
顺序存储
#define max 5
typedef struct
{
datatyoe data[max];
int top;
}SeqStack;
SeqStack *init_SeqStack()
{
SeqStack *s=(SeqStack*)malloc(sizeof(SeqStack));
s->top=-1;
return s;
}
int empty(SeqStack *s)
{
if(s->top==-1) return 0;
else return 1;
}
int push(SeqStack *s,datatype x)
{
if(s->top==max-1) return 0;
else
{
s->top++;
s->data[s->top]=x;
return 1;
}
}
datatype pop_SeqStack(SeqStack *s)
{
datatype x;
if(empty(s)==0) return 0;
else
{
x=s->data[s->top];
s->top--;
return x;
}
}
datatype top_SeqStack(SeqStack *s)
{
if(empty(s)==0) return 0;
else
return s->data[s->top];
}
//多栈共享邻接空间
typedef struct
{
datatype stack[max];
int left,right;
}dupSeqStack;
int init_SeqStack()
{
dupSeqStack *s=(dupSeqStack*)malloc(sizeof(dupSeqStack));
s->right=max;//只能存储到max-1
s->left=-1;
return 1;
}
int push_dupSeqStack(dupSeqStack *s,datatype x,char status)
{
if(s->left+1==s->right) return 0;//栈满
if(status=='L')
{
s->left++;s->stack[s->left]==x; return 1;
}
else if(status=='R')
{
s->right--;s->stack[s->right]=x;return 1;
}
else return 0;
}
datatype pop_dupSeqStack(dupSeqStack *s,char status)
{
datatype x;
if(status=='L'&&s->left!=-1)
{
x=s->stack[s->left];s->left--;return x;
}
else if(status=='R'&&s->right!=max)
{
x=s->stack[s->right];s->right++;return x;
}
else return 0;
typedef struct
{
datatyoe data[max];
int top;
}SeqStack;
SeqStack *init_SeqStack()
{
SeqStack *s=(SeqStack*)malloc(sizeof(SeqStack));
s->top=-1;
return s;
}
int empty(SeqStack *s)
{
if(s->top==-1) return 0;
else return 1;
}
int push(SeqStack *s,datatype x)
{
if(s->top==max-1) return 0;
else
{
s->top++;
s->data[s->top]=x;
return 1;
}
}
datatype pop_SeqStack(SeqStack *s)
{
datatype x;
if(empty(s)==0) return 0;
else
{
x=s->data[s->top];
s->top--;
return x;
}
}
datatype top_SeqStack(SeqStack *s)
{
if(empty(s)==0) return 0;
else
return s->data[s->top];
}
//多栈共享邻接空间
typedef struct
{
datatype stack[max];
int left,right;
}dupSeqStack;
int init_SeqStack()
{
dupSeqStack *s=(dupSeqStack*)malloc(sizeof(dupSeqStack));
s->right=max;//只能存储到max-1
s->left=-1;
return 1;
}
int push_dupSeqStack(dupSeqStack *s,datatype x,char status)
{
if(s->left+1==s->right) return 0;//栈满
if(status=='L')
{
s->left++;s->stack[s->left]==x; return 1;
}
else if(status=='R')
{
s->right--;s->stack[s->right]=x;return 1;
}
else return 0;
}
datatype pop_dupSeqStack(dupSeqStack *s,char status)
{
datatype x;
if(status=='L'&&s->left!=-1)
{
x=s->stack[s->left];s->left--;return x;
}
else if(status=='R'&&s->right!=max)
{
x=s->stack[s->right];s->right++;return x;
}
else return 0;
}
链式存储
typedef struct stacknode
{
datatyoe data;
struct stacknode *next;
}SeqStack;
int empty(SeqStack *top)
{
if(top->next==NULL) return 0;
else return 1;
}
SeqStack *init_SeqStack()
{
SeqStack *top=(SeqStack*)malloc(sizeof(SeqStack));
top->next=NULL;
return top;
}
int empty(SeqStack *top)
{
if(top->next==NULL) return 0;
else return 1;
}
int push(SeqStack *top,datatype x)
{
SeqStack *s=(SeqStack*)malloc(sizeof(SeqStack));
if(s==NULL) return 0;
else
{
s->data=x;
s->next=top->next;top->next=s;
return 0;
}
}
datatype pop_SeqStack(SeqStack *top)
{
datatype x;
SeqStack *s=(SeqStack*)malloc(sizeof(SeqStack));
if(empty(top)==0) return 0;
else
{
s=top->next;top->next=s->next;
x=s->data;free(s);return x;
}
}
datatype top_SeqStack(SeqStack *top)
{
datatype x;
SeqStack *s=(SeqStack*)malloc(sizeof(SeqStack));
if(empty(top)==0) return 0;
else
{
s=top->next;x=s->data;
free(s);return x;
}
{
datatyoe data;
struct stacknode *next;
}SeqStack;
int empty(SeqStack *top)
{
if(top->next==NULL) return 0;
else return 1;
}
SeqStack *init_SeqStack()
{
SeqStack *top=(SeqStack*)malloc(sizeof(SeqStack));
top->next=NULL;
return top;
}
int empty(SeqStack *top)
{
if(top->next==NULL) return 0;
else return 1;
}
int push(SeqStack *top,datatype x)
{
SeqStack *s=(SeqStack*)malloc(sizeof(SeqStack));
if(s==NULL) return 0;
else
{
s->data=x;
s->next=top->next;top->next=s;
return 0;
}
}
datatype pop_SeqStack(SeqStack *top)
{
datatype x;
SeqStack *s=(SeqStack*)malloc(sizeof(SeqStack));
if(empty(top)==0) return 0;
else
{
s=top->next;top->next=s->next;
x=s->data;free(s);return x;
}
}
datatype top_SeqStack(SeqStack *top)
{
datatype x;
SeqStack *s=(SeqStack*)malloc(sizeof(SeqStack));
if(empty(top)==0) return 0;
else
{
s=top->next;x=s->data;
free(s);return x;
}
}
0 0