공부/Data Structure

queue 구현

Lero God 2020. 7. 12. 19:01

stack과 비슷하게 동적으로 메모리하는 부분은 stl의 vector나 deque이용해서 쓰면 되지만

난 그냥 미리 할당된 메모리 공간에서 queue 간단하게 구현함!

 

queue는 stack과 다르게 들어온 순서대로 데이터가 삭제된다!

 

template<typename T>
class queue
{
public:
	// push element
	void Push(T element)
	{
		queueContainer[bottom] = element;
		++bottom;
	}

	// pop top element
	void Pop(void)
	{
		if (bottom > top)
		{
			++top;
		}
	}

	// return top element
	T Top(void) const
	{
		return queueContainer[top];
	}

	// return bottom element
	T Bottom(void) const
	{
		return queueContainer[bottom];
	}

	// whether it's empty
	bool Empty(void) const
	{
		return top == bottom ? true : false;
	}

	// number of elements in container
	int Size(void) const
	{
		int result = bottom - top;
		return result;
	}

private:
	int top;
	int bottom;
	T* queueContainer;

public:
	queue() : top(0), bottom(0)
	{
		queueContainer = new T[containerSize];
	}

	~queue()
	{
		delete[] queueContainer;
	}
};

 

'공부 > Data Structure' 카테고리의 다른 글

double linked list 구현  (0) 2020.07.13
single linked list 구현  (0) 2020.07.12
stack 구현  (0) 2020.07.08