hi there, i'm quite the beginner at c++, just got to my last project for my course to do.
The project asks me to create a family tree using linked list. It says the tree and its components should be templated so it can have a family tree of any object- humans, animals, commercial enterprises etc.
Well i've created a family tree for humans which is able to add nodes, delete nodes etc. But i'm confused about making it generic for other objects. What i currently can only think of is a repeat of all my functions for a different object.
here is my class for a list
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <iostream>
#include <vector>
using namespace std;
template <class T, class T2>
class LinkedList
{
private:
// Declare a structure for the list
struct ListNode
{
T age, children, generation;
T2 name, partner, sex, childname, birth, death, parenta, parentb;
vector<string> child;
struct ListNode *next;
};
ListNode *head; // List head pointer
public:
LinkedList(void) // Constructor
{ head = NULL; }
~LinkedList(void); // Destructor
void appendNode(void);
int readfile(void);
void insertNode(void);
int deleteNode(void);
void displayList(void);
void options(void);
};
// Destructor
// This function deletes every node in the list.
template <class T, class T2>
LinkedList<T, T2>::~LinkedList(void)
{
ListNode *nodePtr, *nextNode;
nodePtr = head;
while (nodePtr != NULL)
{
nextNode = nodePtr->next;
delete nodePtr;
nodePtr = nextNode;
}
}
#endif
and here is part of the 'human' family tree cpp, which contains the functions
#include <iostream>
#include <string>
#include <fstream>
#include "nodes.h"
using namespace std;
void main(void)
{
cout << "\t\t::|| Welcome to the Family Tree Generator ||::" << endl;
cout << "\nWhat would you like to do?" << endl;
cout << endl;
cout << "[1] Start to enter details for family tree" << endl;
cout << "\t-You will need to enter the first member of the family and work your way down the family chain." << endl;
cout << endl;
cout << "[2] Input a family tree from a file" << endl;
cout << "\t-If you have already created a family tree select this option." << endl;
cout << endl;
LinkedList<int, string> list;
int keystroke;
cout << endl;
cout << endl;
cout << "Please select from the options above: ";
do {
fflush(stdin); // flush standard input
scanf_s("%d",&keystroke);
switch( keystroke )
{
case 1:
cout << endl;
// Build the list
list.appendNode();
break;
case 2:
cout << endl;
// Build the list from a file
list.readfile();
break;
default:
cout <<"Please enter a number between 1 and 2: ";
}
}
while (!(1<=keystroke && keystroke<=2));
cout << "Here are the initial values:\n";
list.displayList();
cout << endl;
//cout << "Now inserting the value 5.\n";
//list.insertNode(18480109, "David", "male");
//list.displayList();
cout << endl;
//cout << "Now deleting a member.\n";
//list.deleteNode();
//cout << "Here are the nodes left.\n";
//list.displayList();
cout << endl;
}
template <class T, class T2>
void LinkedList<T, T2>::appendNode(void)
{
ListNode *newNode, *nodePtr, *temp, *gen;
// Allocate a new node & store num
newNode = new ListNode;
int countchild;
cin.ignore();
cin.clear();
cout << "Please enter the name of the person: ";
getline(cin,newNode->name);
newNode->generation = 1; //Enter a dummy value for generation
cout << "Please enter the sex of the person [M] or [F]: ";
getline(cin,newNode->sex);
cout << "Please enter their date of birth (DD/MM/YYYY): ";
getline(cin,newNode->birth);
cout << "Please enter their date of death (DD/MM/YYYY): ";
getline(cin,newNode->death);
cout << "Please enter the name of their partner: ";
getline(cin,newNode->partner);
cout << "How many children : ";
cin >> newNode->children;
countchild = newNode->children;
if (newNode->children != 0){
cout << "\nPlease enter each child's name, seperating by pressing enter : ";
cout << endl;
cin.ignore();
for (int i = 0; i < newNode->children; ++i)
{
cout << "Child [" << i+1 << "]: ";
getline(cin,newNode->childname);
newNode->child.push_back(newNode->childname);
}
}
newNode->next = NULL;
cout << endl;
.
.
.
.
as you can see it takes a void input to the appendnode function, then all the data types are added to the node within this function.
Is there an easier way to make this generic without repeating all the code again, i.e. a different appendnode function etc.
Thanks in advance.