線形リストで新たなノードを挿入する関数

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
パティロケ

線形リストで新たなノードを挿入する関数

#1

投稿記事 by パティロケ » 8年前

コード:

#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");
}
線形リストにおいて入力した実数値の挿入場所を関数search_positionで返し、関数insert_rearで挿入し絶対値の順に並べたいのですが、おそらくinsert_rearがうまく書けていないため数値入力2回目でセグメントエラーが出てしまいます。(実行時2回目puts("pos+")でエラーがでるため)
どのように改良したらいいでしょうか。

かずま

Re: 線形リストで新たなノードを挿入する関数

#2

投稿記事 by かずま » 8年前

パティロケ さんが書きました:どのように改良したらいいでしょうか。
insert_rear を次のように修正し、
main の pos = pos + 1; を削除するとよいでしょう。

コード:

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;
  if (pos == 0) {
    temp->next = *pp;
    *pp = temp;
  }
  else {
    node_type *p = *pp;
    for (i = 1; i < pos; i++)
      p = p->next;
    temp->next = p->next;
    p->next = temp;
  }
  return SUCCESS;
}
print_nodes も修正しないと、数字が全部つながってしまいますよ。

返信

“C言語何でも質問掲示板” へ戻る