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

Previous answer didn\'t work. I need to write the following functions in list.cp

ID: 3848702 • Letter: P

Question

Previous answer didn't work.

I need to write the following functions in list.cpp, add function prototypes for them to list.h and invoke the functions in main.cpp. It should label the output of your test, such as “the list after insertion: “ etc. Thanks!

int sumOfList(node * head)

~compute and return the sum of integers in the linear linked list.

void insert(node *& head, int position, int newInt)

~insert newInt at index “position” where index starts with 0.

// list.h

#include <iostream>
#include <cstring>
#include <cctype>


struct node
{
int data;
node * next;
};

/* These functions are already written and can be called to test out your code */
void build(node * & head); //supplied
void display(node * head); //supplied
void destroy(node * &head); //supplied
void duplicate(node * & new_copy); //provides a duplicate copy of the list

/* *****************YOUR TURN! ******************************** */
//Write your function prototype here:
//

#endif

//main.cpp

#include "list.h"
using namespace std;

int main()
{
node * head = NULL;
build(head);
display(head);

//PLEASE PUT YOUR CODE HERE to call the function assigned

display(head);
destroy(head);

return 0;
}

// list.cpp
#include "list.h"

//put the implementation of your assigned functions here
~

Explanation / Answer

void insert(node *head, int pos, int data)

{

node *temp,*new,*prev = NULL;

int i=1;

new = (struct node *)malloc(sizeof(struct node ));

new->data = data;

if(head == NULL)

{

new->next = NULL;

}

if(pos==1)

{

new->next = head;

}

temp = head;

while(temp != NULL && i<pos)

{

prev = temp;

temp = temp->next;

i++;

}

if(temp == NULL)

{ prev->next = new;

new->next = NULL;

}

else

{

prev->next = new;

new->next = temp;

}

        std::cout<<"Successfully inserted node";

}

int sumOfList(node *head)

{

node *temp;

int sum = 0;

if(head == NULL)

{

                std::cout<<"No elements in the list";

                 return 0;

}

temp = head;

while(temp != NULL)

{

sum += temp -> data;

temp = temp -> next;

}

        std::cout<<"Successfully added all nodes Returning";

}

//Main Method

int main()
{
node * head = NULL;

int sum = 0;
build(head);

//PLEASE PUT YOUR CODE HERE to call the function assigned

std::cout<<"List Before insertion";
display(head);

std::cout<<"Sum of list Before insertion : ";

std::cout<<sumOfList(head);

insert(head, 4 , 432);

std::cout<<"List After insertion";
display(head);

std::cout<<"Sum of list After insertion : ";

std::cout<<sumOfList(head);

destroy(head);

return 0;
}

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