Thread: Queue data structure implementation

  1. #1
    Registered User GeorgeIO's Avatar
    Join Date
    Nov 2017
    Posts
    33

    Queue data structure implementation

    Hi all. I am currently trying to implement a queue data structure in C. The context is as follows:

    A Doctors surgery needs a computer program to provide self-service to patients. Patients use this console to check in on arrivalto the surgery. They can also use the console to enquire about their position in the waiting list, or tofind out how many doctors are currently available at the surgery.The program is also used by doctors to check out (discharge) patients when they have finishedexamining them. Doctors use the program as well to check in and out of their rooms.The program must maintain a waiting list (queue) of all checked in patients, and as soon as one of thedoctors is free, the program must call on the next patient in the queue by displaying a message.

    I need to implement the following queue functions:


    • void enqueue (int n) // append n to the queue
    • int dequeue () // remove the first item in the queue, and return its value
    • int first() // get the first item without removing it
    • int last () // get the last item without removing it
    • void clear () // clear (initialize) the queue
    • bool IsEmpty () // returns true if the queue is empty
    • bool IsFull () // returns true if the queue is full
    • void print () // print the entire queue
    • int position (int n) // returns the position of n in the queue, or -1 if n is not in the queue
    • void remove (int n) // remove n from the queue


    Here's what I've got so far:

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
    #include <stdbool.h>
    #define MAX 5
    
    
    void enqueue(int data);
    void dequeue();
    int first();
    int last();
    int position(int n);
    void print();
    bool isEmpty();
    bool isFull();
    
    
    int queue[MAX]; //Defined array as queue
    int front = -1;
    int rear = -1;
    int count = 0;
    int num = -1;
    int a = 0;
    int b = 0;
    
    
    int main() {
       int choice, id;
       do {
          printf("1.Insert element to queue \n");
          printf("2.Delete element from queue \n");
          printf("3.Display queue \n");
          printf("4. View first in queue \n");
          printf("5. View last in queue \n");
          printf("6. View your position in the queue \n");
          printf("7.Quit \n\n");
          printf("Enter your choice : ");
          scanf("%d", & choice);
    
    
          switch (choice) {
          case 1:
             printf("Enter patient ID #: \n");
             scanf("%d", &id);
             enqueue(id);
             break;
    
    
          case 2:
             dequeue();
             break;
    
    
          case 3:
             print();
             break;
    
    
          case 4:
             first();
             break;
    
    
          case 5:
             last();
             break;
    
    
          case 6:
            printf("Enter patient ID #: \n");
            scanf("%d, &id");
            position(id);
            break;
    
    
          case 7:
             exit(1);
    
    
          default:
             printf("Wrong choice. Please enter a valid choice:\n");
          } /*End of switch*/
       } while (choice != 6);
       getchar();
    } /*End of main()*/
    
    
    bool isEmpty() {
       if (front < 0 || front > rear)
          return true;
       else
          return false;
    }
    
    
    bool isFull() {
          if (rear == MAX - 1)
             return true;
          else
             return false;
       }
       //Enqueue start (Inserts element at rear end)
    void enqueue(int data) {
          system("clear");
          if (isFull()) {
             printf("Queue is full\n");
             return;
          } else {
             ++rear;
             queue[rear] = data;
             if (front == -1)
                front = 0;
          }
    
    
       } //Enqueue end
    
    
    //Dequeue start (Removes element in queue from front end)
    void dequeue() {
          system("clear");
          if (isEmpty()) {
             printf("Nothing in queue\n");
    
    
          } else {
             int item = queue[front];
             front++;
             printf("\n\nYou removed %d from the queue\n\n", item);
    
    
          }
       } //Dequeue end
    
    
    //First start (Returns element at front of queue)
    int first() {
          system("clear");
          if (front == -1) {
             puts("Error: cannot return front from empty queue");
             return -1;
          } else {
             printf("%d\n\n", queue[front]);
          }
       } //First end
    
    
    int last() {
       system("clear");
       if (rear == -1) {
          puts("Error: cannot return rear from empty queue");
          return -1;
       } else {
          printf("%d\n\n", queue[rear]);
       }
    }
    
    
    void print() {
       system("clear");
       if (front == -1) {
          printf("Queue is empty \n");
       } else {
          printf("Queue is:\n");
          for (int i = front; i <= rear; i++)
             printf("%d\n\n", queue[i]);
    
    
       }
    }
    
    
    int position(int n) {
      system("clear");
      n = -1;
      int pos = 1;
      for (int i = front; i <= rear; i++) { // a for loop to run for each element in the queue
        if (queue[i % MAX] == n)  // checks to see whether the integer inputted by the user is currently in the queue array	
    
    
          printf("Number %d is in the queue\n", num); //
    
    
          return;
        }
      }
    I've managed to get most of the functions working but I'm having trouble with the last two (return nth item and remove nth item). If anybody could point me in the right direction as to how I should go about implementing those I would be hugely grateful! Unfortunately pointers and structures are not to be used for this particular program. Thank you.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Also posted here.

  3. #3
    Registered User GeorgeIO's Avatar
    Join Date
    Nov 2017
    Posts
    33
    The position function has been fixed, and is now working. I used the following code to get it to work:

    Code:
    void position(int n) {    system("clear");
        for (int i = front; i <= rear; i++) 
        { 
    
    
            if (queue[i % MAX] == n){
                printf("You are in the queue!\n"); 
                printf("Position in queue is %d \n",(i%MAX+1)-front);
                return;
            }  
        }
    }
    So happy about that. Now to try and implement a function to remove an element defined by the user from the queue.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Queue implementation!
    By proudT in forum C Programming
    Replies: 3
    Last Post: 10-24-2015, 11:06 AM
  2. Queue implementation in C
    By cerr in forum C Programming
    Replies: 13
    Last Post: 01-11-2010, 12:30 PM
  3. Queue implementation
    By Hunter2 in forum C++ Programming
    Replies: 3
    Last Post: 02-19-2005, 01:50 PM
  4. Data structure implementation
    By fkheng in forum C Programming
    Replies: 3
    Last Post: 07-31-2003, 07:44 AM
  5. hash table data structure implementation
    By sanju in forum C Programming
    Replies: 1
    Last Post: 09-27-2002, 05:06 AM

Tags for this Thread