프로그래밍/C#2018. 3. 21. 16:51
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
public class MyStack<T>
    {
        private class Node<T>
        {
            public T Data { get; }
            public Node<T> next;
            public Node(T data)
            {
                Data = data;
            }
        }
 
        private Node<T> head;
        public int Count { get; private set; }
 
        public void Push(T data)
        {
            Console.WriteLine(string.Format("{0} Pushed", data));
            Node<T> pushData = new Node<T>(data);
            pushData.next = head;
            head = pushData;
            Count++;
        }
 
        public T Pop()
        {
            if (head == null)
                throw new Exception("비어있음");
            else
            {
                Node<T> returnNode = head;
                head = head.next;
 
 
                Console.WriteLine(string.Format("{0} Poped", returnNode.Data));
                Count--;
                return returnNode.Data;
 
            }
        }
 
        public void PrintAll()
        {
            Console.WriteLine("Count = " + Count);
 
            Node<T> current = head;
            while (current != null)
            {
                Console.WriteLine(current.Data);
                current = current.next;
            }
        }
 
 
    }
cs


단일 연결리스트를 이용해서 구현.

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

C# Dictionary의 성능에 대해서.  (0) 2018.03.25
C# 자료구조 정리  (0) 2018.03.24
try catch 성능  (0) 2018.03.21
using , Dispose()  (0) 2018.03.21
퀵소트  (0) 2018.03.15
Posted by JinFluenza