Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <assert.h>
- #include "queues.h"
- List newEmptyList() {
- return NULL;
- }
- int isEmptyList (List li) {
- return ( li==NULL );
- }
- void listEmptyError() {
- printf("list empty\n");
- abort();
- }
- List addItem(State s, List li) {
- List newList = malloc(sizeof(struct ListNode));
- assert(newList!=NULL);
- newList->item = s;
- newList->next = li;
- return newList;
- }
- State firstItem(List li) {
- if ( li == NULL ) {
- listEmptyError();
- }
- return li->item;
- }
- List removeFirstNode(List li) {
- List returnList;
- if ( li == NULL ) {
- listEmptyError();
- }
- returnList = li->next;
- free(li);
- return returnList;
- }
- void freeList(List li) {
- List li1;
- while ( li != NULL ) {
- li1 = li->next;
- free(li);
- li = li1;
- }
- return;
- }
- Queue newEmptyQueue () {
- Queue q;
- q.list = newEmptyList();
- q.lastNode = NULL;
- return q;
- }
- int isEmptyQueue (Queue q) {
- return isEmptyList(q.list);
- }
- void emptyQueueError () {
- printf("queue empty\n");
- exit(0);
- }
- int compare(State s, Queue *qp) {
- while(qp->lastNode != NULL) {
- if ((s.time == (qp->list)->item.time) &&
- (s.sg1bot == (qp->list)->item.sg1bot) &&
- (s.sg2bot == (qp->list)->item.sg2bot) &&
- (s.sg1top == (qp->list)->item.sg1top) &&
- (s.sg2top == (qp->list)->item.sg2top)) {
- return 0;
- }
- qp->lastNode = (qp->lastNode->next);
- }
- return 1;
- }
- void enqueue (State s, Queue *qp) {
- if (isEmptyList(qp->list)) {
- qp->list = addItem(s,NULL);
- qp->lastNode = qp->list;
- } else {
- while(qp->lastNode != NULL) {
- if (compare(s,qp) == 1) {
- (qp->lastNode)->next = addItem(s,NULL);
- qp->lastNode = (qp->lastNode->next);
- return;
- }
- if (compare(s,qp) == 0) {
- printf("helped\n");
- qp->lastNode = (qp->lastNode->next);
- return;
- }
- }
- }
- }
- State dequeue(Queue *qp) {
- State s = firstItem(qp->list);
- qp->list = removeFirstNode(qp->list);
- if ( isEmptyList(qp->list) ) {
- qp->lastNode = NULL;
- }
- return s;
- }
- void freeQueue (Queue q) {
- freeList(q.list);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement