Guest User

Untitled

a guest
Jan 22nd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.47 KB | None | 0 0
  1. #include <time.h>
  2.  
  3. struct Queue{
  4.     char message[256];
  5.     struct Queue* next;
  6. };
  7.  
  8.  
  9. void enquene(struct Queue** _emails, char message[256]){
  10.     //initilize the new email to be added
  11.     struct Queue* toAdd;
  12.     toAdd = (struct Queue*) malloc (sizeof(struct Queue));
  13.     toAdd->message = message;
  14.     toAdd->next=NULL;
  15.  
  16.     //if list is empty add it at the front
  17.     if(*_emails == NULL){
  18.         *_emails = toAdd;
  19.         return;
  20.     }
  21.    
  22.     //navigate to the end of the list
  23.     struct Queue* current;
  24.     for (current = *_emails;current->next!=NULL;current = current->next);
  25.  
  26.     //add the new email to the queue
  27.     current->next = toAdd;
  28. }
  29.  
  30. //deletes on element of off the front of the queue and returns it
  31. struct Queue* dequeue (struct Queue** _emails){
  32.     if (**_emails == NULL)
  33.         return _emails;
  34.  
  35.     struct Node* current = *_emails;
  36.     *_emails = (*_emails)->next;
  37.     return current;
  38.     //free?
  39. }
  40.  
  41. int main(){
  42.     //used for random integers
  43.     srand(time(NULL));
  44.    
  45.     int randNum;
  46.    
  47.     struct Queue *emails;
  48.     emails = (struct Queue*) malloc (sizeof(struct Queue));
  49.  
  50.     while(true){
  51.         //enquene every iterations which is 40 iterations a minute so 40 messages a minute
  52.         enquene(emails, "message");
  53.  
  54.         //randNum is a number between 1 and 100
  55.         randNum = 1+(rand()%100);
  56.         //if it is not one of the 25%
  57.         if (randNum >25){
  58.             dequene(emails);
  59.         }
  60.         //otherwise move it to the back
  61.         else{
  62.             enquene(emails, dequene(emails)->message);
  63.         }
  64.         //slows it down to do 40 iterations in a minute
  65.         delay(1500);
  66.     }
  67. }
Add Comment
Please, Sign In to add comment