상세 컨텐츠

본문 제목

STACK / QUEUE

C#

by 탑~! 2018. 6. 29. 15:01

본문

System.Collections.Stack은 자료구조중에 하나인 스택의 방식을 그대로 채용한것으로 FILO(First In Last Out)방식으로 값을 넣고 인출하기에 가장 먼저 넣은 값은 가장 나중에 나오게 됩니다.

 

System.Collections.Stack st = new System.Collections.Stack();
st.Push(1);
st.Push(2);
st.Push(3);

Console.WriteLine(st.Pop());

st.Push(4);

Console.WriteLine(st.Pop());

 

값의 입력은 Push()로 인출은 Pop()메서드로 수행합니다. 참고로 모든 값을 지우고 싶다면 Clear() 메서드를 호출합니다.

 

System.Collections.Queue은 자료구조중에 하나인 큐(Queue)의 방식을 그대로 채용한것으로 FIFO(First In First Out)방식으로 값을 넣고 인출하기에 가장 먼저 넣은 값이 가장 먼저 나오게 됩니다.

 

System.Collections.Queue qu = new System.Collections.Queue();
qu.Enqueue(1);
qu.Enqueue(2);
qu.Enqueue(3);

Console.WriteLine(qu.Dequeue());

qu.Enqueue(4);

Console.WriteLine(qu.Dequeue());

 

값의 입력은 Enqueue()로 인출은 Dequeue()메서드로 수행합니다. 참고로 모든 값을 지우고 싶다면 Clear() 메서드를 호출합니다.



출처: http://lab.cliel.com/entry/C-Stack?category=478966 [CLIEL LAB]

'C#' 카테고리의 다른 글

LINQ  (0) 2018.06.29
MemoryStream / StreamWriter / StreamReader / BinaryWriter / BinaryReader  (0) 2018.06.29
람다식 (LAMBDA EXPRESSION)  (0) 2018.06.29
fixed  (0) 2018.06.29
문자열 보간($)  (0) 2018.06.29

관련글 더보기