Hello, I was trying to add the data in the inputted Nodes for example, if I entered in 1 2 3 4 5,
it would add 1 + 2 + 3 + 4 + 5 = 15
but it just does not show the addNodes at all..
am I prepending wrong?
#include <iostream>
using namespace std;
struct Node{
int data;
struct Node* next;
};
void prepend(struct Node** head, int newData){
struct Node* newNode = new Node;
newNode->data = newData;
newNode->next = (*head);
(*head) = newNode;
}
void print(struct Node* head){
while(head!=NULL){
cout << head->data <<" ";
head = head->next;
}
}
float addNode(Node* head){
int counter = 0;
int sum = 0;
struct Node*curr = head;
while(curr!=NULL){
counter++;
sum += curr->data;
curr = curr->next;
}
return sum;
}
int main(){
int data, input;
Node* head = NULL;
for (int i=0; i<5; i++){
cout<< "Input data: \n";
cin>> input;
prepend(&head, input);
}
print(head);
cout <<endl;
addNode(head);
print(head);
}