2013年12月19日木曜日

開発環境

C実践プログラミング 第3版 (Steve Oualline (著)、 望月 康司 (監訳) (翻訳)、谷口 功 (翻訳)、オライリー・ジャパン)のⅢ部(高度なプログラミング概念)の17章(高度なポインタ)、17-12(プログラミング実習)、実習17-3.をDartで解いてみる。

その他参考書籍

17-12(プログラミング実習)、実習17-3.

コード

sample.dart

import 'dart:html';

void main(){
  String s = 'kamimura';
  InputElement input = querySelector('#f0');
  InputElement run = querySelector('#run_dart');
  InputElement clear = querySelector('#clear');
  Element pre = querySelector('#pre0');
  run.onClick.listen((MouseEvent event){
    pre.text = window.navigator.userAgent + '\n';
    var l = new DoubleList();
    for(var n in nums){
      l.add(n);
    }
    pre.text += '最初\n';
    pre.text += '5の次: ${l.five.next}\n';
    pre.text += '5の前: ${l.five.previous}\n';
    pre.text += '${l}\n';
    l.remove(0);
    pre.text += '0を削除\n';
    pre.text += '${l}\n';
    l.remove(9);
    pre.text += '9を削除\n';
    pre.text += '${l}\n';
    l.remove(5);
    pre.text += '5を削除\n';
    pre.text += '${l}\n';
    l.remove(10);
    pre.text += '10を削除\n';
    pre.text += '${l}\n';
    l.remove(1);    
    l.remove(2);    
    l.remove(3);    
    l.remove(4);    
    l.remove(6);    
    l.remove(7);    
    l.remove(8); 
    pre.text += '全て削除\n';
    pre.text += '${l}\n';
    l.remove(5);
    pre.text += '5を削除\n';
    pre.text += '${l}';
  });
  clear.onClick.listen((MouseEvent event) => pre.text = '');
}

class Box{
  var a;
  Box previous;
  Box next;
  Box(var x){
    a = x;
    previous = null;
    next = null;
  }
  String toString(){
    return a.toString();
  }
}

class DoubleList{
  Box head;
  DoubleList(){
    head = null;
  }
  Box five = null;
  void add(var x){
    if(head == null){
      head = new Box(x);
      if(x == 5){
        five = head;
      }
      return;
    }
    Box current = head;
    Box box = new Box(x);
    if(x == 5){
      five = box;
    }
    while(true){
      if(current.next == null){
        if(x > current.a){
          box.previous = current;
          box.next = null;
          current.next = box;
          if(current.previous == null){
            head = current;
          }
          return;
        }
        box.next = current;
        box.previous = current.previous;
        if(box.previous == null){
          head = box;
          current.previous = box;
          return;
        }
        current.previous.next = box;
        current.previous = box;
        return;
      }
      if(x <= current.a){
        break;
      }
      current = current.next;
    }
    box.previous = current.previous;
    box.next = current;
    if(box.previous == null){
      head = box;
    } else {
      current.previous.next = box;
    }
    current.previous = box;
  }
  void remove(var x){
    if(x == 5){
      five = null;
    }
    Box current = head;
    while(true){
      if(current == null){
        break;
      }
      if(x == current.a){
        if(current.previous == null){
          head = current.next;
        } else {
          current.previous.next = current.next;
        }
        if(current.next != null){
          current.next.previous = current.previous;
        }
        break;
      }
      current = current.next;
    }
  }
  String toString(){
    var result = '';
    var current = head;
    while(current != null){
      result += current.a.toString() + ' ';
      current = current.next;
    }
    if(result == ''){
      result = '空っぽ';
    }
    return result;
  }
}

List<num> nums = [9, 0, 8, 1, 7, 2, 6, 3, 5, 4];













						

0 コメント:

コメントを投稿