# include <stdio.h>
# include <stdlib.h>
# include "queue.h"
# include "linkqueue.h"
int main ( )
{
int n;
int i;
int x;
seqQueue * Q;
linkQueue * M;
do
{
printf ( "请选择你想要实现的功能\n" ) ;
printf ( "------1.使用顺序队列---\n" ) ;
printf ( "------2.使用链式队列---\n" ) ;
scanf ( "%d" , & n) ;
switch ( n)
{
case 1 :
system ( "CLS" ) ;
farhanshu ( & Q) ;
system ( "Pause" ) ;
break ;
case 2 :
system ( "CLS" ) ;
queueFun_2 ( & M) ;
system ( "Pause" ) ;
break ;
default :
printf ( "Selection is error!\n" ) ;
system ( "Pause" ) ;
}
} while ( 1 ) ;
return 0 ;
}
# include <stdio.h>
# include <stdlib.h>
# define MAXLEN 100
typedef struct
{
int data[ MAXLEN] ;
int front;
int rear;
} seqQueue;
void initQueue ( seqQueue * Q)
{
Q-> front= 0 ;
Q-> rear= 0 ;
}
int ifvoid ( seqQueue * Q)
{
if ( Q-> front== Q-> rear)
return 1 ;
else
return 0 ;
}
int iffull ( seqQueue * Q)
{
int x;
x= ( Q-> rear+ 1 ) % MAXLEN;
if ( x== Q-> front)
return 1 ;
else
return 0 ;
}
int queuelength ( seqQueue * Q)
{
return ( Q-> rear- Q-> front+ MAXLEN) % MAXLEN;
}
int inqueue ( seqQueue * Q, int x)
{
if ( iffull ( Q) == 0 )
return 0 ;
Q-> data[ Q-> rear] = x;
Q-> rear= ( Q-> rear+ 1 ) % MAXLEN;
return 1 ;
}
int outqueue ( seqQueue * Q)
{
int * e;
if ( ifvoid ( Q) == 1 )
return 1 ;
* e= Q-> data[ Q-> front] ;
Q-> front= ( Q-> front- 1 ) % MAXLEN;
return 0 ;
}
void ShowQueue ( seqQueue* Q)
{
int i;
printf ( "当前队列输出为" ) ;
for ( i= Q-> front; i< Q-> rear; i++ )
{
printf ( "%d " , Q-> data[ i] ) ;
}
printf ( "\n" ) ;
}
int GetHead ( seqQueue * Q, int * e)
{
if ( Q-> front== Q-> rear)
{
return 1 ;
}
* e= Q-> data[ Q-> front] ;
return 0 ;
}
void farhanshu ( seqQueue * Q)
{
initQueue ( Q) ;
int x;
printf ( "请输入你想要输入的元素,按-999退出" ) ;
while ( x!= - 999 )
{
scanf ( "%d" , & x) ;
if ( x% 2 != 0 )
{
inqueue ( Q, x) ;
ShowQueue ( Q) ;
}
else if ( x% 2 == 0 )
{
outqueue ( Q) ;
ShowQueue ( Q) ;
}
else
{
ShowQueue ( Q) ;
break ;
}
}
}
# include <stdio.h>
# include <stdlib.h>
# include <stdbool.h>
# define SIZE sizeof ( node)
typedef struct LNode
{
int data;
struct LNode * next;
} node;
typedef struct
{
node * front;
node * rear;
} linkQueue;
void initialQueue_2 ( linkQueue * Q)
{
Q-> front = ( node * ) malloc ( SIZE) ;
Q-> rear = Q-> front;
Q-> front-> next = NULL ;
}
bool queueEmpty_2 ( linkQueue * Q)
{
return ( Q-> front == Q-> rear) ;
}
void print ( linkQueue* q)
{
node * p = q-> front-> next;
if ( q-> front== q-> rear) {
printf ( "空队\n" ) ;
}
printf ( "当前队列中的元素为" ) ;
while ( p) {
printf ( "%d " , p-> data) ;
p = p-> next;
}
printf ( "\n\n" ) ;
}
void queueFront_2 ( linkQueue * Q, int * x)
{
if ( queueEmpty_2 ( Q) )
printf ( "空队列,无法取队头元素\n" ) ;
else
* x = Q-> front-> next-> data;
}
void enQueue_2 ( linkQueue * Q, int x)
{
node * P;
P = ( node * ) malloc ( SIZE) ;
P-> data = x;
P-> next = NULL ;
Q-> rear-> next = P;
Q-> rear = P;
}
void outQueue_2 ( linkQueue * Q)
{
node * u;
if ( queueEmpty_2 ( Q) )
printf ( "当前队空 无法执行出队操作\n" ) ;
else
{
u = Q-> front-> next;
Q-> front-> next = u-> next;
free ( u) ;
if ( Q-> front-> next == NULL )
Q-> rear = Q-> front;
}
}
int queueNumber_2 ( linkQueue * Q)
{
int i;
node * p;
i = - 1 ;
p = Q-> front;
while ( ( ! queueEmpty_2 ( Q) ) && Q-> rear-> next != p)
{
p = p-> next;
i++ ;
}
return i;
}
void queueFun_2 ( linkQueue * Q)
{
int m, x;
initialQueue_2 ( Q) ;
printf ( "请输入元素值(输入0结束)\n" ) ;
do
{
scanf ( "%d" , & m) ;
if ( m == 0 )
break ;
else if ( m % 2 != 0 )
{
enQueue_2 ( Q, m) ;
print ( Q) ;
}
else
{
outQueue_2 ( Q) ;
print ( Q) ;
}
} while ( 1 ) ;
if ( Q-> front== Q-> rear)
printf ( "队列为空\n" ) ;
else
{
printf ( "队列中的元素为\n" ) ;
while ( ! queueEmpty_2 ( Q) )
{
queueFront_2 ( Q, & x) ;
printf ( "%d\t" , x) ;
outQueue_2 ( Q) ;
}
printf ( "\n" ) ;
}
}