【队列与栈的实现】

队列功能:

#pragma once

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

typedef int QDataType;

//链式结构:表示队列
typedef struct QListNode
{
	struct QListNode* Next;
	QDataType data;
}QNode;
// 队列的结构
typedef struct Queue
{
 QNode* head;
 QNode* tail; 
}Queue;

// 初始化队列
void QueueInit(Queue* q);

// 队尾入队列
void QueuePush(Queue* q, QDataType data);

// 队头出队列
void QueuePop(Queue* q);

// 获取队列头部元素
QDataType QueueFront(Queue* q);

// 获取队列队尾元素
QDataType QueueBack(Queue* q);

// 获取队列中有效元素个数
int QueueSize(Queue* q);

// 检测队列是否为空,如果为空返回非零结果,如果非空返回0 
int QueueEmpty(Queue* q);

// 销毁队列
void QueueDestroy(Queue* q);

功能实现:

#include"queue.h"


// 初始化队列
void QueueInit(Queue* q) {
	assert(q);
	q->head = q->tail = NULL;
}

// 队尾入队列
void QueuePush(Queue* q, QDataType data) {
	assert(q);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL) {
		perror("malloc fail\n");
		exit(-1);
	}
	newnode->data = data;
	newnode->Next = NULL;


	if (q->tail == NULL) {
		q->head = q->tail = newnode;
	}
	else {
		q->tail->Next = newnode;
		q->tail = newnode;
	}

}

// 队头出队列
void QueuePop(Queue* q) {
	assert(q);
	assert(!QueueEmpty(q));

	if (q->head->Next == NULL) {
		free(q->head);
		q->head = q->tail = NULL;
	}
	else {
		QNode* next = q->head->Next;
		free(q->head);
		q->head = next;
	}

	
}

// 获取队列头部元素
QDataType QueueFront(Queue* q) {
	assert(q);
	assert(!QueueEmpty(q));
	return q->head->data;
}

// 获取队列队尾元素
QDataType QueueBack(Queue* q) {
	assert(q);
	assert(!QueueEmpty(q));
	
	return q->tail->data;
}

// 获取队列中有效元素个数
int QueueSize(Queue* q) {
	assert(q);
	QNode* cur = q->head;
	int size = 0;
	while (cur) {
		++size;
		cur = cur->Next;
	}
	return size;
}

// 检测队列是否为空,如果为空返回非零结果,如果非空返回0 
int QueueEmpty(Queue* q) {
	assert(q);

	return q->head == NULL;
}

// 销毁队列
void QueueDestroy(Queue* q) {
	assert(q);
	QNode* cur = q->head;
	while (cur) {
		QNode* next = cur->Next;
		free(cur);
		cur = next;
	}
	q->head = q->tail = NULL;
}

栈的功能:

#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdbool.h>
#include<stdlib.h>
//#define N 10
//typedef int STDataType;
//
//typedef struct stack {
//	STDataType a[N];
//	int top;
//}ST;

typedef int STDataType;

typedef struct stack {
	STDataType* a;
	int top;
	int capactiy;
}ST;

void stackInit(ST* ps);

void stackdestory(ST* ps);

void stackpush(ST* ps, STDataType x);

void stackpop(ST* ps);

STDataType stacktop(ST* ps);

bool stackEmpty(ST* ps);

int stackSize(ST* ps);

void checkstack(ST* ps);

栈的实现:

#include"stack.h"
void checkstack(ST* ps) {
	assert(ps);
	if (ps->top == ps->capactiy) {
		int newcapacity = ps->capactiy == 0 ? 4 : ps->capactiy * 2;
		STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType) * newcapacity);
		if (tmp == NULL) {
			printf("realloc fail\n");
			exit(-1);
		}
		ps->a = tmp;
		ps->capactiy = newcapacity;
	}
}

void stackInit(ST* ps) {
	assert(ps);
	ps->a = NULL;
	ps->top = 0;
	ps->capactiy = 0;
}

//销毁
void stackdestory(ST* ps) { 
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->top = ps->capactiy = 0;
}

void stackpush(ST* ps, STDataType x) {
	assert(ps);
	if (ps->top == ps->capactiy) {
		int newcapacity = ps->capactiy == 0 ? 4 : ps->capactiy * 2;
		STDataType* tmp = realloc(ps->a, sizeof(STDataType)* newcapacity);
		if (tmp == NULL) {
			printf("realloc fail\n");
			exit(-1);
		}
		ps->a = tmp;
		ps->capactiy = newcapacity;
	}
	ps->a[ps->top] = x;
	ps->top++;
}

void stackpop(ST* ps) {
	assert(ps);
	assert(!stackEmpty(ps));

	return ps->top--;


}

STDataType stacktop(ST* ps) {
	assert(ps);
	assert(!stackEmpty(ps));

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

bool stackEmpty(ST* ps) {
	assert(ps);

	return ps->top == 0;
}

int stackSize(ST* ps) {
	assert(ps);
	return ps->top;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值