c和指针
前言
ADT,全程Abstract data type,即抽象数据类型。
c和指针的ADT主要包括链表、堆栈、队列和树。
c和指针的第十二章讲述的是链表。第十三章讲述的是堆栈、队列和树。
提示:以下是本篇文章正文内容,下面案例可供参考
一、ADT的内存分配
所有的ADT都需要确定一件事情,就是如何申请内存。文中介绍了有三种方式
1、静态数组
2、动态数组
3、链式结构
1.1 静态数组队列
//queue_v1.h文件
#ifndef QUEUE_H
#define QUEUE_H
#include <assert.h>
#include <stdio.h>
//头文件不要定义变量,所以静态数组队列的声明就不放在头文件
#define QUEUE_TYPE int
#define QUEUE_SIZE 10
//插入
void insert(int data);
//出队列
QUEUE_TYPE outQueue();
//判断队列是否满
bool isQueueFull();
//判断队列是否空
bool isQueueEmpty();
#endif // QUEUE_H
//queue_v1.c文件
#include "queue_v1.h"
QUEUE_TYPE queue_adt[QUEUE_SIZE];
int head = 0;
int rear = 0;
void insert(int data)
{
if(isQueueFull())
{
printf("queue is full\n");
return;
}
rear = (rear + 1) % QUEUE_SIZE;
queue_adt[rear] = data;
}
int outQueue()
{
if(isQueueEmpty())
{
printf("queue is empty\n");
return 0;
}
head = (head + 1) % QUEUE_SIZE;
int dat = queue_adt[head];
return dat;
}
bool isQueueFull()
{
return (rear + 1) % QUEUE_SIZE == head;
}
bool isQueueEmpty()
{
return (rear == head);
}
//main.cpp文件
#include <iostream>
#include "queue/queue_v1.h"
using namespace std;
int main()
{
cout << "Hello world!" << endl;
printf("start insert\n");
for(int i = 0; i < 15; i++)
{
insert(i + 1);
printf("insert:%d: %d\n", i, i + 1);
}
printf("start outQueue\n");
for(int i = 0; i < 15; i++)
{
int dat = outQueue();
printf("outQueue:%d: %d\n", i, dat);
}
return 0;
}
//Hello world!
//start insert
//insert:0: 1
//insert:1: 2
//insert:2: 3
//insert:3: 4
//insert:4: 5
//insert:5: 6
//insert:6: 7
//insert:7: 8
//insert:8: 9
//queue is full
//insert:9: 10
//queue is full
//insert:10: 11
//queue is full
//insert:11: 12
//queue is full
//insert:12: 13
//queue is full
//insert:13: 14
//queue is full
//insert:14: 15
//start outQueue
//outQueue:0: 1
//outQueue:1: 2
//outQueue:2: 3
//outQueue:3: 4
//outQueue:4: 5
//outQueue:5: 6
//outQueue:6: 7
//outQueue:7: 8
//outQueue:8: 9
//queue is empty
//outQueue:9: 0
//queue is empty
//outQueue:10: 0
//queue is empty
//outQueue:11: 0
//queue is empty
//outQueue:12: 0
//queue is empty
//outQueue:13: 0
//queue is empty
//outQueue:14: 0
印象中之前看到的关于queue的代码是封装在结构体里面的。至于详细怎么处理的不太清楚了。
2024年1月16日15:58:23
更新上面的问题
看了下之前的代码,形式大概如下,
定义队列结构体
typedef struct Queue_hr
{
int head;
int rear;
int length;
}queue_hr;
typedef struct UartQueue
{
queue_hr hr;
int *item;
int size;
}uartQueue;
使用数组实例化队列
int main()
{
uartQueue writeQueue;
int writeArray[10];
writeQueue.item = writeArray;
writeQueue.size = 10;
printf("%s\n", __FILE__);
Test test;
testFun(&test);
printf("%d, %d\n", test.a, *test.p);
printf("Hello world!\n");
int *p = &g_stu_lianbiao;
stu_list_func(&g_stu_lianbiao);
return 0;
}
二、其他相关
2.1 判断队列状态
在备考的时候,涉及到队列的判断,
即判断队列是否为空方法:rear == front。
判断队列是否满方法(rear + 1) % QUEUE_SIZE == head。
看了下之前写的这篇帖子,验证了确实如此。
总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。