Created
December 11, 2011 21:27
-
-
Save samueljon/1462845 to your computer and use it in GitHub Desktop.
frá dodda fyrir dodda
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
struct linkNode { | |
int data; | |
linkNode* nextnode; | |
}; | |
void thefunction(linkNode* a){ | |
std::cout << "thefunction\n"; | |
while (a->nextnode != NULL){ | |
//Debugging | |
std::cout << "data: "; | |
std::cout<< a->data; | |
std::cout << "\tnextnode: "; | |
std::cout<< a->nextnode; | |
std::cout << "\n"; | |
if(a->nextnode != NULL) | |
a->data += a->nextnode ->data; | |
a = a->nextnode; | |
if(a->nextnode == NULL){ | |
std::cout << "data: "; | |
std::cout<< a->data; | |
std::cout << "\n"; | |
} | |
} | |
} | |
int main() | |
{ | |
linkNode* head=NULL; | |
linkNode* tmp; | |
std::cout << "Populate linked list\n"; | |
for (int i = 0; i<5; i++){ | |
tmp = new linkNode; | |
tmp->data = 5-i; | |
tmp->nextnode = head; | |
head = tmp; | |
std::cout << "data: "; | |
std::cout<< head->data; | |
if(head->nextnode != NULL){ | |
std::cout << "\tnextnode: "; | |
std::cout<< head->nextnode; | |
} | |
std::cout << "\n"; | |
} | |
thefunction(head); | |
cout << "Lausn: "; | |
for (int i=0; i<5; i++){ | |
std::cout << "data: "; | |
std::cout<< head->data; | |
if(head->nextnode != NULL){ | |
std::cout << "\tnextnode: "; | |
std::cout<< head->nextnode; | |
} | |
std::cout << "\n"; | |
cout<< head->data <<", "; | |
head = head->nextnode; | |
} | |
//system("pause"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment