Thread: one million dollar question

  1. #1
    Registered User billholm's Avatar
    Join Date
    Apr 2002
    Posts
    225

    Question one million dollar question

    Hello out there!

    I'm currently programming a linked list using C++ classes and I think I may need some advice. Here are my class declarations:

    template <class TYPE>
    class NODE {
    protected:
    NODE *create_node(T &item);

    private:
    TYPE info;
    NODE *next;
    };

    template <class TYPE>
    class LINKLIST : NODE<TYPE> {
    public:
    LINKLIST(void);
    ~LINKLIST(void);

    void insert_node(TYPE &item, int nodal_pos);
    void delete_node(TYPE &item, int nodal_pos);
    TYPE retrieve_node(TYPE &item, int nodal_pos);

    private:
    NODE *head;
    NODE *temp;
    NODE *rover;
    NODE *trailer;
    int node_count;
    };

    Okay. Basically what is done here is to let NODE call itself recursively and create a NODE dynamically. insert_node() calls create_node() to do this and so on...
    This style works properly when I implement it. But...
    it looks somewhat awkward to me.

    Now my one million dollar questio is:

    Is there some other way to implement my list using only one class? or maybe two classes that looks better?

    Hope you people can give me some advice

    Thanks!

  2. #2
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200

    I have no idea what that means!?

    *COUGH* CODE BLOCKS *COUGH*

    What is C++?

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    49
    Use list in C++ Standard Template Library(STL). Check the source code of STL is a good way to study.
    Hello, everyone.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Um, to be quite honest, I don't separate the node from the head. That is, the head is simply the first object and is never used to store data. It's sole purpose is to tell me whether the list is empty, etc. An entry point.


    Here's a non-templated version with integers:

    Code:
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.c>
    
    
    class List { public: 
    
    List *next;
    
    int data;
    
    
    
    
    List(){ next = NULL; data = 0; }
    
    
    int Count() {
    
    int count = 0;
    List *p = this;
    
    while( p->next )
     {
       p = p->next;  //...NOTE: advance pointer - THEN do stuff...
       ++count;
     }
    
    return count;  
    }
    
    
    
    
    
    
    List *GetTail() { List *t = this;  while( t->next) t = t->next; return t; }
    
    
    
    
    
    List *Push( int num ) {
    
    List *g = GetTail();
    List *New = new List;
    
    g->next = New;
    
    New->data = num;
    
    return New;
    }
    
    
    
    
    
    int *PopList() {
    
    int counted = Count(), k = 0;
    
    if(!counted) return NULL;
    
    int *array = new int[counted];
    
    List *i = this;
    
    while( i->next )
     {
       i = i->next;
       array[ k++ ] = i->data;
     }
    
    return array;
    }
    };
    
    
    
    
    
    
    
    
    
    int main()
    {
    
    
    
    List nums;
    
    char buff[50];
    
    int *array, count, c = 0;
    
    
    
    
    do{
    
    
    printf("\n\n\n Enter A Number, Then \"Enter\". \n\n");
    printf("                   \"x\" Quits...\n\n");   
    
    fgets(buff, 50, stdin);
    
    clrscr();
    
    if( !strcmp( buff, "x\n") || !strcmp( buff, "X\n")  )
    break;
    
    nums.Push( atoi( buff ) );
    
    
    }while(1);
    
    
    count = nums.Count();
    
    array = nums.PopList();
    
    
    printf("Total Numbers Entered: %i\n\n", count);
     
    while( c < count )
     {
       printf(" %i: %i  \n\n", c, array[c++]);
       getch();
     }
    
    
    
    return 0;
    }

  5. #5
    Registered User billholm's Avatar
    Join Date
    Apr 2002
    Posts
    225

    Lightbulb Hmmm...

    You have good points people.

    Thanx for your ideas! I'll try out your strategies when I go home.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  2. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 01:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM