File supplied.o contains code that can build, display, duplicate, and destroy a
ID: 3736255 • Letter: F
Question
File supplied.o contains code that can build, display, duplicate, and destroy a linear linked list For this test, you will need to write the following functions in list.cpp, add function prototypes for them to list.h and invoke the functions in main.cpp. You should label the output of your test, such as "the list after insertion: "etc. 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 .Explanation / Answer
The functions are as follows:
int sumOfList(node *head){
node *p;
p = head;
int sum = 0;
while (p != NULL){
sum = sum + p->data;
p = p->next;
}
return sum;
}
void insert(node *&head, int position, int newInt){
node *p = head;
pos = 0;
node *q = new node;
q->data = newInt;
q->next = NULL;
if (head == NULL && position > 0){
cout << "List is empty.Position is invalid ";
return;
}
if (head == NULL && position == 0){
head = q;
return;
}
if (head != NULL && position == 0){
q->next = head;
head = q;
return;
}
while (p!= NULL && pos < position -1){
p = p->next;
pos++;
}
if (p == NULL){
cout << "Invalid position ";
}
else {
q->next = p->next;
p->next = q;
}
return;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.