📄 circularqueue.c
字号:
#include "../DataProcessing/MySHM.h"/* Definition of circular queue's struct */typedef struct{ int d[MAXVAL]; int front; int rear; int usedSymbol;}CirQUEUE; CirQUEUE cQ_InBox,cQ_OutBox; typedef struct{ int front; int rear; NEWLOC nLoc[MAXVALLOC]; }Cir_NEWLOC; Cir_NEWLOC cirNewLoc;typedef struct{ int front; int rear; TXXX tx[MOVERFLOWVAL]; }Cir_OVERFLOW; Cir_OVERFLOW cirOverFlow;/* Save the instantly Circular status to file */void SaveCircular( SOURCETYPE sType ){ FILE *fp; if( sType == INBOX ) { fp = Fopen("CirQUE_INBOX.data","w+"); Fwrite( &cQ_InBox, sizeof(CirQUEUE), 1, fp ); Fclose( fp ); } else if( sType == OUTBOX) { fp = Fopen("CirQUE_OUTBOX.data","w+"); Fwrite( &cQ_OutBox, sizeof(CirQUEUE), 1, fp ); Fclose( fp ); }}/* Initial the empty circular queue */void INITQUEUE( SOURCETYPE sType ) { FILE *fp; if( sType == INBOX ) { fp = Fopen("CirQUE_INBOX.data","r+"); fread( &cQ_InBox, sizeof(CirQUEUE), 1, fp ); Fclose( fp ); if( cQ_InBox.usedSymbol == FALSE ) cQ_InBox.front = cQ_InBox.rear = MAXVAL-1; printf("!!!!!!!!!!!!!!!!!cQ_InBox.used=%d,cQ_InBox.front=%d cQ_InBox.rear=%d\n",cQ_InBox.usedSymbol,cQ_InBox.front,cQ_InBox.rear); } else if( sType == OUTBOX) { fp = Fopen("CirQUE_OUTBOX.data","r+"); fread( &cQ_InBox, sizeof(CirQUEUE), 1, fp ); Fclose( fp ); if( cQ_OutBox.usedSymbol == FALSE ) cQ_OutBox.front = cQ_OutBox.rear = MAXVAL-1; }}/* Data element Enter the circular queue */int ENQUEUE ( SOURCETYPE sType, int element ){ if( sType == INBOX) { if ( cQ_InBox.front == (cQ_InBox.rear+1) % MAXVAL ) //Fail to enter for the full queue return 0; else //Successfully enter the queue { cQ_InBox.rear = (cQ_InBox.rear+1) % MAXVAL; cQ_InBox.d[cQ_InBox.rear] = element; cQ_InBox.usedSymbol = TRUE; printf("!!!!!!!!!!!!!!SaveCircular!!!!!!!!!!!!!\n"); SaveCircular( INBOX ); return 1 ; } } else if( sType == OUTBOX) { if ( cQ_OutBox.front == (cQ_OutBox.rear+1) % MAXVAL ) //Fail to enter for the full queue return 0; else //Successfully enter the queue { cQ_OutBox.rear = (cQ_OutBox.rear+1) % MAXVAL; cQ_OutBox.d[cQ_OutBox.rear] = element; cQ_OutBox.usedSymbol = TRUE; SaveCircular( OUTBOX ); return 1 ; } } }/* Get the Data element from circular queue */int DEQUEUE( SOURCETYPE sType ){ if( sType == INBOX) { if( cQ_InBox.front == cQ_InBox.rear ) //Fail to get data for the empty queue return 0; else { cQ_InBox.front = ( cQ_InBox.front+1) % MAXVAL ; printf( "Dequeue %d\n",cQ_InBox.d[cQ_InBox.front] ); SaveCircular( INBOX ); return ( cQ_InBox.d[cQ_InBox.front] ) ; } } else if( sType == OUTBOX) { if( cQ_OutBox.front == cQ_OutBox.rear ) //Fail to get data for the empty queue return 0; else { cQ_OutBox.front = ( cQ_OutBox.front+1) % MAXVAL ; printf( "Dequeue %d\n",cQ_OutBox.d[cQ_OutBox.front] ); SaveCircular( OUTBOX ); return ( cQ_OutBox.d[cQ_OutBox.front] ) ; } } }/* Initial the empty circular NEWLOC queue */void INIT_CIRNEWLOC( void ) { cirNewLoc.front = cirNewLoc.rear = MAXVALLOC-1; }/* Data element Enter the circular NEWLOC queue */int ENQUE_CIRNEWLOC ( NEWLOC element ){ if ( cirNewLoc.front == (cirNewLoc.rear+1) % MAXVALLOC ) //Fail to enter for the full queue return 0; else //Successfully enter the queue { cirNewLoc.rear = (cirNewLoc.rear+1) % MAXVALLOC; cirNewLoc.nLoc[cirNewLoc.rear] = element; return 1 ; } }/* Get the Data element from circular NEWLOC queue */int DEQUE_CIRNEWLOC( NEWLOC **new ){ if( cirNewLoc.front == cirNewLoc.rear ) //Fail to get data for the empty queue return 0; else { cirNewLoc.front = ( cirNewLoc.front+1) % MAXVALLOC ; printf( "Dequeue CIRNEWLOC srcID %ld\n",cirNewLoc.nLoc[cirNewLoc.front].dwxx.srcID ); *new = &(cirNewLoc.nLoc[cirNewLoc.front]); return 1; }}/* Initial the empty circular OVERFLOWED new messages queue */void INIT_CIROVERFLOW( void ) { cirOverFlow.front = cirOverFlow.rear = MOVERFLOWVAL-1; }/* Data element Enter the circular OVERFLOWED new messages queue */int ENQUE_CIROVERFLOW ( TXXX element ){ if ( cirOverFlow.front == (cirOverFlow.rear+1) % MOVERFLOWVAL )//Fail to enter for the full queue return 0; else //Successfully enter the queue { cirOverFlow.rear = (cirOverFlow.rear+1) % MOVERFLOWVAL; cirOverFlow.tx[cirOverFlow.rear] = element; return 1 ; } }/* Get the Data element from circular OVERFLOWED new messages queue */int DEQUE_CIROVERFLOW( TXXX **new ){ if( cirOverFlow.front == cirOverFlow.rear ) //Fail to get data for the empty queue return 0; else { cirOverFlow.front = ( cirOverFlow.front+1) % MOVERFLOWVAL ; printf( "Dequeue cirOverFlow srcID %ld\n",cirOverFlow.tx[cirOverFlow.front].srcID ); *new = &(cirOverFlow.tx[cirOverFlow.front]); return 1; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -