'queue'에 해당되는 글 2건

  1. 2009.09.15 [C#] Collections (2)
  2. 2009.06.02 [C#] Collections (3)

[C#] Collections (2)

Programming/.NET Programming 2009. 9. 15. 23:15

Collections 용어 정리

      Collection : 데이터 보관 가능 수정, 삭제 삽입의 기능을 가지고 있음
      ex. *(원형)Linked List, *Stack, *Queue, *List, Array List, Hash, Hash Table 등

      1. Linked List : 노드와 노드 간이 포인터로 연결되어 있어서 노드 간 검색이 가능

      2. Array List :  List와 거의 흡사, List와 구분이 되는 것은 자료형을 따지지 않는 점.
                        노드간의 다른 자료형을 넣을 수 있음. 편하지만 용량을 많이 차지.

      3. List : 사용법은 1차원 배열과 같음,
                Stack, Queue, Linked List의 모든 단점을 보완할 수 있음,
                노드간 검색 가능하고 Stack, Queue의 모든 기능을 소화함.
                넣을 때 insert, 뺄 때 remove
                원하는 인덱스에 넣고 뺄 수 있음(Stack, Queue 대신 이걸 더 많이 씀)

      4. Stack : 넣을 때 push, 뺄 때 pop, LIFO

      5. Queue : 넣을 때 enqueue, 뺄 때 dequeue, FIFO 

      6. Hash : 넓은 범위의 것을 좁게 접근, 인덱스를 가지고 있음.
                  각각 node에 key값과 value값을 가짐.
                  value에 접근할 때 key로 접근이 가능함

      - 배열과 Collection의 가장 큰 차이점 : 정적 - 동적의 차이

 

Stack 예제

using System;

using System.Collections; // Stack을 사용하기 위해 필요한 namespace

using System.Collections.Generic; // Stack을 사용하기 위해 필요한 namespace

namespace Stack

{

class Program

    {

static void Main(string[] args)

        {

            Stack<int> stackTest = new Stack<int>(); // Stack 생성

 

            for (int i = 0; i < 10; i++)

                stackTest.Push(i); // Data 입력

 

            while (stackTest.Count != 0)

                Console.WriteLine(stackTest.Pop()); // Data 출력

        }

    }

} 

 

 

Queue 예제

using System;

using System.Collections; // Queue을 사용하기 위해 필요한 namespace

using System.Collections.Generic; // Queue을 사용하기 위해 필요한 namespace

namespace Queue

{

class Program

    {

        static void Main(string[] args)

        {

            Queue<int> queueTest = new Queue<int>(); // Queue 생성

 

            for (int i = 0; i < 10; i++)

                queueTest.Enqueue(i); // Data 입력

 

            while (queueTest.Count != 0)

                Console.WriteLine(queueTest.Dequeue()); // Data 출력

        }

    }

} 

  

 

Hach Table 예제

using System;

using System.Collections; // Hach Table을 사용하기 위해 필요한 namespace

 

namespace HachTable

{

class Program

    {

        static void Main(string[] args)

        {

            Hashtable table = new Hashtable();

            table.Add("Happy", "^^*");

            table.Add("Sad", "T_T");

            table.Add(0, 5);

            Console.WriteLine(table["Happy"]);

 

            A a = new A();

            Object b = new A();

 

            // 명시되어 있지는 않지만 모든 클래스는 object클래스로 부터 상속 받음.

            // 가장 기본적인 클래스이기 때문.

            // class A는 사용자가 method를 입력하지 않았지만 몇가지 method를 기본적으로 가지고 있음.

            // table.Add(Object Key, Object Value)에는 아무 자료형(class, string, int ...)이나 들어가도 됨

            // Object가 가장 기본이 되는 클래스이기 때문임

        }

    }

    class A { }

}

 

  

 


'Programming > .NET Programming' 카테고리의 다른 글

[C#] Collections (3)  (0) 2009.06.02
[C#] Collections (1)  (0) 2009.06.02
[C#] Enum과 배열  (0) 2009.05.31
[C#] this와 상속  (0) 2009.05.31
[C#] 오버로딩과 오버라이딩  (0) 2009.05.31
:

[C#] Collections (3)

Programming/.NET Programming 2009. 6. 2. 17:06

Collections 용어 정리


      Collection : 데이터 보관 가능 수정, 삭제 삽입의 기능을 가지고 있음
      ex. *(원형)Linked List, *Stack, *Queue, *List, Array List, Hash, Hash Table 등

      1. Linked List : 노드와 노드 간이 포인터로 연결되어 있어서 노드 간 검색이 가능

      2. Array List :  List와 거의 흡사, List와 구분이 되는 것은 자료형을 따지지 않는 점.
                        노드간의 다른 자료형을 넣을 수 있음. 편하지만 용량을 많이 차지.

      3. List : 사용법은 1차원 배열과 같음,
                Stack, Queue, Linked List의 모든 단점을 보완할 수 있음,
                노드간 검색 가능하고 Stack, Queue의 모든 기능을 소화함.
                넣을 때 insert, 뺄 때 remove
                원하는 인덱스에 넣고 뺄 수 있음(Stack, Queue 대신 이걸 더 많이 씀)

      4. Stack : 넣을 때 push, 뺄 때 pop, LIFO

      5. Queue : 넣을 때 enqueue, 뺄 때 dequeue, FIFO 

      6. Hash : 넓은 범위의 것을 좁게 접근, 인덱스를 가지고 있음.
                  각각 node에 key값과 value값을 가짐.
                  value에 접근할 때 key로 접근이 가능함


      - 배열과 Collection의 가장 큰 차이점 : 정적 - 동적의 차이

 

List를 Stack과 Queue처럼 사용하는 방법 예제

using System;

using System.Collections; // List를 구현하기 위해 추가적으로 필요한 namespace

using System.Collections.Generic; // List를 구현하기 위해 추가적으로 필요한 namespace

 

namespace List

{

    class Program

    {

        static void Main(string[] args)

        {

            List<int> Stack = new List<int>(); // Stack List생성

            List<int> Queue = new List<int>(); // Queue List생성

 

            for (int i = 0; i < 10; i++)

            {

                Stack.Add(i);

                Queue.Add(i);

            }

 

            Console.WriteLine("      Stack   Queue");

 

            while (Stack.Count != 0)

            {

                Console.WriteLine("\t" + Stack[Stack.Count - 1] + "\t" + Queue[0]);

                // Stack처럼 사용, Stack.Count - 1는 배열의 마지막 인덱스
               
Stack.RemoveAt(Stack.Count - 1); 
                Queue.RemoveAt(0); // Queue처럼 사용

            }

        }

    }

}

 

 

List Sorting 예제

using System;

using System.Collections; // List를 구현하기 위해 추가적으로 필요한 namespace

using System.Collections.Generic; // List를 구현하기 위해 추가적으로 필요한 namespace

 

namespace List

{

     class Program

    {

        static void Main(string[] args)

        {

            List <int> list = new List<int>(); // List 생성

 

            list.Add(-1); // Data 추가

            list.Add(100);

            list.Add(5);

            list.Add(3);

            list.Add(40);

 

            Console.WriteLine("Not Sorted...");

            for (int i = 0; i < list.Count; i++)

                Console.WriteLine(list[i]); // Sorting되기 전 Data 출력

 

            list.Sort(); // Sorting

 

            Console.WriteLine("\nSorted...");

            for(int i = 0 ; i < list.Count ; i++)

                Console.WriteLine(list[i]); // Sorting Data 출력

            // int, char,string 등 다양한 자료형을 Sorting 할 수 있음

        }

    }

}

 

List Sorting이 불가능한 경우 예제 <IComparer의 필요성)

using System;

using System.Collections; // List를 구현하기 위해 추가적으로 필요한 namespace

using System.Collections.Generic; // List를 구현하기 위해 추가적으로 필요한 namespace

 

class Program

{

    static void Main(string[] args)

    {

        List<A> list = new List<A>();

 

        list.Add(new A(1, 1));

        list.Add(new A(-5, 3));

        list.Add(new A(1, 9));

        list.Add(new A(5, 7));

        list.Add(new A(7, 10));

 

        list.Sort(); // 비교가 불가능하기 때문에 Runtime Error 발생!

    }

}

 

class A

{

    public int a;

    public int b;

 

    public A(int a, int b)

    {

        this.a = a;

        this.b = b;

    }

}

 

 

IComparer 사용하여 문제를 해결한 예제

using System;

using System.Collections; // List를 구현하기 위해 추가적으로 필요한 namespace

using System.Collections.Generic; // List를 구현하기 위해 추가적으로 필요한 namespace

 

namespace Comparer

{

    class Program

    {

        public static void Main(string[] args)

        {

            List<A> list = new List<A>();

 

            list.Add(new A(1, 1));

            list.Add(new A(-5, 3));

            list.Add(new A(1, 9));

            list.Add(new A(5, 7));

            list.Add(new A(7, 10));

 

            list.Sort(new CompareA());

 

            for (int i = 0; i < list.Count; i++)

                Console.WriteLine(list[i].b);

        }

    }

 

    class A

    {

        public int a;

        public int b;

 

        public A(int a, int b)

        {

            this.a = a;

            this.b = b;

        }

    }

 

    // 방법1 : 파라미터 형을 명시(List에서 사용)

    class CompareA : IComparer<A>

    {

        int IComparer<A>.Compare(A x, A y)

        {

            return x.b.CompareTo(y.b);

        }

    }

 

    // 방법2 : 파라미터 형을 명시하지 않음(Array List에서 사용)

    class CompareA2 : IComparer

    {

        int IComparer.Compare(object x, object y)

        {

            A class1 = (A)x;

            A class2 = (A)y;

 

            return class1.b.CompareTo(class2.b);

        }

    }

}

 

 

'Programming > .NET Programming' 카테고리의 다른 글

[C#] Collections (2)  (0) 2009.09.15
[C#] Collections (1)  (0) 2009.06.02
[C#] Enum과 배열  (0) 2009.05.31
[C#] this와 상속  (0) 2009.05.31
[C#] 오버로딩과 오버라이딩  (0) 2009.05.31
: