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

  • 10 Rules that Make or Break Enterprise App Development Projects In today's app-driven world, application development is a top priority. Even so, 68% of enterprise application delivery projects fail. Designing and building applications that pay for themselves and adapt to future needs is incredibly difficult. Executing one successful project is lucky, but making it a repeatable process and strategic advantage? That's where the money is. With help from our most experienced project leads and software engineers, …

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds