如何实现两个结构的链表

时间:2015-11-04 09:04:56

标签: c gcc data-structures linked-list

我想实现这样的数据结构。什么是好方法 实现这一点。

enter image description here

我正在尝试实现信号的链接列表

消息数据的结构

struct dbcMsg_t
{
unsigned char*   message_id;
unsigned char*   message_name;
unsigned char*   message_len;
unsigned char*   message_sender;
unsigned char*   message_comment;
unsigned char*   message_attribute_list;
unsigned char*   message_transmitter_list;
struct sigMsg_t  *SIG_INFO;
};
信号数据的

结构

struct sigMsg_t
{
unsigned char*   signal_name;  
unsigned int     signal_bit_start;
unsigned char    signal_bit_len;
unsigned char    signal_endianess;
unsigned char    signal_signedness;   
struct sigMsg_t  *SIG_INFO_NEXT;
}*start=NULL;

将有一个 MSG DATA数组

struct dbcMsg_t *new_node[MAX_DBC];

我对如何实现这个感到困惑?我的意思是如何插入以及如何显示?

1 个答案:

答案 0 :(得分:1)

实现信号数据节点的标准链接列表,并在消息数据节点中保存头节点的指针。

typedef struct sigMsg_t *Sig_ptr;
typedef struct sigMsg_t {
    unsigned char*   signal_name;  
    unsigned int     signal_bit_start;
    unsigned char    signal_bit_len;
    unsigned char    signal_endianess;
    unsigned char    signal_signedness;   
    Sig_ptr  SIG_INFO_NEXT;
}Sig_node;

typedef struct dbcMsg_t {
    unsigned char*   message_id;
    unsigned char*   message_name;
    unsigned char*   message_len;
    unsigned char*   message_sender;
    unsigned char*   message_comment;
    unsigned char*   message_attribute_list;
    unsigned char*   message_transmitter_list;
    Sig_ptr  SIG_INFO_HEAD;
} Message;

由于您询问了如何实现插入和打印功能:

void insert_to_list_end(Sig_ptr *list, Sig_node data) {
    Sig_ptr newnode, last = *list;
    newnode = (Sig_ptr)malloc(sizeof(Sig_node));
    /*  fill the new load with new data here
    newnode->message_id = data.message_id; 
    ...
    */
    newnode-> SIG_INFO_NEXT = NULL;
    if (last == NULL){
        *list = newnode;
    }//first node
    else{
        while (1) {
            if (last-> SIG_INFO_NEXT == NULL) {
                last-> SIG_INFO_NEXT = newnode;
                break;
            }
            last = last-> SIG_INFO_NEXT;
        }
    }
}

并打印:

//prints whole list using printItem extern function
void print_list(Sig_ptr list) {
    //implement this print func for a node in your main file
    extern void PrintItem(Sig_node c); 

    Sig_ptr aux1, aux2;

    aux1 = list;
    while (aux1 != NULL) {
        PrintItem(aux1);
        aux2 = aux1->SIG_INFO_NEXT;
        aux1 = aux2;
    }
}