简体   繁体   中英

Enqueue function of queue using linked list in c

I'm having a problem when using linked list to build a queue program. Here's the full code.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#define ERROR_VALUE -300000

typedef struct LinkedNode {
    int data;
    struct LinkdedNode* link;
}Node;
Node* front;
Node* rear;

void init_queue() { front = rear = NULL; }
int is_empty() { return (front = NULL && rear == NULL); }


int size() {
    Node* p; 
    int count = 0;
    if (is_empty())
        return 0; 
    for (p = front; p != rear; p = p->link) {
        count++;
        return count + 1; 
    }
}
void enqueue(int e) { 
    Node* p = (Node*)malloc(sizeof(Node));
    p->data = e;
    p->link = NULL; 
    if (is_empty())
        front = rear = p;
    else {
        rear->link = p;
        rear = p;
    }
}
int dequeue() { 
    Node* p = front; 
    int e;
    if (is_empty()) {
        printf("Queue Empty Error!\n");
        return ERROR_VALUE;
    }
    else if (size() == 1) {
        front = rear = NULL;
    }
    else
        front = p->link;
    e = p->data;
    free(p);
    return e;
}
int peek() { 
    if (is_empty()) {
        printf("Queue Empty Error!\n");
        return ERROR_VALUE;
    }
    return front->data;
}

void print_queue() {
    Node* p;
    printf("QUEUE STATUS: size=%d\n", size());
    if (is_empty())
        return;
    for (p = front; p != NULL; p = p->link)
        printf("[%2d] ", p->data);
    printf("\n");
}
int main(void) {
    int val, sel;

    init_queue();
    while (1) {
        do {
            printf("1.ENQUEUE 2.DEQUEUE 3.PEEK 4.STATUS 0.EXIT :");
            scanf("%d", &sel);
        } while (sel < 0 || sel > 4);
        if (sel == 1) {
            printf("1.ENQUEUE VALUE ? ");
            scanf("%d", &val);
            enqueue(val);
        }
        else if (sel == 2) {
            val = dequeue();
            if (val != ERROR_VALUE)
                printf("2.DEQUEUE VALUE = %d\n", val);
        }
        else if (sel == 3) {
            val = peek();
            if (val != ERROR_VALUE)
                printf("3.PEEK VALUE = %d\n", val);
        }
        else if (sel == 4)
            print_queue();
        else if (sel == 0) break;
    }
    return 0;
}

I didn't made is_full() function because linked list is "dynamic". When debugging, the program stops when I try enqueuing value. My guess is that there is something wrong in enqueue function, but cannot find what.

This is wrong:

int is_empty() { return (front = NULL && rear == NULL); }

Note the front = NULL . That means every time you call is_empty() , front gets set to NULL , which then causes is_empty() to return 0 because front = NULL evaluates to NULL .

You need to change is_empty() to

int is_empty() { return (front == NULL && rear == NULL); }

And this is exactly why many programmers use "Yoda conditions" like NULL == front - they prevent this type of bug because if you write = instead of == the code will fail to compile.

And, as you've noticed, such bugs are very hard to spot in your own code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM