与联合作为数据类型的双重链接列表的问题
我必须创建与Union作为数据类型的列表,但事实证明,创建我的列表后,只有在我所有列表中重复的相同值。我该如何处理?
你能帮我这个方向吗?
对您来说,美好的一天
结构,联合和功能声明
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Declaration of list
struct list_t{
struct node_t *head;
struct node_t *tail;
};
struct node_t {
union val_t *vals;
struct node_t *prev;
struct node_t *next;
};
//Declaration of Union
union val_t{
int as_int;
double as_double;
char *as_str;
};
int insert(struct list_t *list, union val_t*value);
功能定义
//Push to create my list
int push_none(struct list_t *list, union val_t*value){
if(list==NULL || value==NULL)return 1;
struct node_t *node = (struct node_t*) malloc(sizeof(struct node_t));
if(node==NULL)return 2;
node->vals =value;
if(list->head==NULL){
node->next=NULL;
node->prev=NULL;
list->head = node;
list->tail=node;
} else{
node->next=NULL;
node->prev = list->tail;
list->tail->next = node;
list->tail = node;
}
return 0;
}
main
int main() {
//initialization of my list
struct list_t * list = malloc(sizeof(struct list_t));
list->tail=NULL;
list->head=NULL;
printf("Create a double linked list of 3 element :");
char buf[100],**ptr=NULL;
int type[3][3];
union val_t values[4];
int as_int1;
double as_double1;
int i=0;
//Data input
while (i<3){
int j=0;
while(j<3){
while(1){
printf("\n1 - INT\n2 -DOUBLE\n3 - CHAR\nEnter type:");
scanf("%d", &type[i][j]);
if(type[i][j]>0 && type[i][j]<4)break;
}
printf("\nEnter value :");
scanf("%s",buf);
if(type[i][j]==1){
as_int1 = (int)strtol(buf,ptr,10);
(values+j)->as_int = as_int1;
}
else if(type[i][j]==2){
as_double1 = strtod(buf,ptr);
((values+j)->as_double) = as_double1;
}
else if(type[i][j]==3){
(values + j)->as_str = strdup(buf);
}
j++;
}
//Creation of the list
push_node(list,values);
i++;
}
i=0;
//Display values
while(list->head!=NULL){
int j=0;
while(j<3){
if(type[i][j]==1){
printf("%d ",list->head->vals->as_int);
}
if(type[i][j]==2){
printf("%lf ",list->head->vals->as_double);
}
if(type[i][j]==3){
printf("%s ",list->head->vals->as_str);
}
j++;
}
printf("\n");
i++;
list->head=list->head->next;
}
return 0;
}
I have to create a list with union as data type but it turns out that after the creation of my list there is only the same value which is repeated on all my list. How Can I handle this?
Can you help me in this direction?
Good day to you
Structure, Union and Function declaration
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Declaration of list
struct list_t{
struct node_t *head;
struct node_t *tail;
};
struct node_t {
union val_t *vals;
struct node_t *prev;
struct node_t *next;
};
//Declaration of Union
union val_t{
int as_int;
double as_double;
char *as_str;
};
int insert(struct list_t *list, union val_t*value);
Function definition
//Push to create my list
int push_none(struct list_t *list, union val_t*value){
if(list==NULL || value==NULL)return 1;
struct node_t *node = (struct node_t*) malloc(sizeof(struct node_t));
if(node==NULL)return 2;
node->vals =value;
if(list->head==NULL){
node->next=NULL;
node->prev=NULL;
list->head = node;
list->tail=node;
} else{
node->next=NULL;
node->prev = list->tail;
list->tail->next = node;
list->tail = node;
}
return 0;
}
Main
int main() {
//initialization of my list
struct list_t * list = malloc(sizeof(struct list_t));
list->tail=NULL;
list->head=NULL;
printf("Create a double linked list of 3 element :");
char buf[100],**ptr=NULL;
int type[3][3];
union val_t values[4];
int as_int1;
double as_double1;
int i=0;
//Data input
while (i<3){
int j=0;
while(j<3){
while(1){
printf("\n1 - INT\n2 -DOUBLE\n3 - CHAR\nEnter type:");
scanf("%d", &type[i][j]);
if(type[i][j]>0 && type[i][j]<4)break;
}
printf("\nEnter value :");
scanf("%s",buf);
if(type[i][j]==1){
as_int1 = (int)strtol(buf,ptr,10);
(values+j)->as_int = as_int1;
}
else if(type[i][j]==2){
as_double1 = strtod(buf,ptr);
((values+j)->as_double) = as_double1;
}
else if(type[i][j]==3){
(values + j)->as_str = strdup(buf);
}
j++;
}
//Creation of the list
push_node(list,values);
i++;
}
i=0;
//Display values
while(list->head!=NULL){
int j=0;
while(j<3){
if(type[i][j]==1){
printf("%d ",list->head->vals->as_int);
}
if(type[i][j]==2){
printf("%lf ",list->head->vals->as_double);
}
if(type[i][j]==3){
printf("%s ",list->head->vals->as_str);
}
j++;
}
printf("\n");
i++;
list->head=list->head->next;
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这结合了我在最高评论中所说的话。
类型
添加到节点结构中是重构代码。它进行了编译并经过了[限制]测试:
这是一些测试输出:
这是一个更清洁的版本。特别是,
node_push
已经简化了:尽管有些不同,但您可能希望在C:写作a C 中的“通用”结构印刷方法
This incorporates what I was saying in my top comments.
type
to node structHere is the refactored code. It compiles and has been [cursorily] tested:
Here is some test output:
Here is a slightly more cleaned up version. In particular,
node_push
has been simplified:Although somewhat different, you may want to have a look at my answer on "generic" structs in C: Writing a 'generic' struct-print method in C