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

  • Managing your company's financials is the backbone of your business and is vital to the long-term health and viability of your company. To continue applying the necessary financial rigor to support rapid growth, the accounting department needs the right tools to most efficiently do their job. Read this white paper to understand the 10 essentials of a complete financial management system and how the right solution can help you keep up with the rapidly changing business world.

  • Flash technology is becoming more prominent in the storage industry. Offering superior speed and reliability when compared to traditional hard disk drives – flash storage is a flexible and increasingly cost-effective technology that can be used to optimize enterprise storage environments. This ebook explores the many uses and benefits of flash storage technology in the enterprise. Check it out to discover and learn all you need to: Optimize storage performance Leverage server flash as storage cache …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds