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

#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();
        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();

        newelement->value = x;

        struct element *iter = listhead;
        struct element *prev = NULL;


        while (iter != NULL && iter->value < x ) {
                prev = iter;
                iter = iter->next;
        }

        if (prev == NULL)
        {
                newelement->next = listhead;
                listhead = newelement;
        }
        else
        {
                prev->next = newelement;
                newelement->next = iter;
        }

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

Related Solutions

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()
Write a Java program that reads a list of 30 fruits from the file “fruits.txt”, inserts...
Write a Java program that reads a list of 30 fruits from the file “fruits.txt”, inserts them into a string array, and sorts the array in alphabetical order. String objects can be compared using relational operators such as <, >, or ==. For example, “abc” > “abd” is false, but “abc” < “abd” is true. Sample output: Before Sorting: Cherry, Honeydew, Cranberry, Lemon, Orange, Persimmon, Watermelon, Kiwifruit, Lime, Pomegranate, Jujube, Pineapple, Durian, Plum, Banana, Coconut, Apple, Tomato, Raisin, Mandarine, Blackberry,...
1. Explain four of the basic principles of lending. 2. List twelve elements that should be...
1. Explain four of the basic principles of lending. 2. List twelve elements that should be included in a banks credit policy.
Written in JAVA Code Write a program that inserts 25 random integers from 0 to 100...
Written in JAVA Code Write a program that inserts 25 random integers from 0 to 100 in order into a LinkedList object. The program should sort the elements, then calculate the sum of the elements and the floating-point average of the elements.
Please complete following c++ code asap using following prototypes complete each missing part / Linked list...
Please complete following c++ code asap using following prototypes complete each missing part / Linked list operations int getLength() const {return length;} void insertNode(College); bool deleteNode(string); void displayList() const; bool searchList(string, College &) const; }; main.cpp /*   Build and procees a sorted linked list of College objects. The list is sorted in ascending order by the college code. Assume that the college code is unique. */ #include <iostream> #include <fstream> #include <string> #include "LinkedList.h" using namespace std; void buildList(const string...
Complete the PoundDog code by adding a constructor having a constructor initializer list that initializes age...
Complete the PoundDog code by adding a constructor having a constructor initializer list that initializes age with 1, id with -1, and name with "NoName". Notice that MyString's default constructor does not get called. Note: If you instead create a traditional default constructor as below, MyString's default constructor will be called, which prints output and thus causes this activity's test to fail. Try it! // A wrong solution to this activity... PoundDog::PoundDog() { age = 1; id = -1; name.SetString("NoName");...
Your work should be readable as well as correct. Always Verify Time! Write code for O(n)...
Your work should be readable as well as correct. Always Verify Time! Write code for O(n) worst-case algorithm that verifies that a tree is actually an AVL tree by detecting imbalance. If imbalance is true, then call the proper rotation function provided in the lecture slides to fix the imbalance condition. 1. Must read AVLNode data from a text file 2. Create a Book Object; and an AVL node object to be inserted into the AVL tree 3. At each...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT