DEV C++实现链队,顺序队

链队

/*
初始化  入队  出队  输出
使用方法:
	LQueue Q;
	LQueue_init(&Q);
*/


#include <iostream>
#include <stdio.h>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;  //Status是函数返回值类型,其值是函数结果状态代码
typedef int ElemType;  //ElemType为可定义的数据类型,此设为int类型

typedef struct QNode {
	ElemType data;
	struct QNode *next;
} QNode, *Qptr;

typedef struct {
	Qptr front;
	Qptr rear;
	int size;
} LQueue;

/**
  * @brief  链队列的初始化
  * @param  LQueue类型
  * @retval Status
  */
Status LQueue_init(LQueue *LQ) {
	LQ->front = LQ->rear = new QNode;
	LQ->front ->next = NULL;
	return OK;
}

/**
  * @brief  链队列入队
  * @param  LQueue类型,ElemType类型
  * @retval Status
  */
Status LQueue_enter(LQueue *LQ, ElemType e) {
	Qptr p = new QNode;
	p->data = e;
	p->next = NULL;
	LQ->rear ->next = p;
	LQ->rear = p;
	return  OK;
}

/**
  * @brief  链队列出队
  * @param  LQueue类型
  * @retval Status
  */
Status LQueue_del(LQueue *LQ) {
	if (LQ->front == LQ->rear ) {
		cout << "队列为空\n";
		return ERROR;
	}
	Qptr p = LQ->front ->next;
	LQ->front ->next = p->next ;
	if (LQ->rear == p)
		LQ->front = LQ->rear ;
	delete p;
	return OK;
}

/**
  * @brief  链队列的输出
  * @param  LQueue类型
  * @retval Status
  */
Status LQueue_output(LQueue *LQ) {
	if (LQ->front == LQ->rear ) {
		cout << "队列为空\n";
		return ERROR;
	}
	Qptr p;
	p = LQ->front ->next;
	cout << "队列:";
	while (p) {
		cout << p->data << " ";
		p = p->next ;
	}
	printf("\n");
	return OK;
}





 test

#include "LQueue.h"

int main() {
	LQueue Q;
	LQueue_init(&Q);
	for (int i = 1; i < 10; ++i)
		LQueue_enter(&Q, i);
	for (int i = 1; i < 10; ++i)
		LQueue_del(&Q);
	LQueue_enter(&Q, 10);
	LQueue_output(&Q);
	return 0;
}

顺序队 

/*
队列创建  求长度  入队  出队  输出
*/


#include <iostream>
#include <stdio.h>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;  //Status是函数返回值类型,其值是函数结果状态代码
typedef int ElemType;  //ElemType为可定义的数据类型,此设为int类型

typedef struct {
	ElemType *base;   //储存空间基地址
	int front;
	int rear;
	int size;
} SqQueue;

/**
  * @brief  队列创建
  * @param  SqQueue类型,队列大小
  * @retval 无
  */
void SqQueue_create(SqQueue *Q, int size) {
	Q->base = new ElemType[size];
	if (!Q->base )
		exit(OVERFLOW);
	cout << "队列创建成功\n";
	Q->front = Q->rear = 0;
	Q->size = size;
}

/**
  * @brief  求队列长度
  * @param  SqQueue类型
  * @retval 队列长度
  */
int SqQueue_length(SqQueue *Q) {
	return (Q->rear - Q->front);
}

/**
  * @brief  元素进队
  * @param  SqQueue类型,ElemTypoe类型
  * @retval Status
  */
Status SqQueue_enter(SqQueue *Q, ElemType e) {
	if (Q->rear == Q->size  ) {
		cout << "队列已满,元素" << e << "入队失败\n";
		return ERROR;
	}
	Q->base [Q->rear++ ] = e;
	return OK;
}

/**
  * @brief  元素出队
  * @param  SqQueue类型
  * @retval Status
  */
Status SqQueue_del(SqQueue *Q) {
	if (Q->front == Q->rear ) {
		cout << "队列为空\n";
		return ERROR;
	}
	Q->front ++;
	return OK;
}

/**
  * @brief  输出队列当前元素
  * @param  SqQueue类型
  * @retval 无
  */
void SqQueue_output(SqQueue *Q) {
	for (int i = Q->front ; i < Q->rear  ; ++i) {
		cout << Q->base [i] << " ";
	}
	cout << "\n";
}

test

#include <stdio.h>
#include "SqQueue.h"

int main() {
	SqQueue Q;
	SqQueue_create(&Q, 10);
	SqQueue_enter(&Q, 3);
	SqQueue_enter(&Q, 4);
	SqQueue_enter(&Q, 5);
	cout << SqQueue_length(&Q) << "\n";
	SqQueue_del(&Q);
	SqQueue_enter(&Q, 6);
	SqQueue_enter(&Q, 8);
	SqQueue_enter(&Q, 9);
	SqQueue_enter(&Q, 10);
	SqQueue_enter(&Q, 11);
	SqQueue_enter(&Q, 12);
	SqQueue_enter(&Q, 13);
	SqQueue_enter(&Q, 14);
	SqQueue_output(&Q);
	return 0;
}

顺序表是一种使用连续内存空间存储数据的线性表数据结构。在Dev C++中创建一个顺序表可以通过定义一个数组并配合一些管理数组的函数来实现。下面是一个简单的顺序表创建的示例代码,包括基本的操作如插入和删除。 ```c #include <stdio.h> #include <stdlib.h> #define MAX_SIZE 100 // 定义顺序表的最大容量 // 顺序表的结构体定义 typedef struct { int data[MAX_SIZE]; // 存储数据的数组 int length; // 顺序表当前长度 } SeqList; // 初始化顺序表 void InitList(SeqList *L) { L->length = 0; // 初始化长度为0 } // 向顺序表中插入元素 int InsertList(SeqList *L, int index, int value) { if (L->length >= MAX_SIZE) { // 检查顺序表是否已满 printf("顺序表已满,无法插入\n"); return -1; } if (index < 1 || index > L->length + 1) { // 检查插入位置是否有效 printf("插入位置无效\n"); return -1; } for (int i = L->length; i >= index; i--) { // 将index及之后的元素后移 L->data[i] = L->data[i - 1]; } L->data[index - 1] = value; // 插入新元素 L->length++; // 长度加1 return 0; } // 从顺序表中删除元素 int DeleteList(SeqList *L, int index) { if (L->length == 0) { // 检查顺序表是否为空 printf("顺序表为空,无法删除\n"); return -1; } if (index < 1 || index > L->length) { // 检查删除位置是否有效 printf("删除位置无效\n"); return -1; } for (int i = index; i < L->length; i++) { // 将index之后的元素前移 L->data[i - 1] = L->data[i]; } L->length--; // 长度减1 return 0; } int main() { SeqList L; InitList(&L); // 初始化顺序表 // 插入元素示例 InsertList(&L, 1, 10); // 在顺序表头部插入元素10 InsertList(&L, 2, 20); // 在顺序表头部插入元素20 InsertList(&L, 3, 30); // 在顺序表头部插入元素30 // 删除元素示例 DeleteList(&L, 2); // 删除顺序表中索引为2的元素 // 打印顺序表中的元素 for (int i = 0; i < L.length; i++) { printf("%d ", L.data[i]); } printf("\n"); return 0; } ``` 这段代码定义了一个顺序表的基本操作,包括初始化、插入和删除元素。在Dev C++中,你可以创建一个新的C++项目,将上述代码粘贴到主文件中,然后编译并运行程序。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值