08/14

이우석·2023년 8월 15일
0

SBS 국기수업

목록 보기
18/120

UQueue - 구현 조건

lNode클래스에서 preNode변수를 제거한 qNode클래스를 사용하여 구현.
필수 구현 함수
생성자, EnQueue(추가), DeQueue(사용), Front(다음에 사용할 값 표시만)
추가
ShowData, Node말고 Array를 이용하여 구현

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace LearnAlgorithm_DataStructure
{
	public class qNode<T>
	{
		public T Value;
		public qNode<T> NextLink;

		public qNode(T value)
		{
			Value = value;
			NextLink = default(qNode<T>);
		}

		public qNode(T value, qNode<T> next)
		{
			Value = value;
			NextLink = next;
		}
	}

	class MyQueue<T>
	{
		private qNode<T> _Head;
		private int _Count = 0;

		public MyQueue()
		{
			_Head = default(qNode<T>);
		}

		public int Size()
		{
			return _Count;
		}

		public MyQueue<T> Size(out int size)
		{
			size = Size();
			return this;
		}

		public bool IsEmpty()
		{
			return _Count == 0;
		}

		public MyQueue<T> IsEmpty(out bool isEmpty)
		{
			isEmpty = IsEmpty();
			return this;
		}

		public MyQueue<T> EnQueue(T value)
		{
			var newNode = new qNode<T>(value);
			_Count++;

			if(_Head == default(qNode<T>))
			{
				_Head = newNode;
				return this;
			}

			var lastNode = _Head;
			while (lastNode.NextLink != default(qNode<T>))
			{
				lastNode = lastNode.NextLink;
			}
			lastNode.NextLink = newNode;

			return this;
		}

		private MyQueue<T> GoNextIfExist()
		{
			if (_Count > 0)
			{
				_Count--;
				qNode<T> newHead = _Head.NextLink;
				qNode<T> newNext = _Head.NextLink?.NextLink;
				_Head = newHead;
				_Head.NextLink = newNext;
			}

			return this;
		}

		public T DeQueue()
		{
			T result = Front();

			GoNextIfExist();

			return result;
		}

		public MyQueue<T> DeQueue(out T value)
		{
			value = Front();
			if(EqualityComparer<T>.Default.Equals(value, default(T)) == true)
			{
				return this;
			}

			GoNextIfExist();

			return this;
		}

		public T Front()
		{
			if (_Count <= 0)
			{
				return default(T);
			}

			return _Head.Value;
		}

		public MyQueue<T> Front(out T value)
		{
			value = Front();

			return this;
		}

		public MyQueue<T> Print()
		{
			var node = _Head;
			string output = string.Empty;
			do
			{
				if (node == default(qNode<T>))
				{
					break;
				}

				output += node.Value?.ToString();
				output += ", ";

				node = node.NextLink;
			}
			while (node != default(qNode<T>));

			output = output.TrimEnd(' ').TrimEnd(',');

			Console.WriteLine(output);

			return this;
		}
	}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LearnAlgorithm_DataStructure
{
	class MyQueueArray<T>
	{
		public MyQueueArray()
		{
			MyArray = new T[0];
		}

		private const int CapacityUnit = 8;
		private T[] MyArray;
		private int _Size = 0;

		public bool IsEmpty { get { return _Size == 0; } }

		private void Expand()
		{
			Array.Resize(ref MyArray, MyArray.Length + CapacityUnit);
		}

		public bool EnQueue(T value)
		{
			if (MyArray.Length <= _Size)
			{
				Expand();
			}

			MyArray[_Size++] = value;

			return true;
		}

		public T DeQueue()
		{
			T result = Front();
			int end = _Size - 1;

			for (int i = 0; i < end; i++)
			{
				MyArray[i] = MyArray[i + 1];
			}

			_Size--;

			return result;
		}

		public T Front()
		{
			return MyArray[0];
		}

		public MyQueueArray<T> Front(out T result)
		{
			result = Front();
			return this;
		}

		public void Print()
		{
			string output = string.Empty;

			for(int i = 0; i < _Size; i++)
			{
				if (MyArray[i] != null)
				{
					output += MyArray[i];
				}
				else
				{
					output += "(null)";
				}

				output += ", ";
			}

			Console.WriteLine(output.TrimEnd(' ').TrimEnd(','));
		}
	}
}

UStack - 구현 조건

lNode클래스에서 preNode변수를 제거한 sNode클래스를 사용하여 구현.
필수 구현 함수
생성자, Push(추가), Pop(사용), Top(다음에 사용할 값 표시만)
추가
ShowData, Node말고 Array를 이용하여 구현

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace LearnAlgorithm_DataStructure
{
	public class sNode<T>
	{
		public T Value;
		public sNode<T> NextLink;

		public sNode(T value)
		{
			Value = value;
			NextLink = default(sNode<T>);
		}

		public sNode(T value, sNode<T> next)
		{
			Value = value;
			NextLink = next;
		}
	}

	class MyStack<T>
	{
		private sNode<T> _Head;
		private int _Count = 0;

		public MyStack()
		{
			_Head = default(sNode<T>);
		}

		public int Size()
		{
			return _Count;
		}

		public MyStack<T> Size(out int size)
		{
			size = Size();
			return this;
		}

		public bool IsEmpty()
		{
			return _Count == 0;
		}

		public MyStack<T> IsEmpty(out bool isEmpty)
		{
			isEmpty = IsEmpty();
			return this;
		}

		public MyStack<T> Push(T value)
		{
			var newNode = new sNode<T>(value);
			_Count++;

			if (_Head == default(sNode<T>))
			{
				_Head = newNode;
				return this;
			}

			sNode<T> newNextLink = _Head;
			_Head = newNode;
			_Head.NextLink = newNextLink;

			return this;
		}

		private MyStack<T> GoNextIfExist()
		{
			if (_Count > 0)
			{
				_Count--;
				sNode<T> newHead = _Head.NextLink;
				sNode<T> newNext = _Head.NextLink?.NextLink;
				_Head = newHead;
				_Head.NextLink = newNext;
			}

			return this;
		}

		public T Pop()
		{
			T result = Top();

			GoNextIfExist();

			return result;
		}

		public MyStack<T> Pop(out T value)
		{
			value = Top();
			if (EqualityComparer<T>.Default.Equals(value, default(T)) == true)
			{
				return this;
			}

			GoNextIfExist();

			return this;
		}

		public T Top()
		{
			if (_Count <= 0)
			{
				return default(T);
			}

			return _Head.Value;
		}

		public MyStack<T> Top(out T value)
		{
			value = Top();

			return this;
		}

		public MyStack<T> Print()
		{
			var node = _Head;
			string output = string.Empty;
			do
			{
				if (node == default(sNode<T>))
				{
					break;
				}

				output += node.Value?.ToString();
				output += ", ";

				node = node.NextLink;
			}
			while (node != default(sNode<T>));

			output = output.TrimEnd(' ').TrimEnd(',');

			Console.WriteLine(output);

			return this;
		}
	}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LearnAlgorithm_DataStructure
{
	class MyStackArray<T>
	{
		public MyStackArray()
		{
			MyArray = new T[0];
		}

		private const int CapacityUnit = 8;
		private T[] MyArray;
		private int _Size = 0;

		public bool IsEmpty { get { return _Size == 0; } }

		private void Expand()
		{
			Array.Resize(ref MyArray, MyArray.Length + CapacityUnit);
		}

		public MyStackArray<T> Push(T value)
		{
			if (MyArray.Length <= _Size)
			{
				Expand();
			}

			MyArray[_Size++] = value;

			return this;
		}

		public T Pop()
		{
			T result = Top();

			_Size--;

			return result;
		}

		public MyStackArray<T> Pop(out T result)
		{
			result = Top();
			_Size--;
			return this;
		}

		public T Top()
		{
			return MyArray[_Size - 1];
		}

		public MyStackArray<T> Top(out T result)
		{
			result = Top();
			return this;
		}

		public int Size()
		{
			return _Size;
		}

		public void Print()
		{
			string output = string.Empty;

			for (int i = 0; i < _Size; i++)
			{
				if (MyArray[i] != null)
				{
					output += MyArray[i];
				}
				else
				{
					output += "(null)";
				}

				output += ", ";
			}

			Console.WriteLine(output.TrimEnd(' ').TrimEnd(','));
		}
	}
}
profile
게임 개발자 지망생, 유니티 공부중!

1개의 댓글

comment-user-thumbnail
2023년 8월 15일

글 잘 봤습니다.

답글 달기