Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

flowchart and pseudocode for the below code #include #include #include //node that stores info for mobile and is used as node in linked // list

flowchart and pseudocode for the below code

#include #include #include

//node that stores info for mobile and is used as node in linked // list que and stack typedef struct Node { char name[50]; int id; struct Node* next; } node;

// add new mobile to linked list void addMobile(node** mobiles, int id, char name[]) { // make new node and fill it with data node *mobile = (node*) malloc(sizeof(node)); mobile->id = id; strcpy( mobile->name, name);

// if linked list is empty if(*mobiles == NULL) { *mobiles = mobile; return; }

// traverse to the right position for new mobile insertion node* temp = *mobiles; node* prev = NULL;

// until loop doesnt reach end of list, // and until new mobile name is greater than alredy present mobiles // beacause mobiles have to be inserted alphabetically by name while(temp != NULL && strcmp(temp->name, name) < 0) { prev = temp; temp = temp->next; }

// if new node is to be inserted before head if(prev == NULL) { mobile->next = *mobiles; *mobiles = mobile; } else //insert after prev { mobile->next = prev->next; prev->next = mobile; } }

// it reads mobiles from file void readMobile(node **mobiles) { FILE* file = fopen("mobiles.txt", "r");

if(! file) { printf("Unable to open mobiles.txt file "); return; }

int id; char name[50];

// get mobile., %[^ ] reads a string till the end of line while(fscanf(file, "%d %[^ ]", &id, name) == 2) { // use above data to add mobile addMobile(mobiles, id, name); } }

void displayMobiles(node *mobiles) { node* temp = mobiles; printf("ID \t Name ");

while(temp != NULL) { printf("%d \t %s ", temp->id, temp->name); temp = temp->next; } }

// it displays orders in queue, queue mobiles have only name // so display only names void displayOrders(node *mobiles) { node* temp = mobiles; printf("Orders for ");

while(temp != NULL) { printf("%s ", temp->name); temp = temp->next; } }

// to add new order in queue at the end void addOrder(node** orders, char name[]) { // make new node node fill with data node *order = (node*) malloc(sizeof(node)); order->next = NULL; strcpy(order->name, name);

// if queue is empty if(*orders == NULL) { *orders = order; return; }

//else traverse to the end of queue and add node* temp = *orders; node* prev = NULL;

while(temp != NULL) { prev = temp; temp = temp->next; }

prev->next = order; }

void saveToFile(node *mobiles) { FILE *file = fopen("mobiles.txt", "w");

node* temp = mobiles;

// get all the mobiles from list and put in file while(temp != NULL) { fprintf(file, "%d %s ", temp->id, temp->name); temp = temp->next; }

printf("Saved data to file "); }

int menu() { printf(" 1.Display stock \t 2.Add new mobile \t 3.Next Order info "); printf("4.Display all orders \t 5.Add new order \t 6.Process next Order ");

printf("7.Cancel last order \t 8.Display last order \t 9.Save to file ");

printf("10.Quit: ");

int choice; scanf("%d", &choice);

return choice; }

int main() { node *mobiles = NULL; readMobile(&mobiles);

node* order_list = NULL; //queue node* stack = NULL;

while(1) { int choice = menu();

if(choice == 1) { if(mobiles == NULL) { printf("No mobiles in stock "); } else displayMobiles(mobiles); }

else if(choice == 2) // add new mobile { printf("Mobile id: "); int id; scanf("%d", &id);

printf("Mobile name: "); char name[50];

//mobile name can contain spaces, so use gets to read its name fflush(stdin); // fflush is used to remove and garbage input already present gets(name);

addMobile(&mobiles, id, name);

printf("Mobile added "); }

else if(choice ==3 ) { if(order_list == NULL) { printf("No orders available "); } else { printf("Order for Mobile: %s ", order_list->name); } }

else if(choice == 4) { if(order_list == NULL) { printf("No orders available "); } else displayOrders(order_list); }

else if(choice == 5) { printf("Mobile name: "); char name[50]; fflush(stdin); gets(name);

addOrder(&order_list, name); printf("Order created "); }

else if(choice == 6) { if(order_list == NULL) { printf("No orders available "); } else { //find mobile in linked list, by comparing its name node* temp = mobiles; node *prev = NULL;

while(temp != NULL) { // if mobile found break out of loop if( strcmp(temp->name, order_list->name) == 0) break; prev = temp; temp = temp->next; }

//if mobile not found in linked list if(temp == NULL) { printf("Order not in stock "); } else { //make a new node to insert in stack, this will be the processed order // fill same data taken from linked list node *newNode = (node*) malloc(sizeof(node)); newNode->id = temp->id; strcpy(newNode->name, temp->name);

// add at top of stack newNode->next = stack; stack = newNode;

//now delete from linked list // if head is to be removed if(prev == NULL) { mobiles = mobiles->next; free(temp); } else { prev->next = temp->next; free(temp); }

//now also delete order from queue as well temp = order_list; order_list = order_list->next; free(temp);

printf("Order processed "); } } }

else if(choice == 7) { if(stack == NULL) { printf("No order processed to cancel "); } else { // add new mobile with same data taken from stack addMobile(&mobiles, stack->id, stack->name);

//remove from stack stack = stack->next;

printf("Last processed order cancelled "); } }

else if(choice == 8) { if(stack == NULL) { printf("No order processed "); } else printf("ID: %d \t Name: %s ", stack->id, stack->name); }

else if(choice == 9) { saveToFile(mobiles); }

else if(choice == 10) { break; } else printf("Invalid choice "); }

return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Unity From Zero To Proficiency Beginner A Step By Step Guide To Coding Your First Game

Authors: Patrick Felicia

1st Edition

1091872023, 978-1091872028

More Books

Students also viewed these Programming questions

Question

why do firms seek to export?

Answered: 1 week ago