C实现链式队列

来源:互联网 发布:淘宝电热护肩定制 编辑:程序博客网 时间:2024/05/16 00:51
/*****************************************                   C实现链式队列*****************************************/#include<stdio.h>#include<stdlib.h>#define OK 1#define ERROR 0typedef int QueueData;typedef struct node{    QueueData data;       //结点数据    struct node * next;       //结点链指针} QueueNode;typedef struct LinkQueue{    QueueNode *rear, *front;} LinkQueue; LinkQueue* InitQueue (void) {    LinkQueue *Q=( LinkQueue * ) malloc( sizeof ( LinkQueue ) );    Q->rear=Q->front=NULL;return Q;} int QueueEmpty ( LinkQueue *Q )  {    return Q->front == NULL;}int GetFront ( LinkQueue *Q, QueueData *x ){    if ( QueueEmpty (Q) ) return 0;    (*x) = Q->front->data;  return 1;}int EnQueue ( LinkQueue **Q, QueueData x ){    QueueNode *p = ( QueueNode * ) malloc( sizeof ( QueueNode ) );    p->data = x; p->next = NULL;    if ( (*Q)->front == NULL )          //空,创建第一个结点       (*Q)->front = (*Q)->rear = p;    else (*Q)->rear = (*Q)->rear->next = p;    return 1;}int DeQueue ( LinkQueue **Q, QueueData *x) {//删去队头结点,并返回队头元素的值    if ( QueueEmpty (*Q) ) return 0;//判队空    QueueNode *p = (*Q)->front;    (*x) = p->data;                           //保存队头的值    (*Q)->front = (*Q)->front->next;  //新队头    if ((*Q)->front == NULL) (*Q)->rear = NULL;    free (p);    return 1;}int main(){       int x=0,i=0;    LinkQueue *Q;Q=InitQueue();for(i=0;i<10;i++){    EnQueue(&Q,i);}for(i=0;i<10;i++){    DeQueue(&Q,&x);printf("%d\n",x);}return 0;}

原创粉丝点击