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.
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.
Is there something wrong with std::list?
How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.
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"
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
userLibrary.cppCode:#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
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 errorsCode:#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; }
Last edited by Swoorup; 04-25-2012 at 11:04 PM.
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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
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?
Every new should be matched by a delete.Originally Posted by Swoorup
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)