컴퓨터 프로그래밍 공부/자료구조와 알고리즘

원형 리스트

게임 개발 2024. 4. 17. 21:52
static int ListNum = -1;



class List
{
public:

	List* CurPtr = this;
	List* BeforePtr = nullptr;
	List* AfterPtr = nullptr;

	int Index = 0;

	int Value = 0;


	bool IsDummy = false;


	~List();
};

// 빈 List 생성
void InitialList();
// 원하는 만큼의 수와 해당 값으로의 초기화된 List 생성
void InitialList(int MaxIndex, int Value);


// 부분 삭제
void DeleteIList(int iter, List* InDeleteList);
// 전체 삭제
void DeleteAllList();


// 삽입
void Push_Front(int InValue, List* CurList);


// 특정 인덱스 조회
void Iter(int iter, List* OutiterList, bool WantValue);
// 전체 조회
void Inquiry();
#include "LikedList.h"
#include <iostream>

List* Dummy = new List;
List* Tail = new List;
List* Head = new List;


int main()
{
	
	InitialList(5, 2);

	std::cout <<  Dummy->BeforePtr->BeforePtr->Value << std::endl;

	Inquiry();
	
	List* OutiterList{};
	Iter(3, OutiterList, true);
	
	DeleteAllList();

	return 0;
}

void InitialList()
{
	if (Tail->AfterPtr == nullptr)
	{

		Dummy->IsDummy = true;

		// 꼬리 뒤에 머리, 꼬리 앞에 더미
		Tail->CurPtr = Tail;
		Tail->AfterPtr = Dummy;
		Tail->BeforePtr = Head;

		// 머리 앞에 꼬리, 머리 뒤에 더미
		Head->CurPtr = Head;
		Head->AfterPtr = Tail;
		Head->BeforePtr = Dummy;

		// 더미 앞에 머리, 더미 뒤에 꼬리
		Dummy->CurPtr = Dummy;
		Dummy->AfterPtr = Head;
		Dummy->BeforePtr = Tail;

		ListNum;
	}
}

void InitialList(int MaxIndex, int Value)
{
	if (Tail->AfterPtr == NULL)
	{

		Dummy->IsDummy = true;

		// 꼬리 뒤에 머리, 꼬리 앞에 더미
		Tail->CurPtr = Tail;
		Tail->AfterPtr = Dummy;
		Tail->BeforePtr = Head;

		// 머리 앞에 꼬리, 머리 뒤에 더미
		Head->CurPtr = Head;
		Head->AfterPtr = Tail;
		Head->BeforePtr = Dummy;

		// 더미 앞에 머리, 더미 뒤에 꼬리
		Dummy->CurPtr = Dummy;
		Dummy->AfterPtr = Head;
		Dummy->BeforePtr = Tail;

		ListNum;
	}

	// 원하는 인덱스 만큼 추가하고
	// 해당 Value로 초기화 하기
	while (MaxIndex > ++ListNum)
	{
		List* NewList = new List;
		List* TempList;

		if (Tail->AfterPtr == Dummy)
		{
			// 새 리스트 앞에 더미, 새 리스트 뒤에 꼬리
			NewList->AfterPtr = Dummy;
			NewList->BeforePtr = Tail;

			// 더미 뒤에 새 리스트
			Dummy->BeforePtr = NewList;

			// 꼬리 앞에 새 리스트
			Tail->AfterPtr = NewList;

			// 인덱스 부여 및 값 초기화
			NewList->Index = ListNum;
			NewList->Value = Value;
		}

		else
		{
			// 임시 리스트는 기존 더미 뒤의 리스트(값이 있음)
			TempList = Dummy->BeforePtr;

			// 새 리스트 앞에 더미, 새 리스트 뒤에 기존 리스트
			NewList->AfterPtr = Dummy;
			NewList->BeforePtr = TempList;

			// 더미 뒤에 새 리스트
			Dummy->BeforePtr = NewList;

			// 기존 리스트 앞에 새 리스트
			TempList->AfterPtr = NewList;


			// 새 리스트 현재 인덱스 추가, 현재 값 추가
			NewList->Index = ListNum;
			NewList->Value = Value;
		}
		
	}
}

void DeleteIList(int iter, List* InDeleteList)
{
	if (InDeleteList == Tail ||
		InDeleteList == Head ||
		InDeleteList == Dummy)
	{
		std::cout << "해당 리스트는 삭제할 수 없습니다. " << std::endl;
		return;
	}

	// 연결해 줄 삭제 리스트 뒷 리스트
	List* DeleteListAtferList = InDeleteList->BeforePtr;
	
	// 당길 인덱스 담아두는 임시 값들
	List* IndexBeforeList = Tail->AfterPtr;
	List* IndexTempList;
	
	// 뒷 리스트들 인덱스 변화
	while (IndexBeforeList != InDeleteList)
	{
		// 인덱스 땡겨주기
		IndexBeforeList->Index--;
		// 당긴 인덱스 앞의 값 가져오기
		IndexTempList = IndexBeforeList->AfterPtr;
		// 당긴 인덱스 앞의 값 담아주기
		IndexBeforeList = IndexTempList;
	}

	// 전 리스트에 삭제리스트 뒷 리스트 값 연결
	DeleteListAtferList->BeforePtr = InDeleteList->BeforePtr;

	ListNum--;
	free(InDeleteList);
	

	std::cout << "해당 인덱스가 비워졌습니다." << std::endl;
}

void DeleteAllList()
{
	List* DeleteList;
	List* TempList;
	while (Tail->AfterPtr != Dummy)
	{
		// 삭제될 리스트는 꼬리 앞의 리스트
		DeleteList = Tail->AfterPtr;
		
		// 삭제 앞의 리스트의 값
		TempList = DeleteList->AfterPtr;
		// 꼬리 앞에 전전 리스트 연결
		// 전전 리스트 뒤에 꼬리 연결
		Tail->AfterPtr = TempList;
		TempList->BeforePtr = Tail;

		ListNum--;
		free(DeleteList);
	}

	std::cout << "리스트 내부의 모든 인덱스가 비워졌습니다." << std::endl;

}

List::~List()
{
	delete this;
}


void Push_Front(int InValue, List* CurList)
{
	List* newList = new List;
	
	// 새 리스트에 인덱스 부여
	ListNum++;
	newList->Index = ListNum;

	// 새 리스트에 값 부여
	newList->Value = InValue;

	// 새 리스트 뒤에 꼬리 연결
	newList->AfterPtr = Tail;
	// 새 리스트 앞에 현재 리스트 연결
	newList->BeforePtr = CurList;
	
	// 현재 리스트 뒤에 새 리스트 연결
	CurList->AfterPtr = newList;
}

void Iter(int iter, List* iterList, bool WantValue)
{
	List* CurList = Tail->AfterPtr;
	List* TempList;
	while (CurList->Index != iter)
	{
		if (CurList->IsDummy)
		{
			std::cout << "해당 인덱스의 리스트는 없는 정보입니다.";
			break;
		}

		TempList = CurList->AfterPtr;
		CurList = TempList;
	}

	if (WantValue)
	{
		std::cout << "해당 리스트의 값 : " << CurList->Value << std::endl;
	}

	

	iterList = CurList;

}

void Inquiry()
{
	List* CurList = Tail->AfterPtr;
	List* TempList;
	while (CurList != Dummy)
	{
		std::cout << CurList->Value << " ";
		TempList = CurList->AfterPtr;
		CurList = TempList;
	}

	
}

'컴퓨터 프로그래밍 공부 > 자료구조와 알고리즘' 카테고리의 다른 글

삽입 정렬 구현  (0) 2024.04.21
선택 정렬 구현  (0) 2024.04.18
연결 리스트(Linked List)  (0) 2024.03.14
리스트 List(배열 기반)  (0) 2024.02.06
트리 순회  (0) 2024.02.06