/*_############################################################################
_##
_## 靜態數組實現的隊列
_## Author: xwlee
_## Time: 2006.12.31
_## Chang'an University
_## Development condition: win2003 Server+VC6.0
_##
_## static_array.cpp 檔案
_##########################################################################*/
#include "stack.h"
#include <stdio.h>
#define QUEUE_SIZE 5 // 隊列中元素的最大數量
//#define ARRAY_SIZE ( QUEUE_SIZE ) // 數組的長度
#define ARRAY_SIZE ( QUEUE_SIZE + 1) // 數組的長度 (方法2)
// 用於儲存隊列元素的數組和指向隊列頭和尾的指標
static QUEUE_TYPE queue[ ARRAY_SIZE ];
static size_t front = 1;
static size_t rear = 0;
static size_t qnumber = 0; // 引入新變數,記錄隊列中的元素數量(方法1)
// create_stack函數
int create_stack( size_t size )
{
return 1;
}
// destroy_stack函數
int destroy_stack( void )
{
return 1;
}
// insert函數
void myinsert( QUEUE_TYPE value )
{
if( is_full() )
{
printf("queue is already full, insert is false./n");
exit(0);
}
rear = ( rear + 1 ) % ARRAY_SIZE;
queue[ rear ] = value;
printf("rear=%d", rear);
qnumber++; // 引入新變數使用.(方法1)
}
// delete函數
void mydelete( void )
{
if( is_empty() ) // 若隊列已空,條件成立.
{
printf("queue already empty, delete is false./n");
exit(0);
}
front = ( front + 1 ) % ARRAY_SIZE;
printf("front=%d", front);
qnumber--; // 引入新變數使用.(方法1)
}
// first函數
QUEUE_TYPE first( void )
{
if( is_empty() ) // 若隊列已空,條件成立.
{
printf("queue already empty./n");
exit(0);
}
return queue[ front ];
}
// is_empty函數
int is_empty( void )
{
return ( rear +1 ) % ARRAY_SIZE == front;
//return qnumber == 0; // 引入新變數使用.
}
// is_full函數
int is_full( void )
{
return ( rear +2 ) % ARRAY_SIZE == front;
//return qnumber == ARRAY_SIZE; // 引入新變數使用.
}