标签:int ima node null == alt pre load nbsp
#include <stdio.h> #include <stdlib.h> #define OK 1; #define ERROR 0; #define OVERFLOW 0; #define TURE 1; #define FALSE 0; typedef int Status; typedef char QElemType; //队型数据 //链队中结点的定义 typedef struct QNode { QElemType data; //数据域 struct QNode *next; //指针域 }QNode,*QueuePtr; //链队的定义 typedef struct { QueuePtr front; //队头指针 QueuePtr rear; //队尾指针 }LinkQueue; //链队的初始化 Status InitQueue(LinkQueue *queue){ queue->front=queue->rear=(QueuePtr)malloc(sizeof(QNode)); if(!queue->front) return OVERFLOW; queue->front->next=NULL; //头结点next域置空 return OK; } //链队的销毁 Status DestroyQueue(LinkQueue *queue){ QNode *p; while(queue->front){ p=queue->front->next; free(queue->front); queue->front=p; } return OK; } //元素入队 Status EnQueue(LinkQueue *queue,QElemType elem){ QueuePtr p=(QueuePtr)malloc(sizeof(QNode)); if(!p) return OVERFLOW; p->data=elem; p->next=NULL; queue->rear->next=p; queue->rear=p; return OK; } //元素出队 Status DeQueue(LinkQueue *queue,QElemType *elem){ if(queue->front==queue->rear) return ERROR; QueuePtr p=queue->front->next; //p指向首元(队头元素) *elem=p->data; //出队元素的值保存到elem queue->front->next=p->next; if(p==queue->rear) queue->rear=queue->front; //如果出队的是最后一个元素 free(p); return OK; } //取队头元素 QElemType GetHead(LinkQueue queue){ if(queue.front==queue.rear) return ERROR; return queue.front->next->data; } int main(void){ LinkQueue queue1; //定义一个链队 //初始化 Status initQueueResult = InitQueue(&queue1); printf("队列queue1初始化结果:%d\n",initQueueResult); //元素入队 QElemType elem1=‘Y‘,elem2=‘C‘; Status enResult = EnQueue(&queue1,elem1); printf("入队执行结果:%d\n",enResult); EnQueue(&queue1,elem2); //将elem2也入队 printf("队尾元素值:%c\n",queue1.rear->data); //元素出队 QElemType elem3; //出队的数据放入elem3 Status deResult = DeQueue(&queue1,&elem3); printf("出队执行结果:%d\n",deResult); printf("出队元素值:%c\n",elem3); printf("队尾元素值:%c\n",queue1.rear->data); //取队头元素 QElemType queueHeadValue = GetHead(queue1); printf("获取队头元素值:%c\n",queueHeadValue); //链队销毁 Status destroyResult = DestroyQueue(&queue1); printf("链队销毁结果:%d\n",destroyResult); printf("\nEND"); return 0; }
标签:int ima node null == alt pre load nbsp
原文地址:https://www.cnblogs.com/petitepluie/p/14599401.html