#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct node
{
int data;
struct node *next;
}node,*pnode;
//该队列包含了一个队头和一个队尾
typedef struct queue
{
pnode front;
pnode rear;
}queue,*pqueue;
//初始化队列
void makenull(pqueue q)
{
q->front=(pnode)malloc(sizeof(node));
q->front->next=NULL;
q->rear=q->front;
}
//检查队列是不是空的
bool empty(pqueue q)
{
return (q->front==q->rear);
}
//返回队列第一个元素值
int front(pqueue q)
{
while(!empty(q))
return q->front->next->data;
}
void enqueue(int value,pqueue q)
{
q->rear->next=(pnode)malloc(sizeof(node));
q->rear=q->rear->next;
q->rear->data=value;
q->rear->next=NULL;
}
int dequeue(pqueue q)
{
if(!empty(q))
{
pnode tmp;
tmp=q->front->next;
q->front=q->front->next;
return tmp->data;
free(tmp);
}
else
{
printf("队列是空的.\n");
}
}
int main()
{
pqueue q = (pqueue)malloc(sizeof(queue));
makenull(q);
enqueue(3,q);
enqueue(5,q);
printf("[%d]", dequeue(q));
printf("[%d]", dequeue(q));
system("Pause");
getch();
}
- [游客] 表示每次运行到cx=2的时候就不能再往下一步进行了! 谁能告诉我是哪里的问题? 06/11 23:38
- [游客] 这是C下的么?main中的 int c[]={6,7,8,9} ; int *p = c; 08/06 15:33
- [游客] 实现队列?呵呵,以前学C的时候也弄过队,循环队列等东西,锻炼了很多思维逻辑上的东西。 博主可以自己 08/06 15:30
- [maxm] 这实验没难度,关键是细心 几个易出错的地儿我都在注释中做了标记 希望大家别跟我一样犯糊涂 (*^ 06/04 20:58
- [maxm] 感谢fangyugirl分享心得 05/08 18:47
- [fangyugirl] 看到你说你在这章徘徊了两周,我跟你差不多的。我是做BIOS的,上班时没有事情了就看书,做课程设计一时 04/23 13:44
- [maxm] 哎,没有自己电脑。 03/19 20:57
- [游客] 然后继续编程 呵呵 03/18 20:56
- [游客] 我被卡在这一章了,就因为这个实验。 03/18 15:31
- [maxm] 最后加了点注释,程序改动如下: ;------------------------------ 03/13 15:52
[ 游客 发表于 2009-08-06 15:30 ]
实现队列?呵呵,以前学C的时候也弄过队,循环队列等东西,锻炼了很多思维逻辑上的东西。
博主可以自己做个栈,然后在其他程序中使用这个栈,也挺有意思。