Making a lib for Linked List. Need a bit of help please :)

Started by
10 comments, last by Agony 17 years ago
Hiya ^^ I wanna make a library for my Linked List implementation so that i can use it with different programs, instead of reimplementing it over and over. my question is, if this is my stucture : ==================== struct node { int somedata; node *next; }; ==================== is there any way i can, um...virtualize the data i am using? for example, instead of "int somedata;" i could use some sort of a blank pointer, something like "void *data", and then later on when i actually use my library in a program, i could change the "void *data" to point to whatever structure i need in that program, something like : ============================= struct node { void *data; node *next; }; struct grades { int grade; char name[30]; }; node *myLinkedList; grades *myData; myLinkedList.data = myData; ============================= i am really not sure of how to actualy do it, so im just guessing here. any help will be appreciated :) Thanks ^^
Advertisement
From your struct definition it looks like you are using C++. In this case you should used std::list rather than your own linked list implementation. Just

#include <list>
Templates;

template <typename T>struct node{  T data;  node<T> *next;};Then you use it as:node<int> root;node<std::string*> root;node< any...type > root;


Look up generic programming and STL.

Also, there's STL containers that take care of just about all containment problems.

Quote:Original post by pavachan
I wanna make a library for my Linked List implementation so that i can use it with different programs, instead of reimplementing it over and over.

Don't. Implement your own linked list to learn how it works, then use the linked list in the Standard C++ Library - everybody else does, it's better implemented than yours, and it's type-agnostic and type-safe.

std::list

Quote:s there any way i can, um...virtualize the data i am using?
for example, instead of "int somedata;" i could use some sort of a blank pointer, something like "void *data", and then later on when i actually use my library in a program, i could change the "void *data" to point to whatever structure i need in that program...

Templates. Which std::list already uses.
Are you using C or C++?

If you're using C++, then why don't you use std::list? Otherwise, if you're only doing it for learning purposes, take a look at templates (specifically "class templates"), which would allow you to, as you put it, "virtualize" your data for multiple data types.

If you're using C, I don't know how to solve your problem. Someone else more experienced with C will have to point you in the right direction.
In C++, std::list.

In C, you have two choices: store a list of void* pointers to existing objects, and cast back to the correct pointer type (as in pre-generics Java and C#, but without the safety).

The other possibility is to use a Macro to define your object and emulate templates. Note that you'll have to replace manipulation functions with macros as well. The end result is, as usual, completely horrible to write, and is very difficult to debug on the user side. But, you have a situation where the list actually owns the objects.

However, since your code looks suspiciously like not-C, but rather like 'C++ without the ++', I'll just advise you to use std::list again. Or, most probably, std::vector.
Moved to For Beginners.
Thanx a lot everyone.
I am using C++, and the code i wrote here was just to illustrate what i mean.
I will use std::list from now, as i have already implemented my own basic list structure, which is not flexible at all :)

Again, thank you very much, and will also look into std::vector.
I wonder if there's an std::tree or something like that.
I'm gonna really have to go over the std namespace and see what it holds.

Thank you!!



[Edited by - pavachan on March 28, 2007 2:15:14 AM]
Quote:I wonder if there's an std::tree or something like that.


Not per se, but std::map, std::multimap, std::set and std::multiset are worth looking at, depending on what the particular use-case is.

Then search them using std::find (#include <algorithm>)
[TheUnbeliever]
Quote:Original post by TheUnbelieverThen search them using std::find (#include <algorithm>)


If an STL container has an algorithm in member function form (e.g. find), then it's almost always preferable to use that instead of the <algorithm> one.

This topic is closed to new replies.

Advertisement