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

全部博文(78)

文章存档

2011年(31)

2010年(47)

分类: C/C++

2010-08-01 17:43:14

/* ************************************************************************
 *       Filename:  queue.h
 *    Description:  chengbin_liu
 *        Version:  1.0
 *        Created:  2010年08月01日 15时06分09秒
 *       Revision:  none
 *       Compiler:  gcc
 *         Author:  YOUR NAME (),
 *        Company: 
 * ************************************************************************/
#ifndef _QUEUE_H_
#define _QUEUE_H_
#include
#include
#include
#define MAX 10
typedef struct item
{
 int gumption;
 int charisma;
}Item;
typedef struct node
{
 Item item;
 struct node *next;
}Node;
typedef struct queue
{
 Node *front;
 Node *rear;
 int item;
}Queue;
void InitializeQueue(Queue *pq);
bool QueueIsFull(const Queue *pq);
bool QueueIsEmpty(const Queue *pq);
int QueueItemCount(const Queue *pq);
bool DeQueue(Item *pitem,Queue *pq);//delete item from queue.
bool EnQueue(Item item,Queue *pq);//insert item to queue.
void EnptyQueue(Queue *pq);
#endif

//===================================================================
/* ************************************************************************
 *       Filename:  queue.c
 *    Description: chengbin_liu
 *        Version:  1.0
 *        Created:  2010年08月01日 15时15分04秒
 *       Revision:  none
 *       Compiler:  gcc
 *         Author:  YOUR NAME (),
 *        Company: 
 * ************************************************************************/
#include "queue.h"
static void CopyToNode(Item item, Node *pn);
static void CopyToItem(Node *pn,Item *pi);
void InitializeQueue(Queue *pq)
{
 pq->front=pq->rear=NULL;
 pq->item=0;
}
bool QueueIsFull(const Queue *pq)
{
 return pq->item==MAX;
}
bool QueueIsEmpty(const Queue *pq)
{
 return pq->item==0;
}
int QueueItemCount(const Queue *pq)
{
 return pq->item;
}
bool EnQueue(Item item,Queue *pq)
{
 Node *pnew;
 if(QueueIsFull(pq))
  return false;
 pnew=(Node *)malloc(sizeof(Node));
 if(pnew==NULL)
 {
  fprintf(stderr,"unable to allocate memory!\n");
  exit(1);
 }
 CopyToNode(item,pnew);
 pnew->next=NULL;
 if(QueueIsEmpty(pq))
  pq->front=pnew;
 else
  pq->rear->next=pnew;
 pq->rear=pnew;
 pq->item++;
 return true;
}
bool DeQueue(Item *pitem,Queue *pq)
{
 Node *pt;
 if(QueueIsEmpty(pq))
  return false;
 CopyToItem(pq->front,pitem);
 pt=pq->front;
 pq->front=pq->front->next;
 free(pt);
 pq->item--;
 if(pq->item==0)
  pq->rear=NULL;
 return true;
}
void EmptyTheQueue(Queue *pq)
{
 Item dummy;
 while(!QueueIsEmpty(pq))
  DeQueue(&dummy,pq);
}
static void CopyToNode(Item item,Node *pn)
{
 pn->item=item;
}
static void CopyToItem(Node *pn,Item *pi)
{
 *pi=pn->item;
}
//==============================================================
阅读(1183) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~