Description Your work at the fleet vehicle startup has been going wl. You now ha
ID: 3591644 • Letter: D
Question
Description Your work at the fleet vehicle startup has been going wl. You now have a few customers, and one of their requests is that they want your software to keep track of the deliveries they need to make, and they want to use the software to decide which delivery to send out first. Your job is to write a command line interface that can keep track of these deliveries. Your program l be given a series of com mands as input. The commands your program should accept are the following: order, delivery, exit exit When the command "exit" is given, your program should exit. You should make sure to clean up any allocated memory before exiting. order If your program is given the command "order", that means an or der is being inputted to your program. Your program then needs to read an order ID, followed by a product name, followed by a quantity. The order ID and the quantity are integers. The product name is a string that is guaranteed to be less than 20 characters.Explanation / Answer
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
//Create a structure for NODE to store item information in a linked list
struct node
{
//To store product id
int ID;
//To store product name
char name[20];
//To store product quantity
int quantity;
//Self reference pointer to point to next node
struct node *link;
};//End of structure
//Initialize pointers as globals so that they do not need to be passed in functions.
struct node *header, *header1, *ptr, *ptr1, *temp;
//Function to order a item and add it to the end of linked list
void Order()
{
int data;
char na[20];
int qty;
//Accepts item information from the user
printf(" Enter Product ID: ");
scanf("%d", &data);
printf(" Enter product Name: ");
scanf("%s", na);
printf(" Enter Product Quantity: ");
scanf("%d", &qty);
//Creates a temporary pointer
temp = (struct node *) malloc(sizeof(struct node));
//ptr pointer to the first linked list header
ptr = header;
//Traverse to the end of the linked list.
while(ptr->link != NULL)
{
//ptr points to the next node
ptr = ptr->link;
}//End of while
//Stores the entered data in temp pointer
temp->ID = data;
strcpy(temp->name, na);
temp->quantity = qty;
//temp link part is pointing to the ptr link
temp->link = ptr->link;
//ptr link is pointing to temp
ptr->link = temp;
}//End of function
//Function to delete a node item from the front of a linked list and adds it in another linked list.
void Delivery()
{
//Checks if the first list is already empty
if(header->link == NULL)
{
printf(" Empty Linked List. Deletion not possible. ");
}//End of if
//If not empty
else
{
//ptr points to the header link
ptr = header->link;
//Loops till end of first linked list
while(ptr != NULL)
{
//Creates a temporary node
temp = (struct node *) malloc(sizeof(struct node));
//ptr1 points to the second linked list header
ptr1 = header1;
//Traverse to the end of the second linked list.
while(ptr1->link != NULL)
{
//ptr1 points to the next node
ptr1 = ptr1->link;
}//End of while
//Adds the first linked list node to the second linked list
temp->ID = ptr->ID;
strcpy(temp->name, ptr->name);
temp->quantity = ptr->quantity;
//temp link part is pointing to the ptr1 link
temp->link = ptr1->link;
//ptr1 link is pointing to temp
ptr1->link = temp;
//Delete node operation
//First linked list header link is pointing to the ptr link
header->link= ptr->link;
//Delete the ptr
free(ptr);
printf(" Delivery ");
//ptr is pointing to ptr link
ptr = ptr->link;
}//End of while
}//End of else
}//End of function
//Function to display the contents of the delivered linked list.
void display()
{
printf(" Items Delivered: ");
//Print the contents of the second linked list starting from header1
ptr = header1;
//Loops till end of the second linked list
while(ptr->link != NULL)
{
//ptr points to the next node
ptr = ptr->link;
//Display the node contents
printf(" %d %s %d", ptr->ID, ptr -> name, ptr -> quantity);
}//End of while
}//End of function
//main function definition
void main()
{
int choice;
int cont = 1;
//Allocate memory for header node and header1 node.
header = (struct node *) malloc(sizeof(struct node));
header1 = (struct node *) malloc(sizeof(struct node));
//Initializes header and header1 node
header->link = NULL;
header1->link = NULL;
//Loops till user choice is not 3
while(1)
{
//Display menu to the user
printf(" 1. Order ");
printf(" 2. Delivery ");
printf(" 3. Exit ");
//Accepts user choice
printf(" Enter your choice: ");
scanf("%d", &choice);
//Selects operation as per use choice
switch(choice)
{
case 1:
Order();
break;
case 2:
Delivery();
break;
case 3:
display();
exit(0);
break;
}//End of switch case
}//End of while
getch();
}//End of main
Sample Run:
1. Order
2. Delivery
3. Exit
Enter your choice: 1
Enter Product ID: 1001
Enter product Name: Lux
Enter Product Quantity: 10
1. Order
2. Delivery
3. Exit
Enter your choice: 1
Enter Product ID: 1002
Enter product Name: Rin
Enter Product Quantity: 40
1. Order
2. Delivery
3. Exit
Enter your choice: 2
Delivery
Delivery
1. Order
2. Delivery
3. Exit
Enter your choice: 3
Items Delivered:
1001 Lux 10
1002 Rin 40