Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm not good at English so may have some spelling errors

I have a small project, manage a library. I use 2 structs:
first struct to manages accounts in library
second struct to manages library books. So how to use Single Linked List to manage both of struct without creating two Linked Lists

I haven't learned OOP

Thanks !!!
Posted
Updated 20-Apr-12 4:52am
v3
Comments
Mohibur Rashid 21-Apr-12 4:45am    
Even though there is three answer, but yet I want to ask what is your actual problem? is this problem with understanding?
5fox 22-Apr-12 4:43am    
Here is the detail:

struct USER
{
//.....
};

struct BOOK
{
//.....
};

struct Node
{
BOOK data;
Node* pNext;
};

int ReadFile(char* fname, Node* head)
{
//read data in binary file and add into linked list
}

int WriteFile(char* fname, Node* head)
{
//write data from linked list and write to binary file to store
}

void InsertAtE(Node* head)
{
//insert a new Node at end of the list
}

etc...

problem is:
with 1 linked list as mentioned above, I only process the BOOK list, I can't process the USER list unless I create an another linked list (struct NodeUser - with data field is USER - and create another function to process with NodeUser )
p/s: how to put code into tag c++ in comment box.

Can I use template<class t> to do it?
T represents struct
 
Share this answer
 
v2
Comments
Richard MacCutchan 20-Apr-12 11:50am    
You might be better to use the STL List, or one of the other standard container classes. If you really do not understand classes and structures it would be better to spend some time studying them before going further with your project.
Emilio Garavaglia 21-Apr-12 10:43am    
Don't post fake answers that are in fact questions. Improve the original question instead (or post another, in unrelated)
Create a third structure and include objects of the first and second structure as its members.
struct One
{
   ...
};

struct Two
{
   ...
};

struct ListContent
{
   One one;
   Two two;
};
And yes, use std::list instead of creating a linked list.
 
Share this answer
 
thank you all for yours help !!!!!!
 
Share this answer
 
Full the linked list with void* pointer, which is actually the pointer of your struct Variable.
Like this:
C++
#include <list>
struct S1
{
	int x;
	int y;
};
struct S2
{
	int x;
	int y;
	int z;
};

void main()
{
	std::list<void*> mylist;
	S1 s1; s1.x = 1; s1.y = 1;
	S2 s2; s2.x = 2; s2.y = 2; s2.z = 2;
	
	mylist.insert(mylist.end(), &s1);
	mylist.insert(mylist.end(), &s2);

	S1* pS1 = NULL;
	S2* pS2 = NULL;
	pS1 = (S1*)mylist.front();
	pS2 = (S2*)mylist.back();
	printf("s1  x=%d, y=%d\n", pS1->x, pS1->y);
	printf("s2  x=%d, y=%d, z=%d\n", pS2->x, pS2->y, pS2->z);
}
 
Share this answer
 
v3
Comments
Emilio Garavaglia 21-Apr-12 10:42am    
The question is tagged C++. void* are not to be introduced in C++ programs not related to C modules.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900