栈和队列的基本实现

栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端 称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出的原则。 

栈的实现:一般可以使用数组和链表存储的方式实现。

队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出的特性。队列:进行插入操作的一端称为队尾 出队列:进行删除操作的一端称为队头

队列的实现:一般可以使用数组和链表存储的方式实现。

 

一,栈顺序存储基本操作的实现:

    存储结构的定义以及基本操作的接口:

//栈的顺序存储
#define STACK_SIZE 100
typedef struct Stack {
    int arr[STACK_SIZE];
    int top;
} Stack; 
void InitStack(Stack *s);
void StackPush(Stack *s, int v);
void StackPop(Stack *s);
int StackTop(Stack *s);
int StackSize(Stack *s);
bool StackEmpty(Stack *s);
void test_stack();

代码实现:

#include"stack.h"
//栈的初始化
void InitStack(Stack *s) {
	s->top = 0;
}
//压栈
void StackPush(Stack *s, int v) {
	if (s->top < STACK_SIZE) {
		s->arr[s->top] = v;
		s->top++;
	}
	else {
		printf("栈已满,不能入栈!\n");
	}
}
//出栈
void StackPop(Stack *s) {
	s->top--;
}
//查看栈顶元素
int StackTop(Stack *s) {
	return s->arr[s->top - 1];
}
//查看元素个数
int StackSize(Stack *s) {
	return s->top;
}
//判断栈是否为空
bool StackEmpty(Stack *s) {
	if (s->top == 0) {
		return true;
	}
	return false;
}

二,栈链式存储基本操作的实现:

    链式存储结构的定义以及基本操作的接口:

//栈的链式存储
typedef struct LStackNode {
	int value;
	struct LStackNode* next;
}	LStackNode;


void InitStack(LStackNode **top);
void StackPush(LStackNode *top, int v);
void StackPop(LStackNode *top);
int StackTop(LStackNode *top);
int StackSize(LStackNode *top);
bool StackEmpty(LStackNode *top);

基本操作的实现:

void InitStack(LStackNode **top) {
	(*top) = (LStackNode*)malloc(sizeof(LStackNode));
	if ((*top) != NULL) {
		(*top)->next = NULL;
	}
	return;
}

void StackPush(LStackNode *top, int v) {
	LStackNode* node = (LStackNode*)malloc(sizeof(LStackNode));
	node->value = v;
	node->next = top->next;
	top->next = node;
}

void StackPop(LStackNode *top) {
	if (top->next == NULL) {
		return;
	}
	LStackNode* next = top->next;
	top->next = top->next->next;
	free(next);
}
int StackTop(LStackNode *top) {
	if (top->next != NULL) {
		return top->next->value;
	}
	return -1;
}
int StackSize(LStackNode *top) {
	int size = 0;
	LStackNode* cur = top;
	while (cur->next != NULL) {
		size++;
		cur = cur->next;
	}
	return size;
}
bool StackEmpty(LStackNode *top) {
	if (top->next == NULL) {
		return true;
	}
	return false;
}

三,队列链式存储基本操作的实现:

    队列链式存储的定义以及借本操作的接口:

//链式存储队列
typedef struct Node {
	int value;
	struct Node *next;
}	Node;
typedef struct Queue {
	Node* head;
	Node* last;
} Queue;
void QueueInit(Queue *q);
void QueuePush(Queue *q, int v);
void QueuePop(Queue *q);
int QueueFront(Queue *q);
int QueueSize(Queue *q);
bool QueueEmpty(Queue *q);

基本操作代码的实现:

void QueueInit(Queue *q) {
	q->head = q->last = NULL;
}
//入队
void QueuePush(Queue *q, int v) {
	Node* node = (Node*)malloc(sizeof(Node));
	node->value = v;
	node->next = NULL;
	if (q->head == NULL) {
		q->head = node;
		q->last = node;
	}
	else {
		q->last->next = node;
		q->last = node;
	}
}
//出队
void QueuePop(Queue *q) {
	if (q->head == NULL) {
		return;
	}
	Node* second = q->head->next;
	free(q->head);
	q->head = second;
	if (q->head == NULL) {
		q->last = NULL;
	}
}
//查看队头元素
int QueueFront(Queue *q) {
	return q->head->value;
}
//查看队长
int QueueSize(Queue *q) {
	int size = 0;
	for (Node* cur = q->head; cur != NULL; cur = cur->next) {
		size++;
	}
	return size;
}
//判断队列是否为空
bool QueueEmpty(Queue *q) {
	return q->head == NULL;
}

四,队列顺序存储基本操作的实现:

    队列顺序存储结构的定义以及基本操作接口:

#define QUEUE_SIZE 100
typedef struct Queue2 {
	int arr[QUEUE_SIZE];
	int front;//定义队列的队头指针
	int rear;//队尾
}	Queue2;

void Queue2Init(Queue2 *q);
void Queue2Push(Queue2 *q, int v);
void Queue2Pop(Queue2 *q);
int Queue2Front(Queue2 *q);
int Queue2Size(Queue2 *q);
bool Queue2Empty(Queue2 *q);

基本操作的代码实现:

void Queue2Init(Queue2 *q) {
	q->front = q->rear = 0;
	for (int i = 0; i < QUEUE_SIZE; i++) {
		q->arr[i] = 0;
	}
}
void Queue2Push(Queue2 *q, int v) {
	if (q->rear == QUEUE_SIZE) {
		return;
	}
	q->arr[q->rear] = v;
	q->rear++;
}
void Queue2Pop(Queue2 *q) {
	if (q->front == q->rear) {
		return;
	}
	q->rear--;
	q->arr[q->rear] = 0;
}
int Queue2Front(Queue2 *q) {
	return q->arr[q->front];
}
int Queue2Size(Queue2 *q) {
	return q->rear;
}
bool Queue2Empty(Queue2 *q) {
	return q->arr[q->front] == q->arr[q->rear];
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章