Sign in to follow this  
WiredCat

linked list of struct that is derived from another c++

Recommended Posts

so i have 8 structures which will share

 

struct_name * next;

struct_name * prev;

 

pointers in it.

 

i wonder if i could create a base class like:

struct TLinkedListWay
{
TLinkedListWay * next;
TLinkedListWay * prev;
};
struct structure1 : public TLinkedListWay
{
char * name;
int name_len;
char * something_else;
int something_else_len;
};
struct structure2 : public TLinkedListWay
{
char * name;
int name_len;
char * something_else;
int something_else_len;
};
struct structure3 : public TLinkedListWay
{
structure1 * First_linked_list;
structure2 * Second_linked_list;
};

now structure3 is a base which holds pointers to first object of structure1 and structure2

 

but i am not sure when i will do:

inline TLinkedListWay * getLastEvent(TLinkedListWay * first_object)
{
TLinkedListWay * p = first_object;
while (p!= NULL) p = p->next;
return p;
}

this will point to the proper place in memory (consider i want to find last object of structure3 and then last object (in that structure of structure1) will this point properly?

 

do you even understand me? :/

 

 

i want to avoid creating separated functions like above for each structure, and i wonder if this could be managed like that, without any bugs like it would point on something else.

 

Share this post


Link to post
Share on other sites
You could CRTP it (let's see if I even remember how to do this in C++):
 
template<typename T>
struct LinkedListNode
{
    LinkedListNode *prev;
    LinkedListNode *next;
};

struct structure1 : public LinkedListNode<structure1>
{
    char *name;
    // etc
};
Though to be honest I haven't used C++ in over a decade and this might be a frowned-upon solution. If I screwed this up, someone be sure to let me know!

As far as your linked-list-of-linked-lists goes, you would need to follow all of the structure3 next links to the end, and then take the structure1 links from it to the end separately:
 
structure1 *getLastEvent(structure3 *first_object)
{
    if (first_object == nullptr)
        return nullptr;

    structure3 *s3 = first_object;

    while (s3->next != nullptr)
        s3 = s3->next;

    structure1 *s1 = s3->First_linked_list;

    if (s1 == nullptr)
        return nullptr;

    while (s1->next != nullptr)
        s1 = s1->next;
    
    return s1;
}
Edited by Nypyren

Share this post


Link to post
Share on other sites

I'm not sure I follow, do you want a linked list that can hold one of several different structure types?

 

Could you do this via the std::List and a structure that was

 

ListNode

{

  enum innerType

  union

  {

     struct1 one;

     struct2 two;

     struct3 three;

  }

 

// or a void* pointer if you're allocating those somewhere

}

Share this post


Link to post
Share on other sites

It's 2 am so don't judge me please,

Maybe what you want is something like this (Didnt understand you properly):

template <typename T>
struct TLinkedListWay
{
	T * next;
	T * prev;
public:
	virtual T* GetLast()
	{
		T* temp = next;
		while (temp != nullptr)
		{
			temp = temp->next;
		}
		return temp;
	}
};
struct structure1 : public TLinkedListWay<structure1>
{
	char * name;
	int name_len;
	char * something_else;
	int something_else_len;
};
struct structure2 : public TLinkedListWay<structure2>
{
	char * name;
	int name_len;
	char * something_else;
	int something_else_len;
};
struct structure3 : public TLinkedListWay<structure3>
{
	structure1 * First_linked_list;
	structure2 * Second_linked_list;

	TLinkedListWay* GetNext()
	{
		auto last = TLinkedListWay::GetLast();
		last->First_linked_list;
		//TODO: W/E you want here.
	}
};

Using templates and virtual MEMBER functions to get the last node from another node,

and then in the TODO, you just get w/e you need frmo the first linked list. 

Edited by WoopsASword

Share this post


Link to post
Share on other sites
struct structure1 {
  std::string name;
  std::string something_else;
};

struct structure3 {
  std::vector<structure1> First_Vector;
  std::vector<structure1> Second_Vector;
};

std::vector<structure3> s3;

s3.back().First_Vector.back();

?

 

It depends on what you're doing, really.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this