Sorted List using Doubly Linked List
- μ΄μ λ¨λ°©ν₯ linked list μμλ NodeType μ next λ§ μ μ©λμ΄ μλ λ°λ©΄ doubly linked list λ back, next 2 κ°μ§κ° μ μ©λμ΄ μ΄μ ν¬μΈν°λ μ μ₯νλ ꡬ쑰λ₯Ό κ°λλ€.
FindItem
- InsertItem, DeleteItem, RetrieveItem λͺ¨λ νΉμ item μ μ°Ύλ κ³Όμ μ΄ νμνκΈ° λλ¬Έμ FindItem ν¨μλ₯Ό μ μ©νλ€.(return location, found)
- FindItem precondition : 리μ€νΈλ λΉμ΄μμ§ μλλ€.
template<class ItemType>
void FindItem(NodeType<ItemType>* listData, ItemType item,
NodeType<ItemType>* &location, bool &found){
bool moreToSearch = true;
location = listData;
found = false;
while(moreToSearch && !found){
if(item < location->info)
moreToSearch = false;
else if(item == location->info)
found = true;
else{
// location = location->next 맨 λμ λ μ μκΈ°μ μλλ‘ λ³κ²½
// moreToSearch = location!=NULL;
if(location->next == NULL)
moreToSearch = false;
else
location = location->next;
}
}
}
- μννλ©° item λ³΄λ€ μλ€λ©΄ λ€μμΌλ‘, κ°λ€λ©΄ found flag λ₯Ό ν΅ν΄μ while λ¬Έ νμΆ
- μ λ ¬λμ΄ μλ μνμ΄κΈ° λλ¬Έμ item λ³΄λ€ μννλ κ°μ΄ λ ν¬λ€λ©΄ item μ λ§λ κ°μ΄ μλ κ²μ΄λ―λ‘ νμν νμκ° μλ€.
- location, found λ &λ‘ λ°μ pass by reference λ‘ ν¨μ μμμ μ§μ λλ κ°μ κ·Έλλ‘ λ°μμ¬ μ μλλ‘ νλ€.
InsertItem
template<class ItemType>
void InsertItem(ItemType item){
NodeType<ItemType>* newNode;
NodeType<ItemType>* location;
bool = found;
newNode = new NodeType<ItemType>;
newNode->info = item;
if(listData != NULL){ // listκ° μ°¨ μλ κ²½μ°
FindItem(listData, item, location, found);
if(location->info > item){ // itemμ΄ μ€κ°μ μμΉν κ²½μ°
newNode->back = location->back;
newNode->next = location;
if(location != listData){
location->back->next = newNode;
}
location->back = newNode;
}
else{ // itemμ΄ endμ μμΉν κ²½μ°
newNode->back = location;
location->next = newNode;
newNode->next = NULL;
}
}
else{ // listκ° λΉ κ²½μ°
listData = newNode;
newNode->next = NULL;
newNode->back = NULL;
}
length++;
}
- μ΄μ , λ€μ λ
Έλλ₯Ό κ°λ¦¬ν€λ ν¬μΈν°λ₯Ό λ³κ²½ν λ 보μ ν κ°μ΄ μ¬λΌμ§μ§ μλλ‘ μμμ μ μν΄μΌ νλ€.
- newNodeβback = locationβback μ°μ
- locationβback = newNode λ€μ
Headers and Trailers
- μμ, λ μ§μ μμ μμΈκ° λ°μνκΈ° λλ¬Έμ Header μ Trailer λ₯Ό μ¬μ©ν΄μ μμΈλ₯Ό λ°°μ νλ λ°©μμ μ¬μ©νκΈ°λ νλ€.
- Idea : 리μ€νΈμ μ λ λ¨μ μ λ μ½μ
, μμ λ₯Ό νμ§ μλλ‘ λ§λ λ€.
- Method : μ λ λ¨μ κ°λ₯ν κ°μ λ²μλ₯Ό μ΄κ³Όν dummy λ
Έλλ₯Ό λ§λ λ€.
- Header : κ°λ₯ν 리μ€νΈ κ°λ³΄λ€ λ μμ κ°μ ν¬ν¨νλ€.
- Trailer : κ°λ₯ν 리μ€νΈ κ°λ³΄λ€ λ ν° κ°μ ν¬ν¨νλ€.
Shallow Copy / Deep Copy
- Shallow Copy : ν΄λμ€ λ©€λ² λ°μ΄ν°λ§ 볡μ¬νκ³ , κ°λ¦¬ν€κ³ μλ λ°μ΄ν°λ 볡μ¬νμ§ μλλ€. μ¦, κ°λ¦¬ν€κ³ μλ λ°μ΄ν°λ λ³Έλ ν΄λμ€ κ°μ²΄μ 곡μ νλ€.
- Deep Copy : ν΄λμ€ λ©€λ² λ°μ΄ν° λΏλ§ μλλΌ κ°λ¦¬ν€κ³ μλ λ°μ΄ν°λ λ°λ‘ μ μ₯νλ€. λ³Έλ ν΄λμ€ κ°μ²΄μ λ°μ΄ν°μ μμΉμ λ€λ₯Έ κ³³μ λ°μ΄ν°λ₯Ό μ μ₯νκ³ κ°λ¦¬ν¨λ€.

Shallow Copy
- λ€μμ κ²½μ° μ΄λ»κ² λ κΉ?
void Func(StackType<ItemType> SomeStack){
ItemType item;
SomeStack.Pop(item);
}
// main
StackType<int> MyStack;
Func(MyStack);
- Func μ νλΌλ―Έν°λ‘ μ λ¬λ μ€νμ pass by value λ‘ μ λ¬λλ€.
- λ€μμ κ²½μ°μλ Shallow Copy κ° μ μ©λλ€.

- κ²°κ³Όλ μ κ·Έλ¦Όκ³Ό κ°λ€.
- μμ
- MyStack μ μ λ¬ν΄ SomeStack μΌλ‘ Shallow Copy λ₯Ό νλ€.
- μ΄λ SomeStack μ top μ MyStack κ³Ό κ°μ μμΉμ κ°μ κ°λ¦¬ν¨λ€.
- μ΄λ SomeStack.Pop(item) μΌλ‘ μΈν΄ SomeStack μ Pop μ νκ³ 6000 μ μ£Όμκ°μ κ°λ¦¬ν¨λ€.
- νμ§λ§, MyStack μ κ²½μ° μ¬μ ν 7000 μ£Όμκ°μ κ°λ¦¬ν€κ³ μκ² λλ€.
Deep Copy
- κΈ°λ³Έ μμ±μλ₯Ό μ¬μ©ν κ²½μ° pass by value λ‘ μμ±λλ€.
- ν¬μΈν°κ° λμ λ°μ΄ν°λ₯Ό κ°λ¦¬ν¬ λ pass by value μ¦, Shallow Copy μ κ²½μ° ν¬μΈν°κ° κ°λ¦¬ν€λ κ°μ 볡μ¬νμ§ λͺ»νλ―λ‘ μμ κ°μ λ¬Έμ κ° λ°μνλ€.
- λ€λ₯Έ λ©λͺ¨λ¦¬ 곡κ°μμ λμ λ°μ΄ν°μ Deep Copy λ₯Ό λ§λλ copy constructor λ₯Ό ν΄λμ€μ λ§λ€μ΄ μ¬μ©ν΄μΌ νλ€.
- Copy constructor
- Pass by reference λ₯Ό μ¬μ©νλ€.
- Copy constructor λ₯Ό λ§λ€μ΄ λ κ²½μ°, pass by value λ‘ λ³΅μ¬λ₯Ό νλλΌλ μ묡μ μΌλ‘ copy constructor κ° νΈμΆλμ΄ λ³΅μ¬λλ€.
template<class ItemType>
class StackType{
public:
StackType(){
cout << "Default Constructor" << endl;
};
StackType(const StackType<ItemType>& anotherStack);
// Copy constructor
{
cout << "Copy Constructor" << endl;
.
.
.
}
~StackType();
private:
NodeType<ItemType>* topPtr;
}
int main(){
StackType<int> SomeStack;
SomeStack.Push(1);
StackType<int> AnotherStack(SomeStack);
}
// Result
// Default Constructor
// Copy Constructor
- μ νλΌλ―Έν°λ₯Ό const λ‘ μ§μ ν κΉ?
- const λ‘ νλΌλ―Έν°λ₯Ό μ§μ νλ©΄ ν΄λΉ νλΌλ―Έν°λ λ³νμ κ°ν μ μλ€.
- νμ§λ§ copy constructor μ κ²½μ° κ°μ λ³νμν€μ§ μκ³ λ³΅μ¬λ₯Ό νλ μν μ νκΈ° λλ¬Έμ const λ‘ λͺ
ννκ² λͺ
μνλ€.
- Copy constructor λ λ€μμ 3 κ°μ§ μν©μμ μ묡μ μΌλ‘ νΈμΆλλ νΉμν λ©€λ² ν¨μμ΄λ€.
- κ°μ²΄ νλΌλ―Έν°λ₯Ό value λ‘ μ λ¬νλ κ²½μ°
- μ μΈμμ κ°μ²΄ λ³μλ₯Ό μ΄κΈ°ννλ κ²½μ°
- ν¨μμ λ°νκ°μΌλ‘ κ°μ²΄λ₯Ό λ°ννλ κ²½μ°
Assignment Operator =
- λ§μ½, ν΄λμ€κ° λμ λ°μ΄ν°λ₯Ό κ°λ¦¬ν€λ λ°μ΄ν° λ©€λ² ν¬μΈν°λ₯Ό κ°μ§κ³ μλ€λ©΄ λμ λ°μ΄ν°μ Deep Copy λ₯Ό λ§λλ ν λΉ μ°μ°μλ₯Ό μμ±ν΄μΌ νλ€.
template<class ItemType>
class StackType{
public:
StackType();
StackType(const StackType<ItemType>& anotherStack);
// Copy constructor
void operator= (const StackType<itemType>& originalStack);
~StackType();
private:
NodeType<ItemType>* topPtr;
}