数据结构“栈(stack)”和“队列(queue)”的简单实现

栈(Stack)和队列(Queue)

系统函数:

#ifndef _SYSUTIL_H_
#define _SYSUTIL_H_
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#include<stdbool.h>

#endif

栈(stack)

顺序栈(SeqStack)

头文件:

#ifndef _STACK_H_
#define _STACK_H_

#include"Sysutil.h"

#define StackElemType int
#define StackSize 8

typedef struct SeqStack
{
    
    
	StackElemType* base;
	size_t capacity;
	int top;
}SeqStack;

//
//顺序栈函数声明

void SeqStackInit(SeqStack* pst, int sz);//初始化
void SeqStackpush(SeqStack* pst, StackElemType x);//入栈
void SeqStackShow(SeqStack* pst);//显示栈
void SeqStackPop(SeqStack* pst);//出栈
void SeqStackDestroy(SeqStack* pst);//销毁栈
StackElemType SeqStackTop(SeqStack* pst);//获取出栈顶元素



#endif 

函数实现.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"

//顺序栈函数实现

bool IsFull(SeqStack* pst)//判满函数
{
    
    
	return pst->top >= pst->capacity;
}
bool IsEmpty(SeqStack* pst)//判空函数
{
    
    
	return pst->top == 0;
}

void SeqStackInit(SeqStack* pst, int sz)
{
    
    
	assert(pst);
	pst->capacity = sz > StackSize ? sz : StackSize;//确保顺序栈的最短长度为8
	pst->base = (StackElemType*)malloc(sizeof(StackElemType)*pst->capacity);
	assert(pst->base);
	pst->top = 0;
}

void SeqStackPush(SeqStack* pst, StackElemType x)
{
    
    
	assert(pst);
	if (IsFull(pst))//判满
	{
    
    
		printf("顺序栈已满,不能插入%d.\n", x);
		return;
	}
	pst->base[pst->top++] = x;//给当前位置赋值然后将top下标前移
}

void SeqStackShow(SeqStack* pst)
{
    
    
	assert(pst);
	for (int i = pst->top - 1; i >= 0; --i)
	{
    
    
		printf("%d\n", pst->base[i]);
	}
}

void SeqStackPop(SeqStack* pst)
{
    
    
	assert(pst);
	if (IsEmpty(pst))
	{
    
    
		printf("顺序栈为空,不能进行出栈!\n");
		return;
	}
	pst->top--;
}

void SeqStackDestroy(SeqStack* pst)
{
    
    
	assert(pst);
	free(pst->base);
	pst->base = NULL;
	pst->capacity = pst->top = 0;
}

StackElemType SeqStackTop(SeqStack* pst)
{
    
    
	assert(pst && !IsEmpty(pst));
	return pst->base[pst->top - 1];
}

链栈(LinkStack)

头文件:

#ifndef LINKSTACK_H_
#define LINKSTACK_H_
#include"Sysutil.h"
#define StackElemType int

typedef struct LinkStackNode//定义节点
{
    
    
	StackElemType data;
	struct LinkStackNode* next;
}LinkStackNode;

typedef struct LinkStack
{
    
    
	LinkStackNode* head;
}LinkStack;

///
//链栈函数声明

void LinkStackInit(LinkStack* pst);//初始化
void LinkStackPush(LinkStack* pst, StackElemType x);//入栈(头插)
void LinkStackShow(LinkStack* pst);//显示链栈
void LinkStackDestroy(LinkStack* pst);//销毁
void LinkStackPop(LinkStack* pst);//出栈
StackElemType LinkStackTop(LinkStack* pst);//获取栈顶元素

#endif

函数实现:

#define _CRT_SECURE_NO_WARNINGS 1
#include"LinkStack.h"


//链栈函数实现
void LinkStackInit(LinkStack* pst)
{
    
    
	pst->head = NULL;
}

void LinkStackPush(LinkStack* pst, StackElemType x)
{
    
    
	assert(pst);
	LinkStackNode* s = (LinkStackNode*)malloc(sizeof(LinkStackNode));
	s->data = x;
	s->next = pst->head;
	pst->head = s;
}

void LinkStackShow(LinkStack* pst)
{
    
    
	assert(pst);
	LinkStackNode* p = pst->head;
	while (p != NULL)
	{
    
    
		printf("%d\n", p->data);
		p = p->next;
	}
}

void LinkStackPop(LinkStack* pst)
{
    
    
	assert(pst);
	if (pst->head != NULL)
	{
    
    
		LinkStackNode* p = pst->head;
		pst->head = p->next;
		free(p);
	}
}

void LinkStackDestroy(LinkStack* pst)
{
    
    
	assert(pst);
	while (pst->head != NULL)
	{
    
    
		LinkStackNode* p = pst->head;
		pst->head = p->next;
		free(p);
	}
}

StackElemType LinkStackTop(LinkStack* pst)
{
    
    
	assert(pst && pst->head != NULL);
	return pst->head->data;
}

队列(Queue)

顺序队列

头文件:

#include"Sysutil.h"

#define QueueElemType int
#define QueueSize 8
typedef struct SeqQueue
{
    
    
	QueueElemType* base;
	size_t capacity;
	int front;
	int rear;
}SeqQueue;

///
//顺序队列函数声明
void SeqQueueInit(SeqQueue* pq, int sz);//初始化
void SeqQueueDestroy(SeqQueue* pq);//销毁
void SeqQueueShow(SeqQueue* pq);//显示
void SeqQueueEn(SeqQueue* pq, QueueElemType x);//入队
void SeqQueueDe(SeqQueue* pq);//出队
QueueElemType SeqQueueBack(SeqQueue* pq);//获取对尾元素
QueueElemType SeqQueuefront(SeqQueue* pq);//获取队头元素

函数实现:

#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqQueue.h"


//
//顺序队列函数实现
void SeqQueueInit(SeqQueue* pq, int sz)
{
    
    
	assert(pq);
	pq->capacity = sz > QueueSize ? sz : QueueSize;//确保最短长度为8
	pq->base = (QueueElemType*)malloc(sizeof(QueueElemType)*pq->capacity);
	assert(pq->base);
	pq->front = pq->rear = 0;
}

void SeqQueueDestroy(SeqQueue* pq)
{
    
    
	assert(pq);
	free(pq->base);
	pq->base = NULL;
	pq->capacity = pq->front = pq->rear = 0;
}

void SeqQueueShow(SeqQueue* pq)
{
    
    
	assert(pq);
	for (int i = pq->front; i <= pq->rear-1; i++)
		printf("%d<--", pq->base[i]);
	printf("Nil.\n");
}

void SeqQueueEn(SeqQueue* pq, QueueElemType x)
{
    
    
	assert(pq);
	if (pq->rear >= pq->capacity)
	{
    
    
		printf("队列已满!\n");
		return;
	}
	pq->base[pq->rear++] = x;
}

void SeqQueueDe(SeqQueue* pq)
{
    
    
	assert(pq);
	if (pq->front == pq->rear)
	{
    
    
		printf("队列已空!\n");
		return;
	}
	pq->front++;
}

QueueElemType SeqQueueBack(SeqQueue* pq)
{
    
    
	assert(pq && (pq->front != pq->rear));
	return pq->base[pq->rear - 1];
}

QueueElemType SeqQueuefront(SeqQueue* pq)
{
    
    
	assert(pq && (pq->front != pq->rear));
	return pq->base[pq->front];
}

链队列

头文件:

#include"Sysutil.h"
#define QueueElemType int 

typedef struct LinkQueueNode
{
    
    
	QueueElemType data;
	struct LinkQueueNode* next;
}LinkQueueNode;

typedef struct LinkQueue
{
    
    
	LinkQueueNode* head;
	LinkQueueNode* tail; //(指向队列的对尾)
}LinkQueue;

//
//链队列函数声明
void LinkQueueInit(LinkQueue* pq);//初始化
void LinkQueueEn(LinkQueue* pq, QueueElemType x);//入队
void LinkQueueDe(LinkQueue* pq);//出队
void LinkQueueShow(LinkQueue* pq);//显示
void LinkQueueDestroy(LinkQueue* pq);//销毁
QueueElemType LinkQueueback(LinkQueue* pq);//获取对尾元素
QueueElemType LinkeQueuefront(LinkQueue* pq);//获取队头元素


函数实现:

#define _CRT_SECURE_NO_WARNINGS 1
#include"LinkQueue.h"

void LinkQueueInit(LinkQueue* pq)
{
    
    
	pq->head = NULL;
	pq->tail = NULL;
}

void LinkQueueEn(LinkQueue* pq, QueueElemType x)
{
    
    
	assert(pq);
	LinkQueueNode* s = (LinkQueueNode*)malloc(sizeof(LinkQueueNode));
	assert(s);
	s->data = x;
	s->next = NULL;
	if (pq->head == NULL)
		pq->head = pq->tail = s;
	else
	{
    
    
		pq->tail->next = s;
		pq->tail = s;
	}
}

void LinkQueueDe(LinkQueue* pq)
{
    
    
	assert(pq);
	if (pq->head != NULL)
	{
    
    
		LinkQueueNode* p = pq->head;
		pq->head = p->next;
		if (pq->head == NULL)
			pq->tail = NULL;
		free(p);
	}
}

void LinkQueueShow(LinkQueue* pq)
{
    
    
	assert(pq);
	LinkQueueNode* p = pq->head;
	while (p != NULL)
	{
    
    
		printf("%d<--", p->data);
		p = p->next;
	}
	printf("Nil\n");
}

void LinkQueueDestroy(LinkQueue* pq)
{
    
    
	assert(pq);
	while (pq->head != NULL)
	{
    
    
		LinkQueueNode* p = pq->head;
		pq->head = p->next;
		free(p);
	}
	pq->head = pq->tail = NULL;
}

QueueElemType LinkQueueback(LinkQueue* pq)
{
    
    
	assert(pq && pq->head);
	return pq->tail->data;
}

QueueElemType LinkeQueuefront(LinkQueue* pq)
{
    
    
	assert(pq && pq->head);
	return pq->head->data;
}

猜你喜欢

转载自blog.csdn.net/qq_43560037/article/details/113141176