|자료구조| 1 | LinkedList_1 | 연결리스트(더미노드 없음)
컴퓨터/자료구조

|자료구조| 1 | LinkedList_1 | 연결리스트(더미노드 없음)

no title

연결리스트 구현

Visual Studio 솔루션 구성


구현부

헤더 : CrocusLinkedList.h

#pragma once
class CrocusLinkedList
{
private :
	int m_data = 0;
	CrocusLinkedList* next;

public :
	CrocusLinkedList();
	void SetData(int _data);
	int GetData();
	void Linking(CrocusLinkedList* _Node1);
	void SearchAll(CrocusLinkedList* head);
};

메소드 : CrocusLinkedList.cpp

#define _CRTDBG_MAP_ALLOC
#include "CrocusLinkedList.h"
#include <iostream>
#include <crtdbg.h>
using namespace std;
CrocusLinkedList::CrocusLinkedList() {
	this->next = nullptr;
}
void CrocusLinkedList::SetData(int _data) {
	m_data = _data;
}
int CrocusLinkedList::GetData() {
	return m_data;
}
void CrocusLinkedList::Linking(CrocusLinkedList* _Node) {
	next = _Node;
}
void CrocusLinkedList::SearchAll(CrocusLinkedList* head) {
	CrocusLinkedList* pos = head;
	while (pos->next != nullptr) {
		cout << pos->m_data << "\n";
		pos = pos->next;
	}
}

적용부

main.cpp

#include <iostream>
#include <algorithm>
#include <limits>
#include "CrocusLinkedList.h"
using namespace std;
/*
* D:\강의\마크다운\개인공부\알고리즘\연결리스트 자료구조
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\include
*/
int main() {
	CrocusLinkedList* head = nullptr;
	CrocusLinkedList* cur = nullptr;
	CrocusLinkedList* tail = nullptr;
	
	CrocusLinkedList Node;
	
	printf("-------------------------------\n1. Setdata(101)\n");
	Node.SetData(101);

	printf("-------------------------------\n2. Getdata\n");
	cout << Node.GetData() << '\n';
	
	int inputArr[10] = { 123,53,26,11,36,2,5,77, 12,90 };
	int N = 10;

	printf("-------------------------------\n3. Linking\n");
	while (N-- > 0) {
		CrocusLinkedList* newNode = nullptr;
		newNode = new CrocusLinkedList;
		newNode->SetData(inputArr[N]);

		printf("Setdata(%d)\n", inputArr[N]);
		if (head == nullptr) { head = newNode; }
		else { 
			tail->Linking(newNode); 
		}
		tail = newNode;
	}

	printf("-------------------------------\n4. SearchAll\n");
	head->SearchAll(head);
	cout << '\n';

	_CrtDumpMemoryLeaks();

	return 0;
}

결과