training

Code I wrote during training
git clone git://git.bitsmanent.org/training
Log | Files | Refs | README

market.c (2029B)


      1 /* Exercise 12.15 */
      2 
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <time.h>
      6 
      7 #define HOURS 12
      8 #define INTER 4
      9 
     10 struct queue_t {
     11    int client;
     12    struct queue_t *next;
     13 };
     14 typedef struct queue_t Queue;
     15 
     16 void enqueue(Queue **hqueuep, Queue **tqueuep, int value);
     17 int dequeue(Queue **hqueuep, Queue **tqueuep);
     18 char isempty(Queue *queuep);
     19 
     20 /* main function */
     21 int main(void)
     22 {
     23    int c_time; /* current client */
     24    int s_time; /* service time */
     25    int i, client;
     26 
     27    Queue *hqueue = NULL, *tqueue = NULL;
     28 
     29    srand( time(NULL ) );
     30 
     31    c_time = 1 + rand() % INTER;
     32 
     33    s_time = 1 + rand() % INTER;
     34    c_time += 1 + rand() % INTER;
     35 
     36    /* For each minute of the work day (HOURS hours) */
     37    for(i = client = 0; i < 60 * HOURS; i++) {
     38       
     39       if( ! --c_time ) {
     40 	 printf("[+]: %d\n", ++client);
     41          enqueue(&hqueue, &tqueue, client);
     42 	 c_time = 1 + rand() % INTER;
     43 
     44          if( ! --s_time ) {
     45 	    printf("[-]: %d\n", dequeue(&hqueue, &tqueue));
     46 	    s_time = 1 + rand() % INTER;
     47          }
     48       }
     49    }
     50 
     51    return 0;
     52 } /* E0F main */
     53 
     54 /*
     55  * Determine if the stack is empty this is a "predicate function"
     56 */
     57 char isempty(Queue *queuep)
     58 {
     59    if( queuep == NULL )
     60       return 1;
     61 
     62    return 0;
     63 } /* eof isempty() */
     64 
     65 /* 
     66  * Put another value in queue
     67 */
     68 void enqueue(Queue **hqueuep, Queue **tqueuep, int value)
     69 {
     70    Queue *newqueue;
     71 
     72    if( (newqueue = malloc(sizeof(Queue))) == NULL) {
     73       printf("Cannot allocate the memory\n");
     74       return;
     75    }
     76    newqueue->client = value;
     77    newqueue->next = NULL;
     78 
     79    if( isempty( *hqueuep ) ) {
     80       *hqueuep = newqueue;
     81    }
     82    else {
     83       ( *tqueuep )->next = newqueue;
     84    }
     85 
     86    *tqueuep = newqueue;
     87 
     88 } /* eof enqueue() */
     89 
     90 /* 
     91  * Show and remove a value from the top of queue
     92 */
     93 int dequeue(Queue **hqueuep, Queue **tqueuep)
     94 {
     95    Queue *tmpqueuep;
     96    int ret;
     97 
     98    ret = ( *hqueuep )->client;
     99    tmpqueuep = *hqueuep;
    100    *hqueuep = ( *hqueuep )->next;
    101 
    102    if( *hqueuep == NULL ) {
    103       *tqueuep = NULL;
    104    }
    105 
    106    free( tmpqueuep );
    107 
    108    return ret;
    109 } /* eof dequeue() */
    110