/* ************************************************************************
* 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;
}
阅读(579) | 评论(0) | 转发(0) |