========================================================================================
栈 和 队 列
--------------------------------
顺序栈的各种基本操作
#include<iostream>
using namespace std;
#define StackSize 100
typedef char DataType;
typedef struct
{
DataType data[StackSize];
int top;
}SeqStack;
void Initial(SeqStack*S) // 栈 置 空
{
S->top=-1;
}
int IsEmpty (SeqStack *S) // 判断栈空
{
return S->top==-1;
}
int IsFull(SeqStack *S) // 判断栈满
{
return S->top==StackSize-1;
}
void push(SeqStack *S,DataType x) //近栈
{
if(IsFull(S))
{cout<<"栈上溢"; }
S->data[++S->top]=x;
}
DataType Pop(SeqStack *S) // 出栈
{
if(IsEmpty(S))
{cout<<"栈下溢";return 0;}
return S->data[S->top--];
}
DataType Top (SeqStack *S) // 取栈顶元素
{
if(IsEmpty(S))
{cout<<"栈空";return 0;}
return S->data[S->top];
}
int main()
{
SeqStack S;
DataType first,sec;
Initial(&S);
push(&S,'p');
push(&S,'o');
first=Top(&S);
Pop(&S);
sec=Top(&S);
cout<<first<<sec<<endl;
return 0;
}
---------------------------------------------------------
链栈的入出栈
#include<iostream>
using namespace std;
typedef char DataType;
typedef struct stacknode
{
DataType data;
struct stacknode*next;
//char *next;
} StackNode;
typedef struct
{
StackNode *top;
}LinkStack;
void Initial(LinkStack *S)
{
S->top=NULL;
}
int IsEmpty(LinkStack*S)
{
return S->top==NULL;
}
void Push(LinkStack *S,DataType x)
{
StackNode *p=new StackNode;
p->data=x;
p->next=S->top;
S->top=p;
}
DataType Pop(LinkStack *S)
{
DataType x;
StackNode *p=S->top;