【发布时间】:2019-07-18 04:01:12
【问题描述】:
创建了一个函数,该函数接受一个 List Struct 指针和一个 double 值,该值是要存储在链表中的数据。当我调用函数来添加值时,它们被存储为头部和尾部,并且以前的值没有被保存。
列表结构:
typedef struct LIST_{
int size;
int (*match)(const void *key1, const void *key2);
void (*destroy)(void *data);
ListElmt *head;
ListElmt *tail;
}List;
list_init 函数:
void list_init(List *pPolynomial, void (*destroy)(void *data)){
pPolynomial->size = 0;
pPolynomial->destroy = destroy;
pPolynomial->head = NULL;
pPolynomial->tail = NULL;
}
附加项功能:
List* appendTerm(List *pPolynomial, double constant){
//inserting value at the end of the list
ListElmt *element;
element = (ListElmt *)malloc(sizeof(ListElmt));
double* d = &constant;
element->data = d;
if(pPolynomial->size == 0){
//if there was no head
pPolynomial->head = element;
pPolynomial->tail = element;
element->next = NULL;
printf("This is the data stored in the head %f \n", *(double*)pPolynomial->head->data);
printf("This is the data stored in the tail %f \n", *(double*)pPolynomial->tail->data);
}
else{
//there is a head
pPolynomial->tail = pPolynomial->tail->next;
pPolynomial->tail->next = element;
element->next = NULL;
printf("else statement: This is the data still stored in the head %f \n", *(double*)pPolynomial->head->data);
printf("This is the data stored in the tail %f \n", *(double*)pPolynomial->tail->data);
}
pPolynomial->size++;
printf("size: %d\n", pPolynomial->size);
return pPolynomial;
}
int main() {
List* listOfInts;
ListElmt *pElmt;
double *pDbl;
int i;
list_init(listOfInts, free);
listOfInts = appendTerm(listOfInts, 5);
listOfInts = appendTerm(listOfInts,6);
listOfInts = appendTerm(listOfInts,7);
pElmt = listOfInts->head;
for (int i = 0; i < 3; i++)
{
double d = *(double *) pElmt->data;
printf("List elem %d = %f\n", i, d);
pElmt = pElmt->next;
}
return (EXIT_SUCCESS);
}
这是程序的输出:
This is the data stored in the head 5.000000
This is the data stored in the tail 5.000000
size: 1
else statement: This is the data still stored in the head 6.000000
This is the data stored in the tail 6.000000
size: 2
else statement: This is the data still stored in the head 7.000000
This is the data stored in the tail 7.000000
size: 3
List elem 0 = 7.000000
List elem 1 = 0.000000
List elem 2 = 0.000000
【问题讨论】:
-
显示
list_init和List的定义。你是存储一个双精度还是一个指针做一个双精度 -
@Inian 我想他可能正在存储指向
void的指针,因为他正在铸造。 -
刚刚添加了list_init和List的定义
标签: c linked-list