Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. c++ implementation of a linked list.

c++ implementation of a linked list.

Scheduled Pinned Locked Moved C / C++ / MFC
c++data-structureshelp
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Pardon me!! I am new here. I am unable to use addNode function. When i declare Node* it doesnt compile. I am trying to implement a linked list in c++ without using structures and only use pure classes.I am rusty here. Any help would be great. head->addNode() and head.addNode() doesnt work.

    #include
    #include
    using namespace std;
    class Node
    {
    int data;
    Node* next;
    public:
    Node()
    {
    data=0;
    next=NULL;
    }
    void addNode(int a)
    {
    if(this!=NULL)
    {
    data=a;
    next=NULL;
    }
    }
    };
    void main()
    {
    Node* head;
    head.addNode(10);
    }

    M S 2 Replies Last reply
    0
    • L Lost User

      Pardon me!! I am new here. I am unable to use addNode function. When i declare Node* it doesnt compile. I am trying to implement a linked list in c++ without using structures and only use pure classes.I am rusty here. Any help would be great. head->addNode() and head.addNode() doesnt work.

      #include
      #include
      using namespace std;
      class Node
      {
      int data;
      Node* next;
      public:
      Node()
      {
      data=0;
      next=NULL;
      }
      void addNode(int a)
      {
      if(this!=NULL)
      {
      data=a;
      next=NULL;
      }
      }
      };
      void main()
      {
      Node* head;
      head.addNode(10);
      }

      M Offline
      M Offline
      Maximilien
      wrote on last edited by
      #2

      addNode is not a member of the Node class.

      class Node
      {
      public:
      void addNode(int a);
      };
      void Node::addNode( int a )
      {
      //...
      }

      your addNode is not good, you need to think things over; you do not allocate a new node; you do not assign the next pointer ... I find that using free functions instead of class methods makes things easier when doing this; a node only contains the data (and the next pointer). if all fails, use std::vector. Good luck.

      Nihil obstat

      1 Reply Last reply
      0
      • L Lost User

        Pardon me!! I am new here. I am unable to use addNode function. When i declare Node* it doesnt compile. I am trying to implement a linked list in c++ without using structures and only use pure classes.I am rusty here. Any help would be great. head->addNode() and head.addNode() doesnt work.

        #include
        #include
        using namespace std;
        class Node
        {
        int data;
        Node* next;
        public:
        Node()
        {
        data=0;
        next=NULL;
        }
        void addNode(int a)
        {
        if(this!=NULL)
        {
        data=a;
        next=NULL;
        }
        }
        };
        void main()
        {
        Node* head;
        head.addNode(10);
        }

        S Offline
        S Offline
        Sivaraman Dhamodharan
        wrote on last edited by
        #3

        void main()
        {
        Node* head;
        head.addNode(10);
        }

        Create the object in the heap first. You just declared a variable stating "I will hold address of Node object in a variable head". But, where does that object? When there is no object head-> will not work. Head.addNode() also wont work as the object is not in stack. Correction:

        void main()
        {
        Node* head = new Node();
        head->addNode(10);
        }

        Object will be created in heap. or

        void main()
        {
        Node head;
        head.addNode(10);
        }

        Object will allocated on stack

        Programming Article

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups