【数据结构】栈和队列

数据结构当中链表之后就是栈和队列了,我把这两种写在一篇博客是因为它们的一些操作较之前的顺序表和链表肯定是简单不少,可以说,就是那里边中的一些基本操作。

并且它们两个的性质还是相反的,栈是后入先出,队列是先入先出。

究竟栈长什么样子呢?你可以把它想象成一个大货车车厢,我们放东西应该先把东西往里放,这个里可以看作栈底,放满了往外拿的时候肯定是先拿外面的,后拿里面的,这就叫先入后出。

队列的话就跟我们平常的队列的话是一样的,只不过这里的队列要从队尾进入,队头出来,这也就是为什么先入后出。

下面我们先看栈,我们用一个顺序表来实现栈,当然空间还应该是动态分配的,所以我们在结构体中要包含一个指针,方便后面的增容。

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

之后我们还要给它初始化一下

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

之后就是一系列的顺序表的操作了,都是比较基础的

#include
#include
#include
#include

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);

#include"stack.h"
void STInit(ST* ps) {
	assert(ps);
	ps->a = NULL;
	ps->top = 0;
	ps->capacity = 0;
}


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

void STPush(ST* ps, STDataType x) {
	assert(ps);
	if (ps->top == ps->capacity) {
		int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType) * newCapacity);
		if (tmp == NULL) {
			perror("realloc failed");
			exit(-1);
		}
		ps->a = tmp;
		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];
}

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

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

我们的队列用一个类似于链表的形式来实现,因为我们只从队头取出元素,如果是顺序表的话还要整体往前挪。同理栈的话是用顺序表的话尾删比较容易,链表的话找上一个就比较麻烦。
我们先定义一个队列的结点,再构成一整个队列,队列需要包括头指针,尾指针和队列的大小

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

typedef struct Queue {
	QNode* head;
	QNode* tail;
	int size;
}Que;

后面也都是比较简单的操作了

#include
#include
#include
#include

typedef int QDataType;
typedef struct QueueNode {
	struct QueueNode* next;
	QDataType data;
}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);

#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(pq->size > 0);
	if (pq->head->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(pq->size > 0);
	return pq->head->data;
}

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

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

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

你可能感兴趣的:(数据结构)