싱글 링크드 리스트(Single Linked List) 구현

< 단순 링크드 리드트 구현을 위한 사전 단계(노드를 4개까지 추가 해보자) >


//메인게임 cpp 파일



//LinkedList 헤더 파일




//LinkedList cpp 파일

#include "LinkedList.h"

//노드 추가 함수
void LinkedList::add_node(int data)
{
//추가하는 노드 동적 할당
node* new_node = new node;
//추가 노드 초기화
new_node->data = data;
new_node->next = NULL;

//1.노드가 하나도 없는경우 (첫번째 노드 추가)
if (_head == NULL)
{
_head = new_node;
cout << _head->data << " -> ";
cout << "NULL" << endl;
}
//2.노드가 하나만 있는 경우 (두번째 노드 추가)
else if (_head->next == NULL)
{
_head->next = new_node;
cout << _head->data << " -> ";
cout << _head->next->data << " -> ";
cout << "NULL" << endl;
}
//3.노드가 두개 있는 경우(세번째 노드 추가)
else if (_head->next != NULL && _head->next->next == NULL)
{
_head->next->next = new_node;

cout << _head->data << " -> ";
cout << _head->next->data << " -> ";
cout << _head->next->next->data << " -> ";
cout << "NULL" << endl;
}
//4.노드가 세개인 경우(네번째 노드 추가)
else if (_head->next->next != NULL && _head->next->next->next == NULL)
{
_head->next->next->next = new_node;

cout << _head->data << " -> "; //1
cout << _head->next->data << " -> "; //2
cout << _head->next->next->data << " -> "; //3
cout << _head->next->next->next->data << " -> "; //4
cout << "NULL" << endl;
}
}





출력 결과




위와 같이 노드를 추가할 수 있다.
계속 노드를 추가 하기 위해 (세번째 노드 추가) (네번째 노드 추가) 부분을 반복문으로 바꿔주는 작업을 해주면 완성






< 단순 링크드 리드트 구현 >



//메인게임 cpp 파일




//LinkedList cpp 파일

#include "LinkedList.h"

//노드 추가 함수
void LinkedList::add_node(int data)
{
//추가하는 노드 동적 할당
node* new_node = new node;
//추가 노드 초기화
new_node->data = data;
new_node->next = NULL;

//1.노드가 하나도 없는경우 (첫번째 노드 추가)
if (_head == NULL)
{
_head = new_node;
cout << _head->data << " -> ";
cout << "NULL" << endl;
}
//2.노드가 하나만 있는 경우 (두번째 노드 추가)
else if (_head->next == NULL)
{
_head->next = new_node;
cout << _head->data << " -> ";
cout << _head->next->data << " -> ";
cout << "NULL" << endl;
}
/*
//3.노드가 두개 있는 경우(세번째 노드 추가)
else if (_head->next != NULL && _head->next->next == NULL)
{
_head->next->next = new_node;

cout << _head->data << " -> ";
cout << _head->next->data << " -> ";
cout << _head->next->next->data << " -> ";
cout << "NULL" << endl;
}
//4.노드가 세개인 경우(네번째 노드 추가)
else if (_head->next->next != NULL && _head->next->next->next == NULL)
{
_head->next->next->next = new_node;

cout << _head->data << " -> "; //1
cout << _head->next->data << " -> "; //2
cout << _head->next->next->data << " -> "; //3
cout << _head->next->next->next->data << " -> "; //4
cout << "NULL" << endl;
}
/*사전단계의 3번째 4번째 노드 추가 코드 주석처리*/
//노드가 두개 이상 있는 경우(세번째 이상 노드 계속 추가)
else if (_head->next != NULL) //두번째 노드가 있는 경우,
{
//반복을 위해 임시 노드 추가
node* temp_node;
temp_node = _head->next; //임시 노드에 두번째 노드가 들어간다
cout << _head->data << " -> ";
cout << _head->next->data << " -> ";

while (temp_node->next != NULL)
{
cout << temp_node->next->data << " -> ";
temp_node = temp_node->next;
if (temp_node->next == NULL)
{
temp_node->next = new_node;
cout << temp_node->next->data << " -> ";
cout << "NULL" << endl;
return;
}
}
if (temp_node->next == NULL)
{
temp_node->next = new_node;
cout << temp_node->next->data << " -> ";
cout << "NULL" << endl;
}
}
}


출력 결과



완료

댓글