一.栈
(一)卡特兰数
n个不同元素进栈,出栈元素不同排列的个数为
1
n
+
1
\frac{1}{n+1}
n+11
C
2
n
n
C^{n}_{2n}
C2nn
(二)栈的顺序存储
1.初始化
#define MaxSize 10
typedef struct {
ElemType data[MaxSize];
int top;//栈顶指针,指向栈顶元素。记录数组下标,从0开始
}SqStack;
void InitStack(SqStack &S) {//初始化
S.top = -1;//不唯一
}
void testStack() {
SqStack S;
InitStack(S);
}
2.判空
bool StackEmpty(SqStack S) {//判空
if (S.top == -1)//由top的值决定
return true;
else
return false;
}
3.进栈操作
bool Push(SqStack &S, ElemType x) {
if (S.top == MaxSize - 1)//判满,由top的值决定
return false;
S.top = S.top + 1;//top初始为-1
S.data[S.top] = x;
return true;
}
4.出栈操作
bool Pop(SqStack &S, ElemType &x) {
if (S.top == -1)
return false;
x = S.data[S.top];
S.top = S.top - 1;
return true;
}
(三)共享栈
#define MaxSize 10
typedef struct {
ElemType data[MaxSize];
int top0;
int top1;
}ShStack;
void InitStack(ShStack &S) {
S.top0 = -1;
S.top1 = MaxSize;
}
在此情况下,判断栈满:top0+1==top1
(四)链栈(栈的链式存储)
与单链表在头结点后的操作类似
typedef struct Linknode {
ElemType data;
struct Linknode *next;
}*LiStack;
二.队列
(一)顺序实现
1.顺序存储
#define MaxSize 10
typedef struct {//定义
Elemtype data[MaxSize];//存放元素
int front, rear;//头尾指针
}SqQueue;
void InitQueue(SqQueue &Q) {//初始化
Q.rear = Q.front = 0;
}
bool QueueEmpty(SqQueue Q) {//判空
if (Q.rear == Q.front)
return true;
}
2.循环队列
(1)队满:(Q.rear + 1) % MaxSize == Q.front
(2)队空:Q.rear == Q.front
(3)队列中元素个数:(Q.rear-Q.front+MaxSize)%MaxSize
[例]已知循环队列的存储空间为数组A[21],front 指向队头元素的前一个位置,rear指向队尾元素,假设当前front和rear的值分别为8和3,则该队列的长度为(3+21-8)%21=16
(4)基本操作
void InitQueue(SqQueue &Q) {//初始化
Q.rear = Q.front = 0;
}
bool QueueEmpty(SqQueue Q) {//判空
if (Q.rear == Q.front)
return true;
}
bool EnQueue(SqQueue &Q, ElemType x) {//入队
if ((Q.rear + 1) % MaxSize == Q.front)//队满
return false;
Q.data[Q.rear] = x;//(rear指向队尾结点的下一个位置)
Q.rear = (Q.rear + 1)%MaxSize;
return true;
}
bool DeQueue(SqQueue &Q,ElemType &x){//出队
if (Q.rear == Q.front)
return false;
x = Q.data[Q.front];
Q.front = (Q.front + 1) % MaxSize;
return true;
}
(二)链式实现
typedef struct LinkNode {//链式队列结点
ElemType data;
struct LinkNode *next;
}LinkNode;
typedef struct {//队列
LinkNode *front,*rear;//头指针和尾指针
}LinkQueue;
1.带头结点的操作
链式队列为空:Q.front == Q.rear
不存在队列满的情况
void InitQueue(LinkQueue& Q) {//初始化
Q.front = Q.rear = (LinkNode*)malloc(sizeof(LinkNode));
Q.front->next = NULL;
}
void EnQueue(LinkQueue &Q,ElemType x){//入队
LinkNode *s = (LinkNode*)malloc(sizeof(LinkNode));
s->data = x;
s->next = NULL;
Q.rear->next = s;
Q.rear = s;
}
bool DeQueue(LinkQueue &Q,ElemType &x){//出队
if (Q.front == Q.rear)//判空
return false;
LinkNode *p=Q.front->next;
x = p->data;
Q.front->next = p->next;
if (Q.rear == p)
Q.rear = Q.front;
free(p);
return true;
}
2.不带头结点
链式队列为空:Q.front ==NULL 且 Q.rear ==NULL
不存在队列满的情况
void InitQueue(LinkQueue &Q){//初始化
Q.front = NULL;
Q.rear = NULL;
}
void EnQueue(LinkQueue &Q,ElemType x){//入队
LinkNode* s = (LinkNode*)malloc(sizeof(LinkNode));
s->data = x;
s->next = NULL;
if (Q.front == NULL) {
Q.front = s;
Q.rear = s;
}
else {
Q.rear->next = s;
Q.rear = s;
}
}
bool DeQueue(LinkQueue &Q, ElemType &x) {//出队
if (Q.front == NULL)
return false;
LinkNode *p = Q.front;
p->data = x;
Q.front = p->next;
if (Q.rear = p) {
Q.rear = NULL;
Q.front = NULL;
}
free(p);
return true;
}