1. Engineering
  2. Computer Science
  3. im writing a main program in c for assignment that...

Question: im writing a main program in c for assignment that...

Question details

I'm writing a main program in C for assignment that adds, deletes, and searches within a linked list. The exit command is also used.

I want to use fgets to get user input, but I'm not sure how to implement it. Per assignment specifications, the linked list has to be implemented using dynamic memory with a command like malloc. How would I use fgets with dynamic memory? I've only ever used it with an array of a specified size.

Here's the main program parameters:

MAIN PROGRAM You should use the above functions to implement a main program which lets the user manipulate a linked list. It is important that the main program never manipulate the data structure directly. All manipulation should be done via the above routines (this is the idea of encapsulation/information hiding/ADTs) The first thing your program should do is initialize an empty linked list Next, the program should prompt the user for input using the prompt and then wait for the user to enter a command followed by the ENTER key. The program then processes the command, as follows: i number (where number is any legal integer) This will insert the number into the list, in order (smallest to largest), provided the node is not already in the list. It should then print the message SUCCESS; NODE ALREADY IN LIST, or OUT OF MEMORY. This will print the list in order, on one line, with the numbers separated by spaces s number This searches for the given number, and prints either FOUND or NOT FOUND d number This deletes the node containing the given number, and prints either SUCCESS or NODE NOT FOUND This should cause the program to exit after freeing all allocated memory (use valgrind to confirm this) (anything else) should print a synopsis of legal commands. The program should return to showing a prompt, and wait for another command.

Here's my main program code:

#include<stdio.h> #include<stdlib.h> #include<ctype.h> struct node int data; struct node *next 0; struct node *init(); int add(struct node list,int number); void print(struct node* list); int delete(struct node* list,int number); int search(struct node* list,int number); int main() struct node* list; list-init(list); char ch; int number; int flag-0 int status; 0 printf(> ; scanf(%c,&ch); switch(ch) case i scanf(%d,&number); if(search(list, number) 0) status-add(list, number); if( ) status-=0 printf( OUT OF MEMORY); else printf( SUCCESS); else printf(nNODE ALREADY PRESENT);

break; case p: printf( Linked list is:); print(list); break; case s scanf(%d,&number); status-search(list, number); if(status-=0) printf(nNOT FOUND); else printf(nFOUND); break; case d: scanf(%d , &number); status-delete(list,number); if(status0) printf( NODE NOT FOUND); else printf(InSUCCESS); break; case x: free(list break; ); while(ch x return 0;

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution