Hey All,
I'm having this weird issue and I'm not sure what to do. In the main function when I put one letter it'll work, once I put a string of letters it doesn't work. I've tried an assortment of stuff and I dunno what to do anymore.
This is the code that works when I put one letter in. Right now in the main it has "first", "second", and "third" and I get error C2664: cannot convert parameter 1 from 'const char[6]' to 'char'.
Thanks in advanced.
#include <iostream>
#include <cstring>
using namespace std;
class CharNode
{
public:
CharNode(){}
CharNode(char d, CharNode *lPtr)
: data(d), linkPtr(lPtr){}
CharNode *getLink() const {return linkPtr;}
char getData() const {return data;}
void setData(char d) {data=d;}
void setLink(CharNode *lPtr) {linkPtr = lPtr;}
void headInsert(CharNode* &head, char d);
void deleteTail (CharNode *before);
void printAll();
private:
char data;
CharNode *linkPtr;
};
void CharNode::headInsert(CharNode* &head, char d)
{
head = new CharNode(d, head);
}
void CharNode::deleteTail(CharNode *head)
{
CharNode *discard, *tail;
tail = head;
// move tail to the last but one node
while ((tail->getLink())->getLink() != NULL)
tail = tail -> getLink();
tail -> setLink(NULL);
discard = tail->getLink();
delete discard;
}
void CharNode::printAll()
{
if (this == NULL)
cout << "Empty list\n";
else
{
int i = 1;
CharNode *tempPtr = this;
cout << "The linked list is: \n";
while (tempPtr != NULL)
{
cout << "Object " << i++ << ": " << tempPtr->getData() << endl;
tempPtr = tempPtr->getLink();
}
}
}
int main()
{
CharNode *head = new CharNode("First", NULL);
head->headInsert(head,"Second");
head->headInsert(head,"Third");
head->printAll();
delete head;
head=NULL;
head->printAll();
system("PAUSE");
return 0;
}