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;
}

Your CharNode contains member "char data;" and you're trying to assign a string "First" to that char. That isn't going to work because "First" is actually a pointer to a null terminated array of chars and you're trying to say: char = pointer to char.
If you want strings in your linked list you need to provide a different member type for the storage in the CharNode class.
This could be an STL string, a char* to memory you'll have to allocate yourself or, the simplest solution is a fixed length char array (if you do this make sure you copy all chars to it, don't just try and copy the pointer).

Thanks MrSpigot. Makes sense.


Ok. I made them pointers. The program runs but it only prints out the first letter of each string. So I tried to make the headinsert function like this:

head = new CharNode((strlen(d)+1), head);

but that doesn't work either.

I'm just looking for an easy way out right now. Let's say the fixed character length is 20. But when I try to implement I get tons more errors. I'm not familiar with linked lists at all, so this is all kinda new to me.

Again, 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;
}
void CharNode::headInsert(CharNode* &head, char *d)
{    
    head = new CharNode(d, head);
}

What the above says is create a new list every time you insert a new item. T'm almost positive that isn't what you want. I strongly recommend you have two classes, not one. One to define a node that will be inserted in a list, and the other to define the list itself. The node class will have one or more data member variables and one (or more if you want more than a singly linked list) pointer variable(s) to the node type itself. When you want to insert a new node into a list you declare memory for a new node, not a new list. Each list should have a node pointer to keep track of the first node in the list which is often called head.

Thanks Lerner. I will take that approach next time.

I messed with the code again and with a little help from a local friend, we fixed it finally. Something I overlooked in the class definition. Thanks for your help guys.

here's the final code in case someone else needs to take a look.

#include <iostream>
#include <cstring>
using namespace std;

class CharNode

{
public:
    CharNode(){}
    CharNode(char d[], CharNode *lPtr)
        : linkPtr(lPtr)
        {
                strcpy(data,d);
        }
    CharNode *getLink() const {return linkPtr;}
    char *getData() {return data;}
    void setLink(CharNode *lPtr) {linkPtr = lPtr;}
    void headInsert(CharNode* &head, char *d);
    void deleteTail (CharNode *before);
    void printAll();
private:
    char data[20];
    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 list is: ";
        while (tempPtr != NULL)
        {
            cout << tempPtr->getData() << " ";
            tempPtr = tempPtr->getLink();
        }
    }
}

int main()
{
    CharNode *head = new CharNode("First", NULL);
    head->headInsert(head,"Second");
    head->headInsert(head,"Third");
    head->printAll();
    cout << endl;
    delete head;
    head=NULL;
    head->printAll();

    system("PAUSE");
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.