队列的顺序存储(纯代码)
#include <stdio.h>
#include <stdlib.h>
#define maxsize 100
typedef int states;
typedef int datatype;
#define ERROR 0
#define FALSE 0
#define OK 1
#define TRUE 1
typedef struct {
datatype data[ maxsize] ;
int head, footer;
} Queue;
states InitQueue ( Queue * s) {
s-> head = 0 ;
s-> footer = 0 ;
return OK;
}
states QueueEmpty ( Queue * q) {
if ( q-> head == q-> footer) {
return TRUE;
} else {
return FALSE;
}
}
states QueueFull ( Queue * q) {
if ( q-> head == maxsize - 1 ) {
return TRUE;
} else {
return FALSE;
}
} ;
states EnterQueue ( Queue * q, datatype x) {
if ( QueueFull ( q) == TRUE) {
printf ( "1213" ) ;
}
q-> data[ q-> head] = x;
q-> head++ ;
return OK;
}
states DelQueue ( Queue * q) {
if ( QueueEmpty ( q) == TRUE) {
return ERROR;
}
q-> data[ q-> footer] = NULL ;
q-> footer++ ;
return OK;
}
datatype GetHeadQueue ( Queue q) {
if ( QueueEmpty ( & q) == TRUE) {
return ERROR;
}
return q. data[ q. head - 1 ] ;
}
states ClearQueue ( Queue * q) {
int i;
if ( QueueEmpty ( q) == TRUE) {
return ERROR;
}
for ( i = q-> footer; i < q-> head; i++ ) {
q-> data[ i] = NULL ;
}
return OK;
}
datatype Out ( Queue q) {
int i;
if ( QueueEmpty ( & q) == TRUE) {
return ERROR;
}
for ( i = q. footer; i < q. head; i++ ) {
printf ( "%d " , q. data[ i] ) ;
}
}
int main ( ) {
Queue q;
int status;
int top;
InitQueue ( & q) ;
printf ( "定义队列,初始化!\n" ) ;
status = QueueEmpty ( & q) ;
printf ( "是否为空%d\n" , status) ;
EnterQueue ( & q, 12 ) ;
printf ( "将12入队\n" ) ;
status = QueueEmpty ( & q) ;
printf ( "是否为空%d\n" , status) ;
top = GetHeadQueue ( q) ;
printf ( "对顶元素%d\n" , top) ;
EnterQueue ( & q, 18 ) ;
printf ( "将18入队\n" ) ;
top = GetHeadQueue ( q) ;
printf ( "对顶元素%d\n" , top) ;
printf ( "队列元素:" ) ;
Out ( q) ;
printf ( "清空队列\n" ) ;
ClearQueue ( & q) ;
printf ( "队列元素:" ) ;
Out ( q) ;
}