同調実験室 - ARGIUS.net

TOP PAGE INDEX Number / 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

13:配列(2) 可変長配列

機能

  1. 可変長配列に要素(文字列型)を追加
  2. 配列の要素数を表示
  3. 配列の全要素を表示

シーケンスの実装だが詳細はそれぞれ別(Cは単方向リスト)

実行結果

$ ./test
size : 4
element (0) = apple
element (1) = bean
element (2) = candy
element (3) = doughnut
$

ソース

C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* 単方向リスト実装 */
typedef struct st_unlist {
  struct st_unlist * node;
  char * value;
} * List;

/* ノードを動的に確保 */
List list_init()
{
  List list;
  list = (List)malloc(sizeof(struct st_unlist));
  list->node  = NULL;
  list->value = NULL;
  return list;
}

/* 値を追加 */
void list_add(List list, char * value)
{
  if (list == NULL) {
    list = list_init();
  }
  if (list->value == NULL) {
    list->value = (char *)malloc(strlen(value)+1);
    if (list->value == NULL) {
      printf("malloc failed.\n");
    } else {
      sprintf(list->value, "%s", value);
    }
    return;
  } else {
    if (list->node == NULL) {
      list->node = list_init();
    }
    list_add(list->node, value);
  }
}

/* 値を取得 */
char * list_get(List list, int index)
{
  List lp = list;
  int i;
  
  for (i = 0; i < index; i++) {
    if (lp == NULL || lp->value == NULL) break;
    lp = lp->node;
  }
  
  if (lp->value == NULL) {
    return "(not found)";
  }
  
  return lp->value;
}

/* サイズを取得 */
int list_size(List list)
{
  List lp = list;
  int i;
  
  for (i = 1; ; i++) {
    if (lp->node == NULL || lp->value == NULL) break;
    lp = lp->node;
  }
  
  return i;
}

/* 実行 */
int main()
{
  List list;
  int i;
  
  list = list_init();
  
  list_add(list, "apple");
  list_add(list, "bean");
  list_add(list, "candy");
  list_add(list, "doughnut");
  
  printf("size : %d\n", list_size(list));
  
  for (i = 0; i < list_size(list); i++) {
    printf("element (%d) = %s\n", i, list_get(list, i));
  }
  
  return 0;
}

C++

#include <iostream>
#include <string>
#include <vector>

int main()
{
  std::vector<std::string> list;
  
  list.push_back("apple");
  list.push_back("bean");
  list.push_back("candy");
  list.push_back("doughnut");
  
  std::cout << "size : " << list.size() << std::endl;
  
  for (unsigned int i = 0; i < list.size(); i++) {
    
    std::cout << "element (" << i << ") = " << list[i] << std::endl;
    
  }
  
  return 0;
}

Java

import java.util.*;

class Main {

    public static void main(String[] args) {
        
        List list = new ArrayList();
        
        list.add("apple");
        list.add("bean");
        list.add("candy");
        list.add("doughnut");
        
        System.out.println("size : " + list.size());
        
        for (int i = 0; i < list.size(); i++) {
            
            System.out.println("element (" + i + ") = " + list.get(i));
            
        }
        
    }

}
    

Perl

my @list = ();

push @list, "apple";
push @list, "bean";
push @list, "candy";
push @list, "doughnut";

printf"size : %d\n", scalar @list;

for (my $i = 0; $i < @list; ++$i) {
    
    printf"element (%d) = %s\n", $i, $list[$i];
    
}

Ruby

list = Array.new

list << "apple"
list << "bean"
list << "candy"
list << "doughnut"

puts "size : #{ list.length }"

i = 0
list.each do |s|
  puts "element (#{i}) = " + s
  i += 1
end

最終更新日 : 2004.08.23

TOP PAGEINDEX Number / 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18