ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

栈和队列(初级)

2022-02-27 17:02:58  阅读:166  来源: 互联网

标签:ps pq 队列 void assert ST Queue 初级


介绍栈和队列

定义:进行插入和删除的一端叫做栈底,另一端叫做栈顶,遵循先进后出,后进先出(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;
}

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

标签:ps,pq,队列,void,assert,ST,Queue,初级
来源: https://blog.csdn.net/Morn_Star_Oliver/article/details/123166238

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有