0

I don't understand why printq() function prints 0, but when I access it back in main() it prints. I don't understand what I am doing wrong, I tried using pointers, but I get some different error in the priority queue. I want to print elements in array pq[10].

EDIT: I realized that the elements are stored but when I use pq[R].data it prints but when I use pq[i].data in printq() and put it inside for loop, it prints zero.

#include <stdio.h>

int F = -1, R = -1;
int item, max = 10;

struct prioq {
    int data;
    int prio;
};

struct prioq pq[10] = { 0 };

void printq()
{
    int i = 0;
    printf("%d,", pq[i].data);
    printf("QUEUE :");
    for (i = 0; i < max; i++) {
        printf("%d,", pq[i].data);
    }
    printf("\n");
    printf("PRIO  :");
    for (i = 0; i < max; i++) {
        printf("%d,", pq[i].prio);
    }
}

void enqueue(int item, int p, struct prioq pq[])
{
    if (F == -1 && R == -1 || F > R) {
        F == 0;
        R == 0;
        pq[R].data = item;
        pq[R].prio = p;

        printf("%d", pq[R].data);
        printf("%d", pq[R].prio);
        printq();

    } else if (R == max-1 || R > max) {
        printf("overflow\n");
    } else if (R < max) {
        R++;
        pq[R].data = item;
        pq[R].prio = p;
        printq();
    }
}

void dequeue(struct prioq pq[])
{
    int large = 0;
    if (F == -1) {
        printf("underflow\n");
    } else {
        int i;
        for (i = 0; i < max; i++) {
            if (pq[i].prio > large) {
                large = i;
            }
        }
    }

    item = pq[large].data;
    pq[large].prio = 0;
    pq[large].data = 0;

    printf("item deleted: %d\n", item);

    printq();
}

void main()
{
    int item = 0;
    int c = 0, p = 0;

    do {
        printf("choose your option\n");
        printf("1.Insert, 2.Delete, 3.Exit\n" );
        scanf("%d", &c);
        switch (c) {
            case 1:
                printf("Enter the priority and element to insert\n");
                scanf("%d %d", &item, &p);
                enqueue(item,p,pq);
                printf("%d", pq[R].data);
                printf("%d", pq[R].prio);
                break;

            case 2:
                dequeue(pq);
                break;

            default:
                c = 3;
                break;
        }
    } while (c != 3);
    printf("exited\n");
}
double-beep
  • 5,031
  • 17
  • 33
  • 41
  • 4
    C++ is not C, it is a completely different language. Please don't use the C++ tag for C code. – Sam Varshavchik Jan 10 '21 at 17:52
  • srry i am new to stackoverflow i just used the recommended tag – Allen Anand Jan 10 '21 at 17:54
  • Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you probably want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Andreas Wenzel Jan 10 '21 at 19:28
  • Even if using a debugger does not actually solve the problem, it should at least help you to isolate the problem further and to create a [mre], so that it will be easier for other people to help you. Most people will not be willing to debug your whole program for you, as that should be your job. – Andreas Wenzel Jan 10 '21 at 19:30
  • I just looked around more and found the "=" error which should be "==", thanks for you time. – Allen Anand Jan 21 '21 at 21:54

1 Answers1

1

In your enqueue function, change the == in the F and R assignments to =.

void enqueue(int item, int p, struct prioq pq[])
{
    if (F == -1 && R == -1 || F > R) {
        F = 0; // Here
        R = 0; // And here
        pq[R].data = item;
        pq[R].prio = p;

        printf("%d", pq[R].data);
        printf("%d", pq[R].prio);
        printq();

    } else if (R == max-1 || R > max) {
        printf("overflow\n");
    } else if (R < max) {
        R++;
        pq[R].data = item;
        pq[R].prio = p;
        printq();
    }
}
Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
Lucas Belfanti
  • 263
  • 1
  • 3
  • 10