如何实现具有两种结构的链表

时间:2020-12-11 07:18:33

I want to implement a data structure like this.What will be a good method to implement this.

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

如何实现具有两种结构的链表

Well i am trying to implement linkedlist for signals

那么我正在尝试实现信号的链表

structure for Msg data

Msg数据的结构

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;
};

structure for Signal data

信号数据的结构

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;

There will be an array of MSG DATA

将有一组MSG DATA

struct dbcMsg_t *new_node[MAX_DBC];

I am confused on how to implement this?I mean how to insert and how to display?

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

1 个解决方案

#1


1  

Implement a standard linked list of signal data nodes and hold a pointer of head nodes in your message data node.

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

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;

Since you asked how to implement insert and print functions:

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

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;
        }
    }
}

And print:

并打印:

//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;
    }
}

#1


1  

Implement a standard linked list of signal data nodes and hold a pointer of head nodes in your message data node.

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

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;

Since you asked how to implement insert and print functions:

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

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;
        }
    }
}

And print:

并打印:

//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;
    }
}