473,385 Members | 1,267 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

how do we read heterogenous linklist?

While creating a heterogenous linklist we know that we use void pointer. But when it comes the time to read the information.
How do we exactly do that?( means
while reading how do we figure out to what datatype does the data belong)
Nov 3 '10 #1
11 2480
mac11
256 100+
I'd say you embed some type identifier into your list node type. An int or enum would do, then you can see that id and know which type of thing is in that node.

Also, I'm assuming you're writing C code? If you have C++ available you shouldn't be writing lists.
Nov 3 '10 #2
Oralloy
988 Expert 512MB
Well, if you're writing in C++, then you can just use inheritence and virtual methods to safely control the elements of the list. Meaning that you should never have to explicitly test the type of an "element".

In C, you have to identify your elements somehow. As mac11 suggested, an effective way to do this is to build all of your elements as extensions of a common model, which has a "type" as its first attribute.

For example:
Expand|Select|Wrap|Line Numbers
  1. enum MyType
  2. {
  3.   TypeA,
  4.   TypeB,
  5. };
  6.  
  7. struct elementBase
  8. {
  9.   MyType theType;
  10. };
  11.  
  12. struct elementA
  13. {
  14.   struct elementBase;
  15.   ...more here...
  16. };
  17.  
  18. struct elementB
  19. {
  20.   struct elementBase;
  21.   ...more here...
  22. };
It's a model I've used successfully in the past. The elegant part is that it lets you address everything as a struct elementBase. The pain is that it requires that you be careful about type casting.

Good Luck!
Nov 3 '10 #3
donbock
2,426 Expert 2GB
Use a union for the various heterogenous types and you can avoid the type casts:
Expand|Select|Wrap|Line Numbers
  1. struct hetero {
  2.    struct hetero *next;
  3.    enum { TYPE1=1, TYPE2, ... } theType;
  4.    union {
  5.       struct { ... } struct1;
  6.       struct { ... } struct2;
  7.       ...
  8.       } body;
  9.    };
Nov 4 '10 #4
Thanks Everyone,
I said exactly what Mac has said, but that fellow (who asked me this and was discussing) was saying something about object in structure, which i wasn't sure of neither he.

Can we use something like object in the structure.
if yes how? what would be code snap?
Nov 4 '10 #5
Oralloy
988 Expert 512MB
unconquerable,

I'm not sure I understand that. Can you give us an example of what you need?

Thanks.
Nov 4 '10 #6
mac11
256 100+
C doesn't have objects, it has structs. Oralloy and donbock gave example pseudo-code above.
Nov 5 '10 #7
Oralloy
988 Expert 512MB
@mac11 - I've done object oriented code in C using the method outlined; the parts I didn't include were the idea of a constructor chain and use of strongly typed function pointers to provide effective virtual methods. If you build your system carefully, use accessor methods, and the like, then you can construct a very effective, typesafe object system in C.

@unconquorable - there are many flavours of object systems in the software world. Because C uses the "struct" as its basic object. The enum is not an object, for all that it looks like one. An "enum" is an abstracted memory mapping mechanism; one which radically simplifies writing types of program. What you don't get with C are directly implemented inheritence, methods, polymorphism, and overloading. C++ extends C to provide these "OO" capabilities. Neither C or C++ provides direct implementation of prototypical object behaviour.

By the way, structures are first class objects in C. For example, in this code I use no pointers, even though I send Complex number objects to the function add, and recieve a Complex object as the result of the computation:
Expand|Select|Wrap|Line Numbers
  1. /*
  2. **  demo.c
  3. **    demonstrate first class objects under C
  4. */
  5.  
  6. #include <stdio.h>
  7.  
  8. /*******************************/
  9.  
  10. struct Complex
  11. {
  12.   double real;
  13.   double imag;
  14. };
  15.  
  16. typedef struct Complex Complex;
  17.  
  18. /*******************************/
  19.  
  20. Complex add(Complex first, Complex second)
  21.  
  22. /*******************************/
  23.  
  24. int main(int argc, char **argv, char **envv)
  25. {
  26.   Complex this = { 1.0, 2.0 };
  27.   Complex that = { 5.0, 6.0 };
  28.  
  29.   Complex sum;
  30.  
  31.   sum = add(this, that);
  32.  
  33.   printf("sum = (%f, %f)\n", sum.real, sum.imag);
  34.  
  35.   return 0;
  36. }
  37.  
  38. /*******************************/
  39.  
  40. Complex add(Complex first, Complex second)
  41. {
  42.   Complex result = { 0.0, 0.0 };
  43.   result.real = first.real + second.real;
  44.   result.imag = first.imag + second.imag;
  45.   return result;
  46. }
  47.  
  48. /*******************************/
Cheers!
Nov 9 '10 #8
donbock
2,426 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. Complex add(Complex first, Complex second);
I'm veering off topic, but about 15 years ago I used a C compiler that burned me -- it implemented functions that return structs in a way that was nonreentrant. The consequence was intermittent errors that took me forever to find. Thus, my C functions never return composite types (structs or unions). (I can hold a grudge!)
Nov 9 '10 #9
Oralloy
988 Expert 512MB
@donbock,

Ouch! Getting burned by a compiler bug absolutely sucks.

I found an (in retrospect) amusing memory allocation bug in the HP/UX FORTRAN compiler about 20 years ago. I had developed a C++ based, IP networked, nulti-threading front-end to a large FORTRAN application suite that the company sold. I hadn't noticed, but "my" code was leaking memory, and would crash after a million or so transactions. Extensive analysis and instrumentation of my code demonstrated to me that I didn't have a memory leak in my C++ code. So, to find the bug, I ended up looking at a linker map of the program to find all users of malloc(). Well....it turns out that one of our FORTRAN modules was calling malloc(), but not free(). There wasn't a call to malloc() in the FORTRAN source, so I generated an assembly code listing of the compiled module to figure out where it was coming from. To make a long story short, the compiler was playing dynamic memory with FORTRAN modules which had entrypoints.

So yeah, I completely understand your grudge. And when it dies, make sure you stuff it and mount it over your mantelpiece.

Cheers!
Nov 9 '10 #10
donbock
2,426 Expert 2GB
No compiler bug. The [C89] C Standard does not offer any guarantees regarding reentrant code or thread-safety.
Nov 11 '10 #11
Oralloy
988 Expert 512MB
Ahhh, yes. You remind me of the various options that were available for PL/I functions - I know that recursive was one of them; and I don't recall about reentrant.

Back in the good 'ole days of 200 users on IBM 360s and 370s with fast (25MHz) CPUs.
Nov 11 '10 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: vjay | last post by:
I want to just create a linklist.The program below goes into an endless loop.The srange behaviour is that i can exit from the program if i create only two nodes.After two goes into infinite loop. ...
6
by: GalenTX | last post by:
I am looking for opinions on a possible approach to coping with bitfields in a heterogenous environment. We've got a bunch of legacy code which maps to hardware using bitfields. The code was...
6
by: jwvai316 | last post by:
I don't really know how to say it so I just say it a nested linklist. How do you make LinkLists inside LinkList? Can anyone help me for this? I think an example program will help me a lot. ...
0
by: smoothkriminal1 | last post by:
the question is u read timetable of 40 students from file n den find da slot where all fourty students dnt hve clash...if any.... may b i ll be able to make clash logic but i m even just nt...
1
by: smoothkriminal1 | last post by:
Write a Code that will pick timetable of 40 students from file using linklist n than find a slot where all the students dont have any class. file can be of any format Student can maximum take 6...
2
Parul Bagadia
by: Parul Bagadia | last post by:
I have written a code for deleting certain value from linklist; it's not working; where as i have written one for deleting a no., after given no. which works fine! I even debugged it; but invain;...
2
by: dynamo | last post by:
this is a basic linklist,there seems to be a runtime error when i run the program(the main function) i suspect it has something to do with my del function but i see nothing wrong can you help.Thanx...
1
by: AnagJohari | last post by:
Actualy i have a prob in the begaining to add the node in the linklist i write the following code i make a display fucnction which display all the value of linklist i check the value in display...
2
by: AnagJohari | last post by:
hello guys i succesfully create a linklist but have a problem to insert a perticualt node from beg in the link list i try so much but not getting the reason why my code is not running i send you...
4
by: Waqas Danish | last post by:
Hello, I am using standard socket functions to send and receive data on a client/server model. I am using K-Develop under Ubuntu 10.04 OS. The application works fine locally but when I deploy it...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.