栈:只能在一端进行插入或删除的线性表,这一端称为栈顶,另一端称为栈底。栈的特点是 先进后出(FILO),就像一个车队开进了一个死胡同,最先开进的车只有等后边的车全部出去,它才能出去。
队列:它是一种操作受限的线性表,限制为仅允许在一端插入,另一端删除,插入的一端称为队尾,删除的一端称为队头。他的特点为 先进先出(FIFO)。
typedef struct
{
int data[maxSize];
int top;
}SqStack;
typedef struct LNode
{
int data;//数据域
struct LNode *next;//指针域
}LNode;
typedef struct
{
int data[maxSize];
int front;
int rear;
}SqQueue;
//队结点类型
typedef struct QNode
{
int data;
struct QNode *next;
}QNode;
//链队类型
typedef struct
{
QNode *front;
QNode *rear;
}LiQueue;
//初始化栈
void initStack(SqStack &st)
{
st.top = -1;
}
//判断栈空
int isEmpty(SqStack st)
{
if(st.top == -1)
return 1;
else
return 0;
}
//进栈
int push(SqStack &st,int x)
{
if(st.top == maxSize-1)//栈满了,不能进栈
return 0;
++(st.top);
st.data[st.top] = x;
return 1;
}
//出栈
int pop(SqStack &st,int &x)
{
if(st.top == -1)//栈空不能出栈
return 0;
x = st.data[st.top];
--(st.top);
return 1;
}
//实际使用时的简单写法
int stack[maxSize];
int top = -1;
stack[++top] = x;
x = stack[top--];
//初始化
void initStack(LNode *&lst)
{
lst = (LNode*)malloc(sizeof(LNode));//制造一个头结点
lst->next = NULL;
}
//判断栈空
int isEmpty(LNode *lst)
{
if(lst -> next == NULL)
return 1;
else
return 0;
}
//进栈
void push(LNode *lst,int x)
{
LNode *p;
p = (LNode*)malloc(sizeof(LNode));
p -> next = NULL;
p->data=x;
p->next = lst->next;
lst->next=p;
}
//出栈
int pop(LNode *lst,int &x)
{
LNode *p;
if(lst->next == NULL)
return 0;
p=lst->next;
x=p->data;
lst->next=p->next;
free(p);
return 1;
}


//初始化队列
void initQueue(SqQueue &qu)
{
qu.front = qu.rear = 0;
}
//判断队空
int isQueueEmpty(SqQueue qu)
{
if(qu.front == qu.rear)
return 1;
else
return 0;
}
//进队
int enQueue(SqQueue &qu,int x)
{
if((qu.rear+1)%maxSize == qu.front)
return 0;
qu.rear = (qu.rear+1)%maxSize;
qu.data[qu.rear] = x;
return 1;
}
//出队
int deQueue(SqQueue &qu,int &x)
{
if(qu.front == qu.rear)
return 0;
qu.front = (qu.front+1) % maxSize;
x = qu.data[qu.front];
return 1;
}

