目录
栈和队列的基本概念:
数据结构中的栈和队列:
栈和队列的基本结构:
1.栈和队列的结构示意图
2.栈和队列中数据的插入和删除
栈和队列的实现:
栈的实现
栈.c
stack.h
源stack.c
队列的实现
队列.c
queue.h
queue.c
栈和队列的基本概念:
在数组中,我们可以通过索引(下标)访问随机元素。在某些特定情况下,我们需要限制处理顺序,于是,产生了栈和队列这两种功能受限的线性结构。
栈和队列,两种不同的处理顺序,先进后出和先进先出,是两个相应的线性数据结构。
数据结构中的栈和队列:
统一用数组实现功能的话
1. 栈(stack)
数据先进后出,后进先出:LIFO(last in first out)
栈只有一个开口,先进去的就到下面,后进来的就在上面(top);要是拿出去的话,肯定从开口端拿出去,所以说是后进先出,先进后出。
入栈:push
出栈:pop
获取栈顶元素:top
判断栈是否已经为满:is_full
判断栈是否已经为空:is _empty
2. 队列(queue)
数据入队规则:先进先出,后进后出:FIFO(first in first out)
队列有队首(front)和队尾(back),数据从队尾入队,从队首出队。
队头(front)指向队列的第一个数据,队尾(back)指向队列中的最后一个数据。
入队:push
出队:pop
队首:front
队尾:back
栈和队列的基本结构:
1. 栈和队列的结构示意图

2. 栈和队列中数据的插入和删除
栈

队列

栈和实现:
stack.h
栈的头文件
    :头文件用于数据类型的声明和功能函数的声明
#pragma once      防止头文件重复包含(vs)
#ifndef STACK     防止头文件重复包含
#define STACK
#define _CRT_SECUTE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
栈的数组实现
typedef int Type;         给数据类型取别名,方便一改全改
#define STACK_P "%d\t"
#define STACK_S "%d"
栈结构的声明
#define STACK_SIZE 10     栈的最大大小
typedef struct Stack{
	Type data[STACK_SIZE];     栈的数据域
	int top;                   栈顶元素下标
}stack;
栈功能的声明
数据入栈函数
void stack_push(Type data[], int *top, Type value);
数据出栈函数
Type stack_pop(Type data[], int *top);
获取栈顶元素
Type stack_top(Type data[], int top);
判断栈满函数
int stack_full(int top);
判断栈空函数
int stack_empty(int top);
#endif
源stack.c
#include"stack.h"
数据入栈函数
void stack_push(Type data[], int *top, Type value)
{
	if (stack_full(*top))
	{
		printf("栈满了,数据入栈失败\n");
		return;
	}
	data[++*top] = value;     向栈顶插入数据元素 
}
数据出栈函数
Type stack_pop(Type data[], int *top)
{
	if (stack_empty(*top))
	{
		printf("栈空了,数据出栈失败\n");
		return -1;
	}
	return data[(*top)--];     返回栈顶元素,栈顶标记-1
}
获取栈顶元素
Type stack_top(Type data[], int top)
{
	if (stack_empty(top))
	{
		printf("栈空了,获取栈顶元素失败\n");
		return-1;
	}
	return data[top];
}
判断栈满函数
int stack_full(int top)
{
	//if (top >= STACK_SIZE - 1)return 1;     栈满了
	//return 0;                               栈没满
	return top >= STACK_SIZE - 1;
}
判断栈空函数
int stack_empty(int top)
{
	return -1 == top;     -1表示栈中没有数据,栈为空
	                       返回1.否则返回0
}
栈.c
#include<stdio.h>
#include"stack.h"     包含栈功能函数的头文件
多文件编程
栈功能的测试函数
void stack_test();
int main(){
	stack_test();
	return 0;
}
void stack_test()
{
	stack s1;		    定义了一个栈
	s1.top = -1;	    初始化栈顶元素下标top的值为-1(-1标识表中没有数据,栈为空)
					    s1.data[s1.top];操作栈顶元素
	Type values = 0;
	do{
		scanf(STACK_S, &values);
		stack_push(s1.data, &s1.top, values);
	} while ('\n' != getchar());
	printf(STACK_P, stack_top(s1.data, s1.top));
	while (!stack_empty(s1.top))
	{
		printf(STACK_P, stack_pop(s1.data, &s1.top));
	}
	putchar('\n');
}队列的实现:
queue.h
#pragma once         vs
#ifndef QUEUE
#define QUEUE
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
队列的实现
typedef int Type;     给数据类型取别名,方便一改全改
#define QUEUE_P "%d\t"
#define QUEUE_S "%d"
#define QUEUE_Pn "%d\n"
队列结构的声明
#define QUEUE_SIZE 10     队列的最大大小
typedef struct Queue{
	Type data[QUEUE_SIZE];     队列的数据域
	int rear;     队尾元素下标
}queue;
队列功能的声明
数据入队函数
void queue_push(Type *data, int *rear, Type value);     Type data[]也是一样的
数据出队函数
Type queue_pop(Type *data, int *rear);
判断队满函数
int queue_full(int rear);
判断队空函数
int queue_empty(int rear);
#endif源queue.c
#include "Queue.h"
队列功能的实现
数据入队函数
void queue_push(Type* data, int* rear, Type value)
{
	if (queue_full((*rear) - 1))
	{
		printf("队列满了,数据入队失败!\n");
		return;
	}
	data[(*rear)++] = value;
}
数据出队函数
Type queue_pop(Type* data, int* rear)
{
	if (queue_empty((*rear) - 1))
	{
		printf("队列为空,数据出队失败!\n");
		return -1;
	}
	Type value = data[0];     记录队首元素
	for (int i = 0; i < (*rear) ; i++)
	{
		data[i] = data[i + 1];
	}
	(*rear)--;     队尾元素下标-1
	return value;     返回队首元素
}
判断队满函数
int queue_full(int rear)
{
	return rear >= QUEUE_SIZE - 1;
}
判断队空函数
int queue_empty(int rear)
{
	return -1 == rear;
}队列.c
#include<stdio.h>
#include "Queue.h"     包含队列的功能函数头文件
多文件编程
队列功能的测试函数
void queue_test();
int main(){
	queue_test();
	return 0;
}
void queue_test()
{
	queue q1;
	q1.rear = 0;     队尾下标初始化为0(0表示队列中没有数据)
	Type values = 0;
	do{
		scanf(QUEUE_S, &values);
		queue_push(q1.data, &(q1.rear), values);
	} while ('\n' != getchar());
	printf("队首为%d\t队尾为%d\n\n", q1.data[0], q1.data[q1.rear - 1]);
	while (q1.rear >0)
	{
		printf(QUEUE_P, queue_pop(q1.data, &(q1.rear)));
	}
	putchar('\n');
}
______________#



















