Question

In: Computer Science

IN C ONLY (follow instruction please) Implement the following functions: Queue * initQueue( ) // Return...

IN C ONLY (follow instruction please)

Implement the following functions:

Queue * initQueue( ) // Return empty queue

// enqueue and dequeue each return an int error code

int enqueue(int, Queue *)

int dequeue(Queue *, int *)

int getQsize(Queue *) // returns # of items in queue

void freeQueue(Queue *) // Free *all* space

Implement the above using a circular-linked list. Again, all operations except freeQueue should take O(1) time.

You will need to document each of your functions to inform the user how to use it and how to interpret its return value. Note that dequeue places the integer removed from the front of the queue into the location specified by the reference provided by the user (the second parameter). Note also that error conditions can arise for both enqueue and dequeue.

Also, try implementing the above with a doubly-circular-linked list. Does it make things easier or harder?

Solutions

Expert Solution

Tried my best to implement the code as per your requirements. Implementing using a Doubly Circular Linked list doesn't make it any easier nor harder. In this case no need to to maintain Tail (Rear) Node in the Queue Struct as Tail Node can be obtained from the previous pointer of the Head Node. I implemented the PrintQ method which is a helper function. Remove it from the code if you wish. I tried my best as far as the Error checking is concerned. Please check it and add any error checking code if necessary. Thanks

#include <stdio.h>
#include <stdlib.h>

typedef struct CLLNode
{
    int data;
    struct CLLNode *next;
} CLLNode;

typedef struct Queue
{
    CLLNode* head; // front
    CLLNode* tail; // rear
    int capacity;
} Queue;

Queue* initQueue()
{
    // initializes the Empty Queue
    Queue* newQueue = (Queue *) malloc(1 * sizeof(Queue));
    newQueue->head = NULL;
    newQueue->tail = NULL;
    newQueue->capacity = 0;

    return newQueue;
}

int enqueue(int value, Queue* queue)
{
    // Add the new integer to the end of the Queue
    // Returns the value appended to the Queue on sucess
    CLLNode* newNode = (CLLNode *) malloc(1 * sizeof(CLLNode));
    newNode->data = value;
    if (queue->capacity)
    {
        newNode->next = queue->head;
        queue->tail->next = newNode;
        queue->tail = newNode;
        queue->capacity += 1;
        return value;
    }
    newNode->next = newNode;
    queue->head = newNode;
    queue->tail = newNode;
    queue->capacity += 1;

    return value;
}

int dequeue(Queue *queue, int* intptr)
{
    // Returns 0 on successfull removing an element from the head (front)
    // returns -1 on removing from an empty queue
    // Use this to check for successfull Enqueue Operation
    if (!queue->capacity)
    {
        printf("Removing an element from an empty queue forbidden\n");
        return -1;
    }

    CLLNode* node = queue->head;
    *intptr = node->data;

    if (queue->capacity == 1)
    {
        queue->head = NULL;
        queue->tail = NULL;
        queue->capacity -= 1;
        free(node);

        return 0;
    }

    // When the size (capacity) of the queue is more than 1
    queue->head = node->next;
    queue->tail->next = node->next;
    free(node);
    queue->capacity -= 1;

    return 0;
}

int getQSize(Queue *queue)
{
    // Return the size or number of elements in the Queue
    return queue->capacity;
}

void freeQueue(Queue *queue)
{
    // Free up the entire Queue by dequeing
    int* temp = (int *) calloc(1, sizeof(int));
    while(queue->capacity)
    {
        dequeue(queue, temp);
    }
    free(queue);
}

void printQ(Queue *queue)
{
    // Helper function just to print the Current Staus of the Queue
    int qIndex = 0;
    CLLNode* current = queue->head;
    while (qIndex < queue->capacity)
    {
        printf("%d ", current->data);
        current = current->next;
        qIndex++;
    }

    printf("\n");
}

int main()
{
    Queue *newQueue = initQueue();

    int* dequedElement = (int *) malloc(sizeof(int));

    int opStatus;
    int index = 0;
    int items[] = {5, 6, 10, 1, -1, 0 , 100, 54};

    for(index = 0; index < sizeof(items) / sizeof(items[0]); index++)
    {
        enqueue(items[index], newQueue);
    }
    printQ(newQueue);

    for (index = 0; index < 3; index++)
    {
        dequeue(newQueue, dequedElement);
        printf("Removing %d\n", *dequedElement);
        enqueue(10 * index, newQueue);
    }

    dequeue(newQueue, dequedElement);
    printQ(newQueue);

    printf("Element removed is %d\n", *dequedElement);
    printf("Size of the Queue id %d\n", getQSize(newQueue));

    freeQueue(newQueue);
}


Related Solutions

Implement a queue - C programming, please read the instruction carefully and implement queue.c using dynamic...
Implement a queue - C programming, please read the instruction carefully and implement queue.c using dynamic array structure given dynarray.h and dynarray.c below The second ADT you'll implement for this assignment is a queue. For this assignment, the interface for the queue (i.e. the structures and the prototypes of functions a user of the queue interacts with) is already defined for you in the file queue.h. Your first task in this assignment is to implement definitions for the functions that...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer list using dynamic array ONLY (an array that can grow and shrink as needed, uses a pointer an size of array). Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: class List { public: // Default Constructor List() {// ... } // Push integer n onto...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
C++ please Fill in for the functions for the code below. The functions will implement an...
C++ please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
USE ONLY THE BELOW FUNCTIONS AND IMPLEMENT THE MISSING PART implement the following missing functions from...
USE ONLY THE BELOW FUNCTIONS AND IMPLEMENT THE MISSING PART implement the following missing functions from the implementation: * reset * intersection * difference Set Set::intersection(Set& s){ Set r; // find intersection return r; } Set Set::difference(Set& s){ Set r; // find difference return r; } void Set::reset(int c){ // increase the capacity and clear the data } driver program int a1[] = {10,5,7,3,9}; Set s1(5); s1.insert(a1,5); s1.print("s1"); int a2[] = {2,9,6}; Set s2(3); s2.insert(a2,3); s2.print("s2"); Set s3 = s1.unionset(s2);...
Write a C++ program (using pointers and dynamic memory allocation only) to implement the following functions...
Write a C++ program (using pointers and dynamic memory allocation only) to implement the following functions and call it from the main function. (1)Write a function whose signature looks like (char*, char) which returns true if the 1st parameter cstring contains the 2nd parameter char, or false otherwise. (2)Create an array of Planets. Populate the array and print the contents of the array using the pointer notation instead of the subscripts.
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
Please use c++ and follow the instruction, I really want to fully understand this question and...
Please use c++ and follow the instruction, I really want to fully understand this question and I will use your code to check where I made mistake (Do not skip steps). I have written my own code which has tons of errors, and I am so confused about this lab. I need help. Lab 6.4 – C++ and Functions – Math Test Critical Review A value-returning function is a function that returns a value back to the part of the...
Study and implement data structure deque (double ended queue, pronounced as "deck"). IN C++ PLEASE
Study and implement data structure deque (double ended queue, pronounced as "deck"). IN C++ PLEASE
write C program to implement the priority queue with the operation insert
write C program to implement the priority queue with the operation insert
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT