-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list.cpp
More file actions
77 lines (71 loc) · 1.35 KB
/
linked_list.cpp
File metadata and controls
77 lines (71 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "linked_list.h"
// TODO: LinkedList 클래스 구현 작성
LinkedList::LinkedList()
{
head_ = nullptr;
size_ = 0;
}
LinkedList::~LinkedList()
{
for(int i = 0; i < size_; i++)
{
Node* temp = head_;
head_ = head_->next_;
delete temp;
}
}
void LinkedList::insert(int index, int value)
{
Node* newNode = new Node(value);
Node* prev = nullptr;
Node* temp = head_;
for(int i = 0; i < index; i++)
{
prev = temp;
temp = temp->next_;
}
if (index != 0)
{
prev->next_ = newNode;
}
else
head_ = newNode;
newNode->next_ = temp;
size_++;
}
int LinkedList::get(int index)
{
Node* temp = head_;
for (int i = 0; i < index; i++)
{
temp = temp->next_;
}
return temp->value_;
}
void LinkedList::remove(int index)
{
Node* prev = head_;
Node* temp = head_;
// temp를 제거 대상 이전노드로 이동
for (int i = 0; i < index; i++)
{
prev = temp;
temp = temp->next_;
}
if(index == 0)
head_ = head_->next_;
else
prev->next_ = temp->next_;
delete temp;
size_--;
}
void LinkedList::print()
{
Node* temp = head_;
for(int i = 0; i < size_; i++)
{
std::cout << temp->value_ << " ";
temp = temp->next_;
}
std::cout << std::endl;
}