main.cpp
Code:
#include <iostream>
//#include <cstring>
#include "include\LinkedList.h"
using namespace std;
int main()
{
LinkedList myList = LinkedList();
cout << "Enter your name: ";
char myName[20];
cin.getline(myName, 20, '\n');
myList.createNode(myName);
cout << "Enter your last name: ";
char myLastName[20];
cin.getline(myLastName, 20, '\n');
myList.createNode(myLastName);
return 0;
}
LinkedList.h
Code:
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
class LinkedList
{
public:
LinkedList();
~LinkedList();
bool createNode(char name[]);
private:
unsigned int length;
struct Node
{
char name[20];
Node *next;
};
Node *startPointer;
};
#endif
LinkedList.cpp
Code:
#include <cstdlib>
#include <iostream>
//#include <cstring>
#include <string.h>
#include "..\include\LinkedList.h"
//default constructor
LinkedList::LinkedList() : length(0), startPointer(NULL){}
//destructor
LinkedList::~LinkedList(){}
bool LinkedList::createNode(char name[])
{
if(startPointer == NULL)
{
startPointer = new Node;//Node is a struct
strcpy((*startPointer).name, name);
std::cout << "Your name is: " << std::endl;
}
else
{
Node *temp = startPointer;
while((*temp).next != NULL)
{
temp = (*temp).next;
/*This isn't working*/
(*temp).next = NULL;
}
(*temp).next = new Node;
strcpy((*temp).name, name);
}
return true;
}