Enqueue and Dequeue Functions

140 views Asked by At

So short context, I need to dequeue my elements of the queue and enqueue them to a temporary queue. When the original queue is empty and the elements are inside the temporary queue, I need to enqueue them back to the original queue. My enqueue function works fine but I think my dequeue function lacks some arguments and I can't think of it. After executing the different functions that call dequeue, the program will always terminate after the complete execution of a function.

Here is a piece of code that addresses the same problem.

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <conio.h>

typedef struct person{
    char firstname[30], lastname[30], sex[10];
    int hfeet, hinches;
    float weight;
}Person;

typedef struct node *nodeptr;

typedef struct node{
    Person data;
    nodeptr next;
}Node;

//Function Declarations
void Add(nodeptr *queue, nodeptr *tail);
void Enqueue(nodeptr *queue, nodeptr *element, nodeptr *tail);
nodeptr Dequeue(nodeptr *queue, nodeptr *head);
void DisplayAll(nodeptr *queue, nodeptr *head, nodeptr *tail);

int main(void) {
    nodeptr queue1 = NULL;
    nodeptr h = NULL;
    nodeptr t = NULL;
    int ch;

    while(1){
    printf("Enter choice: ");
    scanf("%d", &ch);

        switch (ch) {
            case 1:
                Add(&queue1, &t);
                break;
            case 2:
                DisplayAll(&queue1, &h, &t);
                break;  
        }
    } 
    return 0;
 }

void Enqueue(nodeptr *queue, nodeptr *element, nodeptr *tail) {
    if (*tail == NULL) {
       *queue = *element;
       *tail = *element;
     } 
    else {
       (*tail)->next = *element;
       *tail = *element;
    }
}

nodeptr Dequeue(nodeptr *queue, nodeptr *head) {
    nodeptr current = *queue;
    *queue = (*queue)->next;
    *head = *queue;
     return current;
}

void Add(nodeptr *queue, nodeptr *tail){

    nodeptr queue1 = *queue;
    nodeptr t = *tail;
    bool chk = *chked;
    char first[30], last[30];
    int inch, feet;
    float mass;

    nodeptr element = (nodeptr)malloc(sizeof(Node));

        printf("\nEnter First Name: ");
        fflush(stdin);
        gets(first);
        strcpy(element->data.firstname, first);
        printf("Enter Last Name: ");
        fflush(stdin);
        gets(last);
        strcpy(element->data.lastname, last);
        Enqueue(&queue1, &element, &t);
        printf("\nData successfully added.");
        getch();
        system("cls");
    
        *queue = queue1;
        *tail = t;
        *chked = chk;
}

void DisplayAll (nodeptr *queue, nodeptr *head, nodeptr *tail){

    nodeptr queue1 = *queue;
    nodeptr h = *head;
    nodeptr t = *tail;
    nodeptr tempqueue = NULL;
    nodeptr h2 = NULL;
    nodeptr t2 = NULL;  

    while (queue1 != NULL){
        nodeptr current = Dequeue(&queue1, &h);
        printf("\nFirst Name\tLast Name");
        printf("\n%s\t%s", current->data.firstname, current->data.lastname);
        getch();
        Enqueue(&tempqueue, &current, &t2);
    }

    while (tempqueue != NULL){
        nodeptr current = Dequeue(&tempqueue, &h2);
        Enqueue(&queue1, &current, &t);
    }

    *queue = queue1;
    *head = h;
    *tail = t;
    getch();
    system("cls");
}

After Displaying the elements inside the queue, the program will exit.

0

There are 0 answers