如何解决在C中,当嵌套该结构时,如何使用链表打印结构中的所有值?
我创建了两个结构和两个变量n1和n2,这是下面的runnig代码的结果:
10-nick-90.500000
20-wilson-100.500000。
我想要什么:
1。获取elemtype变量值并将其放入LNODE结构中。
2。使用printlist(LNDOE *头)打印值。
请帮助我。
#include <stdio.h>
#include <stdlib.h>
typedef struct elemtype{
int no;
char* name;
double score;
}elemtype;
typedef struct tagnode{
elemtype data;
struct tagnode* next;
}LNODE;
elemtype* print_elemtype(elemtype* data_to_print){
elemtype* p = data_to_print;
if ( p != NULL){
printf("%d-%s-%lf",p->no,p->name,p->score);
}
printf("\n");
return NULL;
}
void creat_link(LNODE** head,int n,void (*input)(elemtype*)){
LNODE* s;
*head = (LNODE*)malloc(sizeof(LNODE));
(*head)->next = NULL;
for(;n>0;n--){
s = (LNODE*)malloc(sizeof(LNODE));
input(&(s->data));
s->next = (*head)->next;
(*head)->next = s;
}
}
int main()
{
elemtype* n1 = malloc(sizeof(elemtype));
elemtype* n2 = malloc(sizeof(elemtype));
n1->name = "nick";
n1->no = 10;
n1->score = 90.5;
n2->name = "wilson";
n2->no = 20;
n2->score = 100.5;
elemtype** s1,**s2;
s1 = &n1;
s2 = &n2;
print_elemtype(n1);
print_elemtype(n2);
free(n1);
free(n2);
n1 = NULL;
n2 = NULL;
}
解决方法
使用类似这样的代码爬入链接列表:
int main(void)
{
struct_type *list = allocat_struct_type();
struct_type *list_copy = list; // always copy the head of the list when your manipulating the list
while (list_copy != NULL) {
printf("%s",list_copy->text_to_display); // text_to_display must be an str type in this exemple
list_copy = list_copy->next;
}
}
如果您仍然无法准确确定何时何地的问题。
请随时问我,希望对您有帮助,我试图了解您会知道什么,但这很模糊。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。