pathqueue.c

Go to the documentation of this file.
00001 
00011 #include "pathqueue.h"
00012 
00023 int queueIsEmpty(NodeQueue *q){
00024         if ((q->first==NULL) && (q->last==NULL))        return 1;
00025         else return 0;
00026 }
00027 
00034 void pushNode(NodeQueue * q, int x, int y){
00035         Node *n= malloc(sizeof(Node));
00036         if (n == NULL) {
00037                 /* Memory could not be allocated, so print an error and exit. */
00038                 fprintf(stderr, "Couldn't allocate memory\n");
00039                 exit(EXIT_FAILURE);
00040         }
00041 
00042         n->x=x;
00043         n->y=y;
00044         n->next=NULL;
00045 
00046         if (queueIsEmpty(q)) {
00047                 q->first=n;
00048                 q->last=n;
00049         } else {
00050                 (q->last)->next=n;
00051                 q->last=n;
00052         }
00053 }
00054 
00061 void pushNodeLast(NodeQueue * q, int x, int y){
00062         Node *n= malloc(sizeof(Node));
00063         if (n == NULL) {
00064                 /* Memory could not be allocated, so print an error and exit. */
00065                 fprintf(stderr, "Couldn't allocate memory\n");
00066                 exit(EXIT_FAILURE);
00067         }
00068 
00069         n->x=x;
00070         n->y=y;
00071         
00072         if (queueIsEmpty(q)) {
00073                 q->first=n;
00074                 q->last=n;
00075                 n->next = NULL;
00076         } else {
00077                 n->next=q->first;
00078                 q->first=n;
00079         }
00080 }
00081 
00088 int popNode(NodeQueue * q, Node * n){
00089         Node * tmp;
00090         if (!queueIsEmpty(q)){
00091                 n->x = (q->first)->x;
00092                 n->y = (q->first)->y;
00093                 tmp = (q->first)->next;
00094                 free((q->first));
00095                 q->first = tmp;
00096                 if ( (q->first) == NULL) q->last = NULL;
00097                 return 1;
00098         } else return 0;
00099 }
00100 
00106 void printQueue(NodeQueue * q){
00107         Node * n = q->first;
00108 
00109         while(n!=NULL){
00110                 printf("(%d,%d)\n", n->x, n->y);
00111                 n=n->next;
00112         }
00113         printf("No more Nodes in the queue!\n");
00114 
00115 }
00116 
00121  NodeQueue * newQueue(void){
00122         NodeQueue * q;
00123         q = malloc(sizeof(NodeQueue));
00124         q->first=NULL;
00125         q->last=NULL;
00126         return q;
00127 }
00133 int delQueue(NodeQueue * q){
00134         if (queueIsEmpty(q)) {
00135                 free(q);
00136                 return 1;
00137         } else return 0;
00138 }
00139 
00147 int isInQueue(NodeQueue * q, int x, int y){
00148                 Node * tmp;
00149                 
00150                 tmp=q->first;
00151                 while (tmp!=NULL){
00152                         if((tmp->x==x)&&(tmp->y==y)) return 1;
00153                         tmp=tmp->next;
00154                 }
00155                 return 0;
00156 }
00157 
00162 void drainQueue(NodeQueue * q){
00163     Node n;
00164         while(popNode(q, &n));
00165 }
00166 

Generated on Thu Sep 13 11:28:28 2007 for DCE-Eurobot by  doxygen 1.5.3