5-11.c
来自「含有大量数据结构的源代码 请大家漫漫品味」· C语言 代码 · 共 72 行
C
72 行
#include "stdio.h"
typedef char datatype;
typedef struct node{
datatype data;
struct node *next;
} position;
typedef struct queue{
position *front;
position *rear;
}queuetype;
//使队列为空:
void MackNull(queuetype * q)
{
q->rear=q->front;
while(q->front!=NULL){
q->front=q->front->next;
free(q->rear);/*释放空间*/
q->rear=q->front;
}
q->front=(position*)malloc(sizeof(position*));
q->front->next=NULL;
q->rear=q->front;
}
// 取队列的队头元素:
datatype Front(queuetype * q)
{
if(Empty(q))
printf("'The queue is empty!");
else
return(q->front->next->data);
}
//删除队列头元素:
void dequeue(queuetype * q)
{
position* p;
if(Empty(q))
printf("'The queue is empty!");
else{
p=q->front;
q->front=q->front->next;
free(p);
}
}
// 在队列中加入新元素:
void Enqueue(datatype x,queuetype * q)
{
position* p;
p=(position*)malloc(sizeof(position*));
p->data=x;
p->next=NULL;
q->rear->next=p;
q->rear=p;
}
//判断是否为空队列:
int Empty(queuetype * q)
{
return (q->front==q->rear);
}
void main()
{
queuetype * m_q;
char m_top;
if(!Empty(m_q))
{
m_top=Front(m_q);
dequeue(m_q);
}
else
Enqueue('c',m_q);
MackNull(m_q);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?