Mercurial > hg > Database > jungle-sharp
view src/main/csharp/jp.ac.u-ryukyu.ie.cr/jungle/data/list/List.cs @ 4:79da77797f7e
list/List.cs fix, and not work addLast
author | Kazuma |
---|---|
date | Mon, 04 Jul 2016 03:50:34 +0900 |
parents | dec15de2c6ff |
children | 02b2ab7bffe6 |
line wrap: on
line source
using UnityEngine; using System.Collections; using System.Collections.Generic; using System; public class List<T> : IEnumerable<T> { private readonly Node<T> head; public List() { this.head = new headNode<T>(); } // T...はC#だとparamsらしい 可変引数型というみたいだ public List(params T[] attributes) { List<T> list = new List<T> (); foreach (T attribute_local in attributes) { list = list.addLast (attribute_local); } this.head = list.getHead (); } private List(Node<T> head) { this.head = head; } public Node<T> getHead() { return head; } public List<T> add(int num, T attribute) { Node<T> newHead = head.add(0, num, attribute); if (newHead == null) return this; return new List<T>(newHead); } public List<T> addLast(T attribute) { Node<T> newHead = head.addLast(attribute); return new List<T>(newHead); } public T index(int num) { int count = 0; Node<T> currentNode = head.getNext(); while (currentNode != null) { if (count == num) { return currentNode.getAttribute (); } currentNode = currentNode.getNext(); count++; } return default(T); } public IEnumerator<T> iterator() { Node<T> currentNode = head.getNext(); while (currentNode.getAttribute() != null) { yield return (T)currentNode.getAttribute(); currentNode = currentNode.getNext(); } } IEnumerator IEnumerable.GetEnumerator() { // call the generic version of the method return this.GetEnumerator(); } public IEnumerator<T> GetEnumerator() { return iterator (); } public List<T> delete(int num) { Node<T> newNode = head.delete(0, num); if (newNode == null) return this; return new List<T>(newNode); } public List<T> replace(int num, T attribute) { Node<T> newHead = head.replaceNode(0, num, attribute); if (newHead == null) return this; return new List<T>(newHead); } public T tail() { return index(length() - 1); } // java code head. public T headList() { return index(0); } public List<T> deleteLast() { return delete(head.length() - 1); } public List<T> deleteHead() { return delete(0); } public int length() { return head.length(); } public string toString() { string pathString = "<"; //IEnumerator<T> iterator = reverseIterator(); while (true) { // pathString += iterator.next(); // if (iterator.hasNext()) // pathString += ","; // else // break; } pathString += ">"; return pathString; } public List<T> append(List<T> list) { IEnumerator<T> iterator = list.iterator(); List<T> newList = this; while (iterator.MoveNext()) { T attribute = iterator.Current; newList = newList.addLast(attribute); } return newList; } // public IEnumerator<T> iterator() { // Node<T> currentNode = head.getNext(); // while (currentNode != null) { // yield return currentNode; // currentNode = currentNode.getNext(); // } // } }