박하의 나날

LinkedList<T>

프로그래밍/Unity
 2016.10.04. 17:07
LinkedList<T>
이중연결리스트를 나타낸다.
 
 
AddFirst  시작위치에 지정된 값이 포함된 새 노드를 추가.
AddLast  끝에 지정한 값이 포함된 새 노드를 추가
AddBefore  지정한 기존 노드 앞에 지정한 새 노드 추가
AddAfter  지정한 기존 노드 다음에 지정한 새 노드 추가
Find  지정한 값이 포함된 첫번재 노드를 찾는다
First  첫번째 노드를 가져온다
Remove  지정된 노드를 제거
RemoveFirst  시작 위치에서 노드를 제거
Removelast  끝에서 노드를 제거
 
 
 
 
 
 
EX)
using System;
using System.Text;
using System.Collections.Generic;
 
namespace LinkedListTEx
{
    class MainClass
    {
        private static void Display(LinkedList<string> words, string test)
        {
            Console.WriteLine(test);
            foreach (string word in words)
            {
                Console.Write(word + " ");
            }
            Console.WriteLine();
            Console.WriteLine();
        }
        
        private static void IndicateNode(LinkedListNode<string> node, string test)
        {
            Console.WriteLine(test);
            if (node.List == null)
            {
                Console.WriteLine("Node '{0}' is not in the list.\n",
                                  node.Value);
                return;
            }
            
            StringBuilder result = new StringBuilder("(" + node.Value + ")");
            LinkedListNode<string> nodeP = node.Previous;
            
            while (nodeP != null)
            {
                result.Insert(0, nodeP.Value + " ");
                nodeP = nodeP.Previous;
            }
            
            node = node.Next;
            while (node != null)
            {
                result.Append(" " + node.Value);
                node = node.Next;
            }
            
            Console.WriteLine(result);
            Console.WriteLine();
        }
 
        public static void Main (string[] args)
        {
            string[] words = { "유니티", "C#으로", "스크립팅", "한다"};
 
            // 연결 리스트의 생성
            LinkedList<string> sentence = new LinkedList<string>(words);
 
            Display(sentence, "연결 리스트의 값은:");
            Console.WriteLine("sentence.Contains(\"유니티\") = {0}", sentence.Contains("유니티"));
 
            sentence.AddFirst("오늘은");
            Display(sentence, "Test 1: 리스트 앞에 '오늘은' 문자열 추가");
 
            LinkedListNode<string> mark1 = sentence.First;
            sentence.RemoveFirst();
            sentence.AddLast(mark1);
            Display(sentence, "Test 2: 마지막 단어를 끝으로 이동");
 
            sentence.RemoveLast();
            sentence.AddLast("오늘만");
            Display(sentence, "Test 3: 마지막 단어를 '오늘만' 으로 변경");
 
            mark1 = sentence.Last;
            sentence.RemoveLast();
            sentence.AddFirst(mark1);
            Display(sentence, "Test 4: 마지막 노드를 첫 노드로 이동");
            
            sentence.RemoveFirst();
            LinkedListNode<string> current = sentence.FindLast("C#으로");
            IndicateNode(current, "Test 5: '스크립팅'이라는 단어를 찾아 가리킨다 ");
            
            sentence.AddAfter(current, "자바스크립트로");
            sentence.AddAfter(current, "그리고");
            IndicateNode(current, "Test 6: '스크립팅' 단어 뒤에 단어 추가");
 
            current = sentence.Find("유니티");
            IndicateNode(current, "Test 7: '유니티' 노드를 가리킨다");
 
            sentence.AddBefore(current, "오늘과");
            sentence.AddBefore(current, "내일은");
            IndicateNode(current, "Test 8: '오늘과', '내일은' 단어를 '유니티' 노드 앞에 추가");
        }
    }
}
 
 
cs

'프로그래밍 > Unity' 카테고리의 다른 글

Dictionary<TKey, TValue>  (0) 2017.04.09
List<T>  (0) 2017.04.09
Generic(일반화), Collection  (0) 2017.04.09
인벤토리, xml연동  (0) 2017.04.09
2016.09.14. 19:28~09.17  (0) 2017.04.09