链队列的基本操作学生信息
时间: 2023-07-07 14:03:24 浏览: 65
链队列是一种基于链表实现的队列数据结构。它的基本操作包括:
1. 队列的初始化:创建一个空的链队列。
2. 入队操作(EnQueue):在队列尾部插入一个新元素。
3. 出队操作(DeQueue):删除队列头部的元素,并返回它的值。
4. 队列长度操作(QueueLength):返回队列中元素的个数。
5. 判空操作(QueueEmpty):判断队列是否为空。
对于学生信息的存储,可以定义一个结构体来表示学生信息,如下所示:
```c
typedef struct student{
char name[20]; //学生姓名
int age; //学生年龄
char gender; //学生性别
int id; //学生学号
}Student;
```
在链队列中,每个节点都存储一个学生信息的结构体,如下所示:
```c
typedef struct qnode{
Student data; //存储学生信息的结构体
struct qnode *next; //指向下一个节点的指针
}QNode, *QueuePtr;
```
然后我们定义一个队列结构体,里面包含队列的头指针和尾指针。
```c
typedef struct{
QueuePtr front; //队列头指针
QueuePtr rear; //队列尾指针
}LinkQueue;
```
有了以上的定义,我们就可以实现链队列的基本操作,具体代码如下所示。
1. 队列的初始化
```c
void InitQueue(LinkQueue *Q){
Q->front = Q->rear = (QueuePtr)malloc(sizeof(QNode));
if(!Q->front) exit(EXIT_FAILURE); //内存分配失败
Q->front->next = NULL; //初始化队列为空
}
```
2. 入队操作(EnQueue)
```c
void EnQueue(LinkQueue *Q, Student data){
QueuePtr p = (QueuePtr)malloc(sizeof(QNode));
if(!p) exit(EXIT_FAILURE); //内存分配失败
p->data = data;
p->next = NULL;
Q->rear->next = p;
Q->rear = p; //修改队尾指针
}
```
3. 出队操作(DeQueue)
```c
void DeQueue(LinkQueue *Q, Student *data){
if(Q->front == Q->rear) return; //队列为空,直接返回
QueuePtr p = Q->front->next;
*data = p->data;
Q->front->next = p->next;
if(Q->rear == p) Q->rear = Q->front; //如果删除的节点是队尾节点,修改队尾指针
free(p); //释放删除节点的空间
}
```
4. 队列长度操作(QueueLength)
```c
int QueueLength(LinkQueue Q){
int len = 0;
QueuePtr p = Q.front->next;
while(p){
len++;
p = p->next;
}
return len;
}
```
5. 判空操作(QueueEmpty)
```c
bool QueueEmpty(LinkQueue Q){
if(Q.front == Q.rear) return true; //队列为空
else return false; //队列非空
}
```
以上就是链队列的基本操作和学生信息的存储方式。通过链队列,我们可以方便地对学生信息进行添加、删除和查询等操作。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)