Create Link List for object

This is a discussion on Create Link List for object within the C++ Programming forums, part of the General Programming Boards category; Code: main.cpp LogMessage logM(); Message m("hello world"); Message mx("hello world"); logM.log(m); logM.log(mx); How can i create a class name LogMessage ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    41

    Create Link List for object

    Code:
    main.cpp
    
                    LogMessage logM();
    
    	Message m("hello world");
    	Message mx("hello world");
    
                    logM.log(m);
                    logM.log(mx);
    How can i create a class name LogMessage so that I can store the message object in a link list ?? What code should i implement in the log function ??

    Code:
    LogMessage.cpp
    
    class LogMessage{
     
    LogMessage::LogMessage()
    {
    
    }
    
    void LogMessage::log(Message * m)
    {
    
    }
    
    }

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Use an STL list or vector. STL Tutorial.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sounds like homework to me. What do you mean " What code should i implement in the log function ??"? How about putting in there whatever your log function is supposed to do? I bet that'd do the job! What do you think?

    Do you know how to create a linked list?
    Yes? Good, then write one, and add your stuff to it.
    No? How about reading one of the countless posts on the forum about them? You do know how to search don't you?

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    41
    The following code is to store integer in a link list. How can i modify it so that it is able to store objects in a link list ? For your information, i am not asking solution directly for my assignment. I am asking where i can seek help so that i know how to start with it only.

    Code:
    struct node{
    	int data;
    	struct node *next;
    }*list,*prev,*temp,*tmpdisp,*newnode;
    
    
    	newnode = (struct node*) malloc(sizeof(struct node));	
    	newnode ->data = element;
    	newnode ->next = NULL;
    
    	if (list == NULL)
    		list = newnode;
    	else 
    	{
    		list->next = newnode;
    		list = list->next;
    	}

  5. #5
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    I'm not completely sure what you're asking how to do, but from my understanding all you should have to do is replace the line initializing data
    Code:
    struct node{
                int data;
    to be whatever object type you want. You may also have to change what element is. I dont see where it is initialized into the structure, is it a global?

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    winsonlee--the code you posted looks like you are using C syntax for declaring the struct and for memory allocation but you are trying to use C++ syntax for for LogMessage. C++ syntax for the struct follows, using Message as the data member of the struct type. I've not actually compiled or tested the code, but I'm confident that it isn't far from useable.
    Code:
    struct node
    {
    Message m;
    node * next;
    };
     
    If you had a logMessages class, which would basically be a list of Messages, then the definition of a log method using C++ memory allocation syntax might look something like this.
     
     
    void logMessages::log(Message * msg)
    {
    node * newNode = new node;
    newNode->m = *msg;
    newNode->next = NULL;
     
    if(head == NULL)
    	head = newNode;
    else
    {
    	newNode->next = head;
    	head = newNode;
    }
    }
     
    to log messages to a logMessages object you could do something like this:
     
    Message m1("hello world");
    Message m2("goodbye");
     
    logMessages myMessageLog;
    myMessageLog.log(&m1);
    myMessageLog.log(&m2);
    There are lots of other ways to do basically the same thing, but that's a workable start at least. Each message logged will be placed at the start of the list of messages which is maintained by head, with logs maintained in sequential order of logging in.

    Note: In order for this to work, the type called Message must be assignable (have a valid assignment operator available) and head must be a member of the logMessages class be of type node * with value of NULL assigned to it by the default logMessages constructor.
    Last edited by elad; 08-05-2004 at 02:51 PM.

  7. #7
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    >> LogMessage logM();

    this isnt Java. Dont use the brackets if the constructor takes 0 args, use

    LogMessage logM;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List Insert prob
    By Bebs in forum C Programming
    Replies: 8
    Last Post: 12-03-2008, 10:28 PM
  2. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 02:48 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  4. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21