【问题标题】:Descending Priority Queue using Linked List without taking Priority as input使用链表下降优先级队列而不以优先级作为输入
【发布时间】:2021-04-03 14:06:00
【问题描述】:

我尝试使用以下代码将元素插入Descending Priority Queue。我很无助,因为这个问题有几个答案,人们将priority 作为用户的输入。在这里,我尝试使用以下条件进行排序:

#include<stdio.h>
#include<stdlib.h>
struct qElem 
{
    int ele;
    int priority;
    struct qElem *next;
};

struct queue
{
    struct qElem *front, *rear;
    int size;
};

void enQueue(struct queue *q, int ele)
{
    struct qElem *temp;
    temp = (struct qElem *) malloc (sizeof(struct qElem));
    temp->ele = ele;
    temp->next = NULL;

    if (q->rear == NULL) {
        q->rear = temp;
        q->front = temp;
    }
    else if (q-> rear -> ele < temp->ele){
        q-> rear  = temp;
        temp->next = q->rear;
    }
    ++ q->size;
}
void display_pqueue(struct queue *q)    {
    if(q->front == NULL)
      printf("\nQueue is Empty!!!\n");
   else{
      struct qElem *temp = q->front;
      while(temp->next != NULL){
     printf("%d--->",temp->ele);
     temp = temp -> next;
      }
      printf("%d--->NULL\n",temp->ele);
   }
}


插入未按顺序进行。请在EnQueueDisplay 操作中帮助我。

【问题讨论】:

  • 在队列中只能将节点添加到尾部。如果您需要在特定位置添加节点,那么您的数据结构就是链表。如果你被告知要创建一个队列,那么也许你应该按元素值的降序打印元素。
  • 您的 enQueue 函数缺少迭代代码,以便按降序插入队列元素。因为您的队列没有特定限制,并且您无法知道在运行时可以排队多少项。

标签: c linked-list priority-queue


【解决方案1】:

您的 enQueue 不会扫描插入点。

而且,您没有提供 [单独] 优先级作为参数,因此您无法正确进行插入。

为了说明,我使用ele 来设置两个 elepriority

有多种插入情况需要处理:

  1. 空列表
  2. 在后面插入
  3. 在中间插入
  4. 在非空列表的第一个元素之前插入

无论如何,这是代码。请注意,虽然它设置了rear,但这没有经过测试:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

struct qElem {
    int ele;
    int priority;
    struct qElem *next;
};

struct queue {
    struct qElem *front, *rear;
    int size;
};

#ifdef DEBUG
#define dbgprt(_fmt...) \
    printf(_fmt)
#else
#define dbgprt(_fmt...) \
    do { } while (0)
#endif

int
show(struct qElem *cur)
{
    int val;

    if (cur != NULL)
        val = cur->priority;
    else
        val = -1;

    return val;
}

void
enQueue(struct queue *q, int ele)
{
    struct qElem *temp;
    struct qElem *cur;
    struct qElem *prev;

    temp = malloc(sizeof(*temp));
    temp->ele = ele;
#if 1
    temp->priority = ele;
#endif
    temp->next = NULL;

    dbgprt("enQueue: ele=%d\n",ele);

    // find correct insertion point (place to insert _after_)
    // NOTE: this is descending (e.g. 3->2->1). For ascending, use ">"
    prev = NULL;
    for (cur = q->front;  cur != NULL;  cur = cur->next) {
        if (cur->priority < ele)
            break;
        prev = cur;
    }

    do {
        dbgprt("prev=%d\n",show(cur));
        dbgprt("cur=%d\n",show(cur));
        dbgprt("front=%d\n",show(q->front));
        dbgprt("rear=%d\n",show(q->rear));

        // empty list
        if (q->front == NULL) {
            q->front = temp;
            q->rear = temp;
            break;
        }

        // insert at front
        if (prev == NULL) {
            temp->next = q->front;
            q->front = temp;
            break;
        }

        // insert in priority place
        temp->next = prev->next;
        prev->next = temp;
    } while (0);

    // new rear of list
    if (prev == q->rear)
        q->rear = temp;

    // increase number of elements in list
    q->size += 1;
}

void
display_pqueue(struct queue *q)
{
    if (q->front == NULL)
        printf("\nQueue is Empty!!!\n");
    else {
        struct qElem *temp = q->front;

        while (temp->next != NULL) {
            printf("%d--->", temp->ele);
            temp = temp->next;
        }

        printf("%d--->NULL\n", temp->ele);
    }
}

void
dotest(struct queue *q,...)
{
    va_list ap;

    memset(q,0,sizeof(*q));

    printf("\n");
    printf("dotest:");

    va_start(ap,q);
    while (1) {
        int val = va_arg(ap,int);
        if (val < 0)
            break;
        printf(" %d",val);
        enQueue(q,val);
    }

    printf("\n");
    display_pqueue(q);

    struct qElem *next;
    for (struct qElem *temp = q->front;  temp != NULL;  temp = next) {
        next = temp->next;
        free(temp);
    }
}

int
main(void)
{
    struct queue q;

    dotest(&q,5,1,3,4,12,99,18,19,20,-1);

    return 0;
}

【讨论】:

  • 有没有办法在这个程序的结构中排除优先级参数?
  • 当然,这取决于你想要什么。但是,我不确定你在寻找什么。我猜:你想要 two 结构,一个用于数据[没有优先级或链接],另一个没有数据但只有队列相关成员。所以(例如):struct myData { int ele; int x; int y; int arr[100]; };struct qElem { int priority; struct qElem *next; struct myData *data; }; 这样,您可以让单个数据元素同时位于 多个 队列中。如果您能更好地描述您的需求,我可以相应地编辑我的答案。
猜你喜欢
  • 2016-09-23
  • 1970-01-01
  • 2011-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-13
  • 1970-01-01
  • 2011-03-20
相关资源
最近更新 更多