标签:
原文地址:http://blog.csdn.net/hguisu/article/details/7674195
1 #include "stdafx.h" 2 #include "stdlib.h" 3 #include <stdio.h> 4 #include <iostream> 5 using namespace std; 6 7 #define TRUE 1 8 #define FALSE 0 9 #define OK 1 10 #define ERROR 0 11 #define INFEASIBLE -1 12 #define OVERFLOW -2 13 #define QUEUEEMPTY -3 14 15 16 typedef int Status; 17 typedef int ElemType; 18 19 typedef struct LNode{ 20 ElemType data; 21 struct LNode* next; 22 }LNode,*LinkList; 23 24 typedef struct queue{ 25 LinkList front; 26 LinkList rear; 27 }QUEUE; 28 29 void InitQueue(QUEUE *Q); 30 void EnQueue(QUEUE *Q,ElemType elem); 31 void DeQueue(QUEUE *Q,ElemType *elem); 32 void GetFront(QUEUE Q,ElemType *elem); 33 bool QueueEmpty(QUEUE Q); 34 int FreeQueue(QUEUE Q); 35 36 //初始化队列 37 void InitQueue(QUEUE *Q) 38 { 39 Q->front = (LinkList)malloc(sizeof(LNode)); 40 if(Q->front == NULL) exit(ERROR); 41 Q->rear = Q->front; 42 } 43 44 //入队 45 void EnQueue(QUEUE *Q,ElemType elem) 46 { 47 LinkList s; 48 s = (LinkList)malloc(sizeof(LNode)); 49 if(!s) exit(ERROR); 50 s->data = elem; 51 s->next = NULL; 52 Q->rear->next = s; 53 Q->rear = s; 54 } 55 56 //出队 57 void DeQueue(QUEUE *Q,ElemType *elem) 58 { 59 LinkList s; 60 if(QueueEmpty(*Q)) exit(ERROR); 61 *elem = Q->front->next->data; 62 s = Q->front->next; 63 Q->front->next = s->next; 64 free(s); 65 } 66 67 //获取队头元素内容 68 void GetFront(QUEUE Q,ElemType *elem) 69 { 70 if(QueueEmpty(Q)) exit(ERROR); 71 *elem = Q.front->next->data; 72 } 73 74 //判断队列Q是否为空 75 bool QueueEmpty(QUEUE Q) 76 { 77 if(Q.front == Q.rear) return TRUE; 78 return FALSE; 79 } 80 81 //释放队列 82 int FreeQueue(QUEUE *Q) 83 { 84 while(Q->front) 85 { 86 Q->rear = Q->front->next; 87 free(Q->front); 88 Q->front = Q->rear; 89 } 90 return OK; 91 } 92 93 int _tmain(int argc, _TCHAR* argv[]) 94 { 95 QUEUE Q; 96 InitQueue(&Q); 97 EnQueue(&Q,1); 98 EnQueue(&Q,2); 99 ElemType e; 100 DeQueue(&Q,&e); 101 cout <<"De queue:"<<e; 102 103 FreeQueue(&Q); 104 105 DeQueue(&Q,&e); 106 cout <<"De queue:"<<e; 107 system("pause"); 108 return 0; 109 }
标签:
原文地址:http://www.cnblogs.com/nightcatcher/p/4464013.html