#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define SUCCESS 1
#define FAILURE 0
typedef double data_type; //データの型
typedef struct node_tag {
data_type data; //データ
struct node_tag *next; //struct node_tag型の次のノードへのポインタnext
} node_type; //ノードの型
//線形リストの初期化
void init(node_type **pp){
*pp = NULL;
}
node_type *new_node(data_type x, node_type *p){
node_type *temp;
temp = (node_type *)malloc(sizeof(node_type));
if(temp == NULL)
return NULL; //割り当て失敗
else{
temp->data = x;
temp->next = p; //次へのポインタNULLなど
return temp;
}
}
int insert_front(node_type **pp, data_type x){
node_type *temp;
temp = new_node(x, *pp);
if(temp == NULL)
return FAILURE;
*pp = temp;
return SUCCESS;
}
int insert_rear(node_type **pp, data_type x, int pos){
node_type *temp;
int i;
temp = new_node(x, NULL);
if(temp == NULL)
return FAILURE;
while(*pp != NULL){
for(i=0;i<3-pos;i++){
pp = &((*pp)->next);
}
}
*pp = temp;
return SUCCESS;
}
int remove_front(node_type **pp){
node_type *temp;
if(*pp != NULL){
temp = (*pp)->next;
free(*pp);
*pp = temp;
return SUCCESS;
}
else return FAILURE;
}
void print_nodes(node_type *p){
while (p != NULL){
printf("%g", p->data);
p = p->next;
}
printf("\n");
}
//関数:総和
double all(node_type *p, double total){
total = 0;
while (p != NULL){
//printf("%lf\n", total);
total += p->data;
// printf("%lf\n", total);
p = p->next;
}
return total;
}
int nodeCount(node_type *temp)
{
int ctr;
ctr = 0;
while (temp != NULL) {
ctr ++;
temp = temp->next;
}
return ctr;
}
//ノード挿入位置
int search_position(node_type *p, data_type x){
int i;
i=0;
while((p!=NULL)&&(fabs(x)>fabs(p->data))){
p = p->next;
i++;
}
return i;
}
int main()
{
int i, pos;
double fig=0;
double total=0;
char ch;
node_type *list;
init(&list); //(**list);
for(i=0;i<3;i++){
puts("データを入力してください。");
printf("%lf\n", fig);
scanf("%lf", &fig);
printf("%lf\n", fig);
pos = search_position(list, fig);
puts("did search_pos");
pos = pos+1;
puts("pos+");
insert_rear(&list, fig, pos);
puts("ノード挿入完了");
fig = 0;
puts("fig初期化済");
}
print_nodes(list);
total = all(list, total);
//puts("all did");
printf("SUM = %lf\n", total);
//puts("sum did");
}
どのように改良したらいいでしょうか。