队列的简单实现
程序代码:
//静态数组实现: #define QueueType int void Insert( QueueType value );//将一个值插入队列 void Delete( void );//将一个值从队列中删除,但不弹出 QueueType First( void );//将一个值弹出队列,但不删除 int is_empty( void );//空队列检查 int is_full( void );//满队列检查
程序代码:
#include "Quen.h" #include <stdio.h> #include <assert.h> #define QUEUE_MAX_SIZE 100 #define ARRAY_MAX_SIZE ( QUEUE_MAX_SIZE + 1 ) static QueueType QueueArray[ ARRAY_MAX_SIZE ]; static int Frnot = 1; static int Rear = 0; void Insert( QueueType value ) { if( is_full() ) { printf( "队列已满" ); return; } else { Rear = ( Rear + 1 ) % ARRAY_MAX_SIZE; QueueArray[ Rear ] = value; } } void Delete( void ) { assert( !is_empty() ); Frnot = ( Frnot + 1 ) % ARRAY_MAX_SIZE; } QueueType First( void ) { assert( !is_empty() ); return QueueArray[ Frnot ]; } int is_empty( void ) { return ( Rear + 1 ) % ARRAY_MAX_SIZE == Frnot;//空队列返回1,否则返回0 } int is_full( void ) { return ( Rear + 2 ) % ARRAY_MAX_SIZE == Frnot; }
程序代码:
//动态数组实现: #define QueueType int void CreateQueue( void ); void DestroyQueue( void ); void Insert( QueueType value ); void Delete( void ); QueueType First( void ); int IsEmpty( void ); int IsFull( void );
程序代码:
#include <stdio.h> #include <stdlib.h> #include <assert.h> #include "MallocQueue.h" #define QUEUESIZE 124 static QueueType *Queue; static int CurrentSize = QUEUESIZE + 1; static int Read = 0; static int Frnot = 1; void Insert( QueueType value ) { if( IsFull() || NULL == Queue ) { printf( "队列已满,插入队列失败\n" ); return; } Read = ( Read + 1 ) % CurrentSize; Queue[ Read ] = value; } void Delete( void ) { if( !IsEmpty() && NULL != Queue ) Frnot = ( Frnot + 1 ) % CurrentSize; } QueueType First( void ) { assert( !IsEmpty() ); assert( NULL != Queue ); return Queue[ Frnot ]; } int IsFull( void ) { int i; QueueType *Temp; i = ( Read + 2 ) % CurrentSize; if( NULL == Queue ) { printf( "队列不存在\n" ); return 1; } if( i == Frnot ) { CurrentSize += QUEUESIZE; Temp = ( QueueType * )realloc( Queue, CurrentSize * sizeof( QueueType ) ); if( NULL == Temp ) { CurrentSize -= QUEUESIZE; return 1; } Queue = Temp; return 0; } return 0; } int IsEmpty( void ) { return ( Read + 1) % CurrentSize == Frnot; } void CreateQueue( void ) { if( NULL == Queue ) { Queue = ( QueueType * )malloc( CurrentSize * sizeof( QueueType ) ); if( NULL == Queue ) printf( "创建队列失败\n" ); } else { printf( "队列已存在,创建失败\n" ); return; } } void DestroyQueue( void ) { if( NULL != Queue ) { free( Queue ); Queue = NULL; CurrentSize = QUEUESIZE + 1; Read = 0; Frnot = 1; } else { printf( "队列不存在,销毁失败\n" ); return; } }
程序代码:
//链表实现 #define QueueType int void CreateQueue( void ); void DestroyQueue( void ); void Insert( QueueType value ); void Delete( void ); QueueType First( void ); int IsFull( void ); int IsEmpty( void );
程序代码:
#include <stdio.h> #include <stdlib.h> #include <assert.h> #include "ListQueue.h" struct Node{ QueueType Value; struct Node *Next; }; static struct Node *Queue; void CreateQueue( void ) { } void DestroyQueue( void ) { struct Node *Temp; struct Node *Next; if( NULL == Queue ) return; Next = Queue; Queue = NULL; while( NULL != Next ) { Temp = Next; Next = Next->Next; free( Temp ); } } int IsFull( void ) { return Queue == NULL; } int IsEmpty( void ) { return Queue == NULL; } void Insert( QueueType value ) { struct Node *Next; struct Node **Temp; struct Node *NewCell; Temp = &Queue; for( Next = *Temp; NULL != Next; Next = *Temp ) Temp = &Next->Next; NewCell = ( struct Node * )malloc( sizeof( struct Node ) ); if( NULL == NewCell ) { printf( "插入队列失败\n" ); return; } NewCell->Value = value; *Temp = NewCell; NewCell->Next = NULL; } QueueType First( void ) { assert( !IsEmpty() ); return Queue->Value; } void Delete( void ) { struct Node *Temp; assert( !IsEmpty() ); Temp = Queue; Queue = Queue->Next; free( Temp ); }