본문 바로가기

Study/DataStructure

LinkedList

https://github.com/applejin0105/DataStructure/blob/e1c6dde7f506c76e6fc676a15f2f92fb46fda7cf/Header/LinkedList.h

 

DataStructure/Header/LinkedList.h at e1c6dde7f506c76e6fc676a15f2f92fb46fda7cf · applejin0105/DataStructure

자료구조 연습. Contribute to applejin0105/DataStructure development by creating an account on GitHub.

github.com

특징

  • 이중 연결 리스트 구조 (양방향 노드 연결).
  • 앞/뒤 삽입 모두 가능 (AddFront, AddBack).
  • C++ 스타일 반복자 지원 (begin(), end()) : Iterato
  • 중간 삽입 및 삭제에 효율적.
  • 포인터 기반.
AddFront(val) void 앞에 값 추가
AddBack(val) void 뒤에 값 추가
Insert(i, val) bool i번째 위치에 삽입
Remove(val) bool 해당 값 삭제
RemoveAt(i) bool i번째 인덱스 삭제
Get(i, out) bool i번째 값 조회
Contains(val) bool 값 존재 여부 확인
IndexOf(val, i) bool 값의 인덱스를 찾아 반환
Clear() void 전체 노드 삭제
Size() int 현재 크기 반환
IsEmpty() bool 비어있는지 확인
begin() / end() Iterator 반복자 반환

 

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

int main()
{
	LinkedList<int> list;

	cout << "[1] AddFront / AddBack Test\n";
	list.AddFront(10);
	list.AddBack(20);
	list.AddFront(5);
	for (auto val : list) cout << val << " ";
	cout << "\n";

	cout << "[2] Insert Test\n";
	list.Insert(1, 15);  // 중간 삽입
	for (auto val : list) cout << val << " ";
	cout << "\n";

	cout << "[3] Remove / RemoveAt Test\n";
	list.Remove(15);
	list.RemoveAt(0);
	for (auto val : list) cout << val << " ";
	cout << "\n";

	cout << "[4] Get / IndexOf / Contains Test\n";
	int val = -1, idx;
	if (list.Get(1, val)) cout << "Get[1]: " << val << "\n";
	if (list.IndexOf(20, idx)) cout << "IndexOf(20): " << idx << "\n";
	cout << "Contains 20? " << (list.Contains(20) ? "Yes" : "No") << "\n";

	cout << "[5] Clear Test\n";
	list.Clear();
	cout << "IsEmpty? " << (list.IsEmpty() ? "Yes" : "No") << "\n";
}

'Study > DataStructure' 카테고리의 다른 글

Tree(BST/Red-Black Tree)  (0) 2025.05.23
HashTable  (0) 2025.05.15
Queue  (0) 2025.04.21
Stack  (0) 2025.04.21
Array  (0) 2025.04.21