I need a list of double links in C, but it should be for different types. In C ++ we use templates for this.
You can find examples of C in the list of double links with
You can take some methods, one of which involves storing a zero * in your ADT.
I have always found it a little bit of a linked list, because you have to allocate it yourself to the list separately. In other words, to assign a node, you have to allocate both nodes and its payload separately (and they should be remembered to clean them even when they are removed).
An approach that I have used in the past is like a 'variable size' structure:
typedef struct _tNode {struct _tNode * prev; Struct _tNode * Next; Four payloads [1]; } TNode; It is no longer that see is of variable size but let's allocate a structure:
typedef struct {The name of char [30]; Four orders [50]; Four phones [20]; } Tipperson; TNode * node = molok (tNode) - 1 + size (tipperson); You now have a node, which for all purposes and purposes, looks like this:
typedef struct _tNode {struct _tNode * prev; Struct _tNode * Next; Four names [30]; Four orders [50]; Four phones [20]; } TNode; Or, in graphical form (where [n] means n bytes):
< Code> + ------------ + | Prev [4] | + ------------ + | Next [4] | + ------------ + + ----------- + | Payload [1] | | Name [30] | & Lt; - Overlap + ------------ + ----------- + | Addr [50] | + ----------- + | Phone [20] | + ----------- +
Thinking how you can correctly address the payload, it can be done as follows:
node-> Prev = NULL; Node-> Next = null; TPerson * person = & amp; (Node-> payload); Dali Strakpy (person-> name, "Richard Cranean") for easy change in payload; Strapepi (person-> Acer, "10 Smith Street"); Strcpy (person-> phone, "555-5555"); This cast line should be entered in the actual payload character (in tNode type), the actual TPizer payload type .
By using this method, you can pick any payload type in a node, even different payload types in each node , if you have the structure Prefer more:
typedef struct _tNode {struct _tNode * prev; Struct _tNode * Next; Int payload type; // Allows different payload types on each node. Four payloads [1]; } TNode; and use payloadType to store an indicator as what the payload actually is.
Its benefit in a union is that it does not waste space, as can be seen with the following:
union {int fourbytes; Four handbags [100]; } U;
Where are you sold every time you store an integer type (for a 4-byte integer) in the list.
In payload type TNode you can easily find out what type of payload this node is taking, so your code can decide how it has been processed. Go. You can use something with these lines:
#define defined PAYLOAD_UNKNOWN # # define PAYLOAD_MANAGER 1 # PAYLOAD_EMPLOYEE2 define #PAYLOAD_CONTRACTOR 3 Define
or (possibly better):
typed ephem {PAYLOAD_UNKNOWN, PAYLOAD_MANAGER, PAYLOAD_EMPLOYEE, PAYLOAD_CONTRACTOR} tPayLoad;
The only thing to look at is to make sure the alignment of the payload is correct. Since my payload placeholder and payload are both all char type, which is not a problem, however, if your payload has more stringent alignment requirements (such as harder than pointers, then you have to May need to be adjusted).
Although I have never seen an environment with an alignment, it is possible according to the strict ISO code, according to the Pointer.
You can usually obtain the necessary alignment by using the data type for payload placeholder, which has strict alignment requirements such as:
long payload;
In the past, it happens to me that you probably do not have an array as the requirement payload placeholder is just something that You can find out. I suspect that my special phrase listens back to that day where I am only collecting an array (instead of a structure) and referring directly to them. In that case, you can use the payload [] without doing any other type.
Comments
Post a Comment