使用数据结构(优先级队列)的C编程将字符和整数一起存储和显示在队列中

时间:2018-10-05 16:56:39

标签: c data-structures priority-queue

#include  <stdio.h>
#include  <stdlib.h>   
#define MAX 5

    void insert_by_priority(int);
    void delete_by_priority(int);
    void create();
    void check(int);
    void display_pqueue();

    int pri_que[MAX];
    int front, rear;

    void priority()
    {
        int n, ch;
        char v[20];

        printf("\n1 - Insert");
        printf("\n2 - Delete");
        printf("\n3 - Display");
        printf("\n4 - Exit");

        create();

        while (1)
        {
            printf("\nEnter your choice : ");    
            scanf("%d", &ch);

            switch (ch)
            {
            case 1: 
                printf("\nEnter age to be inserted : ");
                scanf("%d",&n);
                printf("\nEnter name:");
                scanf("%s",&v);
                insert_b

y_priority(n);
                    printf("%s");
                                break;
                case 2:
                    printf("\nEnter age to delete : ");
                    scanf("%d",&n);
                    delete_by_priority(n);
                    break;
                case 3: 
                    display_pqueue();
                    break;
                case 4: 
                    exit(0);
                default: 
                    printf("\nChoice is incorrect, Enter a correct choice");
                }
            }
        }

         void create()
        {
            front = rear = -1;
        }


        void insert_by_priority(int data)
        {
            if (rear >= MAX - 1)
            {
                printf("\nQueue overflow no more elements can be inserted");
                return;
            }
            if ((front == -1) && (rear == -1))
            {
                front++;
                rear++;
                pri_que[rear] = data;
                return;
            }    
            else
                check(data);
            rear++;
        }

                    void check(int data)
        {
            int i,j;

            for (i = 0; i <= rear; i++)
            {
                if (data >= pri_que[i])
                {
                    for (j = rear + 1; j > i; j--)
                    {
                        pri_que[j] = pri_que[j - 1];
                    }
                    pri_que[i] = data;
                    return;
                }
            }
            pri_que[i] = data;
        }

                    void delete_by_priority(int data)
        {
            int i;

            if ((front==-1) && (rear==-1))
            {
                printf("\nQueue is empty no elements to delete");
                return;
            }

            for (i = 0; i <= rear; i++)
            {
                if (data == pri_que[i])
                {
                    for (; i < rear; i++)
                    {
                        pri_que[i] = pri_que[i + 1];
                    }

                pri_que[i] = -99;
                rear--;

                if (rear == -1) 
                    front = -1;
                return;
                }
            }
            printf("\n%d not found in queue to delete", data);
        }

                   void display_pqueue()
        {
            if ((front == -1) && (rear == -1))
            {
                printf("\nQueue is empty");
                return;
            }

            for (; front <= rear; front++)
            {
                printf(" %d ", pri_que[front]);
            }

            front = 0;
        }

我在优先级队列上有此代码,但是我无法像已排序的记录一样在整数旁边插入字符串

AGE |姓名

78 | abcd

75 | bcdfe
.............等

我尝试使用scanf语句,但无济于事,它分别打印出来,还是我必须使用emplace?代码将发生什么变化?但是如何使用用户输入来实现?还是必须实现另一个数据结构?如果是,那怎么办?

0 个答案:

没有答案