본문 바로가기

자료구조

[자료구조] 리스트 - 01. 링크드 리스트 < 예제 >

앞서 봤던 SLL을 구현하여 코드상에서 확인해보겠다.
<공부한것을 기록하기에 틀린게 있을 수 있습니다 . 틀린 점 말씀해주시면 감사하겠습니다>

01 . 헤더 파일

//SLL.h

#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include<stdio.h>
#include<stdlib.h>

typedef int ElementType;

typedef struct tagNode
{
	ElementType Data;
	struct tagNode* NextNode;
}Node;

//생성
Node* SLL_CreateNode(ElementType _Data);
//소멸
void SLL_DestroyNode(Node* Node);
//추가
void SLL_AppendNode(Node** _Head, Node* _NewNode);
//앞에 삽입
void SLL_InsertNodeAtFront(Node* Head, Node* NewNode);
//뒤에 삽입
void SLL_InsertNodeAtBackward(Node** List, Node* Current, Node* NewNode);
//헤드에 삽입
void SLL_InsertHead(Node** Head, Node* NewNode);
//삭제
void SLL_DeleteNode(Node** Head, Node* Remove);
//모든 노드 삭제
void SLL_DeleteAllNode(Node** List);
//노드 탐색
Node* SLL_SearchNodeAt(Node* Head, int location);
//노드 수 세기
int SLL_GetNodeCount(Node* Head);
//노드 출력
void SLL_PrintNode(Node* Head);


#endif

02 . 함수

#include "SLL.h"

//생성
Node* SLL_CreateNode(ElementType _data)
{
	Node* NewNode = (Node*)malloc(sizeof(Node));
	NewNode->Data = _data;
	NewNode->NextNode = NULL;
	return NewNode;
}

//소멸
void SLL_DestroyNode(Node* Node)
{
	free(Node);
}

//추가
void SLL_AppendNode(Node** Head,Node* NewNode)
{
	if ((*Head) == NULL)	
	{
		(*Head) = NewNode;
	}
	else
	{
		Node* Tail = (*Head);
		while (Tail->NextNode != NULL)
		{
			Tail = Tail->NextNode;
		}
		Tail->NextNode = NewNode;
	}
}

//앞에 삽입
void SLL_InsertNodeAtFront(Node*Current, Node* NewNode)
{
	NewNode->NextNode=Current->NextNode;
	Current->NextNode = NewNode;
}

//뒤에 삽입
void SLL_InsertNodeAtBackward(Node**Head,Node*TargetNode,Node*NewNode)
{
	if ((*Head) == TargetNode)
	{
		SLL_InsertHead(Head, NewNode);
	}
	else
	{
		Node* tempNode = (*Head);
		while (tempNode->NextNode != TargetNode)
		{
			tempNode = tempNode->NextNode;
			if (tempNode->NextNode == NULL)//타겟 노드가 없다면
			{
				printf("%s\n", "타겟노드가 존재하지 않는다");
				return;
			}
		}
		NewNode->NextNode = tempNode->NextNode;
		tempNode->NextNode = NewNode;
	}
}
//헤드 삽입
void SLL_InsertHead(Node**Head ,Node* NewNode)
{
	if ((*Head) == NULL)
	{
		(*Head) = NewNode;
	}
	else
	{
		NewNode->NextNode = (*Head);
		(*Head) = NewNode;
	}
}

//삭제
void SLL_DeleteNode(Node**Head,Node* Current)
{
	Node* tempNode = (*Head);
	while (tempNode->NextNode != Current)
	{
		tempNode = tempNode->NextNode;
	}
	tempNode->NextNode = Current->NextNode;
	Current->NextNode = NULL;
	SLL_DestroyNode(Current);
}

//모든 노드 삭제
void SLL_DeleteAllNode(Node** List)
{
	Node *tempNode = *List;
	Node* DeleteNode = NULL;

	while (tempNode!= NULL)
	{
		DeleteNode = tempNode;
		//printf("%d\n",tempNode->Data);
		tempNode = tempNode->NextNode;
		SLL_DestroyNode(DeleteNode);
	}
	*List = NULL;
	printf("%s\n", "모두삭제");
	//{
	//	Node* NextNode = tempNode->NextNode;
	//	SLL_DestroyNode(tempNode);
	//	tempNode = NextNode;
	//}
	////SLL_AppendNode(List, SLL_CreateNode(3));
	//
}

//노드 탐색
Node* SLL_SearchNodeAt(Node* List, int Count)
{
	Node* Current = List;
	while (--Count >= 0)
	{
		Current=Current->NextNode;
		if (Current == NULL)
		{
			printf("%s\n", "리스트의 범위를 벗어난 카운트입니다");
			return NULL;
		}
	}
	return Current;
}

//노드 수 세기
int SLL_GetNodeCount(Node* Head)
{
	int count = 0;
	Node* Tail = Head;
	while (Tail!=NULL)
	{
		Tail = Tail->NextNode;
		count++;
	}
	return count;
}

//출력
void SLL_PrintNode (Node *Head)
{
	int count = SLL_GetNodeCount(Head);
	for (int i = 0; i < count; i++)
	{
		Node* Curr = SLL_SearchNodeAt(Head, i);
		printf("%d\n", Curr->Data);
	}
    printf("%s\n","출력 종료");
}

03 . 사용

#include "SLL.h";

int main()
{
	int i = 0;
	int count = 0;
	Node* List = NULL;
	Node* Current = NULL;
	Node* NewNode = NULL;

	//노드 생성 및 추가
	for ( i = 0; i < 5; i++)
	{
		SLL_AppendNode(&List, SLL_CreateNode(i));
	}
	//출력
	SLL_PrintNode(List);
	//앞에 노드 삽입
	SLL_InsertNodeAtFront(SLL_SearchNodeAt(List, 3), SLL_CreateNode(100));
	//출력
	SLL_PrintNode(List);
	//뒤에 노드 삽입
	SLL_InsertNodeAtBackward(&List,SLL_SearchNodeAt(List, 1), SLL_CreateNode(120));
	//출력
	SLL_PrintNode(List);
	//헤드 삽입
	SLL_InsertHead(&List, SLL_CreateNode(333));
	//삭제
	SLL_DeleteAllNode(&List);

	//노드 생성 및 추가
	for (i = 0; i < 3; i++)
	{
		SLL_AppendNode(&List, SLL_CreateNode(i));
	}
	//출력
	SLL_PrintNode(List);
	Node* temp = SLL_SearchNodeAt(List,2);
	printf("%d\n",temp->Data);
}