Special implementations of queues
The combination of two-stack LIFO enables the queue to be FIFO, simple and efficient, and the time complexity of queuing and outbound can go to O (1)
SQueue.h
#ifndef _squeue_h_#define_squeue_h_typedefvoidSqueue; Squeue*squeue_create ();voidSqueue_destroy (squeue*queue);voidSqueue_clear (squeue*queue);intSqueue_append (squeue* queue,void*item);void* Squeue_retrieve (squeue*queue);void* Squeue_header (squeue*queue);intSqueue_length (squeue*queue);#endif
Squeue.c
#include <stdio.h>#include<malloc.h>#include"LinkStack.h"#include"SQueue.h"typedefstruct_tag_squeue{Linkstack*Instack; Linkstack*Outstack;} Tsqueue; Squeue* Squeue_create ()//O (1){tsqueue* ret = (tsqueue*)malloc(sizeof(Tsqueue)); if(Ret! =NULL) {ret->instack =linkstack_create (); RET->outstack =linkstack_create (); if((Ret->instack = = NULL) | | (Ret->outstack = =NULL)) {Linkstack_destroy (ret-instack); Linkstack_destroy (ret-outstack); Free(ret); RET=NULL; } } returnret;}voidSqueue_destroy (squeue* queue)//O (n){squeue_clear (queue); Free(queue);}voidSqueue_clear (squeue* queue)//O (n){tsqueue* Squeue = (tsqueue*) queue; if(Squeue! =NULL) {Linkstack_clear (Squeue-instack); Linkstack_clear (Squeue-outstack); }}intSqueue_append (squeue* queue,void* Item)//O (1){tsqueue* Squeue = (tsqueue*) queue; if(Squeue! =NULL) {Linkstack_push (Squeue-instack, item); }}void* Squeue_retrieve (squeue* queue)//O (1){tsqueue* Squeue = (tsqueue*) queue; void* ret =NULL; if(Squeue! =NULL) { if(Linkstack_size (squeue->outstack) = =0 ) { while(Linkstack_size (Squeue->instack) >0) {Linkstack_push (Squeue->outstack, Linkstack_pop (squeue->instack)); }} RET= Linkstack_pop (squeue->outstack); } returnret;}void* Squeue_header (squeue* queue)//O (1){tsqueue* Squeue = (tsqueue*) queue; void* ret =NULL; if(Squeue! =NULL) { if(Linkstack_size (squeue->outstack) = =0 ) { while(Linkstack_size (Squeue->instack) >0) {Linkstack_push (Squeue->outstack, Linkstack_pop (squeue->instack)); }} RET= Linkstack_top (squeue->outstack); } returnret;}intSqueue_length (squeue* queue)//O (1){tsqueue* Squeue = (tsqueue*) queue; intRET =-1; if(Squeue! =NULL) {ret= Linkstack_size (squeue->instack) + linkstack_size (squeue->outstack); } returnret;}
Main.c
#include <stdio.h>#include<stdlib.h>#include"SQueue.h"/*Run this program using the console Pauser or add your own getch, System ("pause") or input loop*/intMainintargcChar*argv[]) {Squeue* Queue =squeue_create (); inta[Ten] = {0}; inti =0; for(i=0; i<Ten; i++) {A[i]= i +1; Squeue_append (Queue, a+i); } printf ("Header:%d\n", *(int*) Squeue_header (queue)); printf ("Length:%d\n", Squeue_length (queue)); for(i=0; i<5; i++) {printf ("Retrieve:%d\n", *(int*) Squeue_retrieve (queue)); } printf ("Header:%d\n", *(int*) Squeue_header (queue)); printf ("Length:%d\n", Squeue_length (queue)); for(i=0; i<Ten; i++) {A[i]= i +1; Squeue_append (Queue, a+i); } while(Squeue_length (Queue) >0) {printf ("Retrieve:%d\n", *(int*) Squeue_retrieve (queue)); } squeue_destroy (queue); return 0;}
Data Structures-Linear tables-queues