Home > OS >  Do not understand how to insert node into Nth position
Do not understand how to insert node into Nth position

Time:06-15

Fill in for the given missing months profits and then print a total list with the missing months added, in their corresponding position, while calculating the sum and average of all the months values

I want to be able to use the Insert_Nth() function to move May to after March. Instead, it is printing after January, which obviously is not correct, but I don't know how to do that, or understand how to, either.

I am guessing that I should use an if statement, and maybe a for loop, to precisely locate the position in the linked list and insert it, but I don't know how to do that.

#include <iostream>
#include <stdio.h>
#include <iomanip> //library needed for setprecision 

using namespace std;

class node {
public:
    int value = 0;
    string month;
    node* next;
};

struct NewValues {
    long int a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, may = 0;
} n;

void front(node* *head)
{
    node* Dec = new node();
    node* Nov = new node();
    node* Oct = new node();
    
    Dec->value = n.c;
    Dec->month = "December 2021: $";
    Dec->next = *head;
    *head = Dec;

    Nov->value = n.b;
    Nov->month = "November 2021: $";
    Nov->next = Dec;
    *head = Nov;

    Oct->value = n.a;
    Oct->month = "October 2021: $";
    Oct->next = Nov;
    *head = Oct;
}

void end(node* *head)
{
    node* June = new node();
    node* July = new node();        
    node* Aug = new node();
        
        //find the last node
            node* last = *head;
                while (last->next != NULL) {
                    last = last->next;
                }

    Aug->value = n.f;
    Aug->month = "August 2022: $";
    Aug->next = NULL;
    last->next = Aug;
    
    July->value = n.e;
    July->month = "July 2022: $";
    July->next = Aug;
    last->next = July;
    
    June->value = n.d;
    June->month = "June 2022: $";
    June->next = July;
    last->next = June;
}

void Insert_Nth(node* previous)
{
    //1: check if previous node is null
        if(previous == NULL) {
        cout << "\nPrevious cannot be null" << endl;
        return;
        }
    
    //2: prepare a newnode assign value
    node* May = new node();
    May->month = "May 2022: $";
    May->value = n.may;
    
    //3: insert newnode after previous
    May->next = previous->next;
    previous->next = May;
}

void sumOfNodes(node* head, long double* sum)
{
    // if head = NULL
    if (!head)
        return;

    // recursively traverse the remaining nodes
    sumOfNodes(head->next, sum);

    // accumulate sum
    *sum = *sum   head->value;
}

int sumOfNodesUtil(node* head)
{
    long double sum = 0;

    // find the sum of  nodes
    sumOfNodes(head, &sum);

    // required sum
    return sum;
}

void printList(node* n)
{
    cout << "\nList of earnings from October 2021 - August 2022\n" << endl;
    
    while (n != NULL) {
        cout << n->month << n->value << endl;
        n = n->next;
    }
}

void deleteList(node* *head_ref)
{
    /* deref head_ref to get the real head */
    node* current = *head_ref;
    node* next = NULL;
  
        while (current != NULL) {
            next = current->next;
            free(current);
            current = next;
        }
  
    /* deref head_ref to affect the real head back
        in the caller. */
    *head_ref = NULL;
}

int main()
{
    cout << "Hello, This program takes in input for unaccounted months from your";
    cout << " salary over the months and prints them out, \nalong with the total and average." << endl;
    cout << "\nThere are a total There are 11 months and 7 months unaccounted for..." << endl;
    cout << "please enter in the data for the missing months when prompted" << endl;
    
    cout << "\n------------------------" << endl;
    
    cout << "October 2021: $";
    cin >> n.a;

    cout << "November 2021: $";
    cin >> n.b;

    cout << "December 2021: $";
    cin >> n.c;

    cout << "May 2022: $";
    cin >> n.may;
    
    cout << "June 2022: $";
    cin >> n.d;

    cout << "July 2022: $";
    cin >> n.e;

    cout << "August 2022: $";
    cin >> n.f;
    
    cout << "------------------------" << endl;
    
    node* head = new node();
    node* second = new node();
    node* third = new node();
    node* fourth = new node();

    head->value = 500;
    head->month = "January 2022: $";
    head->next = second;

    second->value = 125;
    second->month = "Febuary 2022: $";
    second->next = third;

    third->value = 200;
    third->month = "March 2022: $";
    third->next = fourth;

    fourth->value = 300;
    fourth->month = "April 2022: $";
    fourth->next = NULL;

    Insert_Nth(head);
    front(&head);
    end(&head);
    printList(head);
    
    long double avg = sumOfNodesUtil(head) / 11;

    cout << "\n\n------------------------" << endl;

    cout << "Total earnings: $" << setprecision(6) << sumOfNodesUtil(head) << endl;
    cout << "Average: $" << setprecision(6) << avg << endl;
    
    cout << "------------------------" << endl;

    cout << "\nDeleting Linked List..." << endl;
    
    deleteList(&head);
    
    cout << "\nLinked List Deleted\n\n\tProgram Terminated..." << endl;
}

CodePudding user response:

Fixed Code

previously I had forgotten to set a value for the previous, so it wasnt given a specified place to insert in the linked list, but it has one now.

#include <iostream>
#include <stdio.h>
#include <iomanip> //library needed for setprecision 

using namespace std;

class node {
public:
    int value = 0;
    string month;
    node* next;
};

struct NewValues {
    long int a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, may = 0;
} n;

void front(node* *head)
{
    node* Dec = new node();
    node* Nov = new node();
    node* Oct = new node();
    
    Dec->value = n.c;
    Dec->month = "December 2021: $";
    Dec->next = *head;
    *head = Dec;

    Nov->value = n.b;
    Nov->month = "November 2021: $";
    Nov->next = Dec;
    *head = Nov;

    Oct->value = n.a;
    Oct->month = "October 2021: $";
    Oct->next = Nov;
    *head = Oct;
}

void end(node* *head)
{
    node* June = new node();
    node* July = new node();        
    node* Aug = new node();

    //if linked list is empty, newnode will be a head node
        if (*head == NULL) {
            *head = June;
            return;
        }
        
        //find the last node
            node* last = *head;
                while (last->next != NULL) {
                    last = last->next;
                }

    Aug->value = n.f;
    Aug->month = "August 2022: $";
    Aug->next = NULL;
    last->next = Aug;
    
    July->value = n.e;
    July->month = "July 2022: $";
    July->next = Aug;
    last->next = July;
    
    June->value = n.d;
    June->month = "June 2022: $";
    June->next = July;
    last->next = June;
}

void Insert_Nth(node* previous, node* *fourth)
{
    //1: check if previous node is null
        if(previous == NULL) {
        cout << "\nPrevious cannot be null" << endl;
        return;
        }
    
    //2: prepare a newnode assign value
    node* May = new node();
    May->month = "May 2022: $";
    May->value = n.may;
    
    previous = *fourth;
    
    //3: insert newnode after previous
    May->next = previous->next;
    previous->next = May;
}

void sumOfNodes(node* head, long double* sum)
{
    // if head = NULL
    if (!head)
        return;

    // recursively traverse the remaining nodes
    sumOfNodes(head->next, sum);

    // accumulate sum
    *sum = *sum   head->value;
}

int sumOfNodesUtil(node* head)
{
    long double sum = 0;

    // find the sum of  nodes
    sumOfNodes(head, &sum);

    // required sum
    return sum;
}

void printList(node* n)
{
    cout << "\nList of earnings from October 2021 - August 2022\n" << endl;
    
    while (n != NULL) {
        cout << n->month << n->value << endl;
        n = n->next;
    }
}

void deleteList(node* *head_ref)
{
    /* deref head_ref to get the real head */
    node* current = *head_ref;
    node* next = NULL;
  
        while (current != NULL) {
            next = current->next;
            free(current);
            current = next;
        }
  
    /* deref head_ref to affect the real head back
        in the caller. */
    *head_ref = NULL;
}

int main()
{
    cout << "Hello, This program takes in input for unaccounted months from your";
    cout << " salary over the months and prints them out, \nalong with the total and average." << endl;
    cout << "\nThere are a total There are 11 months and 7 months unaccounted for..." << endl;
    cout << "please enter in the data for the missing months when prompted" << endl;
    
    cout << "\n------------------------" << endl;
    
    cout << "October 2021: $";
    cin >> n.a;

    cout << "November 2021: $";
    cin >> n.b;

    cout << "December 2021: $";
    cin >> n.c;

    cout << "May 2022: $";
    cin >> n.may;
    
    cout << "June 2022: $";
    cin >> n.d;

    cout << "July 2022: $";
    cin >> n.e;

    cout << "August 2022: $";
    cin >> n.f;
    
    cout << "------------------------" << endl;
    
    node* head = new node();
    node* second = new node();
    node* third = new node();
    node* fourth = new node();

    head->value = 500;
    head->month = "January 2022: $";
    head->next = second;

    second->value = 125;
    second->month = "Febuary 2022: $";
    second->next = third;

    third->value = 200;
    third->month = "March 2022: $";
    third->next = fourth;

    fourth->value = 300;
    fourth->month = "April 2022: $";
    fourth->next = NULL;
    
    front(&head);
    end(&head);
    Insert_Nth(head,&fourth);
    printList(head);
    
    long double avg = sumOfNodesUtil(head) / 11;

    cout << "\n\n------------------------" << endl;

    cout << "Total earnings: $" << setprecision(6) << sumOfNodesUtil(head) << endl;
    cout << "Average: $" << setprecision(6) << avg << endl;
    
    cout << "------------------------" << endl;

    cout << "\nDeleting Linked List..." << endl;
    
    deleteList(&head);
    
    cout << "\nLinked List Deleted\n\n\tProgram Terminated..." << endl;
}
  • Related