Question

In: Computer Science

Complete the code that inserts elements into a list. The list should always be in an...

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

Solutions

Expert Solution

Please look at my code and in case of indentation issues check the screenshots.

------------main.c----------------

#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* insertnewelement(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();
   printlist(listhead);
   for (int i = 3; i < 100; i += 11)
   {
       listhead = insertnewelement(listhead, i);
   }
   printf("\n\n");
   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();       //allocate the memory for the new element
   newelement->value = x;               //assign the value x to the new element

   if (listhead == NULL){               //check if the list is empty
       listhead = newelement;            //if list is empty the head will point to the new element, next will be NULL
       listhead->next = NULL;
       return listhead;
   }

   if (listhead->value > x){           //check if the new element's value is less than head, then insert newelement at the beginning
       newelement->next = listhead;   
       listhead = newelement;            //new element will become the listhead
       return listhead;
   }  

   struct element *iter = listhead;   //create an iterator pointer

   while (iter->next != NULL && iter->next->value < x) //loop through the elements which are smaller, than x
   {
       iter = iter->next;               //move to next element
   }                                   //when the loop ends iter->next will have value larger than x
   newelement->next = iter->next;       //update the newelement's next element as iter->next
   iter->next = newelement;            //iter will point to 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;
   }
}

--------------Screenshots-------------------

------------Output-----------------

---------------------------------------------------------------------------------------
Please give a thumbs up if you find this answer helpful.
If it doesn't help, please comment before giving a thumbs down.
Please Do comment if you need any clarification.
I will surely help you.

Thankyou


Related Solutions

Complete the code that inserts elements into a list. The list should always be in an...
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...
Complete the code that inserts elements into a list. The list should always be in an...
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...
Complete the code that inserts elements into a list. The list should always be in an...
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...
Complete the code that inserts elements into a list. The list should always be in an...
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...
Complete the code(in C) that inserts elements into a list. The list should always be in...
Complete the code(in C) 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...
The List method addAll(i,c) inserts all elements of the Collection c into the list at position...
The List method addAll(i,c) inserts all elements of the Collection c into the list at position i. (The add(i,x) method is a special case where c = {x}.) Explain why, for the data structures in this chapter, it is not efficient to implement addAll(i,c) by repeated calls to add(i,x). Design and implement a more efficient implementation.
One of the elements that is always found on a list of key leadership traits is...
One of the elements that is always found on a list of key leadership traits is trust.Subdivides trust into cognitive trust and affective trust. Which one do you prefer in your business relationships? Why? How have you built this trust?
This function will receive a list of elements with duplicate elements, this function should remove the...
This function will receive a list of elements with duplicate elements, this function should remove the duplicate elements in the list and return a list without duplicate elements. The elements in the returned list must be in the same order that they were found in the list the function received. A duplicate element is an element found more than one time in the specified list. JAVA
Linked List: Complete the following code to create a linked list from an Array. After creating...
Linked List: Complete the following code to create a linked list from an Array. After creating the list, display the elements of the linked list iteratively. Write two others function called as RDisplayTailRecursion(first) and RDisplayTailRecursion(first) which will print elements of the linked list using the tail and head recursions respectively. #include <stdio.h> #include <stdlib.h> struct Node { }*first=NULL; void create(int A[], int n) { for(i=1; i<n; i++) { } } void Display(struct Node*p) { while(p!=NULL) { } } void RDisplayTailRecursion...
In C++ please. 3. splice() is a specialized method that inserts one list into another in...
In C++ please. 3. splice() is a specialized method that inserts one list into another in constant time but destroys the first list. Explain why this may be implemented for the list but not for any other sequential container. Give an example usage of splice()
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT