Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.39 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <assert.h>
  4. #include "queues.h"
  5.  
  6. List newEmptyList() {
  7.     return NULL;
  8. }
  9.  
  10. int isEmptyList (List li) {
  11.     return ( li==NULL );
  12. }
  13.  
  14. void listEmptyError() {
  15.     printf("list empty\n");
  16.     abort();
  17. }
  18.  
  19. List addItem(State s, List li) {
  20.     List newList = malloc(sizeof(struct ListNode));
  21.     assert(newList!=NULL);
  22.     newList->item = s;
  23.     newList->next = li;
  24.     return newList;
  25. }
  26.  
  27. State firstItem(List li) {
  28.     if ( li == NULL ) {
  29.         listEmptyError();
  30.     }
  31.     return li->item;
  32. }
  33.  
  34. List removeFirstNode(List li) {
  35.     List returnList;
  36.     if ( li == NULL ) {
  37.         listEmptyError();
  38.     }
  39.     returnList = li->next;
  40.     free(li);
  41.     return returnList;
  42. }
  43.  
  44. void freeList(List li) {
  45.     List li1;
  46.     while ( li != NULL ) {
  47.         li1 = li->next;
  48.         free(li);
  49.         li = li1;
  50.     }
  51.     return;
  52. }
  53.  
  54. Queue newEmptyQueue () {
  55.     Queue q;
  56.     q.list = newEmptyList();
  57.     q.lastNode = NULL;
  58.     return q;
  59. }
  60.  
  61. int isEmptyQueue (Queue q) {
  62.     return isEmptyList(q.list);
  63. }
  64.  
  65. void emptyQueueError () {
  66.     printf("queue empty\n");
  67.     exit(0);
  68. }
  69.  
  70. int compare(State s, Queue *qp) {
  71.     while(qp->lastNode != NULL) {
  72.         if ((s.time == (qp->list)->item.time) &&
  73.             (s.sg1bot == (qp->list)->item.sg1bot) &&
  74.             (s.sg2bot == (qp->list)->item.sg2bot) &&
  75.             (s.sg1top == (qp->list)->item.sg1top) &&
  76.             (s.sg2top == (qp->list)->item.sg2top)) {
  77.             return 0;
  78.         }
  79.         qp->lastNode = (qp->lastNode->next);
  80.     }
  81.     return 1;
  82. }
  83.  
  84. void enqueue (State s, Queue *qp) {
  85.     if (isEmptyList(qp->list)) {
  86.         qp->list = addItem(s,NULL);
  87.         qp->lastNode = qp->list;
  88.     } else {
  89.         while(qp->lastNode != NULL) {
  90.             if (compare(s,qp) == 1) {
  91.  
  92.                 (qp->lastNode)->next = addItem(s,NULL);
  93.                 qp->lastNode = (qp->lastNode->next);
  94.                 return;
  95.             }
  96.             if (compare(s,qp) == 0) {
  97.                 printf("helped\n");
  98.                 qp->lastNode = (qp->lastNode->next);
  99.                 return;
  100.             }
  101.         }
  102.     }
  103. }
  104.  
  105. State dequeue(Queue *qp) {
  106.     State s = firstItem(qp->list);
  107.     qp->list = removeFirstNode(qp->list);
  108.     if ( isEmptyList(qp->list) )  {
  109.         qp->lastNode = NULL;
  110.     }
  111.     return s;
  112. }
  113.  
  114. void freeQueue (Queue q) {
  115.     freeList(q.list);
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement