In: Computer Science
/*instructions: please do not change the general structure of the code that I already have
please write it in C programming
Define the interface for a queue ADT and implement it in two
ways: one with a dummy node (or nodes) and one without.
In addition to the two standard queue functions you’ll need a
function to create/initialize an empty queue,
a function to free an existing queue (which may not be
empty),
and a function to return the size (number of keys) in the
queue.
All functions should be as efficient as possible, e.g., the
complexity of enqueue and dequeue must be O(1).
An additional requirement is that the underlying data structure
should not be a doubly-linked list.*/
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int num;
struct node *next;
}Node;
typedef struct{
Node* front
Node* rear;
int size;
}Queue;
//Define the interface for a queue ADT
Queque* initializeQueque()
{
}
Queque* insert(int item)
{
}
Queque* delete()
{
}
void printList(Node* Q)
{
}
//get the size of the queque at postion [-1]
//return the size (number of keys) in the queue.
int getsize(Node* Q)
{
}
int main()
{
int option,item;
Node* Queque;
while(1){
printf("1.Insert number to queque\n2.Delete number from
queque\n3.Display numbers in queque\n4.Exit\nEnter a
option:");
scanf("%d",&option);
switch(option){
case 1:
printf("Enter the number:");
scanf("%d",&item);
Queque=insert(item);
break;
case 2:
Queque=delete();
break;
case 3:
printList(Queque);
break;
case 4:
return 1;
default:
printf("\nWrong input!\n");
break;
}
}
}
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int num;
struct node *next;
}Node;
typedef struct{
Node* front;
Node* rear;
int size;
}Queue;
//Define the interface for a queue ADT
Queue* initializeQueque()
{
Queue* q = (Queue*)malloc(sizeof(Queue));
q->rear=NULL;
q->front=NULL;
q->size=0;
}
Queue* insert(Queue*q,int item)
{
Node* newnode = (Node*)malloc(sizeof(Node));
newnode->num = item;
newnode->next=NULL;
if(q->rear==NULL){
q->rear=newnode;
q->front = q->rear;
}
else{
q->rear->next = newnode;
q->rear = newnode;
}
q->size+=1;
return q;
}
Queue* delete_(Queue*q)
{Node*temp = q->front;
if(q->front==q->rear){
q->front=NULL;
q->rear=NULL;
}
else{
q->front=q->front->next;
}
printf("%d is deleted\n",temp->num);
q->size-=1;
free(temp);
}
void printList(Queue* Q)
{
Node*temp=Q->front;
while(temp){
printf("%d ",temp->num);
temp=temp->next;
}
}
//get the size of the queque at postion [-1]
//return the size (number of keys) in the queue.
int getsize(Queue* Q)
{
return Q->size;
}
int main()
{
int option,item;
Queue* q= initializeQueque();
while(1){
printf("1.Insert number to queque\n2.Delete number from
queque\n3.Display numbers in queque\n4.Exit\nEnter a
option:");
scanf("%d",&option);
switch(option){
case 1:
printf("Enter the number:");
scanf("%d",&item);
q=insert(q,item);
break;
case 2:
delete_(q);
break;
case 3:
printList(q);
break;
case 4:
return 1;
default:
printf("\nWrong input!\n");
break;
}
}
}