상세 컨텐츠

본문 제목

Doubly Linked List 02

자료구조&알고리즘/C++

by geminanolja 2024. 12. 11. 09:00

본문

 

Noded 삽입 삭제 코드 구현

노드 삽입 그림!

 

#pragma once
#include <iostream>
using namespace std;

class Node
{
	//typedef int T; Old style
	using T = int;
public :

	//constructor :int타입의 변수를 받아서 T data에 넣어주고
	//prev & next는  nullptr로 초기화
	Node(int data) : data(data), prev(nullptr), next(nullptr) { }

public:
	T data;
	Node* prev;
	Node* next;
};


class List
{
public:
	List()
	{
		//더미 노드 두개를 앞뒤로 만들기
		_head = new Node(0);
		_tail = new Node(0);
		_head->next = _tail;
		_tail->prev = _head;
	}
	~List()
	{
		Node* node = _head;
		while (node)//null check
		{
			Node* deletedNode = node;
			node = node->next;
			delete deletedNode;
		}
	}

	Node* GetNode(int index)
	{
		Node* node = _head->next;
		if (node == _tail) return nullptr;//[head] [tail]이면 아무것도 없음
		for (int i = 0; i < index; i++)
		{
			if (node == _tail->prev) return nullptr;//node가 끝까지 다옴
			node = node->next;
		}
		cout << "index " << index << "번에 있는 값은 ?" << node->data << endl;
		return node;
		
	}

	Node* AddInTheMiddle(int ind, int data)
	{
		Node* indexNode = GetNode(ind);
		if (indexNode == nullptr) return nullptr;
		Node* addedNode = new Node(data);
		addedNode->next=indexNode->next;
		addedNode->prev=indexNode;

		// indexNode 다음 노드의 prev를 새 노드로 설정 (만약 존재한다면)
		if (indexNode->next != nullptr)
			indexNode->next->prev = addedNode;

		indexNode->next = addedNode;
		return addedNode;
	}

	Node* AddAtHead(int data)
	{
		Node* node = new Node(data);
		Node* nextNode = _head->next;

		node->next = nextNode;
		nextNode->prev = node;
		_head->next = node;
		node->prev = _head;

		return node;
	}
	void Print()
	{
		Node* node = _head->next;
		while (node != _tail)
		{
			cout << "node data :" << node->data;
			cout << " node address : " << node;
			cout << " node prev :" << node->prev;
			cout << " node next :" << node->next;
			node = node->next;
			cout << endl;
		}

	}

	Node* AddAtTail(int data)
	{
		Node* node = new Node(data);
		Node* prevNode = _tail->prev;
		node->prev = prevNode;
		prevNode->next = node;
		_tail->prev = node;
		node->next = _tail;

		return node;
	}

private:
	Node* _head = nullptr;
	Node* _tail = nullptr;

};

 

 

#include <iostream>
#include "List.h"
using namespace std;


int main()
{
    cout << "Hello World!\n";
    List list01;
    list01.AddAtHead(10);
    list01.AddAtHead(20);
    list01.AddAtHead(30);
    list01.AddAtTail(5);

    cout << "Contexts" << endl;
    list01.Print();
    cout << endl;

    List* DynamicList = new List();
    DynamicList->AddAtHead(40);
    DynamicList->AddAtHead(50);
    DynamicList->AddAtTail(80);
    DynamicList->AddAtTail(35);
    cout << "DynamicList Contexts" << endl;
    DynamicList->Print();
    cout << endl;

    DynamicList->GetNode(2);
    DynamicList->AddInTheMiddle(2, 22);
    DynamicList->Print();



    delete DynamicList;
    return 0;
}

Result

 

'자료구조&알고리즘 > C++' 카테고리의 다른 글

vector 벡터  (0) 2024.12.14
자료구조 별 Big O 시간 복잡도  (1) 2024.12.13
Big O notation  (2) 2024.12.13
Doubly linked List 03 Delete Node  (2) 2024.12.11
Doubly Linked List  (2) 2024.12.11

관련글 더보기