(数据结构初阶)栈和队列

目录

一 栈

1.栈的基本概念

2.栈的核心接口代码实现

         3.栈的代码实现(完整)

二 队列

1.队列的基本概念

2.队列核心接口的实现

3.队列的实现(全部)


一 栈

1.栈的基本概念

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

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

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

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

2.栈的核心接口代码实现

(1)基本结构

typedef int STDateType;
struct Stack
{
	STDateType* a;
	int top;       //栈顶
	int capacity;  //容量
};
//typedef struct Stack  ST;
typedef struct Stack  Stack;

(2)初始化

//初始化
void StackInit(Stack* pst)
{
	assert(pst);

	pst->a = (STDateType*)malloc(sizeof(STDateType)*4);
	pst->top = 0;
	pst->capacity = 4;
}

(3)销毁

//销毁
void StackDestroy(Stack* pst)
{
	assert(pst);

	free(pst->a);
	pst->a = NULL;
	pst->capacity = pst->top = 0;
}

(4)入栈

//压栈(性质就决定了在栈顶出入数据)
void StackPush(Stack* pst, STDateType x)
{
	assert(pst);

	if (pst->capacity == pst->top)
	{
		STDateType* temp = realloc(pst->a,sizeof(STDateType)*pst->capacity*2);
		if (NULL == temp)
		{
			printf("realloc fail \n");
			exit(-1);
		}
		pst->a = temp;
		pst->capacity *= 2;
	}
	pst->a[pst->top] = x;
	pst->top++;
}

(5)出栈

//弹栈
void StackPop(Stack* pst)
{
	assert(pst);
	assert(!StackEmpty(pst));

	pst->top--;

}

(6)取栈顶位置数据

//取数据
STDateType StackTop(Stack* pst)
{
	assert(pst);
	assert(!StackEmpty(pst));

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

(7)判断栈是否为空

//空返回1,非空返回0
bool StackEmpty(Stack* pst)
{
	assert(pst);
    
	return pst->top == 0;
}

(8)求栈的长度

//求栈的长度
int StackSize(Stack* pst)
{
	assert(pst);

	return pst->top;
}

3.栈的代码实现(完整)

(1)stack.h

#define _CRT_SECURE_NO_WARNINGS 1

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

typedef int STDateType;
struct Stack
{
	STDateType* a;
	int top;       //栈顶
	int capacity;  //容量
};
//typedef struct Stack  ST;
typedef struct Stack  Stack;

void StackInit(Stack* pst); //初始化
void StackDestroy(Stack* pst);//销毁

//性质就决定了在栈顶出入数据
void StackPush(Stack* pst, STDateType x);//压栈
void StackPop(Stack* pst);//弹栈

STDateType StackTop(Stack* pst);//取数据

//空返回1,非空返回0
bool StackEmpty(Stack* pst);
int StackSize(Stack* pst); //求栈的容量

(2)stack.c

#define _CRT_SECURE_NO_WARNINGS 1

#include"stack.h"

//初始化
void StackInit(Stack* pst)
{
	assert(pst);

	pst->a = (STDateType*)malloc(sizeof(STDateType)*4);
	pst->top = 0;
	pst->capacity = 4;
}

//销毁
void StackDestroy(Stack* pst)
{
	assert(pst);

	free(pst->a);
	pst->a = NULL;
	pst->capacity = pst->top = 0;
}

//压栈(性质就决定了在栈顶出入数据)
void StackPush(Stack* pst, STDateType x)
{
	assert(pst);

	if (pst->capacity == pst->top)
	{
		STDateType* temp = realloc(pst->a,sizeof(STDateType)*pst->capacity*2);
		if (NULL == temp)
		{
			printf("realloc fail \n");
			exit(-1);
		}
		pst->a = temp;
		pst->capacity *= 2;
	}
	pst->a[pst->top] = x;
	pst->top++;
}

//弹栈
void StackPop(Stack* pst)
{
	assert(pst);
	assert(!StackEmpty(pst));

	pst->top--;

}

//取数据
STDateType StackTop(Stack* pst)
{
	assert(pst);
	assert(!StackEmpty(pst));

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

//空返回1,非空返回0
bool StackEmpty(Stack* pst)
{
	assert(pst);
    
	return pst->top == 0;
}

//求栈的长度
int StackSize(Stack* pst)
{
	assert(pst);

	return pst->top;
}



(3)test.c

#define _CRT_SECURE_NO_WARNINGS 1

#include"stack.h"

void test()
{
	Stack st;
	StackInit(&st);
	StackPush(&st, 1);
	StackPush(&st, 2);
	StackPush(&st, 3);
	StackPush(&st, 4);
	StackPush(&st, 5);

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

	StackDestroy(&st);
}

int main()
{
	test();
	return 0;
}

二 队列

1.队列的基本概念

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

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

2.队列核心接口的实现

(1)队列的结构

typedef int QDateType;
typedef struct QueueNode
{
	struct QueueNode* next;
	QDateType date;
}QueueNode;

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

(2)初始化

//初始化
void QueueInit(Queue* pq)
{
	pq->head = pq->tail = NULL;
}

(3)销毁

//销毁
void QueueDestroy(Queue* pq)
{
	QueueNode* cur = pq->head;
	while (cur)
	{
		QueueNode* next = cur->next;
		free(cur);
		cur = next;
	}
	pq->head = pq->tail = NULL;
}

(4)入队

//入队
void QueuePush(Queue* pq, QDateType x)
{
	assert(pq);
	QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
	if (NULL == newnode)
	{
		printf("malloc fail\n");
		exit(-1);
	}
	newnode->date = x;
	newnode->next = NULL;

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

(5)出队

//出队
void QueuePop(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	if (NULL == pq->head->next)
	{
		free(pq->head);
		pq->head = pq->tail = NULL;
	}
	else
	{
		QueueNode* next = pq->head->next;
		free(pq->head);
		pq->head = next;
	}
}

(6)取队头数据

//取队头数据
QDateType QueueFront(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));

	return pq->head->date;
}

(7)取队尾数据

//取队尾数据
QDateType QueueBack(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));

	return pq->tail->date;
}

(8)判断队列是否为空

//空返回 false  不为空返回 true
bool QueueEmpty(Queue* pq)
{
	assert(pq);

	return pq->head == NULL;
}

(9)队列长度

//队列长度
int Queuesize(Queue* pq)
{
	int size = 0;
	QueueNode* cur = pq->head;
	while(cur)
	{
		size++;
		cur = cur->next;
	}
	return size;
}

3.队列的实现(全部)

(1)queue.h

#define _CRT_SECURE_NO_WARNINGS 1

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

typedef int QDateType;
typedef struct QueueNode
{
	struct QueueNode* next;
	QDateType date;
}QueueNode;

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

void QueueInit(Queue* pq);
void QueueDestroy(Queue* pq);

void QueuePush(Queue* pq, QDateType x);
void QueuePop(Queue* pq);

QDateType QueueFront(Queue* pq);
QDateType QueueBack(Queue* pq);
bool QueueEmpty(Queue* pq);
int Queuesize(Queue* pq);

(2)queue.c

#define _CRT_SECURE_NO_WARNINGS 1

#include"queue.h"

//初始化
void QueueInit(Queue* pq)
{
	pq->head = pq->tail = NULL;
}

//销毁
void QueueDestroy(Queue* pq)
{
	QueueNode* cur = pq->head;
	while (cur)
	{
		QueueNode* next = cur->next;
		free(cur);
		cur = next;
	}
	pq->head = pq->tail = NULL;
}

//入队
void QueuePush(Queue* pq, QDateType x)
{
	assert(pq);
	QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
	if (NULL == newnode)
	{
		printf("malloc fail\n");
		exit(-1);
	}
	newnode->date = x;
	newnode->next = NULL;

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

//出队
void QueuePop(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	if (NULL == pq->head->next)
	{
		free(pq->head);
		pq->head = pq->tail = NULL;
	}
	else
	{
		QueueNode* next = pq->head->next;
		free(pq->head);
		pq->head = next;
	}
}

//取队头数据
QDateType QueueFront(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));

	return pq->head->date;
}

//取队尾数据
QDateType QueueBack(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));

	return pq->tail->date;
}

//空返回 false  不为空返回 true
bool QueueEmpty(Queue* pq)
{
	assert(pq);

	return pq->head == NULL;
}

//队列长度
int Queuesize(Queue* pq)
{
	int size = 0;
	QueueNode* cur = pq->head;
	while(cur)
	{
		size++;
		cur = cur->next;
	}
	return size;
}

(3)test.c

#define _CRT_SECURE_NO_WARNINGS 1

#include"queue.h"

void text()
{
	Queue q;
	QueueInit(&q);

	QueuePush(&q, 1);
	QueuePush(&q, 2);
	QueuePush(&q, 3);
	QueuePush(&q, 4);

	while (!QueueEmpty(&q))
	{
		printf("%d ", QueueFront(&q));
		QueuePop(&q);
	}
	printf("\n");

	QueueDestroy(&q);
}

int main()
{
	text();
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zhang丶&|!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值