#ifndef QUEUE_H_INCLUDED
#define QUEUE_H_INCLUDED
typedef int ElemType;
typedef struct SingleNode
{
ElemType data;
struct SingleNode* next;
} Node;
typedef struct linkQueue
{
Node* front;
Node* rear;
} Queue;
void initQueue ( Queue* Q, int ms)
{
Q-> front = Q-> rear = NULL ;
}
void enQueue ( Queue* Q, ElemType item)
{
Node* r = malloc ( sizeof ( Node) ) ;
r-> data = item;
r-> next = NULL ;
if ( Q-> rear == NULL )
{
Q-> front = Q-> rear = r;
}
else
{
Q-> rear-> next = r;
Q-> rear = Q-> rear-> next;
}
}
ElemType outQueue ( Queue* Q)
{
if ( Q-> front == NULL )
{
printf ( "链队为空,无法删除,退出运行!\n" ) ;
exit ( 1 ) ;
}
else
{
ElemType temp = Q-> front-> data;
Node* p = Q-> front;
Q-> front = p-> next;
if ( Q-> front == NULL )
{
Q-> rear = NULL ;
}
free ( p) ;
return temp;
}
}
ElemType peekQueue ( Queue* Q)
{
if ( Q-> front== NULL )
{
printf ( "链队为空!,无法删除,退出运行!\n" ) ;
exit ( 1 ) ;
}
return Q-> front-> data;
}
int emptyQueue ( Queue* Q)
{
return Q-> front == NULL ;
}
void clearQueue ( Queue* Q)
{
Node* p = Q-> front;
while ( p != NULL )
{
Q-> front = Q-> front-> next;
free ( p) ;
p = Q-> front;
}
Q-> rear = NULL ;
}
#endif
#pragma once
在这里插入代码片
#include <stdlib.h>
#include <stdio.h>
#include "Queue.h"
int main ( )
{
int i, x = 12 ;
Queue d, * q = & d;
initQueue ( q, 1 ) ;
enQueue ( q, 35 ) ;
enQueue ( q, 2 * x + 3 ) ;
enQueue ( q, - 16 ) ;
printf ( "%d " , peekQueue ( q) ) ;
printf ( "%d\n" , outQueue ( q) ) ;
for ( i = 0 ; i < 30 ; i + = 2 )
{
enQueue ( q, i) ;
}
while ( ! emptyQueue ( q) )
{
printf ( "%d " , outQueue ( q) ) ;
}
printf ( "\n" ) ;
clearQueue ( q) ;
return 0 ;
}