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

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

  • When it comes to desktops – physical or virtual – it's all about the applications. Cloud-hosted virtual desktops are growing fast because you get local data center-class security and 24x7 access with the complete personalization and flexibility of your own desktop. Organizations make five common mistakes when it comes to planning and implementing their application management strategy. This eBook tells you what they are and how to avoid them, and offers real-life case studies on customers who didn't …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds