#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct node{
int value;
struct node * pNext;
}NODE,*PNODE;
typedef struct queue{
PNODE front;
PNODE rear;
}QUEUE,*PQUEUE;
void init_queue(PQUEUE pQueue);
void destory_queue(PQUEUE pQueue);
void clean_queue(PQUEUE pQueue);
bool is_empty(PQUEUE pQueue);
void get_head(PQUEUE pQueue,int *e);
void add_elem(PQUEUE pQueue);
void del_tail(PQUEUE pQueue);
void get_length(PQUEUE pQueue);
void trea_queue(PQUEUE pQueue);
int main()
{
int e = 0;
QUEUE queue;
init_queue(&queue);
add_elem(&queue);
trea_queue(&queue);
destory_queue(&queue);
trea_queue(&queue);
return 0;
}
void init_queue(PQUEUE pQueue)
{
pQueue->front = pQueue->rear = NULL;
}
void add_elem(PQUEUE pQueue)
{
int val;
while(scanf("%d",&val) && val != -1){
PNODE p = (PNODE)malloc(sizeof(NODE));
if(pQueue->front ==NULL && pQueue->rear == NULL){
p->value = val;
p->pNext = NULL;
pQueue->front = pQueue->rear = p;
}else{
p->value = val;
p->pNext = NULL;
pQueue->rear->pNext = p;
pQueue->rear = p;
}
}
}
void trea_queue(PQUEUE pQueue)
{
PNODE p = pQueue->front;
for(;p;p = p->pNext){
printf("%d ",p->value);
}
printf("
");
}
void del_tail(PQUEUE pQueue)
{
PNODE p = pQueue->front;
if(pQueue->front == pQueue->rear){
pQueue->front = pQueue->front->pNext;
free(p);
pQueue->front = pQueue->rear = NULL;
}else{
pQueue->front = pQueue->front->pNext;
free(p);
}
}
void get_length(PQUEUE pQueue)
{
if(pQueue->front == NULL && pQueue->rear == NULL){
printf("ERRORs
");
}else{
int cnt = 0;
PNODE p = pQueue->front;
while(p){
cnt++;
p = p->pNext;
}
printf("The length of the queue is %d
",cnt);
}
}
bool is_empty(PQUEUE pQueue)
{
if(pQueue->front == NULL && pQueue->rear == NULL){
printf("The queue is empty
");
return true;
}else{
printf("The queue is not empty
");
return false;
}
}
void get_head(PQUEUE pQueue,int *e)
{
if(is_empty(pQueue)){
printf("SORRY IS EMPTY
");
}else{
*e = pQueue->front->value;
}
}
void clean_queue(PQUEUE pQueue)
{
PNODE p = pQueue->front;
while(p){
p->value = 0;
p = p->pNext;
}
}
void destory_queue(PQUEUE pQueue)
{
PNODE p = pQueue->front;
while(pQueue->front != NULL && pQueue->rear != NULL){
del_tail(pQueue);
}
}