初识栈和队列/栈和队列的实现

去成为你想成为的人!带上无所畏惧的勇气。在你我身上,勇气是除了生命之外最珍贵的礼物。

一. 栈的实现

1.1 栈的概念及结构
:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。 进行数据插入和删除操作的一端 称为栈顶,另一端称为栈底。 栈中的数据元素遵守后进先出 LIFO Last In First Out )的原则。
压栈 :栈的插入操作叫做进栈 / 压栈 / 入栈, 入数据在栈顶
出栈 :栈的删除操作叫做出栈。 出数据也在栈顶

学习目标:   用数组结构来实现栈

#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
// 支持动态增长的栈
typedef int STDataType;
typedef struct Stack
{
	STDataType* _a;
	int _top;		// 栈顶
	int _capacity;  // 容量 
}Stack;
// 初始化栈 
void StackInit(Stack* ps);
// 入栈 
void StackPush(Stack* ps, STDataType data);
// 出栈 
void StackPop(Stack* ps);
// 获取栈顶元素 
STDataType StackTop(Stack* ps);
// 获取栈中有效元素个数 
int StackSize(Stack* ps);
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0 
int StackEmpty(Stack* ps);
// 销毁栈 
void StackDestroy(Stack* ps);

#对栈结构体的分析 

typedef struct Stack
{
	STDataType* _a;//用数组来存储数据,
	int _top;		// —top表示栈顶元素的下一个位置,其值比栈顶元素下标大一
	int _capacity;  // 容量 
}Stack;

1.对栈完成初始化 void StackInit(Stack* ps);

void StackInit(Stack* ps) {
	assert(ps);
	ps->_a = NULL;
	ps->_capacity = 0;
	ps->_top = 0;
}

2.入栈操作的实现 

void StackPush(Stack* ps, STDataType data) {
	assert(ps);
	if (ps->_capacity == ps->_top) {
		ps->_capacity = ps->_capacity == 0 ? 4 : 2 * ps->_capacity;
		STDataType* tmp = (STDataType*)realloc(ps->_a, sizeof(STDataType) * ps->_capacity);
		if (tmp == NULL) {
			perror("realloc fail");
			exit(-1);
		}
		ps->_a = tmp;
	}
	ps->_a[ps->_top] = data;
	ps->_top++;
}

3.出栈操作的实现 

void StackPop(Stack* ps) {
	assert(ps);
	assert(ps->_top > 0);
	ps->_top--;
}

4. 获取栈顶元素

STDataType StackTop(Stack* ps) {
	assert(ps);
	assert(ps->_top > 0);
	return ps->_a[ps->_top-1];
}

5. 获取栈中有效元素个数

int StackSize(Stack* ps) {
	assert(ps);
	return ps->_top;
}

6. 检测栈是否为空,如果为空返回非零结果,如果不为空返回0 

int StackEmpty(Stack* ps) {
	assert(ps);
	return !ps->_top;
}

7.栈的销毁 

void StackDestroy(Stack* ps) {
	assert(ps);
	free(ps->_a);
	//free空指针会怎么样?看上传的代码
	ps->_a = NULL;
	ps->_capacity = ps->_top = 0;
}

 二.队列的实现

2.1 队列的概念及结构
队列 :只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出
FIFO(First In First Out) 入队列:进行插入操作的一端称为 队尾 出队列:进行删除操作的一端称为 队头

学习目标: 用链式结构来实现队列

typedef int QDataType;
// 链式结构:表示队列 
typedef struct QListNode
{
	struct QListNode* _next;
	QDataType _data;
}QNode;

// 队列的结构 
typedef struct Queue
{
	QNode* _front;
	QNode* _rear;
}Queue;

// 初始化队列 
void QueueInit(Queue* q);
// 队尾入队列 
void QueuePush(Queue* q, QDataType data);
// 队头出队列 
void QueuePop(Queue* q);
// 获取队列头部元素 
QDataType QueueFront(Queue* q);
// 获取队列队尾元素 
QDataType QueueBack(Queue* q);
// 获取队列中有效元素个数 
int QueueSize(Queue* q);
// 检测队列是否为空,如果为空返回非零结果,如果非空返回0 
int QueueEmpty(Queue* q);
// 销毁队列 
void QueueDestroy(Queue* q);

 1.初始化队列

void QueueInit(Queue* q) {
	assert(q);
	q->_front = q->_rear = NULL;
}

2.队尾入队列 

void QueuePush(Queue* q, QDataType data) {
	assert(q);
	QNode* next = (QNode*)malloc(sizeof(QNode));
	next->_data = data;
	next->_next = NULL;
	if (q->_front == NULL) {
		q->_front = q->_rear = next;
	}
	else {
		q->_rear->_next = next;
		q->_rear = next;
	}
}

3. 队头出队列

void QueuePop(Queue* q) {
	assert(q);
	assert(q->_front != NULL);
	if (q->_front->_next == NULL) {
		free(q->_front);
		q->_front = q->_rear = NULL;
	}
	else {
		QNode* tmp = q->_front->_next;
		free(q->_front);
		q->_front = tmp;
	}
}

4.获取队列头部元素,即head处元素 

QDataType QueueFront(Queue* q) {
	assert(q);
	return q->_front->_data;
}

5. 获取队列尾部元素

QDataType QueueBack(Queue* q) {
	assert(q);
	return q->_rear->_data;
}

6. 获取队列中有效元素的个数 

int QueueSize(Queue* q) {
	assert(q);
	int x = 0;
	QNode* cur = q->_front;
	while (cur != NULL) {
		x++;
		cur = cur->_next;
	}
	return x;
}

7. 检测队列是否为空,如果为空返回非零结果,如果非空返回0 

int QueueEmpty(Queue* q) {
	assert(q);
	return q->_front == NULL;
}

8. 销毁队列

void QueueDestroy(Queue* q) {
	assert(q);
	
	while (q->_front!= NULL) {
		QueuePop(q);
	}
}

 

 

 人会后悔的只有一事,便是活的不够勇敢。 —— 特德·休斯

 

 

 

 

 

 

 

 

 

 

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值