Question: c program linked list #include #include // struct ndoe *previous; } Node; Node* insert(Node* listHead, int dataToInsert) { // we need to return a new

c program linked list

#include #include //

struct ndoe *previous; } Node; Node* insert(Node* listHead, int dataToInsert) { // we need to return a new head of linkedlist because // the head may change after insertion if (listHead == NULL) { // list is emtpy, let's insert our first element! printf("inserting first element with data %d ",dataToInsert); Node *newNode = malloc(sizeof(Node)); newNode->data = dataToInsert; newNode->next = NULL; return newNode; } else { // list is not empty, let's insert at the end printf("inserting element with data %d ",dataToInsert); Node *current = listHead; // CAUTION! while (current != NULL) { if (current->next == NULL) { // if it's end of list... Node *newNode = malloc(sizeof(Node)); newNode->data = dataToInsert; newNode->next = NULL; current->next = newNode; return listHead; } else { // otherwise... current = current->next; } } } } void printList(Node* listHead) { printf("printing list: "); Node *current = listHead; while (current != NULL) { printf("%d ",current->data); current = current->next; } printf(" "); }

Node* deleteSpecific(Node *listHead, int dataValue) { // assume data to delete always exist and is always unique in // this exercise printf("deleting %d from list ", dataValue); struct node *ptr,*preptr; ptr = listHead; if(ptr ->data ==dataValue) { listHead = deleteFirst(listHead); return listHead; } else { while (ptr->data != dataValue) { preptr = ptr; ptr = ptr->next; } preptr->next = ptr->next; free(ptr); return listHead; } }

c program linked list #include #include // struct ndoe *previous; } Node;

Proble m-5 Insertion Sort (Hard, Optional) Please further update the insert function so that we will always insert a Node in a position so that the previous Node, if not NULL, is always larger and the next Node, if not NULL, is always smaller.+ If you are successful, you have implemented a sorting method called INSERTION SORT! This exercise is a bit harder and optional, so l will not give to much hints. A successful output should look like (not original main): inserting first element with data 201 8 printing list: 2018-* inserting element with data 2001 inserting element with data 1998 inserting element with data 1999 inserting element.with data 2020 printing list: 2020 2018 2001 1999 1998 deleting 1998 from list printing list: 2020 2018 2001 1999 deleting 2018 from list printing list: 2020 2001 1999

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!