int main(){ QUEUE Queue = queueCreate(5); int i=0,data=2; for(i=1;i<4;i++){ if(enqueue(Queue,i)) printf("enqueue :%i\n",i); //printf("fghfhf %i\n",Queue->s1->index ); } STACK stack3=queueToStack(Queue); dequeue(Queue,&data); printf("dequeue :%i\n",data);//3 //printf("thrth %i\n",Queue->s1->index ); queuePeek(Queue,&data); printf("Peek :%i\n",data);//4 queuePeek(Queue,&data); printf("Peek :%i\n",data);//4 queuePeek(Queue,&data); printf("Peek :%i\n",data);//4 dequeue(Queue,&data); printf("dequeue :%i\n",data);//3 for(i=5;i<11;i++){ if(enqueue(Queue,i)) printf("enqueue :%i\n",i); } queuePeek(Queue,&data); printf("Peek :%i\n",data); for(i=5;i<11;i++){ if(dequeue(Queue,&data)) printf("dequeue :%i\n",data); } queueDestroy(Queue); while(stackPop(stack3,&data)){ printf("queue to stack elements :%i\n",data); } }
int main() { Queue queue; queueCreate(&queue, 5); queuePush(&queue, 1); queuePush(&queue, 2); queuePop(&queue); printf("%d\n", queuePeek(&queue)); queuePop(&queue); queuePush(&queue, 1); queuePush(&queue, 2); printf("%d\n", queuePeek(&queue)); queuePush(&queue, 3); printf("%d\n", queuePeek(&queue)); printf("%d\n", queuePeek(&queue)); queueDestroy(&queue); return 0; }
static void takeABus (unsigned int k, unsigned int id) { /* enter critical region */ if (semDown (semgid, sh->access) == -1) { perror ("error on the down operation for semaphore access (PA)"); exit (EXIT_FAILURE); } /* insert your code here */ int i,counter=0; //she queues queueIn(&sh->fSt.busQueue,id); for(i=0;i<T;i++) { //if the number of queueing passengers if(queuePeek(&sh->fSt.busQueue,i) != EMPTYPOS) { counter++; } } //equals the number os seats in the bus if(counter==T) { //she informs the bus driver it is time start boarding if (semUp (semgid, sh->waitingDrive) == -1) { perror ("error on the up operation for semaphore access (PA)"); exit (EXIT_FAILURE); } } // Change State sh->fSt.st.passStat[k][id].stat = AT_THE_ARRIVAL_TRANSFER_TERMINAL; saveState (nFic,k,&(sh->fSt)); if (semUp (semgid, sh->access) == -1) { perror ("error on the up operation for semaphore access (PA)"); exit (EXIT_FAILURE); } /* insert your code here */ //she waits her turn if (semDown (semgid, sh->pass[id]) == -1) { perror ("error on the down operation for semaphore access (PA)"); exit (EXIT_FAILURE); } }
int main() { Queue *queue = malloc(sizeof(Queue)); queueCreate(queue, 10); queuePush(queue, 1); queuePush(queue, 2); queuePush(queue, 3); queuePop(queue); queuePop(queue); assert(queuePeek(queue) == 3); assert(queueEmpty(queue) == 0); queueDestroy(queue); return 0; }
int TestQueue() { Queue* Q; int i; Q = malloc(sizeof(struct Queue)); queueCreate(Q, 10); for (i = 0; i < 100; ++i) queuePush(Q, i); while (!queueEmpty(Q)) { printf("%d ", queuePeek(Q)); queuePop(Q); } printf("\n"); queueDestroy(Q); }
int main (int argc, char **argv) { int i; int val[] = {2,3,6,8,10,12,14,16,18,20,21,22,23,24,25}; int value; Queue q ; queueCreate(&q, 1024); for(i = 0; i < 15; i++) { queuePush(&q, val[i]); print_list(q.node); } for(i = 0; i < 15; i++) { value = queuePeek(&q); printf("val = %d\n", value); queuePop(&q); print_list(q.node); } queueDestroy(&q); return 0; }
void saveState (char *nFic, unsigned int k, FULL_STAT* p_fSt) { FILE *fic; /* file descriptor */ char *dName = "log", /* default log file name */ *fName; /* log file name */ unsigned int p, i; /* counting variables */ if ((nFic == NULL) || (strcmp (nFic, "") == 0)) fName = dName; else fName = nFic; if ((fic = fopen (fName, "a")) == NULL) { perror ("error on opening for appending the log file"); exit (EXIT_FAILURE); } fprintf (fic, "%2u %2u", k, p_fSt->plHold[k].nBags); switch (p_fSt->st.porterStat) { case WAITING_FOR_A_PLANE_TO_LAND: fprintf (fic, " WPTL "); break; case AT_THE_PLANES_HOLD: fprintf (fic, " APHL "); break; case AT_THE_LUGGAGE_BELT_CONVEYOR: fprintf (fic, " ALBC "); break; case AT_THE_STOREROOM: fprintf (fic, " ASTR "); break; } fprintf (fic, "%2u %2u", p_fSt->convBelt.n, p_fSt->nToTBagsPSR); switch (p_fSt->st.driverStat) { case PARKING_AT_THE_ARRIVAL_TERMINAL: fprintf (fic, " PAAT "); break; case DRIVING_FORWARD: fprintf (fic, " DRFW "); break; case PARKING_AT_THE_DEPARTURE_TERMINAL: fprintf (fic, " PADT "); break; case DRIVING_BACKWARD: fprintf (fic, " DRBW "); break; } for (i = 0; i < N; i++) if (queuePeek (&(p_fSt->busQueue), i) != EMPTYPOS) fprintf (fic, "%3d", queuePeek (&(p_fSt->busQueue), i)); else fprintf (fic, " -"); fprintf (fic, " "); for (i = 0; i < T; i++) //if (i < p_fSt->bus.nOccup) if (p_fSt->bus.seat[i] != EMPTYST) fprintf (fic, "%3u", p_fSt->bus.seat[i]); else fprintf (fic, " -"); fprintf (fic, "\n"); for (p = 0; p < N; p++) { switch (p_fSt->st.passStat[k][p].stat) { case AT_THE_DISEMBARKING_ZONE: fprintf (fic, "ADZ"); break; case AT_THE_LUGGAGE_COLLECTION_POINT: fprintf (fic, "LCP"); break; case AT_THE_BAGGAGE_RECLAIM_OFFICE: fprintf (fic, "BRO"); break; case EXITING_THE_ARRIVAL_TERMINAL: fprintf (fic, "EAT"); break; case AT_THE_ARRIVAL_TRANSFER_TERMINAL: fprintf (fic, "ATT"); break; case TERMINAL_TRANSFER: fprintf (fic, "TTF"); break; case AT_THE_DEPARTURE_TRANSFER_TERMINAL: fprintf (fic, "DTT"); break; case ENTERING_THE_DEPARTURE_TERMINAL: fprintf (fic, "EDT"); break; } if (p_fSt->st.passStat[k][p].sit == FD) fprintf (fic, " FDT"); else fprintf (fic, " TRT"); fprintf (fic, "%3u %3u ", p_fSt->st.passStat[k][p].nBagsReal, p_fSt->st.passStat[k][p].nBagsAct); } fprintf (fic, "\n"); if (fclose (fic) == EOF) { perror ("error on closing the log file"); exit (EXIT_FAILURE); } }
struct Stack* parseCmdLine(const char* const clStr) { struct CmdLine* cmdLine; struct Queue* tokens; char *token, *nextToken, *command; char **operators, **files; bool background, pushed, pipedIn, pipeOut; struct Stack* commandStack; int i; struct LinkedList* parameters; tokens=tokenize(clStr); commandStack=stackCreate(); pipedIn=false; pipeOut=false; while (!queueIsEmpty(tokens)) { //Get Command pushed=false; command=(char *)queueGetFront(tokens); //Parse Parameters parameters=listCreate(); if (!queueIsEmpty(tokens)) { while (!queueIsEmpty(tokens)) { token=(char *)queuePeek(tokens); if (!isDelimiter(token)) { listAppend(parameters, token); queueGetFront(tokens); } else { break; } } } //Check for Redirects operators=(char **)malloc(sizeof(char*)*REDIRECTS); files=(char **)malloc(sizeof(char*)*REDIRECTS); for (i=0; i<REDIRECTS; i++) { operators[i]=NULL; files[i]=NULL; } if (!queueIsEmpty(tokens)) { for (i=0; i<REDIRECTS && !queueIsEmpty(tokens); i++) { token=(char *)queuePeek(tokens); if (strcmp(token, CL_TOKEN_GREATER_THAN)==0 || strcmp(token, CL_TOKEN_LESS_THAN)==0) { operators[i]=token; queueGetFront(tokens); token=(char *)queueGetFront(tokens); files[i]=token; } else { break; } } } //Check for background process or pipe background=false; pipeOut=false; if (!queueIsEmpty(tokens)) { token=(char *)queuePeek(tokens); if (strcmp(token, CL_TOKEN_AMP)==0) { queueGetFront(tokens); background=true; } else if (strcmp(token, CL_TOKEN_PIPE)==0) { queueGetFront(tokens); pipeOut=true; } } //Add Command to Stack of Commands pushCmdLine(commandStack, command, parameters, operators, files, pipedIn, pipeOut, background); pushed=true; pipedIn=pipeOut; if (background || !pipeOut) { break; //Terminated because in background or syntax error } } if (!pushed) { free(operators); free(files); } queueDestroy(tokens, false); return commandStack; }