Circular Queue in C
Currently working on a code that gives us an integer value (1,2,3) along side with a datatype either integer/char/string. Here,
1 -> Enqueue
2 -> Dequeue
3 -> Display
For different testcases i am given with different data types. Like testcase 1 has integer values and testcase 2 with string names.
I need help in handling all the different input types for C. The following is my code for simple integer values.
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
};
struct node *head, *tail = NULL;
void enq(int x){
struct node *nade = malloc(sizeof(struct node));
nade->data = x;
nade->next = head;
if (head == NULL)
head = tail = nade;
else{
tail->next = nade;
tail = nade;
}
}
void dq(){
struct node *temp;
if (head == NULL)
printf("Deque is Empty\n");
else if (head == tail){
printf("%d is deleted\n", head->data);
head = NULL;
}
else{
printf("%d is deleted\n", head->data);
tail->next = head->next;
head = head->next;
}
}
void display(){
if (head == NULL)
printf("Empty\n");
else{
struct node *temp;
printf("Circular Queue ");
for (temp = head; temp != tail; temp = temp->next)
printf("%d ", temp->data);
printf("%d\n", temp->data);
}
}
int main(){
int t; scanf("%d", &t);
do{
switch (t)
{
int x;
case 1:
scanf("%d", &x);
enq(x);
break;
case 2:
dq();
break;
case 3:
display();
break;
}
scanf("%d", &t);
}while (t != 4);
return 0;
}