Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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; } }

image text in transcribed

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

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 Principles Programming And Performance

Authors: Patrick O'Neil, Elizabeth O'Neil

2nd Edition

1558605800, 978-1558605800

More Books

Students also viewed these Databases questions