Thread: a linkedlist creating by itself or something like that..,

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    142

    a linkedlist creating by itself or something like that..,

    hi,

    was wondering if this is possible? and how?
    say..,

    class LinkList()
    {
    int item;
    LinkList *pNext;

    public:
    Insert(int value);
    };

    my questino is, how do i implement Insert? pls take note that pNext is private.., sorry if my subject is quite vague i'm not also clear about my problem..,

    thanks,

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    Do you mean something like this:

    Code:
    class LinkList()
    {
      LinkList(int)
      ~LinkList();
    
      int Item;
      LinkList *pNext;
    
      public:
      Insert(int);
    };
    
    LinkList(int Value)
    {
      Item = Value;
      pNext = NULL; // Not needed
    }
    
    ~LinkList();
    {
      if (pNext != NULL)
      {  
        delete pNext;
      }
    }
    
    void Insert(int Value);
    {
      if (pNext == NULL)
      {  
        pNext = new LinkList(Value);
      }
      else
      {
        pNext->Insert(Value);
      }
    }
    BTW this aint a fully linked list, but on the site you can find a tutorial that describes it in debth... Best of luck
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    142
    um.., yeah something like that!

    well, this sort of class gives me problem so i'll just make the linked list a seperate struct and a class that handles this linked list,

    thanks for your time btw,

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Zahl
    Do you mean something like this:

    Code:
    LinkList(int Value)
    {
      Item = Value;
      pNext = NULL; // Not needed
    }
    Watch out -- the NULL or 0 IS needed there or else when you go to delete the node you'd risk "deleting" some uninitialized value representing a location in memory


    Originally posted by Zahl
    Do you mean something like this:

    Code:
    ~LinkList();
    {
      if (pNext != NULL)
      {  
        delete pNext;
      }
    }
    The pNext != NULL check isn't necissary. You can simply do

    delete pNext;

    without a condition.

    Also, the the function you showed would NOT insert, it would just place the new node at the end of the entire list -- you're just recursively calling the same function for each object in the list until the end, which is not "insertion."

    I realize it was mostly just quick pseudo-code, but the class name and scope operator were missing prior to the member function name in the definition and you accidently put semicolons after the names.
    Last edited by Polymorphic OOP; 12-12-2002 at 11:30 PM.

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    Ups
    Well except the NULL thing (which is obivius when I think about it) it was all done pretty fast.
    I stand corrected
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Something about a linkedlist program..
    By ozumsafa in forum C Programming
    Replies: 8
    Last Post: 10-17-2007, 01:14 PM
  3. Something about typedef (data structures)(Linkedlist)
    By ozumsafa in forum C Programming
    Replies: 13
    Last Post: 09-29-2007, 04:44 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM