Chinaunix首页 | 论坛 | 博客
  • 博客访问: 347175
  • 博文数量: 78
  • 博客积分: 3380
  • 博客等级: 中校
  • 技术积分: 857
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-16 19:39
文章分类

全部博文(78)

文章存档

2011年(31)

2010年(47)

分类: C/C++

2010-08-31 21:34:42

/* ************************************************************************
 *       Filename:  Queue.c
 *    Description: 
 *        Version:  1.0
 *        Created:  08/31/2010 09:02:58 PM
 *       Revision:  none
 *       Compiler:  gcc
 *         Author:  chengbin_liu,
 *        Company: 
 * ************************************************************************/
#include
#include
#include
typedef struct _Node
{
 int data;
 struct _Node *next;
}node;
typedef struct _Queue
{
 node *front;
 node *rear;
}MyQueue;
//========================================================================
MyQueue *CreateQueue()
{
 MyQueue *q=(MyQueue *)malloc(sizeof(MyQueue));
 q->front=NULL;
 q->rear=NULL;
 return q;
}
//=======================================================================
MyQueue *enqueue(MyQueue *q,int data)
{
 node *newP=NULL;
 newP=(node *)malloc(sizeof(node));
 newP->data=data;
 newP->next=NULL;
 if(q->rear==NULL)
 {
  q->front=q->rear=newP;
 }
 else
 {
  q->rear->next=newP;
  q->rear=newP;
 }
 return q;
}
//======================================================================
MyQueue *dequeue(MyQueue *q)
{
 node *pnode=NULL;
 pnode=q->front;
 if(pnode==NULL)
 {
  printf("empty queue!\n");
 }
 else
 {
  q->front=q->front->next;
  if(q->front==NULL)
  {
   q->rear=NULL;
  }
  free(pnode);
 }
 return q;;
}
//=======================================================================
void print(MyQueue *q)
{
 node *pnode=q->front;
 if(pnode==NULL)
 {
  printf("empty queue!\n");
  return ;
 }
 printf("data:");
 while(pnode!=q->rear)
 {
  printf("%d",pnode->data);
  pnode=pnode->next;
 }
 printf("%d",pnode->data);
}
//========================================================================
int main()
{
 MyQueue *hp=CreateQueue();
 enqueue(hp,1);
 enqueue(hp,2);
 enqueue(hp,3);
 enqueue(hp,4);
 enqueue(hp,5);
 print(hp);
 dequeue(hp);
 print(hp);
 return 0;
}

 
阅读(548) | 评论(0) | 转发(0) |
0

上一篇:链表合并

下一篇:常用函数

给主人留下些什么吧!~~