Like Tree3Likes
  • 1 Post By iMalc
  • 1 Post By laserlight
  • 1 Post By nimitzhunter

help about queue, linked lists

This is a discussion on help about queue, linked lists within the C++ Programming forums, part of the General Programming Boards category; I need to make a class that can store dynamic structures which will be linked as a chain of list. ...

  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,509
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  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,308
    Bah, this one was really begging for it...
    Linked-list C++
    nimitzhunter likes this.
    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-26-2012 at 12:04 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,170
    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.
    Swoorup likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    458
    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'.
    Swoorup likes this.
    "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
    22,170
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 11: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, 03:36 PM
  4. Queue's and linked lists
    By Saggio in forum C++ Programming
    Replies: 3
    Last Post: 10-22-2007, 08:14 AM
  5. question on linked lists(stack with linked lists)
    By dionys in forum C Programming
    Replies: 1
    Last Post: 06-02-2004, 12:08 PM

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