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

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • With the average hard drive now averaging one terabyte in size, the fallout from the explosion of user-created data has become an overwhelming volume of potential evidence that law-enforcement and corporate investigators spend countless hours examining. Join Us and SANS' Rob Lee for our 45-minute webinar, A Triage and Collection Strategy for Time-Sensitive Investigations, will demonstrate how to: Identify the folders and files that often contain key insights Reduce the time spent sifting through content by …

Most Popular Programming Stories

More for Developers

RSS Feeds