In: Computer Science
Complete the code that inserts elements into a list. The list should always be in an ordered state.
-----------------------------------------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
/* list of nodes, each with a single integer */
struct element {
struct element *next;
int value;
};
/* protypes for functions defined after main */
struct element *elementalloc(void);
struct element *listinitialize();
struct element *insertelement(struct element *, int);
void printlist(struct element *);
/* main
* Creates an ordered list
* Elements added to the list must be inserted maintaining the
list
* in an ordered state
*/
int main() {
struct element *listhead = NULL;
listhead = listinitialize();
for (int i = 3; i < 100; i+=11){
listhead = insertnewelement(listhead, i);
}
printlist(listhead);
}
/* allocate memory for a new list element */
struct element *elementalloc(void) {
return (struct element *)malloc(sizeof(struct element));
}
/* simple list initialization function */
struct element *listinitialize() {
const int numbers[7] = {4, 9, 13, 18, 27, 49, 60};
struct element *newlist = NULL;
struct element *tail = NULL;
struct element *temp = NULL;
for (int i = 0; i < 7; i++) {
if (newlist == NULL) {
newlist = elementalloc();
newlist->next = NULL;
newlist->value = numbers[i];
tail = newlist;
} else {
temp = elementalloc();
temp->value = numbers[i];
temp->next = NULL;
tail->next = temp;
tail = tail->next;
}
}
return newlist;
}
/* function to insert elements into an ordered list */
struct element *insertnewelement(struct element *listhead, int x)
{
struct element *newelement;
newelement = elementalloc();
struct element *iter = listhead;
while( ) {
}
return listhead;
}
/* print the list and the respective memory locations in list
order */
void printlist(struct element *listhead)
{
while (listhead != NULL) {
printf("Memory: %p contains value: %d\n", listhead,
listhead->value);
listhead = listhead->next;
}
}
Please find the requested program below. Also including the screenshot of sample output and screenshot of code to understand the indentation.
Please provide your feedback
Thanks and Happy learning!
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
/* list of nodes, each with a single integer */
struct element {
struct element *next;
int value;
};
/* protypes for functions defined after main */
struct element *elementalloc(void);
struct element *listinitialize();
struct element *insertelement(struct element *, int);
void printlist(struct element *);
/* allocate memory for a new list element */
struct element *elementalloc(void) {
return (struct element *)malloc(sizeof(struct element));
}
/* simple list initialization function */
struct element *listinitialize() {
const int numbers[7] = { 4, 9, 13, 18, 27, 49, 60 };
struct element *newlist = NULL;
struct element *tail = NULL;
struct element *temp = NULL;
for (int i = 0; i < 7; i++) {
if (newlist == NULL) {
newlist = elementalloc();
newlist->next = NULL;
newlist->value = numbers[i];
tail = newlist;
}
else {
temp = elementalloc();
temp->value = numbers[i];
temp->next = NULL;
tail->next = temp;
tail = tail->next;
}
}
return newlist;
}
/* function to insert elements into an ordered list */
struct element *insertnewelement(struct element *listhead, int x) {
struct element *newelement;
newelement = elementalloc();
//Initialize the new element node with values
newelement->value = x;
newelement->next = NULL;
struct element *iter = listhead;
struct element *prev = NULL;
bool bInserted = false;
//Check if the new element is going to be the first elemnt in the list
//That means the given listhead was NULL
if (listhead == NULL)
{
listhead = newelement;
}
else
{
//This means there is atleast one elemnt in the given list
while (iter != NULL) {
if (x <= iter->value)
{
//Check whether the element should be added in the front of the list
if (prev == NULL)
{
newelement->next = iter;
listhead = newelement;
bInserted = true;
break;
}
else //ie prev != NULL
{
newelement->next = prev->next;
prev->next = newelement;
bInserted = true;
break;
//}
}
}
prev = iter;
iter = iter->next;
}
if (bInserted != true)
{
//This means the elemnt needs to be added as the last node in the list
prev->next = newelement;
}
}
return listhead;
}
/* print the list and the respective memory locations in list order */
void printlist(struct element *listhead)
{
while (listhead != NULL) {
printf("Memory: %p contains value: %d\n", listhead, listhead->value);
listhead = listhead->next;
}
}
/* main
* Creates an ordered list
* Elements added to the list must be inserted maintaining the list
* in an ordered state
*/
int main() {
struct element *listhead = NULL;
listhead = listinitialize();
for (int i = 3; i < 100; i += 11){
listhead = insertnewelement(listhead, i);
}
printlist(listhead);
return 0;
}