数据结构与算法分析-队列的实现

 #ifndef _QUEUE_H #define _QUEUE_H struct QueueRecord; typedef struct QueueRecord *Queue; struct Node; typedef struct Node *PtrNode; int isEmpty( Queue Q ); Queue CreateQueue(); void DisposeQueue( Queue Q ); void MakeEmpty( Queue Q ); void Enqueue( int X, Queue Q ); int Front( Queue Q ); void Dequeue( Queue X ); #endif

 

#include <stdio.h> #include <stdlib.h> #include <assert.h> #include <malloc.h> #include "queue.h" struct QueueRecord { PtrNode Front; PtrNode Rear; }; struct Node { int Element; PtrNode Next; }; int isEmpty( Queue Q ) { return Q->Rear == NULL; } Queue CreateQueue() { Queue q; q = malloc( sizeof(struct QueueRecord) ); assert( q!=NULL ); q->Front = NULL; q->Rear = NULL; return q; } void Enqueue( int X, Queue Q ) { PtrNode q; assert ( Q != NULL ); q = malloc( sizeof(struct Node) ); assert ( q != NULL); q->Element = X; q->Next = NULL; if ( Q->Rear == NULL ) { Q->Front = q; Q->Rear = q; } else { Q->Rear->Next = q; Q->Rear = q; } } void Dequeue( Queue Q ) { PtrNode q; assert( Q!= NULL ); if ( Q->Front != NULL ) { q = Q->Front; Q->Front = q->Next; free(q); } } int Front( Queue Q ) { PtrNode tmp; assert( Q!=NULL ); if ( Q->Front != NULL) { tmp = Q->Front; return tmp->Element; } return -1; } void DisposeQueue( Queue Q ) { PtrNode tmp, del; assert( Q != NULL ); tmp = Q->Front; while( tmp != NULL ) { del = tmp->Next; free(tmp); } } void main() { Queue q; int tmp; q = CreateQueue(); assert( q!= NULL); Enqueue( 1, q ); tmp = Front(q); printf("---%d---/n", tmp); Enqueue( 2,q ); tmp = Front(q); printf("---%d---/n", tmp); Dequeue( q ); tmp = Front(q); printf("---%d---/n", tmp); }

 

 

你可能感兴趣的:(数据结构与算法分析-队列的实现)