Obtaining Direct Access to a Linklist Class' Elements

Environment: VC6

Well, all of you must know the MFC CList Class. I was having trouble using it so I made my own class and thought I would share it with you. The problem with the MFC class is that it does not provide direct access to individual elements of the list and sometimes you really need that facility.

This class is quite simple. Just add the LinkList.h file into your project. Make an instance of it, which will be your Linked List. Then add an element to the head or tail just as it was done using CList. Here's an example:

CLinkList < CString > MyList; // Your LinkList Object
                              // containing CStrings as
                              // its elements.

MyList.AddHead(.M.);          // Adds CString With data .M. to
                              // the head of your Link
                              // List.

MyList.AddHead(.YaZiR.);      // Adds CString With data .YaZiR.
                              // to the head of your
                              // Link List.

MyList.AddTail(.Aheer.);      // Adds CString With data .Aheer.
                              // to the tail of of your
                              // Link List.

Accessing the individual elements is quite simple. I have defined Element as a member of the CLinklist class, which you would use to access the elements. Initially, it is a NULL pointer. Use the specimen loop given below.

MyList.Element = MyList.Head; // Or MyList.Tail;
for(int i = 0 ;  i < MyList.GetTotal() ; i++ )
{
  //...............
  //     Use MyList.Element to work on elements
  //..............
  MyList.Element = MyList.GetNext(); // Or MyList.GetPrev();
}

Well, that is all thare is to it. Feel free to use this code as it is or modify it as you like. Have fun.

Downloads

Download source - 1 Kb


Comments

  • good job

    Posted by Legacy on 09/07/2002 12:00am

    Originally posted by: saady

    good jobi was looking for dat but i need some more ;)

    Reply
  • Memory Leaks (and why is this here???)

    Posted by Legacy on 08/12/2002 12:00am

    Originally posted by: Chet

    There are memory leaks. None of the nodes go away when the list is destroyed.

    Reply
  • Carry on ! Ignore the complainers !

    Posted by Legacy on 07/31/2002 12:00am

    Originally posted by: Hans Wedemeyer

    There are those that feel everybody must conform, they seem to think everybody in the world must to as they do, nobody is allowed to "Thinking out side of the box" .
    They are most likely very good programmers and produce some real neat code..... but nothing original.

    You had the guts to take a step outside the box, and it shows initiative and creativity.

    However when you interview for a job, remember to answer with the standard answers to keep the establishment happy... The minute they see someone that "can think" they will feel challenged...

    Good work, keep using that brain...

    Reply
  • Targeted Audience.

    Posted by Legacy on 07/30/2002 12:00am

    Originally posted by: YaZiR

    The site is not only for the experts but also for the beginners. It is a CODE site and should contain code of all levels.
    This tutorial is mainly for the beginners.
    That CList class is very confusing for beginners. For a beginner using it can be a pain in the ***.
    So it is just to make there life a lil easier.
    :)

    Reply
  • Why on earth is this here?

    Posted by Legacy on 07/29/2002 12:00am

    Originally posted by: Heinrich

    It blows my mind to find this sort of ... here!

    Reply
  • Nice work

    Posted by Legacy on 07/29/2002 12:00am

    Originally posted by: John

    Really nice work.... i really needed this.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • The rapid evolution of enterprise storage technologies, combined with external forces, like the explosion of big data, can cause Linux® and server administrators to play catch-up when it comes to storage. Running a bunch of monolithic storage devices and proprietary, disconnected technologies forces administrators to spend valuable time creating and managing complex solutions. To reduce complexity and enable rapid deployment of new technologies and applications, server administrators need a single open …

  • It's time high-level executives and IT compliance officers recognize and acknowledge the danger of malicious insiders, an increased attack surface and the potential for breaches caused by employee error or negligence. See why there is extra emphasis on insider threats.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds