栈
栈是一种只允许在一端插入和删除结点的线性表,允许被插入和删除的一端称为栈顶,另一端则称为栈底
顺序栈
定义数据元素的结构体和栈的结构体
1 2 3 4 5 6 7 8 9 10 11
| #define MAX_LENGTH 100
typedef int ElemType;
typedef struct Stack { ElemType data[MAX_LENGTH]; int top; } Stack;
|
初始化栈
1 2 3 4 5 6 7 8 9
|
void initStack(Stack *stack) { stack->top = -1; }
|
判断栈是否为空
1 2 3 4 5 6 7 8
|
bool isEmpty(Stack stack) { return (stack.top == -1); }
|
数据入栈
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
bool push(Stack *stack, ElemType elemType) { if (stack->top == MAX_LENGTH - 1) return false;
stack->top++; stack->data[stack->top] = elemType; return true; }
|
数据出栈
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
bool pop(Stack *stack, ElemType *elemType) { if (stack->top == -1) return false;
*elemType = stack->data[stack->top]; stack->top--; return true; }
|
读取栈顶元素
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
bool getTop(Stack *stack, ElemType *elemType) { if (stack->top == -1) return false;
*elemType = stack->data[stack->top]; return true; }
|
共享栈
两个顺序栈共享一个一维数组的数据空间
定义数据元素的结构体和栈的结构体
1 2 3 4 5 6 7 8 9 10 11 12
| #define MAX_LENGTH 100
typedef int ElemType;
typedef struct ShareStack { ElemType data[MAX_LENGTH]; int top1; int top2; } ShareStack;
|
初始化栈
1 2 3 4 5 6 7 8
|
void initStack(ShareStack *stack) { stack->top1 = -1; stack->top2 = MAX_LENGTH; }
|
判断栈是否为空
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
bool Stack1Empty(ShareStack stack) { return (stack.top1 == -1); }
bool Stack2Empty(ShareStack stack) { return (stack.top1 == MAX_LENGTH); }
|
数据入栈
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
bool push1(ShareStack *stack, ElemType elemType) { if (stack->top2 - stack->top1 == 1) return false;
stack->top1++; stack->data[stack->top1] = elemType; return true; }
bool push2(ShareStack *stack, ElemType elemType) { if (stack->top2 - stack->top1 == 1) return false;
stack->top2--; stack->data[stack->top2] = elemType; return true; }
|
数据出栈
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
bool pop1(ShareStack *stack, ElemType *elemType) { if (stack->top1 == -1) return false;
*elemType = stack->data[stack->top1]; stack->top1--; return true; }
bool pop2(ShareStack *stack, ElemType *elemType) { if (stack->top2 == MAX_LENGTH) return false;
*elemType = stack->data[stack->top2]; stack->top2++; return true; }
|
读取栈顶元素
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
bool getTop1(ShareStack *stack, ElemType *elemType) { if (stack->top1 == -1) return false;
*elemType = stack->data[stack->top1]; return true; }
bool getTop2(ShareStack *stack, ElemType *elemType) { if (stack->top2 == MAX_LENGTH) return false;
*elemType = stack->data[stack->top2]; return true; }
|
队列
顺序储存结构
定义数据元素的结构体和栈的结构体
1 2 3 4 5 6 7 8 9 10
| #define MAX_LENGTH 100
typedef int ElemType;
typedef struct Queue { ElemType data[MAX_LENGTH]; int front, rear; } Queue;
|
初始化队列
1 2 3 4
| void init(Queue *queue) { queue->front = 0; queue->rear = 0; }
|
判断指定队列是否为空队
1 2 3
| bool isEmpty(Queue queue) { return queue.front == queue.rear; }
|
数据入队
1 2 3 4 5 6 7 8 9 10
| bool enQueue(Queue *queue, ElemType elem) { if ((queue->rear + 1) % MAX_LENGTH == queue->front) return false;
queue->data[queue->rear] = elem; queue->rear = (queue->rear + 1) % MAX_LENGTH; return true; }
|
数据出队
1 2 3 4 5 6 7 8 9 10
| bool deQueue(Queue *queue, ElemType *elem) { if (queue->front == queue->rear) return false;
*elem = queue->data[queue->front]; queue->front = (queue->front + 1) % MAX_LENGTH; return true; }
|
链式储存结构
带头指针的链式储存结构队列,链式储存结构的队列不限长度存入数据
定义数据元素的结构体和栈的结构体
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| typedef int ElemType;
typedef struct Node { ElemType data; struct Node *next; } Node;
typedef struct { Node *front; Node *rear; } LinkQueue;
|
初始化队列
1 2 3 4 5 6
| void init(LinkQueue *linkQueue) { linkQueue->front = (Node*)malloc(sizeof(Node)); linkQueue->rear = linkQueue->front;
linkQueue->front->next = NULL; }
|
判断队列是否为空
1 2 3
| bool isEmpty(LinkQueue linkQueue) { return linkQueue.front == linkQueue.rear; }
|
数据入队
1 2 3 4 5 6 7 8 9
| void enQueue(LinkQueue *linkQueue, ElemType elem) { Node *newNode = (Node*)malloc(sizeof(Node)); newNode->data = elem; newNode->next = NULL;
linkQueue->rear->next = newNode; linkQueue->rear = newNode; }
|
数据出队
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| bool deQueue(LinkQueue *linkQueue, ElemType *elem) { if (linkQueue->rear == linkQueue->front) return false;
Node *temp = linkQueue->front->next; *elem = temp->data;
linkQueue->front->next = temp->next;
if (linkQueue->rear == temp) linkQueue->rear = linkQueue->front;
free(temp);
return true; }
|