(( C program only )) Plan (some cases are missed below) // create a node tempN =
ID: 3820159 • Letter: #
Question
(( C program only ))
Plan (some cases are missed below)
// create a node
tempN = malloc(sizeof(Node))
// set the name of the node tempN
copy name to(*tempN).name
// set the pointer of the node tempN
(*tempN).next = NULL
// insert tempN to the end of the list
(*tail).next = tempN;
// set tail to point to the last node
tail = tempN;
l. 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. Otherwisc, print the name of the first customcr in the line and removc 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 10Explanation / Answer
//pseudocode
1) if user selects 0, check linked list
2) if list is empty give warning to user and show the menu
3) if list is not empty, then remove the customer from list , go to menu
4) if user selects 1 ie add user , then customer is added at the end of the list
5)if user selects option 2 , then program exists.
//program
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node
{
char name[20];
struct node *next;
};
typedef struct node Node;
Node *list;
int main()
{
int choice;
do
{
printf("0.Call a custmer 1.Add a customer 2.Quit ");
printf("Enter your choice: ");
scanf("%d", &choice);
if (choice == 0)
{
if (list == NULL)
{
printf("List is empty ");
}
else
{
Node *tmp ;
printf("Name of the customer in line: %s ", list->name);
if (list->next != NULL)
{
tmp = list->next;
free(list);
list = tmp;
}
else
{
list = NULL;
}
}
}
if (choice == 1)
{
char str[20];
Node *tmp;
printf("Enter the customer name to be placed in call: ");
scanf("%s", str);
tmp = (Node*)malloc(sizeof(Node));
strcpy(tmp->name, str);
tmp->next = NULL;
if (list == NULL)
{
list = tmp;
}
else
{
list->next = tmp;
}
}
} while (choice != 2);
}
----------------------------------------------------------------------
//output
0.Call a custmer
1.Add a customer
2.Quit
Enter your choice: 1
Enter the customer name to be placed in call: Jack
0.Call a custmer
1.Add a customer
2.Quit
Enter your choice: 1
Enter the customer name to be placed in call: Jim
0.Call a custmer
1.Add a customer
2.Quit
Enter your choice: 0
Name of the customer in line: Jack
0.Call a custmer
1.Add a customer
2.Quit
Enter your choice: 0
Name of the customer in line: Jim
0.Call a custmer
1.Add a customer
2.Quit
Enter your choice: 2
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.