栈:
0 Maxsize-1
bottom top 栈顶可以操作,栈底不可以
性质:先进后出
空栈:不含任何元素的栈,top=-1(有一个元素,top=0)
栈的结构:typedef struct SqStack{
int data[Maxsize];
int top;
} STACK;
进栈: int Push(STACK *S,int e)
{
if(S->top==Maxsize-1)
return ERROR;
S->top++;
S->data[S->top]=e;
return OK;
}
出栈: int Pop(STACK *S,int *e)
{
if(S->top==-1)
return ERROR;
*e=S->data[S->top];
S->top--;
return OK;
}
共享栈:typedef struct {
int data[Maxsize];
int top1;int top2;
}SqDoubleStack;
满栈:top1+1=top2
链栈:typedef struct StackNode{
int data;
Struct StackNode *next;
}StackNode,*LinkStackPtr;
typedef struct LinkStack{
LinkStackPtr top;
int count;
}LinkStack;
链栈的出栈S->top=S->top->next;
入栈p->next=S->top;
S->top=p;
队列:
性质:先进先出
队头:允许删除的一端 Front
队尾:允许插入的一端 Rear
队列定义:typedef struct{
int data[Maxsize];
int front;
int rear;
}SqQueue;
初始化空队列 :
int InitQueue(SqQueue *Q)
{
Q->front=0;
Q->rear=0;
return OK;
}
循环队列(入队)
int EnQueue(SqQueue *Q,int e)
{
if((Q->rear+1)%Maxsize==Q->front)
return ERROR;
Q->data[Q->rear]=e;
Q->rear=(Q->rear+1)%Maxsize;
return OK;
}
循环队列(出队)
int DeQueue(SqQueue *Q,int *e)
{
if(Q->rear==Q->front)
return ERROR;
*e=Q->data[Q->front];
Q->front=(Q->front+1)%Maxsize;
return OK;
}
求队列长度:int QueueLength(SqQueue Q)
{
return (Q->rear-Q->front+Maxsize)%Maxsize;
}