汇编网首页登录博客注册
maxm的学习博客
博客首页博客互动【做检测题】论坛求助

我的博客

个人首页 |  我的文章 |  我的相册 |  我的好友 |  最新访客 |  文章收藏 |  论坛提问 |  友情链接 |  给我留言  
图片载入中
  •  做不了第一个就做最好的, 做不了最好的就做第一个。
  • 『姓名』:
  • 『性别』:保密『发送消息
  • 个人说明:不要执着于学习步骤, 宁愿多看点知识
  • 详细信息『加为好友』
学习动态
最新留言
友情链接

[2009-08-04 00:27] queue

#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();
}
评论次数(1)  |  浏览次数(964)  |  类型(c) |  收藏此文  | 

[  游客   发表于  2009-08-06 15:30  ]

实现队列?呵呵,以前学C的时候也弄过队,循环队列等东西,锻炼了很多思维逻辑上的东西。
博主可以自己做个栈,然后在其他程序中使用这个栈,也挺有意思。

 
 请输入验证码  (提示:点击验证码输入框,以获取验证码