|
// Runtime Structured Linear Recursion
#include <iostream>
using std::cout;
using std::endl;
// Node for Single Link List
struct Node
{
int value;
Node* next;
};
void AddItem(Node** pHead, int iData)
{
if (*pHead == NULL)
{
(*pHead) = new Node();
(*pHead)->value = iData;
(*pHead)->next = NULL;
}
else
{
Node* pTemp = *pHead;
while (pTemp->next != NULL)
{
pTemp = pTemp->next;
}
Node* newNode = new Node();
newNode->value = iData;
newNode->next = NULL;
pTemp->next = newNode;
}
}
// Singly Link List Traversal using Linear Recursion
// Structure Linear Recursion
void PrintNode(Node* pHead)
{
if (pHead != NULL)
{
PrintNode(pHead->next);
std::cout << pHead->value << std::endl;
}
else return;
}
int min(int a, int b)
{
return a < b ? a : b;
}
void main()
{
Node* pHead = NULL;
AddItem(&pHead, 10);
AddItem(&pHead, 20);
AddItem(&pHead, 30);
AddItem(&pHead, 40);
AddItem(&pHead, 50);
PrintNode(pHead);
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.