数据结构(栈和对列)

一,栈

1.概念和结构

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

压栈:栈内数据的插入叫做压栈,入栈的数据在栈顶
出栈:栈内数据的删除叫做出栈,出栈的数据在栈底

2.栈的实现

栈的实现一般可以使用数组或者链表实现,相对而言数组的结构实现更优一些。因为数组在尾上插入数据的代价比较小。数组的缓存利用率也更高。

下面是用C语言实现栈的代码:

Stack.h
#pragma once

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

//采用数组实现的优势是可以直接在栈顶尾插尾删
//而且数组的缓存利用率比链表高
typedef int STDataType;
//采用动态数组实现
typedef struct Stack {
	STDataType* a;
	int top;//标记栈顶
	int capacity;
}ST;

//初始化栈
void STInit(ST* ps);
//销毁栈
void STDestroy(ST* ps);
//入栈
void STPush(ST* ps,STDataType x);
//出栈
void STPop(ST* ps);
//获取栈顶元素
STDataType STTop(ST* ps);
//求栈长度
int STSize(ST* ps);
//判断是否为空
bool STEmpty(ST* ps);
Stack.c
#define _CRT_SECURE_NO_WARNINGS 1

#include "Stack.h"

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

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

void STPush(ST* ps,STDataType x) {
	assert(ps);
	//若满了则扩容
	if (ps->top == ps->capacity) {
		int newcapacity = ps->capacity == 0 ? 5 : ps->capacity * 2;
		//realloc中,当ps->a为空时,作用和malloc相同
		STDataType* temp = (STDataType*)realloc(ps->a,sizeof(STDataType) * newcapacity);
		if (temp == NULL) {
			perror("realloc failed");
			exit(-1);
		}
		ps->a = temp;
		ps->capacity = newcapacity;
	}
	ps->a[ps->top] = x;
	ps->top++;
}

void STPop(ST* ps) {
	assert(ps);
	//为空时不能删除栈顶
	assert(ps->top > 0);
	ps->top--;
}

STDataType STTop(ST* ps) {
	assert(ps);
	//栈为空时不能取出栈顶元素
	assert(ps->top > 0);
	return ps->a[ps->top-1];//top代表的是栈顶元素的下一位
}

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

bool STEmpty(ST* ps) {
	assert(ps);
	/*if (ps->top == 0) {
		return true;
	}
	else {
		return false;
	}*/
	return ps->top == 0;
}

测试用例:

Test.c
#define _CRT_SECURE_NO_WARNINGS 1

#include "Stack.h"

void testStack() {
	ST s;//如果定义为结构体指针,则要先初始化
	STInit(&s);
	STPush(&s, 1);
	STPush(&s, 2);
	STPush(&s, 3);
	STPush(&s, 4);
	STPush(&s, 5);
	//输出,要遍历栈,就要一边遍历一边出栈
	while (!STEmpty(&s)) {
		printf("%d ", STTop(&s));
		STPop(&s);
	}
	//STPop(&s);


	STDestroy(&s);
}

int main() {
	testStack();
	

	return 0;
}

结果:

在这里插入图片描述

二,队列

1.概念和结构

队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出FIFO(First In First Out)

入队列:进行插入操作的一端称为队尾
出队列:进行删除操作的一端称为队头

2.队列的实现

队列也可以数组和链表的结构实现,使用链表的结构实现更优一些,因为如果使用数组的结构,出队列在数组头上出数据,效率会比较低。

下面是用C语言实现队列的代码:

Queue.h
#pragma once

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

//选用单链表来实现栈,好处是便于入队列和出队列(需要大量移动元素)

typedef int QDataType;
typedef struct QueueNode {
	QDataType data;
	struct QueueNode* next;
}QNode;

//再建立一个结构体保存队列的头和尾,可以避免使用二级指针,插入删除更加方便
typedef struct Queue {
	QNode* head;
	QNode* tail;
	int size;
}Que;

//初始化队列
void QueueInit(Que* pq);
//销毁队列
void QueueDestroy(Que* pq);
//入队列
void QueuePush(Que* pq, QDataType x);
//出队列
void QueuePop(Que* pq);
//取队头
QDataType QueueFront(Que* pq);
//取队尾
QDataType QueueBack(Que* pq);
//判断队列是否为空
bool QueueEmpty(Que* pq);
//求队列长
int QueueSize(Que* pq);
Queue.c
#define _CRT_SECURE_NO_WARNINGS 1

#include "Queue.h"

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

void QueueDestroy(Que* pq) {
	assert(pq);
	QNode* cur = pq->head;
	while (cur) {
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}
	pq->head = pq->tail = NULL;
	pq->size = 0;
}

void QueuePush(Que* pq, QDataType x) {
	assert(pq);
	QNode* newNode = (QNode*)malloc(sizeof(QNode));
	if (newNode == NULL) {
		perror("malloc failed");
		exit(-1);
	}
	newNode->data = x;
	newNode->next = NULL;
	if (pq->tail == NULL) {
		pq->head = pq->tail = newNode;

	}
	else {
		pq->tail->next = newNode;
		pq->tail = newNode;
	}
	pq->size++;
}



void QueuePop(Que* pq) {
	assert(pq);
	//对列为空时不能出对列
	assert(!QueueEmpty(pq));
	//当只有一个元素时
	if (pq->tail->next = NULL) {
		free(pq->head);
		pq->head = pq->tail = NULL;
	}
	else {
		QNode* next = pq->head->next;
		free(pq->head);
		pq->head = next;
	}
	pq->size--;
}

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

QDataType QueueBack(Que* pq) {
	assert(pq);
	assert(!QueueEmpty(pq));
	return pq->tail->data;
}

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

int QueueSize(Que* pq) {
	assert(pq);
	return pq->size;
}

测试用例:

Test.c
#define _CRT_SECURE_NO_WARNINGS 1

#include "Queue.h"

void Queuetest() {
	Que q;
	QueueInit(&q);
	QueuePush(&q, 1);
	QueuePush(&q, 2);
	QueuePush(&q, 3);
	QueuePush(&q, 4);
	QueuePush(&q, 5);
	//和栈一样,遍历时要先出对列
	while (!QueueEmpty(&q)) {
		printf("%d ", QueueFront(&q));
		QueuePop(&q);
	}
	QueueDestroy(&q);
}

int main() {
	Queuetest();

	return 0;
}

结果:
在这里插入图片描述

  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值