개발 공부/컴퓨터 과학
[자료구조] 큐(Que), 스택(Stack) 배열로 구현
도이(doi)
2021. 6. 29. 15:07
728x90
큐 (Queue)
큐 (Queue)는 먼저 추가된 데이타가 먼저 출력 처리되는(FIFO, First In First Out) 자료 구조로서 입력된 순서대로 처리해야 하는 상황에 이용된다. Queue는 맨 뒤(tail)에 데이타를 계속 추가하고, 맨 앞(head)에서만 데이타를 읽기 때문에 순차적으로 데이타를 처리하게 된다.
using System;
using System.Collections.Generic;
using System.Text;
namespace dataStructure_Test.MyListClass
{
class MyQue
{
public string[] array;
public int arrayNum;
public int top;
public void arrayInst(int n)
{
arrayNum = n;
array = new string[n];
top = 0;
}
public void Push(int value)
{
if (array[top] == null)
{
array[top] = value.ToString();
Console.WriteLine("*== push ==*");
Console.WriteLine(array[top]);
Console.WriteLine(top);
top++;
}
else
{
Console.WriteLine("no array.");
}
}
public void Pop()
{
Console.WriteLine("*== pop ==*");
if (array[0] != null)
{
Console.WriteLine(array[0]);
top--;
if (top == 0)
{
Console.WriteLine("null");
}
array[0] = null;
for (int i = 0; i < top; i++)
{
array[i] = array[i + 1];
//Console.WriteLine(array[i]);
//Console.WriteLine("this is the line : ");
//Console.WriteLine(i);
}
}
else
{
Console.WriteLine("no element in the array.");
}
}
public void Clear()
{
Console.WriteLine("*== clear ==*");
for (int i = 0; i < arrayNum; i++)
{
array[i] = null;
if (array[i] == null)
{
Console.WriteLine("null");
}
}
}
}
}
스택 (Stack)
스택 (Stack)은 가장 나중에 추가된 데이타가 먼저 출력 처리되는(LIFO, Last In First Out) 자료 구조로서 가장 최신 입력된 순서대로 처리해야 하는 상황에 이용된다. 스택은 개념적으로 한 쪽 끝에서만 자료를 넣거나 뺄 수 있는 구조로 되어 있다. 자료는 스택에 저장하는 것은 Push라 하고, 가장 최근 것부터 꺼내는 것은 Pop이라 한다.
using System;
using System.Collections.Generic;
using System.Text;
namespace dataStructure_Test.MyListClass
{
public class MyStack
{
public int arrayNum;
string[] array;
public static int top = 0;
public void SetNum(int Value)
{
arrayNum = Value;
array = new string[arrayNum];
}
public void Push(int value)
{
if (array[top] == null)
{
array[top] = value.ToString();
Console.WriteLine("*== push ==*");
Console.WriteLine(array[top]);
Console.WriteLine(top);
top++;
}
}
public string Pop()
{
string popVal = "";
Console.WriteLine("*== pop ==*");
top--;
if (array[top] != null)
{
popVal = array[top];
array[top] = null;
}
Console.WriteLine(popVal);
Console.WriteLine(top);
return popVal;
}
public void Clear()
{
Console.WriteLine("*== clear ==*");
for (int i = 0; i < arrayNum; i++)
{
array[i] = null;
if (array[i] == null)
{
Console.WriteLine("null");
}
}
}
}
}
mainCS
using System;
using System.Collections.Generic;
using System.Text;
using dataStructure_Test.MyListClass;
namespace dataStructure_Test
{
class MainCS
{
static void Main()
{
int arrayNum = 5;
MyStack st = new MyStack();
MyQue q = new MyQue();
st.SetNum(arrayNum);
//Console.WriteLine("*== this is Stack ==*");
//st.Push(2);
//st.Push(3);
//st.Pop();
//st.Pop();
//st.Push(5);
//st.Push(5);
//st.Push(5);
//st.Clear();
Console.WriteLine("*== this is Que ==*");
q.arrayInst(5);
q.Push(1);
q.Push(2);
q.Push(3);
q.Push(4);
q.Push(5);
q.Pop();
q.Pop();
q.Pop();
q.Pop();
q.Pop();
q.Pop();
q.Pop();
q.Push(1);
}
}
}
728x90
반응형