-2

I wrote this Linked List code and I am not able to create a single linked list since the value pointed by memory location of nodeValue in main function keep changing which in turn changes the head and tail value. I solved this by creating a Node object array((like nodeValue[5]) and passing the value, but this limits to 5 values. Is there a way to efficient way to code this without using a array of objects?

#include<iostream>
#include<string>

using namespace std;

class Node
{
public:
    int value;
    Node *nextNodePointer;
};

class linkedList
{
private:
    int count = 0;
public:
    Node *Head;
    Node *Tail;
    void AddNodeAfter(Node *);
    //void removeNodeAfter(Node *);
    void displayValues();
};

void linkedList::AddNodeAfter(Node *temp)
{
    if (this->count == 0)
    {
        Head = temp;
        Tail = temp;
        count++;
    }

    else
    {
        Tail->nextNodePointer = temp;
        Tail = temp;
        count++;
    }
}



Node createNodeObjects()
{
    cout<< endl << "Enter integer value :";
    Node temp;
    cin >> temp.value;
    temp.nextNodePointer = NULL;
    return temp;
}

void linkedList::displayValues()
{
    if (count == 0)
    {
        cout << endl << "Nothing to display";
    }

    else
    {
        Node value;
        value = *Head;
        for (int i = 1; i <= count; i++)
        {
            cout << endl << "Value: " << value.value;
            value = *value.nextNodePointer;
        }
    }
}

int main()
{
    cout << "Creating basic linked list" << endl;
    linkedList LinkedList;
    Node nodeValue;
    while (1)
    {
        cout << endl << "Do you want to add a value to Node ?<Y/N> : ";
        char choice;
    cin >> choice;
    if (choice == 'Y')
    {
        nodeValue = createNodeObjects();
        LinkedList.AddNodeAfter(&nodeValue);
    }
    else
        if (choice == 'N')
        {
            LinkedList.displayValues();
            break;
        }
        else
            cout << "Wrong choice" << endl;

}
}
Premsai G
  • 1
  • 1
  • 2
    Have you yet studied the `new` keyword? – Sam Varshavchik Apr 18 '16 at 02:40
  • 1
    You need to learn about dynamic memory allocation. Start here: [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Ivan Aksamentov - Drop Apr 18 '16 at 02:44
  • *Is there a way to efficient way to code this without using a array of objects* -- `std::forward_list`. Also, why should `main` know anything about Nodes? That is the linked list's job to figure out what, where, when, and how to use Nodes. All the `main` program should be doing is saying "add this integer to your list". – PaulMcKenzie Apr 18 '16 at 03:00
  • I have changed the code where I am allocating nodeValue as nodeValue = new Node; and passing the nodeValue as pointer, This is working fine and i was able to create a working linked list. I also read that using heap is not a recommended way(correct me if I am wrong). Are my changes correct? @Drop – Premsai G Apr 18 '16 at 13:40
  • @PremsaiG Hard to say without seeing the entire code. If it works for you or fulfills specifications/assignment, then it is correct. Note that usually we don't expose nodes of the list to the user, as PaulMcKenzie stated. i.e. I would hide node allocation inside functions. You might want to take a look at the interface of the [`std::list`](http://en.cppreference.com/w/cpp/container/list) to get an idea of a good interface. If you have specific issues, post it as another question (don't forget to use a debugger and to add an [mcve](http://stackoverflow.com/help/mcve)) – Ivan Aksamentov - Drop Apr 18 '16 at 20:23

1 Answers1

0

In C++ , you can use list library ... http://www.cplusplus.com/reference/list/list/

BusyTraveller
  • 183
  • 3
  • 14