Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is my code and I need it to be double checkecked please. CS1 LAB - Recursion with Linked List Now, you already know about

image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribedThis is my code and I need it to be double checkecked please.
CS1 LAB - Recursion with Linked List Now, you already know about linked list and also recursion. Now in this lab we will try to write code on sorted linked list insertion and deletion using recursion. We will also write a set of other utility functions using recursion! Here is the list of functions and you have to use recursion for all of them: struct node insert(struct node* list, int d); struct node* del(struct node* list,int d); vold print struct node "list): vold freeList(struct node* list); void copy (struct node *a, struct node **s); For your help, the prototypes and the main function is provided here: int main() { int number = 8, choice=e; struct node *plist=NULL; struct node *nList = NULL; // Let the user add values antil they enter -1. while(choice! 4) Get the operation printf(" Do you want to (1)insert, (2)delete, (3) copy (4)quit. In"); scanf("%d", &choice); printf("Your choice is xain, choice); // Execute the operation, if (choice w 1) { // Get the number. printf("Enter the value to insert "); scanf("%d", &number); plist - Insert(plist, number): 11 Look at the list. printf("Items in linked list: "); print(plast); //printf(" "); > else if (choice - 2) { 1/ Get the number printf("Enter the value to delete. In"); scanf("%d", &number); plist - del(plist, number); Look at the list printf("Items in linked list: "); print(plist); //printf(" "); } else if (choice * 3) { if (nlist) freeList(nlist); copy (plist, enlist); //passing reference of nlist as it is not returning anything // Look at the list. printf("Items in New linked list: "); print(nlist); // printf(" "); } else { break; ) freelist(nList); freelist(list); printf(" Bye.. "); return 0; Most of the functions will be solved in the lab and it is expected that you will follow the TA and will try to work on it and submit your work in webcourses under Lab7. The code written in the lab will not be uploaded as you are typing it anyway and you will be adding more feature. Also, use memory leak detector to check whether you have memory leak or not! It should be also part of your submission. Sample input/output: (starts with a new line) Do you want to (1) insert, (2) delete, (3) Copy (4) quit. 1 Your choice is 1 Enter the value to insert 4 Items in linked list: ->4 Do you want to (1) insert. 12 delete (3) Cony mitt Your choice is 1 Enter the value to insert 2 6 Items in linked list: ->4->6 Do you want to (1) insert, (2) delete, (3) Copy (4) quit. 1 Your choice 151 Enter the value to insert Items in linked list: ->2->4->6 Do you want to (1)insert. (2) delato, (3) Copy (4) quit. 1 Your choice is 1 Enter the value to insert 1 Items in linked list: ->]->2-4-6 Do you want to (1) innert, (2) delete, (3)Copy (9) quit. 1 Your choice is 1 Enter the value to insert 3 Items in linked list: >1->2->3=>4->6 Do you want to (1) incert, (2) delete, (3) Copy (4) quit. 2 Your choice is 2 Enter the value to delete. Items in linked list: ->1->2->4->6 Do you want to (1) insert, (2) delete. (3) Copy (4) quit. Your choice is 3 Items in NEW linked list ->1->2-4-> Do you want to (1) insert, (2) delete. (3) Copy (4)quit. 4 Your choice is 4 3 3 Bye.. [ 2 3 #include 4 #include 5 #include "leak_detector_c.h" 6 7 struct node 8 { 9 int data; 10 struct node *next; 11 }; 12 13 struct node insert(struct node list, int d); 14 struct node" del(struct node* list, int d); 15 void copy ( struct node *a, struct node **s); 16 void print( struct node* list); 17 18 19 int main() 20. { 21 atexitCreport_mem_leak); // leak detector 22 int number -0, choice; 23 struct node *plist=NULL; 24 struct node *nList NULL; 25 26 //add values untill they enter 4. 27 while(choice! .4) 28 { 29 // Operation 30 printf(" Do you want to (1)insert, (2)delete, (3)Copy, (4)quit.In); 31 scanf("%d", &choice); 32 33 printf("Your choice is %d ", choice); 34 35 //Excute the Operation. 36 if (choice 1) Ctrl+S //Excute the Operation. if (choice 1) { 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 //Get the number print ("Enter the value to insert "); scanf("%d", &number); plist = insert(pList, number); // look at list printf("Items in linked list: "); print(plist); //print(" "); } else if (choice 2) I { //Get number printf("Enter the value to delete. "); scanf("%d", number); pList - del(plist, number); // Loo at the list. printf("Items in linked list: "); print(plist); //print(" "); } else if (choice 3) { if(nlist) freelist(nlist); copyCplist, &List);// passing reference printf("Items in NEW linked list: "); print(nlist); //print(" "); } else { 69 57 } Ctrl+S 58 else if (choice 3) 59 { 60 if(nList) 61 freeList(nlist); 62 63 copyCpList, &List);// passing reference 64 printf("Items in NEW linked list: "); 65 print(nlist); 66 //print(" "); 67 } 68 else { 70 break: 71 } 72 } 73 freelist(nlist); freelist(nlist); 75 printf(" Bye.. "); return; 77 } 78 //reversively inserts item 79 struct *node insert(struct node "list, int item) 80 - { 81 if(list => NULL || item data) 82 { 83 struct node "pNew (struct node *) Calloc(sizeof(node))); 84 pNew->data - item; 85 pNew->data - list; 86 return pNew; 87 } 88 list->next insert(list->next, item); 89 return list; 90 } 91 74 76

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

Step: 3

blur-text-image

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

Database Concepts

Authors: David M Kroenke, David J Auer

6th Edition

0132742926, 978-0132742924

More Books

Students also viewed these Databases questions

Question

Describe Table Structures in RDMSs.

Answered: 1 week ago