栈和队列

▶栈的顺序存储结构

typedef struct {
	ElementType *base;		// 在栈构造之前和销毁之后,base的值为null
	ElementType *top;		// 栈顶指针
	int stacksize;			// 当前分配的存储容量(ElementType)为单位)
}Stack;

C语言部分代码实现如下:

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

typedef struct {
	ElementType *base;		//栈底
	ElementType *top;		//栈顶
	int stackSize; 			//存储容量
}Stack;
static Stack stack = {0};

Status initStack(Stack *stack)
{
	stack->base = (ElementType *)malloc(STACK_INIT_SIZE * sizeof(ElementType));

	if (!stack->base)
	{
		exit(OVERFLOW);
	}

	stack->top = stack->base;
	stack->stackSize = STACK_INIT_SIZE;

	return OK;
}

Status destroyStack(Stack *stack)
{
	if (stack->top != stack->base)
	{
		free(stack->top);
		stack->top = NULL;
		free(stack->base);
		stack->base = NULL;
		stack->stackSize = 0;
	}
	else
	{
		free(stack->top);
		stack->top = NULL;
		stack->base = NULL;
	}

	return OK;
}

Status clearStack(Stack *stack)
{
	ElementType *temp = NULL;
	temp = stack->top;
	if (stack->top != stack->base)
	{
		*(stack->top--) = 0;
	}

	return OK;
}

Status stackIsEmpty(Stack *stack)
{
	if (stack->top == stack->base)
	{
		return TRUE;
	}

	return FALSE;
}

int stackLength(Stack *stack)
{
	int length = 0;
	ElementType *temp = NULL;

	temp = stack->top;

	while (temp != stack->base)
	{
		length++;
		temp--;
	}

	return length;
}

Status getTop(Stack *stack, ElementType *top)
{
	ElementType *temp = NULL;

	temp = stack->top;

	if (stack->top == stack->base)
	{
		return ERROR;
	}

	*top = *(--temp);

	return OK;
}

Status push(Stack *stack, ElementType element)
{
	if ((stack->top - stack->base) >= stack->stackSize)
	{
		stack->base = (ElementType *)realloc(stack->base, (stack->stackSize + STACKINCREMENT) * sizeof(ElementType));
		if (!stack->base)
		{
			return ERROR;
		}

		stack->stackSize += STACKINCREMENT;
		stack->top = stack->base + stack->stackSize;
	}

	*stack->top++ = element;

	return OK;
}

Status pop(Stack *stack, ElementType *element)
{
	if (stack->top == stack->base)
	{
		return ERROR;
	}

	*element = *(--stack->top);

	return OK;
}


▶队列的顺序存储结构

typedef struct {
	ElementType *base;				// 初始化的动态分配存储空间
	int front;						// 头指针,若队列不空,指向队列头元素
	int rear;						// 尾指针,若队列不空,指向队列尾元素的下一个位置
}LinearQueue;

C语言实现的部分代码如下:

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

#define TRUE 1				//true
#define FALSE 0				//false
#define OK 1				//success
#define ERROR 0				//fail
#define INFEASIBLE -1		//infeasible  不可实行的
#define OVERFLOW -2			//overflow

#define MAXQSIZE 100

typedef int ElementType;
typedef int Status;

typedef struct CircularQueueNode {
	ElementType *base;		//初始化的动态分配存储空间
	int front;				//头指针,若队列为空,指向队列头元素
	int rear;				//尾指针,若队列为空,指向队列尾元素的下一个位置
}CircularQueue;

CircularQueue circularQueue = {0};

Status initCircularQueue(CircularQueue *circularQueue);
int lengthCircularQueue(CircularQueue *circularQueue);
Status insertCircularQueue(CircularQueue *circularQueue, ElementType element);
Status deleteCircularQueue(CircularQueue *circularQueue, ElementType *element);

Status initCircularQueue(CircularQueue *circularQueue)
{
	circularQueue->base = (ElementType *)malloc(MAXQSIZE * sizeof(ElementType));

	if (!circularQueue->base)
	{
		exit(OVERFLOW);
	}

	circularQueue->front = circularQueue->rear = 0;

	return OK;
}

int lengthCircularQueue(CircularQueue *circularQueue)
{
	return (circularQueue->rear - circularQueue->front + MAXQSIZE) % MAXQSIZE;
}

Status insertCircularQueue(CircularQueue *circularQueue, ElementType element)
{
	if ((circularQueue->rear + 1) % MAXQSIZE == circularQueue->front)
	{
		return ERROR;
	}

	circularQueue->base[circularQueue->rear] = element;
	circularQueue->rear = (circularQueue->rear + 1) % MAXQSIZE;

	return OK;
}

Status deleteCircularQueue(CircularQueue *circularQueue, ElementType *element)
{
	if (circularQueue->front == circularQueue->rear)
	{
		return ERROR;
	}

	*element = circularQueue->base[circularQueue->front];
	circularQueue->front = (circularQueue->front + 1) % MAXQSIZE;

	return OK;
}

int main()
{
	int i = 0;
	ElementType element = 0;
	ElementType *temp = NULL;

	initCircularQueue(&circularQueue);

	for (i = 0; i < 100; i++)
	{
		insertCircularQueue(&circularQueue, i);
	}

	for (i = 0; i < 67; i++)
	{
		deleteCircularQueue(&circularQueue, &element);
	}

	printf("%d== \n", lengthCircularQueue(&circularQueue));

	for (i = 0; i < 99; i++)
	{
		printf("%d, ", circularQueue.base[circularQueue.front++]);
	}

	getchar();

	return 0;
}



▶队列的链式存储结构

typedef struct QueueNode {
	ElementType element;
	struct QueueNode *next;
}QueueNode, *Queue;

typedef struct {
	Queue front;
 Queue rear;}LinkQueue;


C语言实现的部分代码如下:

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

#define TRUE 1				//true
#define FALSE 0				//false
#define OK 1				//success
#define ERROR 0				//fail
#define INFEASIBLE -1		//infeasible  不可实行的
#define OVERFLOW -2			//overflow

typedef int ElementType;
typedef int Status;

typedef struct QueueNode {
	ElementType element;
	struct QueueNode *next;
}QueueNode, *Queue;

typedef struct {
	Queue front;
	Queue rear;
}LinkQueue;

LinkQueue linkQueue = {0};

Status initQueue(LinkQueue *linkQueue);
Status destroyQueue(LinkQueue *linkQueue);
Status clearQueue(LinkQueue *linkQueue);
Status isEmpty(LinkQueue *linkQueue);
int length(LinkQueue *linkQueue);
Status getHead(LinkQueue *linkQueue, ElementType *head);
Status insertElement(LinkQueue *linkQueue, ElementType *element);
Status deleteElement(LinkQueue *linkQueue, ElementType *element);

Status initQueue(LinkQueue *linkQueue)
{
	linkQueue->front = linkQueue->rear = (Queue)malloc(sizeof(QueueNode));

	if (!linkQueue->front)
	{
		return ERROR;
	}

	linkQueue->front->next = NULL;

	return OK;
}

Status destroyQueue(LinkQueue *linkQueue)
{
	while (linkQueue->front)
	{
		linkQueue->rear = linkQueue->front->next;
		free(linkQueue->front);
		linkQueue->front = linkQueue->rear;
	}

	return OK;
}

Status clearQueue(LinkQueue *linkQueue)
{
	Queue temp = NULL;

	temp = linkQueue->front;

	while (temp != linkQueue->rear)
	{
		temp->next->element = 0;
		temp = temp->next;
	}

	return OK;
}

Status isEmpty(LinkQueue *linkQueue)
{
	if (linkQueue->front == linkQueue->rear)
	{
		return TRUE;
	}

	return FALSE;
}

int length(LinkQueue *linkQueue)
{
	int length = 0;
	Queue temp = NULL;

	temp = linkQueue->front;

	while (temp != linkQueue->rear)
	{
		length++;
		temp = temp->next;
	}

	return length;
}

Status getHead(LinkQueue *linkQueue, ElementType *head)
{
	if (linkQueue->front != linkQueue->rear)
	{
		*head = linkQueue->front->next->element;
	}

	return OK;
}

Status insertElement(LinkQueue *linkQueue, ElementType element)
{
	Queue node = NULL;

	node = (Queue)malloc(sizeof(QueueNode));

	if (!node)
	{
		exit(OVERFLOW);
	}
	else
	{
		node->element = element;
		node->next = NULL;
		linkQueue->rear->next = node;
		linkQueue->rear = node;
	}

	return OK;
}

Status deleteElement(LinkQueue *linkQueue, ElementType *element)
{
	Queue temp = NULL;

	if (linkQueue->front == linkQueue->rear)
	{
		return ERROR;
	}

	temp = linkQueue->front->next;
	*element = temp->element;

	linkQueue->front->next = temp->next;

	if (linkQueue->rear == temp)
	{
		linkQueue->rear = linkQueue->front;
	}

	free(temp);

	return OK;
}

int main()
{
	int i = 0;
	Queue temp = NULL;
	ElementType element = 0;

	initQueue(&linkQueue);
	temp = linkQueue.front;

	for (i = 0; i < 100; i++)
	{
		insertElement(&linkQueue, i);
	}

	getHead(&linkQueue, &element);
	//destroyQueue(&linkQueue);
	//clearQueue(&linkQueue);

	printf("%d--\n", isEmpty(&linkQueue));
	printf("%d==\n", length(&linkQueue));
	printf("%d++\n", element);

	deleteElement(&linkQueue, &element);

	for (i = 0; i < 99; i++)
	{
		printf("%d, ", temp->next->element);
		temp = temp->next;
	}

	getchar();

	return 0;
}


                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值