栈和队列的实现

目录

1.栈

1.1栈的概念及结构

1.2栈的实现

2.队列

2.1队列的概念及结构

2.2队列的实现

1.栈

1.1栈的概念及结构

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

压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶。

出栈:栈的删除操作叫做出栈。出数据也在栈顶。

1.2栈的实现

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

使用数组实现栈是因为CPU在读取数据的时候会连续读取一段内存的数据,然后查找有没有需要的数据,如果没有会再次去读取数据,再次去读取数据的时候会把前面读取的数据给挤掉,如果使用链表实现栈,每次读取数据都会读取到一些没用的数据,因为链表数据在内存中不是连续存放的,如果前面有下一次需要读取的数据就有可能被挤掉需要重新读取数据,这样会浪费掉不少时间。

栈函数接口

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

typedef int STDataType;

typedef struct stack
{
	STDataType* a;//动态数组开辟
	int top;//栈顶
	int capacity;//容量
}ST;


void StackInit(ST* ps);
void StackDestory(ST* ps);

void StackPush(ST* ps, STDataType x);
void StackPop(ST* ps);

//栈不能打印数据
//void StackPrint(ST* ps);

bool StackEmpty(ST* ps);

STDataType StackTop(ST* ps);

int StackSize(ST* ps);

完整代码

main.c

#include "stack.h"

void test()
{
	ST st;

	StackInit(&st);
	StackPush(&st, 1);
	StackPush(&st, 2);
	printf("%d ", StackTop(&st));
	StackPop(&st);
	StackPush(&st, 3);
	StackPush(&st, 4);
	printf("%d ", StackTop(&st));
	StackPop(&st);
	StackPush(&st, 5);

	while (!StackEmpty(&st))
	{
		printf("%d ", StackTop(&st));
		StackPop(&st);
	}
	printf("\n");

	StackDestory(&st);
}

int main()
{
	test();

	return 0;
}

stack.c

#include "stack.h"

void StackInit(ST* ps)
{
	assert(ps);
	ps->a = (STDataType*)malloc(sizeof(STDataType) * 3);
	if (ps->a == NULL)
	{
		perror("malloc fail\n");
		return;
	}
	ps->capacity = 3;
	ps->top = 0;
}

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

void StackPush(ST* ps, STDataType x)
{
	assert(ps);
	if (ps->top == ps->capacity)
	{
		STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType) * ps->capacity * 2);
		if (tmp == NULL)
		{
			perror("realloc fail");
			exit(-1);
		}
		else 
		{
			ps->a = tmp;
			ps->capacity *= 2;
		}
		
	}
	ps->a[ps->top] = x;
	ps->top++;
}
void StackPop(ST* ps)
{
	assert(ps);
	assert(ps->top > 0);

	ps->top--;
}

bool StackEmpty(ST* ps)
{
	assert(ps);

	return ps->top == 0;//判断真返回非零的数,假返回0,top不等于0时为假,返回0,等于0时为真返回非零的数
}

STDataType StackTop(ST* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));

	return ps->a[ps->top - 1];
}

int StackSize(ST* ps)
{
	assert(ps);

	return ps->top;
}

stack.h

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

typedef int STDataType;

typedef struct stack
{
	STDataType* a;//动态数组开辟
	int top;//栈顶
	int capacity;//容量
}ST;


void StackInit(ST* ps);
void StackDestory(ST* ps);

void StackPush(ST* ps, STDataType x);
void StackPop(ST* ps);

//栈不能打印数据
//void StackPrint(ST* ps);

bool StackEmpty(ST* ps);

STDataType StackTop(ST* ps);

int StackSize(ST* ps);

用数组实现的栈和顺序表是差不多的,只不过是在顺序表上加上了限制条件。

2.队列

2.1队列的概念及结构

队列:只允许在一端进行插入数据操作,在另一端进行删除据操作的特殊线性表,队列具有先进先出FIFO(First In First Out) 入队列:进行插入操作的一端称为队尾 出队列:进行删除操作的一端称为队头,所以需要额外两个指针来维护队头和队尾。

2.2队列的实现

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

队列函数接口

#define _CRT_SECURE_NO_WARNLNGS 1

#pragma once
#include <assert.h>
#include <stdlib.h>
#include <stdio.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;//记录数据个数
}Queue;


void QueueInit(Queue* pq);//创建队列
void QueueDestory(Queue* pq);
void QueuePush(Queue* pq, QDataType x);
void QueuePop(Queue* pq);
int QueueSize(Queue* pq);//返回数据个数
bool QueueEmpty(Queue* pq);//判断队列是否为空
QDataType QueueFront(Queue* pq);//返回队头数据
QDataType QueueBack(Queue* pq);//返回队尾数据

完整代码

main.c

#define _CRT_SECURE_NO_WARNINGS 1

#include "Queue.h"


void test()
{
	Queue 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);
	}

	QueueDestory(&q);
}

int main()
{
	test();

	return 0;
}

Queue.c

#define _CRT_SECURE_NO_WARNINGS 1

#include "Queue.h"

void QueueInit(Queue* pq)
{
	assert(pq);
	pq->head = NULL;
	pq->tail = NULL;
	pq->size = 0;
}
void QueueDestory(Queue* 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(Queue* pq, QDataType x)
{
	assert(pq);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc fail\n");//输出错误原因,但不会终止程序,需要return
		return;
	}
	newnode->next = NULL;
	newnode->data = x;
	
	if (pq->head == NULL)
	{
		assert(pq->tail == NULL);
		pq->head = pq->tail = newnode;
	}
	else
	{
		pq->tail->next = newnode;
		pq->tail = newnode;
	}

	pq->size++;
}

void QueuePop(Queue* pq)
{
	assert(pq && 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--;
}

int QueueSize(Queue* pq)
{
	assert(pq);

	return pq->size;
}

bool QueueEmpty(Queue* pq)
{
	assert(pq);

	return pq->size == 0;
}
QDataType QueueFront(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));

	return pq->head->data;
}

QDataType QueueBack(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));

	return pq->tail->data;
}

Queue.h

#define _CRT_SECURE_NO_WARNLNGS 1

#pragma once
#include <assert.h>
#include <stdlib.h>
#include <stdio.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;//记录数据个数
}Queue;


void QueueInit(Queue* pq);//创建队列
void QueueDestory(Queue* pq);
void QueuePush(Queue* pq, QDataType x);
void QueuePop(Queue* pq);
int QueueSize(Queue* pq);//返回数据个数
bool QueueEmpty(Queue* pq);//判断队列是否为空
QDataType QueueFront(Queue* pq);//返回队头数据
QDataType QueueBack(Queue* pq);//返回队尾数据

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值