具有优先级的动态队列,添加功能不起作用

时间:2014-02-11 22:33:36

标签: c queue

这是我对如何实现队列链接队列的看法。由于add函数无法正常工作,我无法测试其他函数,除了看起来正在工作的new_queue之外。

#include <stdlib.h>

struct Qelement {
    struct Qelement *next;
    int prio;
    const char *dataptr;
}; 

typedef struct Qelement *Queue;

// creates an empty queue
Queue new_queue ()
{
        Queue q = (Queue)malloc(sizeof(struct Qelement));
        q->next = NULL;
        q->prio = 0;
        q->dataptr = NULL;     
        return q;
}

// removes the queue and all its elements
void delete_queue (Queue q)
{
        Queue tmp;
        while (q->next != NULL)
        {
                tmp = q->next;
                free (q);
                q = tmp;
        }

        free(q);
}

// emmpties queue
void clear (Queue q)
{
        Queue tmp;
        while (q->next != NULL)
        {
                tmp = q->next;
                free (q);
                q = tmp;
        }

        q->next = NULL;
        q->prio = 0;
        q->dataptr = NULL;
}

// get length of queue
int size (Queue q)
{
        Queue orginal = q;

        int length = 0;
        while (q->next != NULL)
        {
                ++length;
                q = q->next;
        }

        ++length;
        q = orginal;

        return length;
}

// add an element to queue
void add (Queue q, int priority, Datatyp *d)
{
        Queue tmp;
        Queue previous = q;

        Queue element = (Queue)malloc (sizeof(struct Qelement));
        element->next = NULL;
        element->prio = priority;
        element->dataptr = d;

        if (priority > previous->prio)
        {
                tmp = q;
                q = element;
                element->next = tmp;           
        }
        else
        {
                if (previous->next != NULL)
                {
                        while (priority <= previous->next->prio)
                        {
                                previous = previous->next;
                        }

                        previous->next = element;
                        element->next = previous->next->next;
                }
                else
                {
                        //previous->next = element;
                }
        }
}

// return first element
Datatyp *get_first (Queue q)
{
        return q->dataptr;
}

// removes first element
void remove_first(Queue q)
{
        Queue tmp = q->next;
        q = q->next;
        free(tmp);
}

int main(int argc, char *argv[]) {
    const char *daniel = "Daniel";
    const char *lisa = "Lisa";
    const char *a[] = {"Kalle", "Olle", "Eva", lisa, "Stina", 
                      "Peter", "Anna", daniel, "Johan", "Maria"};
    const int correctOrder1[] = {3, 7, 2, 6, 1, 5, 9, 0, 4, 8 };
    const int correctOrder2[] = {5, 4, 7, 3, 6};

    Queue q = new_queue();
    int i;

    for (i=0; i<10; i++) {
        add(q, i%4, a[i]); 
    }

    printf("Size = %d\n\n", size(q)); // says 1, should say 10
    ...
}

主函数中的最后一个printf为1,所以我的看法是添加部分无法正常工作。任何指针?

1 个答案:

答案 0 :(得分:0)

在函数add中,您需要传递Queue*,以便在函数外部实际更改它:

void add (Queue* q, int priority, Datatyp *d)
{
    ...
    Queue previous = *q;
    ...
    if (priority > previous->prio)
    {
        tmp = *q;
        *q = element;
        element->next = tmp;           
    }
    ...
}

然后,在功能main中,您需要拨打add(&q, i%4, a[i])而不是add(q, i%4, a[i])

BTW,在尝试更改q的所有其他功能中遇到同样的问题!!!

因此,解决此问题的另一种方法是将Queue q声明为全局变量,而不是将其传递给每个函数。但这会阻止你使用几个不同的队列......