簡體   English   中英

同一節點中的不同結構鏈表C

[英]Different structs in the same node Linked Lists C

我的任務有些困難,想知道是否有人可以指出我正確的道路。

我希望創建一個支持不同節點使用的鏈表,因為我需要使用不同的數據集。

目前,我正在使用三種結構:

struct equipment_data {
    char EquipID[4+1]; /* 4 Max */
    char EquipName[31+1]; /* 31 Max */
    unsigned TotalNumber;
};

struct member_data {
    unsigned MemberID;
    char LastName[31+1]; /* 31 Max */
    char FirstName[31+1]; /* 31 Max */
};

struct loan_data {
    unsigned MemberID;
    char EquipID[4+1]; /* 4 Max */
    unsigned Number;
};

我需要以某種方式在同一節點內使用它。

struct ets {
    struct node *equipment_data;
    struct node *member_data;
    struct node *loan_data;
    unsigned equip_count;
    unsigned member_count;
    unsigned loan_count;
};

struct node {
    void *data;
    struct node *next;
};

看起來我需要創建一個ADT鏈接列表。 能否請你幫忙? 謝謝!

我將為您需要支持的類型創建結構,然后在鏈接列表節點中創建帶有類型指示符的聯合:

typedef struct { int a, b, c;    } Type1;
typedef struct { char buf[80];   } Type2;
typedef struct { int a; float b; } Type3;

typedef union {
    Type1 t1,
    Type2 t2,
    Type3 t3;
} Anytype;

typedef struct node {
    int thistype;   // 1 for type1, 2 for type2 etc.
    Anytype data;
    struct node *next;
} Listnode;

只要確保您在每個Listnode正確設置此thistype Listnode

Listnode *node = malloc(sizeof(Listnode));
node->thistype = 1;  // example: Type1, the 3 ints
node->t1.a = 1;
node->t1.b = 2;
node->t1.c = 3;
node->next = someothernode;

您可以使用switch來訪問數據:

Listnode *node;
switch (node->thistype) {
    case 1: 
        // do stuff with node->t1.a, node->t1.b, node->t1.c
        break
    case 2:
        // do stuff with node->t2.buf
        break;
    case 3:
        // do stuff with node->t3.a, node.t3.b
        break
}

您可能應該創建一個指向各種結構的節點。

struct node {
struct equipment_data *eq_data; // This pointers to equipment struct
struct member_data * me_data; // ......
struct load_data * lo_data; // .....
unsigned equip_count;
unsigned member_count;
unsigned loan_count;
struct node* next_node; // this points to the next node in the 

};

您可以將結構集的變量放置在struct節點中。

struct node {
void *data;
struct ets *var;
struct node *next;
};

現在,您可以訪問所有結構。

枚舉要存儲的數據類型

typedef enum type{ equipment, member,loan,ets} type;    
typedef struct lnk_lst
{
  type data_type;
  void* data;
  struct lnk_lst* next;
} lnk_lst ;

初始化會像

equipment_data e1;
lnk_lst* node=(lnk_lst*)malloc(sizeof(lnk_lst));
node->data_type=equipment;

//if created dynamically
   node->data=malloc(sizeof(equipment_data));
//just to point existing equipment_data
   node->data=(void*)(&e1);

node->next=NULL;

評估列表就像

switch(node->data_type)
{
 case equipment:
   printf("%d",((equipment_data*)(node->data))->TotalNumber);
   puts( ((equipment_data*)(node->data))->EquipID );
   puts( ((equipment_data*)(node->data))->EquipName );
 case member:
   //code to read member_data
 case loan:
   //code to read loan_data
 case ets:
   //code to read ets
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM