Output Example: Enter your choice: 1 to insert an element into the list. 2 to de
ID: 3620087 • Letter: O
Question
Output Example:
Enter your choice:
1 to insert an element into the list.
2 to delete an element from the list.
3 to end.
?4
Invalid choice.
Enter your choice:
1 to insert an element into the list.
2 to delete an element from the list.
3 to end.
?1
Enter a character: B
The list is:
B ==> NULL
?1
Enter a character: C
The list is:
B ==> C ==> NULL
?1
Enter a character: A
The list is:
A ==> B ==> C ==> NULL
?2
Enter character to be deleted: C
C deleted.
The list is:
A ==> B ==> NULL
?2
Enter character to be deleted: A
A deleted.
The list is:
B ==> NULL
?2
Enter character to be deleted: B
B deleted.
List is empty.
?3
End of run.
Explanation / Answer
#include #include #define L_NULL ((LLIST *) NULL) #define lalloc() ((LLIST *)malloc(sizeof(LLIST))) typedef struct link { int val; struct link *next; } LLIST; LLIST *head; int insert(LLIST **head, int value) { register LLIST *p, *q; /* pointers to walk linked list */ /* * you can argue whether or not this next line had a bug, * but it certainly was poor style, since lalloc is * called below! */ register LLIST *temp; /* points to new node */ /* * construct new node for the linked list */ if ((temp = lalloc()) == NULL) return(0); temp->val = value; temp->next = L_NULL; /* * if an empty list, make this node the head */ if (*head == NULL){ *head = temp; return(1); } /* * walk the linked list; after this loop, * you'll need to insert the new node between * p and q */ for(p = *head; p != NULL && p->val next) q = p; /* * insert the new node and return success */ q->next = temp; temp->next = p; return(1); } int delete(LLIST **head, int value) { register LLIST *p, *q; /* used to locate node in list */ /* * if an empty list, nothing to delete */ if (*head == NULL) return(0); /* * value is at the head of linked list */ if ((*head)->val == value){ /* make head point to next one */ p = *head; *head = (*head)->next; /* free it and return success */ (void) free(p); return(1); } /* * figure out where the node to be deleted is * after this loop, if there is a node with the given * value in the linked list, p points to it, and * q points to its predecessor */ for(p = *head; p != NULL && p->val next) q = p; /* see if the value is in the list */ if (p->val != value) return(0); /* * release the deleted node and return success */ (void) free(p); return(1); } void prforw(LLIST *head) { register LLIST *p; /* pointer in a for loop */ /* * walk the list, printing as you go */ for(p = head; p != NULL; p = p->next) printf("%d -> ", p->val); printf("NULL "); } void prback(LLIST *head) { /* if list is empty, print nothing */ if (head == NULL) return; /* print rest of list */ (void) prback(head->next); /* print this node */ printf("%d ", head->val); } int main(){ while(1) { int choice,num; printf("Enter your choice: 1 to insert an element into the list. 2 to delete an element from the list. 3 to end. "); scanf("%d",&choice); if(choice==3) { break; } if(choice ==1) { printf("Enter number to insert:"); scanf("%d",&num); insert(&head,num); } else if(choice ==2) { printf("Enter number to delete:"); scanf("%d",&num); delete(&head,num); } else { printf(" Invalid choice "); } prforw(head); } system("pause"); }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.