Implementing Queue Data Structure in C

CCBeginner
Practice Now

Introduction

In this project, you will learn how to implement a queue data structure in C. Queues are widely used in computer science, for example, in message queues that are used to transmit data in a computer.

👀 Preview

$ gcc queue.c -o queue
$ ./queue
11250

ðŸŽŊ Tasks

In this project, you will learn:

  • How to implement the front() method to return the value of the front element in the queue
  • How to implement the pop() method to remove and return the front element from the queue
  • How to implement the count() method to return the number of elements currently in the queue
  • How to implement the is_empty() method to check if the queue is empty

🏆 Achievements

After completing this project, you will be able to:

  • Understand the basic operations of a queue data structure
  • Implement the core methods of a queue in C
  • Apply your knowledge of queues to solve real-world problems

Implement the front() Method

In this step, you will learn how to implement the front() method of the queue.

The front() method should return the value of the front element in the queue. If the queue is empty, the method should exit the program with exit(-1).

Follow the steps below to complete this step:

  1. Open the queue.c file located at /home/labex/project/queue.c.
  2. Locate the front() method in the code.
  3. Implement the front() method as follows:
static int front() {
    if(!phead) {
        exit(-1);
    }
    return phead->val;
}

The front() method first checks if the phead pointer is NULL, which indicates an empty queue. If the queue is empty, the method exits the program with exit(-1).

If the queue is not empty, the method returns the value stored in the phead node, which represents the front element of the queue.

Implement the pop() Method

In this step, you will learn how to implement the pop() method of the queue.

The pop() method should remove and return the front element from the queue. If the queue is empty, the method should exit the program with exit(-1).

Follow the steps below to complete this step:

  1. Open the queue.c file located at /home/labex/project/queue.c.
  2. Locate the pop() method in the code.
  3. Implement the pop() method as follows:
static int pop() {
    if(!phead) {
        exit(-1);
    }
    int val = phead->val;
    phead = phead->next;
    return val;
}

The pop() method first checks if the phead pointer is NULL, which indicates an empty queue. If the queue is empty, the method exits the program with exit(-1).

If the queue is not empty, the method stores the value of the phead node, which represents the front element of the queue. Then, it updates the phead pointer to the next node, effectively removing the front element from the queue. Finally, the method returns the stored value.

Implement the count() Method

In this step, you will learn how to implement the count() method of the queue.

The count() method should return the number of elements currently in the queue.

Follow the steps below to complete this step:

  1. Open the queue.c file located at /home/labex/project/queue.c.
  2. Locate the count() method in the code.
  3. Implement the count() method as follows:
static int count() {
    int cnt = 0;
    for(struct node* p = phead; p; p=p->next) {
        cnt++;
    }
    return cnt;
}

The count() method initializes a cnt variable to 0. Then, it traverses the queue by following the next pointers of the phead node and its successors. For each node encountered, it increments the cnt variable. Finally, the method returns the cnt value, which represents the number of elements in the queue.

Implement the is_empty() Method

In this step, you will learn how to implement the is_empty() method of the queue.

The is_empty() method should return 1 if the queue is empty, and 0 otherwise.

Follow the steps below to complete this step:

  1. Open the queue.c file located at /home/labex/project/queue.c.
  2. Locate the is_empty() method in the code.
  3. Implement the is_empty() method as follows:
static int is_empty() {
    return phead == NULL;
}

The is_empty() method simply checks if the phead pointer is NULL, which indicates an empty queue. If phead is NULL, the method returns 1, indicating that the queue is empty. Otherwise, it returns 0, indicating that the queue is not empty.

Congratulations! You have now implemented all the required methods for the queue data structure. You can now compile and execute the program to test the functionality of the queue.

  1. Compile and execute:
$ gcc queue.c -o queue
$ ./queue
00000

The expected output is as follows:

$ gcc queue.c -o queue
$ ./queue
11250
âœĻ Check Solution and Practice

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other C Tutorials you may like