Reputation: 19026
This is my work but not home work. I am trying to come up decide how best to represent a data I have using a native data structure(By native i mean I cannot use STL in C++, its a C++ code. Its our design decision not to use STL and have no way to change it or violate it)
Data is as follows:
There is a variable say int to denote ipaddr say e.g. 192.168.10.77(lets say this is stored in a int which is uniquely obtained from the 192.168.10.77 ) which lets call deviceId.
Now for each such deviceId there are other fields multiple services associated to it say service 1, service2, etc... Number of services associated with each device is variable from one deviceId to another.
So a sample of data could look like:
192.168.10.77
service1
service2
service3
service4
192.168.10.98
service1
service2
192.168.10.97
service1
Right now I store it in a inefficient data structure as below.
I have a struct as:
typedef struct
{
deviceId d;
serviceId s;
}mystr;
And then I use an array of a struct which holds
mystr mylist[64];
so entries of this array look like
mylist[0].d = 192.168.10.77
mylist[0].s = service1
mylist[1].d = 192.168.10.77
mylist[1].s = service2
mylist[2].d = 192.168.10.77
mylist[2].s = service3
mylist[3].d = 192.168.10.77
mylist[3].s = service4
mylist[4].d = 192.168.10.98
mylist[4].s = service1
mylist[5].d = 192.168.10.98
mylist[5].s = service2
... ... and so on ....
Essentially the deviceId value is duplicated as many times as there are services associated to it.
I need some thing more efficient because later I have to group all the services associated with a device.
So to do that I might have to search for deviceId and club the services for it together. I don't know how I am going to search by deviceId as it can have any value.
What is a better way to represent this data using some optimal data structure ?
[ I can see it as linked list but could not come up with the struct Node{ }
for it ]
Upvotes: 2
Views: 198
Reputation: 151
You can have link list of devices with link list of services
typedef Service{
serviceId s;//store service id
Service *nextService;//link to the different services supported by the device, end by null
}*deviceServiceList;
typedef struct Device{
deviceId d;//store the device id
Service *s;//store the list of service available for this device
}myDevices;
the devices can be stored in a array of devices or u can create a link list of devices
Upvotes: 0
Reputation: 77
typedef struct
more than 1 service for 1 device is required
{
deviceId d;
serviceId s[100];
`}mystr;
mystr mylist[64];`
or you can have a linked list like
` struct device
{
device id;
*service;
}
typedef struct serviceid{
{
*service;
}service;
` point to null by last service id
Upvotes: 2
Reputation: 12429
How about this library to store it as a basic hash table?
http://troydhanson.github.com/uthash/
Disclaimer: I'm only vaguely familiar with C
Upvotes: 0
Reputation: 950
Consider using gethostbyname and gethostbyaddr. First try one, if it fails, try the other. Then you only have to store one string.
Upvotes: 0