栈和队列(初级)

介绍栈和队列

定义:进行插入和删除的一端叫做栈底,另一端叫做栈顶,遵循先进后出,后进先出(Last In First Out)
经典操作:压栈,出栈
有两种栈,一种是数组栈,一种是链式栈,两种都可以,相比之下数组栈的效率更高
下面是核心功能的实现
头文件

#pragma once

#include<stdio.h>
#include<stdbool.h>
#include<assert.h>
#include<stdlib.h>

typedef int STDataType;
typedef struct Stack {
	STDataType* a;
	int top;
	int capacity;
}ST;

void StackInit(ST* ps);
void StackDestroy(ST* ps);
void StackPush(ST* ps, STDataType x);
void StackPop(ST* ps);
STDataType StackTop(ST* ps);
bool StackEmpty(ST* ps);
int StackSize(ST* ps);

源文件

#include "Stack.h"

void StackInit(ST* ps) {
	assert(ps);
	ps->a = NULL;
	ps->capacity = ps->top = 0;
}

void StackDestroy(ST* ps) {
	assert(ps);
	free(ps->a);
	ps->a = NULL;//置空是一个好习惯
}

void StackPush(ST* ps, STDataType x) {
	assert(ps);
	if (ps->capacity == ps->top) {
		int newpacacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		STDataType* temp = (STDataType*)realloc(ps->a, sizeof(STDataType) * newpacacity);
		if (temp == NULL) {
			printf("申请空间失败!");
			exit(-1);
		}
		ps->a = temp;
		ps->capacity = newpacacity;
	}

	ps->a[ps->top++] = x;
}

void StackPop(ST* ps) {
	assert(ps);
	assert(!StackEmpty(ps));
	ps->top--;
}


STDataType StackTop(ST* ps) {
	assert(ps);
	assert(!StackEmpty(ps));
	return ps->a[ps->top-1];//这里要注意
}

bool StackEmpty(ST* ps) {
	assert(ps);
	return ps->top == 0;
}

int StackSize(ST* ps) {
	assert(ps);
	return ps->top;
}

队列

只允许在一端插入,在另一端删除,先进先出(FIFO)
入队列:进入插入的一端称为队尾
出队列:进行删除的一端叫做队头
在这里插入图片描述
下面是核心功能的实现
头文件

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>

typedef int QDataType;

typedef struct QueueNode {
	struct QueueNode* next;
	QDataType x;
}QueueNode;

typedef struct Queue {
	QueueNode* tail;
	QueueNode* head;
};

void QueueInit(Queue* pq);
void QueueDestroy(Queue* pq);
void QueuePush(Queue* pq, QDataType X);
void QueuePop(Queue* pq);
QDataType QueueFront(Queue* pq);
bool QueueEmpty(Queue* pq);
int QueueSize(Queue* pq);

源文件

#include "Queue.h"

void QueueInit(Queue* pq) {
	assert(pq);
	pq->head = pq->tail = NULL;
}

void QueueDestroy(Queue* pq) {
	assert(pq);
	
	QueueNode* cur = pq->head;
	while (cur) {
		QueueNode* qnext = cur->next;
		free(cur);
		cur = qnext;
	}
	//别忘了置空
	pq->head = pq->tail = NULL;
}

void QueuePush(Queue* pq, QDataType X) {
	assert(pq);

	QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
	newnode->x = X;
	newnode->next = NULL;

	//极端情况的判断(一个元素都没有的时候)
	if (pq->head == NULL) {
		pq->head = pq->tail = newnode;
	}
	else {
		pq->tail->next = newnode;
		pq->tail = newnode;
	}
}

void QueuePop(Queue* pq) {
	assert(pq);
	assert(!QueueEmpty(pq));

	QueueNode* tempNode = pq->head->next;
	free(pq->head);
	pq->head = tempNode;
	//极端情况的判断(删完了head指向空那么我们也要手动将tail也指向空)

	if (pq->head == NULL) {
		pq->tail = NULL;
	}

}

QDataType QueueFront(Queue* pq) {
	assert(pq);
	assert(!QueueEmpty(pq));
	return pq->head->x;
}

bool QueueEmpty(Queue* pq) {
	assert(pq);
	return pq->head == NULL;
}

int QueueSize(Queue* pq) {
	assert(pq);
	QueueNode* cur = pq->head;
	int count = 0;

	while (cur) {
		count++;
		QueueNode* qnext = cur->next;
		cur = qnext;
	}
	return count;
}

测试代码

#include "Stack.h"
#include "Queue.h"

void StackTest() {
	ST stack1;
	StackInit(&stack1);
	StackPush(&stack1, 1);
	StackPush(&stack1, 2);
	StackPush(&stack1, 3);
	while (!StackEmpty(&stack1)) {
		printf("%d\n", StackTop(&stack1));
		StackPop(&stack1);
	}
	StackDestroy(&stack1);
}


void QueueTest() {
	Queue q1;
	QueueInit(&q1);
	QueuePush(&q1, 1);
	QueuePush(&q1, 2);
	QueuePush(&q1, 3);
	QueuePush(&q1, 4);
	
	QueuePop(&q1);
	QueuePop(&q1);
	QueueDestroy(&q1);

}

int main() {
	//StackTest();
	QueueTest();


	return 0;
}

此外,
自己还可以思考
①怎么用两个队列来实现栈的功能
②怎么用两个栈实现队列的功能
③循环队列的原理
这样基本就可以入门了
后期还会更新此篇

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值