使用链表的优先级队列

本文介绍了如何使用C语言实现一个优先级队列,包括队列的基本操作如插入、删除,以及在优先级队列中根据元素优先级进行删除,展示了队列中元素的动态变化。
摘要由CSDN通过智能技术生成

优先级队列是一种特殊类型的队列,其中每个元素都是与优先级相关联,并根据其优先级提供服务。如果元素有相同的优先级,那么根据它们在队列中的排列顺序。

元素本身的值可以用于分配优先级。例如:最高值的元素被视为最高的优先级元素。但是,在其他情况下,我们可以假设最小值的元素作为最高优先级元素。在其他情况下,我们可以根据我们的需要确定优先级。

在队列中,根据先进先出规则,而在优先级队列中,则根据优先级删除这些值。

#include <stdio.h>
#include <stdlib.h>
#define NULL ((void *)0)

struct node
{
    int data;
    struct node *next;
};

struct node *front, *rear;

/* 初始化队列*/
void createqueue() { front = rear = NULL; }

int empty()
{
    if (front == NULL)
        return 1;
    else
        return 0;
}

void insert(int x)
{
    struct node *pnode;

    pnode = (struct node *)malloc(sizeof(struct node));
    if (pnode == NULL)
    {
        printf("Memory overflow. Unable to insert.\n");
        exit(1);
    }

    pnode->data = x;
    pnode->next = NULL; /*新增节点总是最后一个节点*/

    if (empty())
        front = rear = pnode;
    else
    {
        rear->next = pnode;
        rear = pnode;
    }
}

int removes()
{
    int min;
    struct node *follow, *follow1, *p, *p1;

    if (empty())
    {
        printf("\nQueue Underflow. Unable to remove.");
        exit(1);
    }

    /* 在优先级队列中找到最小值的节点*/
    p = p1 = front;
    follow = follow1 = NULL;
    min = front->data;
    while (p != NULL)
    {
        if (p->data < min)
        {
            min = p->data;
            follow1 = follow;
            p1 = p;
        }
        follow = p;
        p = p->next;
    }

    /* 根据最小值删除节点 */

    if (p1 == front) /* 删除第一个节点.*/
    {
        front = front->next;
        if (front == NULL) /* 删除唯一的一个节点 */
            rear = NULL;
    }
    else if (p1 == rear) /* 删除最后一个节点 */
    {
        rear = follow1;
        rear->next = NULL;
    }
    else /* 删除其它节点*/
        follow1->next = p1->next;

    free(p1);
    return min; 
}

void show()
{
    struct node *p;

    if (empty())
        printf("Queue empty. No data to display \n");
    else
    {
        printf("Queue from front to rear is as shown: \n");

        p = front;
        while (p != NULL)
        {
            printf("%d ", p->data);
            p = p->next;
        }

        printf("\n");
    }
}

void destroyqueue() { front = rear = NULL; }

int main()
{
    int x, ch;
    createqueue();
    do
    {
        printf("\n******Menu******\n");
        printf("1:Insert \n");
        printf("2:Remove \n");
        printf("3:exit \n");
        printf("Enter your choice: ");
        scanf("%d", &ch);

        switch (ch)
        {
        case 1:
            printf("Enter element to be inserted: ");
            scanf("%d", &x);
            insert(x);
            show();
            break;

        case 2:
            x = removes();
            printf("Element removed is: %d\n", x);
            show();
            break;

        case 3:
            break;
        }
    } while (ch != 3);

    destroyqueue();

    return 0;
}

运行结果:

插入元素

******Menu******
1:Insert 
2:Remove 
3:exit 
Enter your choice: 1
Enter element to be inserted: 12
Queue from front to rear is as shown: 
12 

******Menu******
1:Insert 
2:Remove 
3:exit 
Enter your choice: 1
Enter element to be inserted: 10
Queue from front to rear is as shown: 
12 10 

******Menu******
1:Insert 
2:Remove 
3:exit 
Enter your choice: 1
Enter element to be inserted: 3
Queue from front to rear is as shown: 
12 10 3 

******Menu******
1:Insert 
2:Remove 
3:exit 
Enter your choice: 1
Enter element to be inserted: 90
Queue from front to rear is as shown: 
12 10 3 90 

******Menu******
1:Insert 
2:Remove 
3:exit 
Enter your choice: 1
Enter element to be inserted: 66
Queue from front to rear is as shown: 
12 10 3 90 66 

******Menu******
1:Insert 
2:Remove 
3:exit 
Enter your choice: 1
Enter element to be inserted: 88
Queue from front to rear is as shown: 
12 10 3 90 66 88 

******Menu******
1:Insert 
2:Remove 
3:exit 
Enter your choice: 1 
Enter element to be inserted: 89
Queue from front to rear is as shown: 
12 10 3 90 66 88 89 

******Menu******
1:Insert 
2:Remove 
3:exit 
Enter your choice: 1
Enter element to be inserted: 18
Queue from front to rear is as shown: 
12 10 3 90 66 88 89 18 
 

 根据优先级删除,最小值具有高优先级被删除。

******Menu******
1:Insert 
2:Remove 
3:exit 
Enter your choice: 2
Element removed is: 3
Queue from front to rear is as shown: 
12 10 90 66 88 89 18 

 

******Menu******
1:Insert 
2:Remove 
3:exit 
Enter your choice: 2
Element removed is: 10
Queue from front to rear is as shown: 
12 90 66 88 89 18 

 

******Menu******
1:Insert 
2:Remove 
3:exit 
Enter your choice: 2
Element removed is: 12
Queue from front to rear is as shown: 
90 66 88 89 18 

 

******Menu******
1:Insert 
2:Remove 
3:exit 
Enter your choice: 2
Element removed is: 18
Queue from front to rear is as shown: 
90 66 88 89 

 

******Menu******
1:Insert 
2:Remove 
3:exit 
Enter your choice: 2
Element removed is: 66
Queue from front to rear is as shown: 
90 88 89 

 

******Menu******
1:Insert 
2:Remove 
3:exit 
Enter your choice: 2
Element removed is: 88
Queue from front to rear is as shown: 
90 89 

 

******Menu******
1:Insert 
2:Remove 
3:exit 
Enter your choice: 2
Element removed is: 89
Queue from front to rear is as shown: 
90 

 

******Menu******
1:Insert 
2:Remove 
3:exit 
Enter your choice: 2
Element removed is: 90
Queue empty. No data to display

大家好!我是编码小哥,欢迎关注,持续分享更多实用的编程经验和开发技巧,共同进步。

  • 15
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编码小哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值