Thread: help about queue, linked lists

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    83

    help about queue, linked lists

    I need to make a class that can store dynamic structures which will be linked as a chain of list.

    But I can't think of a way to do it? Can someone guide me with proper directions.

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

  3. #3
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Is there something wrong with std::list?
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Bah, this one was really begging for it...
    Linked-list C++
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    83
    Google didn't produce what I wanted precisely :P
    Anyway I have worked it out, by using a void* pointer in the linked list so that it could contain dynamic types, structures.

    userlibrary.h
    Code:
    #ifndef USERLIBRARY_H
    #define USERLIBRARY_H
    #include "main.h"
    #include <stdint.h>
    
    
    
    
    class CLinkedList
    {
    private:
        struct list
        {
            void* NodeData;
            list* link;
        } *rootNode;
        size_t nodeSize;
    public:
    
    
        CLinkedList(uint32_t _nodeSize);
        ~CLinkedList();
        void pushNode(void* newNode);
        void popNode();
        void DestroyList();
        void* GetNodeData(int delta);
    };
    
    
    #endif
    userLibrary.cpp
    Code:
    #include "userLibrary.h"#include <stdio.h>
    #include <cstdlib>
    
    
    
    
    CLinkedList::CLinkedList(uint32_t _nodeSize)
    {
        nodeSize = _nodeSize;
        rootNode = NULL;
    }
    
    
    CLinkedList::~CLinkedList()
    {
    
    
    }
    
    
    void CLinkedList::pushNode(void* newNode)
    {    
        list* transverser = rootNode;
        if (rootNode != NULL)
        {
            while (transverser != NULL)
                transverser = transverser->link;
            transverser = (list*)malloc(sizeof(list));
    
    
            transverser->link = NULL;
            transverser->NodeData = malloc(nodeSize);
        
            memcpy(transverser->NodeData, newNode, nodeSize);
        }
        else
        {
            rootNode = (list*)malloc(sizeof(list));
    
    
            rootNode->link = NULL;
            rootNode->NodeData = malloc(nodeSize);
        
            memcpy(rootNode->NodeData, newNode, nodeSize);
        }
    }
    
    
    void CLinkedList::popNode()
    {
        list* transverser = rootNode;
        if (transverser != NULL)
        {
            if (transverser->link != NULL)
            {
                while (transverser->link != NULL)
                    transverser = transverser->link;
        
                free(transverser->NodeData);
                transverser->NodeData = NULL;
                free(transverser);
                transverser = NULL;
            }
            else
            {
                free(rootNode->NodeData);
                rootNode->NodeData = NULL;
                free(rootNode);
                rootNode = NULL;
            }
        }  
    }
        
    
    
    void CLinkedList::DestroyList()
    {
        while ( rootNode != NULL)
            popNode();
    }
    
    
    
    
    void* CLinkedList::GetNodeData(int delta)
    {
        list* transverser = rootNode;
        for (int i = 0; i < delta; i++)
        {
            if (transverser != NULL)
               transverser = transverser->link;
            else 
                break;
        }
        if (transverser != NULL)
            return (transverser->NodeData);
        else
            return NULL;
    
    
    }
    Suggestion, criticism or corrections are welcomed. You can noticed that the getNodeData is badly made. But couldn't think of the other way except using the list directly in referencing code, which I thought would be prone to errors
    Last edited by Swoorup; 04-25-2012 at 11:04 PM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It would be better if you renamed list to node.

    <malloc.h> is non-standard. You probably want <cstdlib> for malloc, but then you should be using new and delete instead.

    You should implement or disable the copy constructor and copy assignment operator for CLinkedList. rootNode and nodeSize should probably be private.

    Since list (or node) is implementation detail, you should declare, but not define, it in the header. Then you define it in the source file.

    I assume that all this is merely for practice, and you would use std::list otherwise.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Just a few points:
    1. use cstdlib instead of stdlib.h etc...
    2. Use new/delete for dynamics memory allocation.
    3. Use template instead of void*.
    4. Where is destroylist()??? And call DestroyList in the destructor as well.
    5. Don't make everything public. Keep your variables 'private'.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    83
    Changes done!

    Never made much use of C++. So, I tend to use C functions.

    When using new operator inside a function, would it be cleaned at the stack cleanup or do we have to use delete?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Swoorup
    When using new operator inside a function, would it be cleaned at the stack cleanup or do we have to use delete?
    Every new should be matched by a delete.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double Linked Dynamic Lists Vs Unrolled Linked Lists
    By lantzvillian in forum C Programming
    Replies: 6
    Last Post: 02-14-2012, 01:07 PM
  2. Replies: 4
    Last Post: 05-01-2010, 10:19 PM
  3. Need help with FIFO Queue using Singly Linked Lists
    By astou in forum C++ Programming
    Replies: 6
    Last Post: 03-15-2008, 02:36 PM
  4. Queue's and linked lists
    By Saggio in forum C++ Programming
    Replies: 3
    Last Post: 10-22-2007, 07:14 AM
  5. question on linked lists(stack with linked lists)
    By dionys in forum C Programming
    Replies: 1
    Last Post: 06-02-2004, 11:08 AM