C 多个链表可以共享相同的结构吗?

C 多个链表可以共享相同的结构吗?,c,struct,linked-list,macros,C,Struct,Linked List,Macros,我有很多不同的链表结构,都有不同的数据类型变量。比如: #include <stdio.h> #include <conio.h> #include <stdlib.h> struct scorel { int no; struct scorel *rp; }; struct namel{ char a[10]; struct scorel *rp; struct namel *dp; }; void main(){

我有很多不同的链表结构,都有不同的数据类型变量。比如:

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

struct scorel {
    int no;
    struct scorel *rp;
};
struct namel{
    char a[10];
    struct scorel *rp;
    struct namel *dp;
};
void main(){
    int i,j,n,m;
    struct namel *temp,*head,*end,**s;
    struct scorel *tmp,*had,*nd;
    clrscr();
    printf("enter no of students");
    scanf("%d",&n);
    end=NULL;
    for(i=0;i<n;i++){
        temp=(struct namel*)malloc(sizeof(struct namel));
        printf("enter name\n");
        scanf("%s",temp->a);
        temp->rp=NULL;
        temp->dp=NULL;
        *(s+i)=temp;
        if(end==NULL){
            end=head=temp;
        }
        else {
            end->dp=temp;
            end=temp;
        }
        printf("enter no of scores");
        scanf("%d",&m);
        nd=NULL;
        for(j=0;j<m;j++){
            tmp=(struct scorel*)malloc(sizeof(struct scorel));
            printf("enter score\n");
            scanf("%d",&tmp->no);
            tmp->rp=NULL;
            if(nd==NULL){
                nd=had=tmp;
                temp->rp=tmp;
            }
            else {
                nd->rp=tmp;
                nd=tmp;
            }
        }
    }
    for(i=0;i<n;i++){
        temp=*(s+i);
        printf("%s-->",temp->a);
        tmp=temp->rp;
        while(tmp!=NULL){
            printf("%d-->",tmp->no);
            tmp=tmp->rp;
        }
        printf("\n");
    }
    getch();
}
但在这里,我们只能使用一种类型的变量。
我希望每个链表都有不同类型的变量。

您可以用两种方式创建链表

第一种方法是将
next
字段与数据结构一起添加,然后您可以仅在一个列表中访问此结构

struct myData {
    struct myData* next;
    int a;
    char b;
    float c;
}
稍后,当您将项目添加到链表中时,只需找到最后一个项目,然后设置
last->next=current
current->next=NULL


另一种可能是将数据和列表条目的结构分开

//Linked list structure
struct nodeEntry {
    struct nodeEntry* next;
    void* data;
};

struct myData {
    int a;
    char b;
    float c;
};
现在,要将数据添加到链接列表中,您可以执行以下类似操作:

nodeEntry* ent = malloc(*ent);
ent->data = pointerToMyData;
ent->next = NULL;

//Now find the last item in linked list and do:
last->next = ent;
在第二种情况下,链表中的每个项目都有自己的
next
对象,这允许您在多个链表中拥有单一的数据结构

这种方法称为多重链表


请输入代码。这不是C++问题。请删除C++标签<代码>。这种方法称为多链接列表< /代码>,它与此无关,实际上是这样。单个项目可以在多链接列表中。@4386427数据并不重要。它是列表本身,也就是将节点链接在一起的东西-一个指针或多个指针。您提供的链接非常清楚。@4386427通过我的方法,您可以对多个链接条目使用相同的数据结构。当然,数据输入在使用的地方没有引用。是的,一个数据对象可以在多个列表中-true。但这与多链接列表无关。
nodeEntry* ent = malloc(*ent);
ent->data = pointerToMyData;
ent->next = NULL;

//Now find the last item in linked list and do:
last->next = ent;