Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

When I try to insert at the end of my unordered list with the following code it

ID: 3798326 • Letter: W

Question

When I try to insert at the end of my unordered list with the following code it works EXCEPT it puts a 0 after my last inserted item for some reason? Can you see where its putting in the 0 at the end

list2.insert(41)

Desired output:  67,34,69,24,78,58,62,64,5,45,81,27,61,91,95,42,27,36,41

Actual output:  67,34,69,24,78,58,62,64,5,45,81,27,61,91,95,42,27,36,41,0


template
bool OListType::insert(const T& item)
{
NodeType *temp, *curr, *prev;

temp = new NodeType;

temp->info = item;
temp->link = NULL;
   if (this->head == NULL) {
       this->head = temp;
       this->head->link = NULL;
       ++this->count;
   }
   else {
       curr = this->head;
       prev = NULL;
       while (curr != NULL && curr->info < item) {
           prev = curr;
           curr = curr->link;
       }
       if (prev == NULL) {
           temp->link = this->head;
           this->head = temp;
           ++this->count;
       }
       else {
           temp->link = curr;
           prev->link = temp;
           ++this->count;
       }
   }
   return true;
}

Explanation / Answer

I run your code and i got too many error ....if you put whole code then i shall check it proper...by the way i'll give you an alternate of this code....which represent the unordered list....if you have further any problem then fell free ask to me.

void insert_unorderdlist(Node **linkp, const int value)
{
    Node *newnode = 0;

    /* Let's create an unordered singly linked list */
    /* insert new value at the end of list */
    while (*linkp != NULL)
        linkp = &(*linkp)->link;

    /* Let's allocate memory to newnde */
    newnode = (Node *)malloc(sizeof(Node));

    /* verify if memory allocated to newnode successfully */
    if (newnode == NULL) {
        printf("Not sufficient Memory! ");
        exit(EXIT_FAILURE);
    }

    /* write in value in value field of newnode */
    newnode->value = value;

    /* insert newnode in the list */
    /* at the end of the list */
    *linkp = newnode;
    newnode->link = NULL;
}

if problem then ask to me.

thanks

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote