SinglyLinkedList, Stack, Queue 구현 연습

·2022년 12월 6일
0
#include <iostream>
#include <stdexcept>

struct Node
{
	int Value;
	Node* Next;
};

///////////////////////// slist /////////////////////////
class slist
{
public:
	slist();
	~slist();

	void push_back(int _NewValue);
	void push_front(int _NewValue);
	void pop_back();
	void pop_front();
	void clear();
	void insert(int _Index, int _NewNalue);

	int front();
	int back();

private:
	Node* create_node(int _NewValue);

private:
	Node* Head;
};

slist::slist()
	: Head(nullptr)
{
}

slist::~slist()
{
	clear();
}

void slist::push_back(int _NewValue)
{
	Node* NewNode = create_node(_NewValue);
	Node* CurNode = Head;
	if (Head != nullptr)
	{
		while (CurNode->Next != nullptr)
		{
			CurNode = CurNode->Next;
		}

		CurNode->Next = NewNode;
	}
	else
	{
		Head = NewNode;
	}
}

void slist::push_front(int _NewValue)
{
	Node* NewNode = create_node(_NewValue);
	if (Head == nullptr)
	{
		Head = NewNode;
	}
	else
	{
		NewNode->Next = Head;
		Head = NewNode;
	}
}

void slist::pop_back()
{
	Node* CurNode = Head;
	Node* PrevNode = Head;
	if (Head == nullptr)
	{
		return;
	}

	while (CurNode->Next != nullptr)
	{
		PrevNode = CurNode;
		CurNode = CurNode->Next;
	}

	if (Head == CurNode) // EdgeCase
	{
		Head = nullptr;
	}
	delete CurNode;
	CurNode = nullptr;
	PrevNode->Next = nullptr;
}

void slist::pop_front()
{
	if (Head == nullptr)
	{
		return;
	}

	Node* NextNode = Head->Next;
	delete Head;
	Head = NextNode;

}

void slist::clear()
{
	while (Head != nullptr)
	{
		pop_back();
	}
}

void slist::insert(int _Index, int _NewValue)
{
	if (Head == nullptr && _Index == 0)
	{
		push_front(_NewValue);
		return;
	}

	if (_Index == 0)
	{
		push_front(_NewValue);
		return;
	}

	Node* CurNode = Head;
	for (int i = 0; i < _Index-1; i++)
	{
		if (CurNode->Next == nullptr)
		{
			throw std::invalid_argument("Index가 list의 크기보다 큽니다.");
		}
		else
		{
			CurNode = CurNode->Next;
		}
	}

	Node* NewNode = create_node(_NewValue);
	NewNode->Next = CurNode->Next;
	CurNode->Next = NewNode;
}

int slist::front()
{
	if (Head == nullptr)
	{
		throw std::exception("빈 list 입니다.");
	}
	return Head->Value;
}

int slist::back()
{
	if (Head == nullptr)
	{
		throw std::exception("빈 list 입니다.");
	}
	Node* CurNode = Head;
	while (CurNode->Next != nullptr)
	{
		CurNode = CurNode->Next;
	}
	return CurNode->Value;
}

Node* slist::create_node(int _NewValue)
{
	Node* NewNode = new Node();
	NewNode->Value = _NewValue;
	return NewNode;
}

///////////////////////// Queue /////////////////////////
class Queue
{
public:
	Queue();
	~Queue();
	void push(int _NewValue);
	void pop();
	int front();
	int back();

	slist List;
};

Queue::Queue()
	: List()
{

}

Queue::~Queue()
{

}

int Queue::front()
{
	return List.front();
}

int Queue::back()
{
	return List.back();
}

void Queue::push(int _NewValue)
{
	List.push_back(_NewValue);
}

void Queue::pop()
{
	List.pop_front();
}

///////////////////////// Stack /////////////////////////
class Stack
{
public:
	void push(int _NewValue);
	void pop();
	int top();
	
private:
	slist List;
};

void Stack::push(int _NewValue)
{
	List.push_back(_NewValue);
}

void Stack::pop()
{
	List.pop_back();
}

int Stack::top()
{
	return List.back();
}

int main()
{
	slist List;
	List.insert(0, 2);

	try
	{
		List.insert(2, 2);
	}
	catch (std::exception ex)
	{
		List.insert(0, 3);
	}

}

0개의 댓글