数据结构代码实现(循环队列的实现,数组)

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define MAXQSIZE 6
typedef struct QUEUE
{
	 
	 int	*pBase;
	 int	front;
	 int	rear;
}QUEUE, *PQUEUE;
void	init_queue( PQUEUE );
bool	traverse_queue( PQUEUE );
bool	en_queue( PQUEUE, int val );
bool	de_queue( PQUEUE, int *val );
bool	is_full( PQUEUE );
bool	is_empty( PQUEUE );
int main()
{
	 QUEUE	q;
	 int 	val;
	 init_queue( &q );
	 en_queue( &q, 5 );
	 en_queue( &q, 4 );
	 en_queue( &q, 3 );
	 en_queue( &q, 2 );
	 en_queue( &q, 1 );
	 de_queue( &q, &val );
	 printf( "you delete is %d\n", val );
	 traverse_queue( &q );
	 
	 
	 
	 
		 return 0;
}


void init_queue( PQUEUE pQ )
{
	 pQ->pBase = (int *) malloc( sizeof(int) * MAXQSIZE );
	 if( NULL == pQ->pBase )
	  {
		 	printf( "malloc failure\n" );
		 	exit( -1 );
		 
	}else   {
		 pQ->front = 0;
		    pQ->rear = 0;
	}
	 return;
}


bool is_empty( PQUEUE pQ )
{
	 if( pQ->rear == pQ->front )
	  return true;
	 else
	  return false;
}


bool is_full( PQUEUE pQ )
{
	 if( (pQ->rear + 1) % MAXQSIZE == pQ->front )
	  return true;
	 else
	  return false;
	 
}


bool traverse_queue( PQUEUE pQ )
{
	  if ( is_empty( pQ ) )
		  {
			 printf( "queue is empty\n" );
			 return false;
			 
		}
		  else
			  {
				   int i = pQ->front;
				   while ( i != pQ->rear )
					   {
						  printf( "%d  ", pQ->pBase[i] );
						  i++;
						  i = i % MAXQSIZE;
						  
					}
					 return true;
				 
				 
					 
			}
			}
			bool en_queue( PQUEUE pQ, int val )
			{
				 if( (is_full( pQ ) ) )
				  {
					  printf( "queue is full \n" );
					  return false;
					 
				}
				 else
					  {
					 pQ->pBase[pQ->rear]	= val;
					 pQ->rear		= (pQ->rear + 1) % MAXQSIZE;  
						 
				}
				 return true;
				 
					 
			}


			bool de_queue( PQUEUE pQ, int *val )
			{
				 if( is_empty( pQ ) )
				  {
					  printf( "queue is empty \n" );
					  return false;
					 
				}
				 else
					  {   
					      *val = pQ->pBase[pQ->front];
					      pQ->front = (pQ->front + 1) % MAXQSIZE;
					      
						      }
				 return true;
				 
			}


发布了5 篇原创文章 · 获赞 0 · 访问量 415

猜你喜欢

转载自blog.csdn.net/guidgeek/article/details/78471890