How would you go about creating this function? f. Write a function morePositveNu
ID: 3702214 • Letter: H
Question
How would you go about creating this function?
f. Write a function morePositveNumber to determine the list has more positive numbers than negative numbers. Print the results and list of all positive numbers and all negative numbers. To do this you will build two more lists in the morePositiveNumbers function and pass the correspondent head pointer back to the mainO. The main0 then can call the printList to print these two lists out. void morePositiveNumber(node *head, node * positiveHead, node **negativeHead);Explanation / Answer
ANSWER:
int posnum=0,negnum=0;
node *poshead, *neghead,*headList;
main(){
morePositiveNumber(headList,poshead,neghead);
cout<<"Positive Numbers: "<<posnum<<endl;
printList(positiveHead);
cout<<"Negative Numbers: "<<negnum<<endl;
printList(negativeHead);
if(posnum > negnum)
cout<<"Number of positive numbers are more"<<endl;
else
cout<<"Number of negative numbers are more"<<endl;
}
void morePositiveNumber(node head, node * positiveHead, node ** negativeHead) {
node *temp;
temp = head;
positiveHead = negativeHead = NULL;
while(temp->next){
if(temp->data >=0){
// Positive
node *pos = new node;
pos->data = temp->data;
pos->next = positiveHead;
positiveHead = pos;
posnum++;
}else {
// Negative
node *neg = new node;
neg->data = temp->data;
neg->next = negativeHead;
negativeHead = neg;
negnum++;
}
temp = temp->next;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.