栈和队列(一)

目录

一、栈(后入先出)

1、概念和结构

2、栈的实现(顺序表)

主程序(test.c) 

头文件(stack.h)

调用函数(stack.c)

二、队列(先入先出)

1、队列的概念及结构

2、队列的实现

主程序(test.c)

头文件(queue.h)

调用函数(queue.c)


一、栈(后入先出)

1、概念和结构

栈是一种特殊的线性表,只允许在固定的一端进行插入和删除数据

  • 栈顶:进行数据插入和删除操作的一端
  • 栈底:另外一端
  • 压栈:栈的插入叫做进栈/压栈/入栈,入数据在栈顶
  • 出栈:栈的删除操作叫做出栈,出数据也在栈顶

2、栈的实现(顺序表)

栈用数组实现比较好

  • 主程序(test.c) 

#define _CRT_SECURE_NO_WARNINGS 1

#include "Stack.h"

int main()
{
	ST stack;

	//栈初始化
	STInit(&stack);


	//入栈
	STpush(&stack, 4);
	STpush(&stack, 3);
	STpush(&stack, 2);
	//出栈
	STpop(&stack);
	STpush(&stack, 1);
	STpop(&stack);
	STpop(&stack);

	//栈中数据的个数 
	STsize(&stack);

	//查询栈顶元素
	printf("栈顶的元素为->%d\n", STTop(&stack));

	return 0;
}
  • 头文件(stack.h)

#define _CRT_SECURE_NO_WARNINGS 1

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

typedef int STDatatype;
typedef struct Stack
{
	int* a;
	int top;//top =0 指向栈顶元素下一个元素位置,top=-1指向栈顶
	int capacity;
}ST;

//栈初始化
void STInit(ST* para);
//销毁栈
void STdestroy(ST* para);
//入栈
void STpush(ST* para, STDatatype x);
//出栈
void STpop(ST* para);
//栈中数据的个数
void STsize(ST* para);
//判断栈是否为空
bool STempty(ST* para);
//查询栈顶元素
STDatatype STTop(ST* para);

  • 调用函数(stack.c)

#define _CRT_SECURE_NO_WARNINGS 1
#include "Stack.h"

//栈初始化
void STInit(ST* para)
{
	assert(para);

	para->a = (STDatatype*)malloc(sizeof(STDatatype) * 4);
	para->capacity = 4;
	para->top = -1;
}

//销毁栈
void STdestroy(ST* para)
{
	assert(para);
	free(para->a);
	para->capacity = 0;
	para->top = -1;
}

//入栈
void STpush(ST* para, STDatatype x)
{
	assert(para);
	if (para->top == para->capacity)
	{
		para->capacity = para->capacity * 2;
		int* new = (STDatatype*)realloc(para->a ,sizeof(STDatatype) * (para->capacity));
		if (new == NULL)
		{
			perror("malloc:");
			return NULL;
		}
		para->a = new;
	}
	para->top++;
	para->a [para->top ] = x;
}

//出栈
void STpop(ST* para)
{
	assert(para);
	assert(!STempty(para));

	printf("%d ", para->a[para->top]);
	para->top--;
}

//栈中数据的剩余个数
void STsize(ST* para)
{
	assert(para);
	printf("\n栈中还有%d个数据 \n", para->top + 1);
}

//判断栈是否为空
bool STempty(ST* para)
{
	assert(para);

	return para->top == -1;
}

//栈顶元素
STDatatype STTop(ST* para)
{
	assert(para);
	assert(!STempty(para));

	return para->a[para->top];
}


二、队列(先入先出)

1、队列的概念及结构

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

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

2、队列的实现

单链表比较适合实现队列,定义头指针和尾指针

  • 主程序(test.c)

#define _CRT_SECURE_NO_WARNINGS 1
#include "queue.h"

void Print(Queue* ps)
{
	printf("队头数据为->%d\n", QueueFront(ps));
	printf("队尾数据为->%d\n", QueueBack(ps));
}
int main()
{
	//创建队列
	Queue queue;

	//初始化
	QueueInit(&queue);

	//入队列
	Queuepush(&queue, 1);
	Queuepush(&queue, 2);
	Queuepush(&queue, 3);
	Queuepush(&queue, 4);
	Print(&queue);

	//出队列
	Queuepop(&queue);
	//查看队列中的元素个数
	int n = Queuesize(&queue);
	printf("%d\n", n);
	Print(&queue);

	Queuepush(&queue, 5);
	n = Queuesize(&queue);
	printf("%d\n", n);
	Print(&queue);

	//销毁队列
	QueueDestroy(&queue);
	return 0;
}
  • 头文件(queue.h)

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

typedef int QDataType;
typedef struct Queuenode
{
	QDataType a;
	struct Queuenode* next;
}Qnode;

typedef struct Queue
{
	Qnode* tail;
	Qnode* head;
	int size;
}Queue;

//初始化队列
void QueueInit(Queue* ps);
//销毁队列
void QueueDestroy(Queue* ps);
//入队列
void Queuepush(Queue* ps, QDataType x);
//出队列
void Queuepop(Queue* ps);
//查看队列中的元素个数
int Queuesize(Queue* ps);
//判断队列是否为空
bool QueueEmpty(Queue* ps);
//查看队首
QDataType QueueFront(Queue* ps);
//查看队尾
QDataType QueueBack(Queue* ps);
  • 调用函数(queue.c)

#define _CRT_SECURE_NO_WARNINGS 1
#include "queue.h"

//创建新节点
Qnode* Creatnode(QDataType x)
{
	Qnode* node = (Qnode*)malloc(sizeof(Qnode));
	if (node == NULL)
	{
		perror("malloc:");
		return NULL;
	}
	node->a = x;
	node->next = NULL;
	return node;
}

//初始化
void QueueInit(Queue* ps)
{
	assert(ps);

	ps->head = NULL;
	ps->size = 0;
	ps->tail = NULL;
}

//判断队列是否为空
bool QueueEmpty(Queue* ps)
{
	assert(ps);

	return ps->size == 0;
}

//销毁队列
void QueueDestroy(Queue* ps)
{
	assert(ps);
	Qnode* cur = ps->head;
	while (cur)
	{
		Qnode* next = cur->next;
		free(cur);
		cur = next;
	}

	ps->head = ps->tail = NULL;
	ps->size = 0;
}

//入队列
void Queuepush(Queue* ps, QDataType x)
{
	assert(ps);

	Qnode* newnode = Creatnode(x);
	if (ps->head == NULL)
	{
		ps->head = ps->tail = newnode;
	}
	else
	{
		ps->tail->next = newnode;
		ps->tail = newnode;
	}
	ps->size++;
}

//出队列
void Queuepop(Queue* ps)
{
	assert(ps);
	//断言看ps里面是否有数据,非空执行,空的话报错
	assert(!QueueEmpty(ps));

	if (ps->head == ps->tail)
	{
		free(ps->head);
		ps->head = ps->tail = NULL;
		ps->size--;
	}
	else
	{
		Qnode* cur = ps->head;
		ps->head = ps->head->next;
		free(cur);
		cur = NULL;
		ps->size--;
	}
}
//查看队列中的元素个数
int Queuesize(Queue* ps)
{
	assert(ps);

	return ps->size;
}

//查看队首
QDataType QueueFront(Queue* ps)
{
	assert(ps);
	assert(!QueueEmpty(ps));

	return ps->head->a;
}
//查看队尾
QDataType QueueBack(Queue* ps)
{
	assert(ps);
	assert(!QueueEmpty(ps));

	return ps->tail->a;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值