Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help with a lab in C language. Correct answer will get a thumbs up! Write p

ID: 3820500 • Letter: N

Question

Need help with a lab in C language. Correct answer will get a thumbs up!

Write pseudocode and translate it to C-program for the following problem. In this problem, you will create and maintain a virtual line for a service. Your program will display a menu with the following items 0. Call a customer 1. Add a customer 2. Quit Please input your command (0-2): When 0 is selected, if there is no customer in the line, give some warning such as there is no customer for now. Otherwise, print the name of the first customer in the line and remove the customer from the line. When 1 is selected, ask the name of the customer and put the customer to the end of the line. When 2 is selected, your program exits. You must use linked lists in your pseudocode and C-program. No standard library function is allowed to delete or insert a node to the linked list. The data structure of the linked list can be defined in the Data section of your pseudocode, in a manner mentioned in Lab 10.

Explanation / Answer

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

// A linked list (LL) node to store a queue entry
struct QNode
{
char key[100];
struct QNode *next;
};

// The queue, front stores the front node of LL and rear stores ths
// last node of LL
struct Queue
{
struct QNode *front, *rear;
};

// A utility function to create a new linked list node.
struct QNode* newNode(char k[])
{
struct QNode *temp = (struct QNode*)malloc(sizeof(struct QNode));
strcpy(temp->key, k);
temp->next = NULL;
return temp;
}

// A utility function to create an empty queue
struct Queue *createQueue()
{
struct Queue *q = (struct Queue*)malloc(sizeof(struct Queue));
q->front = q->rear = NULL;
return q;
}

// The function to add a key k to q
void enQueue(struct Queue *q, char k[])
{
// Create a new LL node
struct QNode *temp = newNode(k);

// If queue is empty, then new node is front and rear both
if (q->rear == NULL)
{
q->front = q->rear = temp;
return;
}

// Add the new node at the end of queue and change rear
q->rear->next = temp;
q->rear = temp;
}

// Function to remove a key from given queue q
struct QNode *deQueue(struct Queue *q)
{
// If queue is empty, return NULL.
if (q->front == NULL)
return NULL;

// Store previous front and move front one node ahead
struct QNode *temp = q->front;
q->front = q->front->next;

// If front becomes NULL, then change rear also as NULL
if (q->front == NULL)
q->rear = NULL;
return temp;
}

void printMenu()
{
printf("0. Call a customer ");
printf("1. Add a customer ");
printf("2. Quit ");
printf("Please input your command (0-2): ");
}

int main()
{
struct Queue *q = createQueue();
  
while(1)
{
printMenu();
int choice;
scanf("%d", &choice);
if (choice == 0)
{
struct QNode *n = deQueue(q);
if (n == NULL)
{
printf("No customer in line to call. ");
}
else
{
printf("Calling %s ", n->key);
}
}
else if (choice == 1)
{
char name[100];
printf("Enter a name: ");
scanf("%s", name);
enQueue(q, name);
}
else if (choice == 2)
{
break;
}
else
{
printf("Please choose form given menu. ");
}
}
return 0;
}