Question
Fix the errors in C code #include #include void insertAt(int *, int); void Delete(int *); void replaceAt(int *, int, int); int isEmpty(int *, int); int
Fix the errors in C code
#include
void insertAt(int *, int); void Delete(int *); void replaceAt(int *, int, int); int isEmpty(int *, int); int isFull(int *, int); void removeAt(int *, int); void printList(int *, int);
int main() { int *a; int arraySize=0,l=0,loc=0; int choice;
while(1) { printf(" Main Menu"); printf(" 1.Create list 2.Insert element at particular position 3.Delete list. 4. Remove an element at given position 5.Replace an element at given position 6. Check the size of the list 7. Check if the list is empty 8. Check if the list is full 9. Print the current List 10. Exit "); printf(" Enter your Choice: "); scanf("%d", &choice); system("clear") ; //for clearing std. output/console.
switch(choice) { case 1: //Creating the list { printf(" Enter the size of list you want to create:"); scanf("%d", &arraySize); a = (int *)malloc(arraySize*(sizeof(int))); printf(" List created."); break; }
case 2: //Insert the element at given position if(a!=NULL) { printf(" Enter the position you want to insert: "); scanf("%d", &l); if(l>arraySize) //If user gives position which is out of bound { printf(" Invalid input. List only contains %d elements.",arraySize); break; } insertAt(a,l); printf(" Inserted at location %d",l); printList(a,arraySize); break; } else { printf(" List doesn't exist. First create list."); break; }
case 3: //Delete the list if(a!=NULL) { Delete(a); break; } else { printf(" List doesn't exist. First create list."); break; }
case 4: //Remove element at given position if(a!=NULL) { printf(" Enter the position you want to delete: "); scanf("%d", &l); if(l > arraySize) //If user gives position which is out of bound { printf(" Invalid input. List only contains %d elements.",arraySize); break; } removeAt(a, l); break; } else { printf(" List doesn't exist. First create list."); break; }
case 5: //Replace At if(a!=NULL) { printf(" Enter the position you want to replace: "); scanf("%d", &l); printf(" Enter the digit to replace with: "); scanf("%d", &loc); if(l > arraySize) //If user gives position which is out of bound { printf(" Invalid input. List only contains %d elements.",arraySize); break; } replaceAt(a,l,loc); break; } else { printf(" List doesn't exist. First create list."); break; }
case 6: //Check size of list if(a!=NULL) { printf(" Size of list is: %d",arraySize); break; } else { printf(" List doesn't exist. First create list."); break; }
case 7: // Check if empty if(a!=NULL) { if(isEmpty(a,arraySize)) { printf(" List is empty."); break; } else { printf(" List is not empty."); break; } } else { printf(" List doesn't exist. First create list."); break; }
case 8: // Check if full if(a!=NULL) { if(isFull(a,arraySize)) { printf(" List is full."); break; } else { printf(" List is not full."); break; } } else { printf(" List doesn't exist. First create list."); break; }
case 9: //Print list printList(a, arraySize); break; case 10: exit(0);
default: printf(" Invalid input Enter the correct choice."); } } return 0; }
//Functions
void insertAt(int *a, int l) { int b; printf(" Enter the no. you want to insert: "); scanf("%d",&b); a[l-1]=b; }
void replaceAt(int *a, int l,int loc) { a[l-1]=loc; }
void Delete(int *a) { free(a); printf(" List deleted."); }
int isEmpty(int *a, int l) { int i=0; while(i
int isFull(int *a, int l) { int i=0; while(i
void removeAt(int *a, int l) { a[l]=0; printf(" Item removed."); }
void printList(int *a, int size) { printf(" The Elements of The list are:"); for(int i=0;i
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started