Advertisement

Linked List and Void Pointers

Started by February 28, 2001 05:37 PM
3 comments, last by Owie 23 years, 11 months ago
Im trying to create linked list code without having a certain structure that it is based on. eg. normally (in textbooks) they show you to make a linked list structure that is already preplanned especially for your project. for example: struct NODE{ NODE *Next; SOMESTRUCTURE Data; } *Head; I am trying to make it so that the "SOMESTRUCTURE" does not have to be defined in the linked list code itself. I am trying to do this using void pointers. This way, the code is not limited to a specific type of linked list. So I could have a linked list of type OBJECTS and of the type MISSILES. this is how I initiallize the linked lists void *InitList( unsigned int Size ) { void *LinkList; int n = Size / sizeof(char); // how many bytes in Size LinkList = new char[n]; return LinkList; } Can this be done? I came into some problems when accessing elements of the nodes. I couldnt cast a void to a temp stucture. Anyway, does anyone have any ideas how to make it better? Has anyone tried this before? Edited by - owie on February 28, 2001 6:51:21 PM
You can either make your own template structure or use the STL list class, also a template class for just this kinda thing.

Harry.
Harry.
Advertisement
Oh just to make it clear, the template structure would be something like this:

template <class NODE>
struct NODE{
NODE *Next;
SOMESTRUCTURE Data;
} *Head;

You could use template <class NODE, class SOMESTRUCTURE> if you wanted Data to be a different type too.

Harry.

[edited - replaced the < and > bits with &lt; and &gt;]

Edited by - HarryW on February 28, 2001 7:13:47 PM
Harry.
  // Linked List and Node Templatetemplate <class D> class node {  public:  D data;  node <D> *child;  node <D> *parent;  node(void) {    child = NULL;    parent = NULL;  }};template <class T> class LinkedList {  public:    T *head;    unsigned int nodes;    T *AddNode(void) {      nodes++;      T *newnode = new T;      if(head!=NULL) {        head->parent = newnode;        newnode->child = head;      }      head = newnode;      return newnode;    }    T *InsertNode(T *after) {      if(after==NULL) {        return addnode();      } else {        nodes++;        T *newnode = new node <T>;        newnode->parent = after;        newnode->child = after->child;        after->child = newnode;        return newnode;      }    }    void DeleteNode(T *delnode) {      if(delnode!=NULL) {        nodes--;        if(delnode==head) {          if(delnode->child!=NULL) {            delnode->child->parent = NULL;            head = delnode->child;          } else {            head = NULL;          }        } else {          if(delnode->parent!=NULL) {            delnode->parent->child = delnode->child;          }          if(delnode->child!=NULL) {            delnode->child->parent = delnode->parent;          }        }        delete delnode;        delnode = NULL;      }    }    LinkedList(void) {      head = NULL;      nodes = 0;    }    ~LinkedList(void) {      T *current = head;      T *temp;      while(current!=NULL && nodes!=0) {        temp = current->child;        delete current;        current = temp;      }    }};// Example:LinkedList <node <int> > MyList;node <int> *newint = MyList.AddNode();newint->data = 5;node <int> *insint = MyList.InsertNode(newint);insint->data = 10;MyList.DeleteNode(newint);  


"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
Resist Windows XP''s Invasive Production Activation Technology!
http://www.gdarchive.net/druidgames/
quote:
Original post by HarryW

Oh just to make it clear, the template structure would be something like this:

template
struct NODE{
NODE *Next;
SOMESTRUCTURE Data;
} *Head;

[edited - replaced the < and > bits with < and >]

Edited by - HarryW on February 28, 2001 7:13:47 PM



Just a quick q, how is this going to work? I always thought that the data should be the one that''s the template. As in

  template <class T> class Node{protected:  Node *nextNode;  T data;public:  //constructors,destructors & other methods here};  

==========================================In a team, you either lead, follow or GET OUT OF THE WAY.

This topic is closed to new replies.

Advertisement