• toc {:toc}

Review

  • 같은 νƒ€μž…μ˜ μ•„μ΄ν…œμ΄ μˆœμ„œλ₯Ό μ§€λ‹Œλ‹Œ κ·Έλ£Ή. ν•˜λ‚˜μ˜ μž…κ΅¬λ‘œ λ“€μ–΄κ°€κ³  λ‚˜μ˜€λŠ” ꡬ쑰

  • LIFO(Last in First out) ꡬ쑰

  • Queue

  • 같은 νƒ€μž…μ˜ μ•„μ΄ν…œμ˜ μˆœμ„œκ°€ μžˆλŠ” κ·Έλ£Ή. λ’€μ—μ„œ λ“€μ–΄κ°€κ³  μ•žμœΌλ‘œ λ‚˜μ˜€λŠ” ꡬ쑰

  • FIFO(First in First out) ꡬ쑰

  • List vs Array

  • 맀우 μœ μ‚¬ν•˜λ‚˜, 같은 κ°œλ…μ€ μ•„λ‹ˆλ‹€.

  • Array : ꡬ체적인 데이터 νƒ€μž…

    • λ‹€λ₯Έ ADT듀을 κ΅¬ν˜„ν•˜λŠ”λ° μ‚¬μš©λ  수 μžˆλ‹€.
  • List : 좔상적인 데이터 νƒ€μž…(Abstract Data Type, ADT)

    • κ°œλ…μ μΈ 데이터 νƒ€μž…
    • arrayλ₯Ό μ‚¬μš©ν•˜μ§€ μ•Šκ³  κ΅¬ν˜„ν•  수 μžˆλ‹€.

Stack Implementation(Linked List)

  • ADT μž₯점 : κ΅¬ν˜„μ˜ 방식이 변경될 수 μžˆλ‹€. ADT κ°œλ…μ„ 잘 κ΅¬ν˜„ν•˜κΈ°λ§Œ ν•˜λ©΄ 되기 λ•Œλ¬Έμ΄λ‹€.

  • μŠ€νƒμ˜ 동적 λ°°μ—΄ κ΅¬ν˜„μ˜ 단점 - μŠ€νƒμ˜ μ΅œλŒ€ 크기λ₯Ό νŒŒλΌλ―Έν„°λ‘œ 전달받아 생성돼야 ν•œλ‹€.

    • μŠ€νƒμ˜ 크기λ₯Ό 쀑간에 λ™μ μœΌλ‘œ λ³€ν™˜ν•  수 μ—†λ‹€.
  • μŠ€νƒμ˜ 크기λ₯Ό νŒŒλΌλ―Έν„°λ‘œ λ°›μ§€ μ•Šκ³  push ν•  λ•Œ μŠ€νƒ μš”μ†Œμ˜ 곡간을 λ™μ μœΌλ‘œ ν• λ‹Ήν•  수 μžˆλ‹€.

    • μ–΄λ–»κ²Œ?
    • Linked Structure!
  • Operation β€˜new’

    • free store, heap을 이용 κ°€λŠ₯ν•˜λ‹€λ©΄ newλŠ” μš”μ²­λœ 객체λ₯Ό ν• λ‹Ήν•˜κ³  ν• λ‹Ήλœ μ£Όμ†Œλ₯Ό λ°˜ν™˜ν•œλ‹€.
class StackType{
public:
	StackType();
	~StackType();
	bool IsEmpty();
	bool IsFull();
	void Push(ItemType item);
	void Pop();
	ItemType Top();
private:
	NodeType* topPtr;
}
 
struct NodeType{
	ItemType info;
	NodeType* next;
}
 
newItem = 'B';
// Push
if (IsFull())
	throw FullStack();
NodeType* location;
location = new NodeType<char>  // μƒˆλ‘œ λ“€μ–΄μ˜¬ μœ„μΉ˜ 동적 ν• λ‹Ή
location->info = newItem;      // info, next에 정보, μœ„μΉ˜ μ§€μ •
location->next = topPtr;
topPtr = location;             // top location μˆ˜μ •
 
// Pop
if(IsEmpty())
	throw EmptyStack();
else{
	NodeType* tempPtr;
	tempPtr = topPtr;
	topPtr = topPtr->next;
	delete tempPtr;
}
 
// Top
if (IsEmpty())
	throw EmptyStack();
else
	return topPtr->info;
  • Linked List

  • NodeType : 값을 지닐 ItemTypeκ³Ό λ‹€μŒ NodeType을 λ‚˜νƒ€λ‚΄λŠ” 포인터λ₯Ό κ°€μ§„λ‹€. β‡’ info(data) + next(pointer)

  • Comparing stack implementations

OperationArray ImplementationLinked Implementation
Class constructorO(1) top을 -1둜 μ„€μ •O(1) Null NodeType 포인터 생성
Make EmptyO(1) Indexλ₯Ό 0으둜 μ„€μ •O(N) μˆœνšŒν•˜λ©° ν• λ‹Ή ν•΄μ œ
IsFullO(1) top == Max인지 확인O(1) heap λ©”λͺ¨λ¦¬ 였λ₯˜ 확인
IsEmptyO(1) top == 0인지 확인O(1) 첫 μ‹œμž‘ location이 Null인지 확인
PushO(1) 맨 μœ„μ— λΆ™μ—¬μ£ΌκΈ°O(1) μƒˆλ‘œμš΄ λ…Έλ“œ 생성, top λ…Έλ“œλ‘œ μ—°κ²°
PopO(1) 맨 μœ„ κ°’ λΉΌκΈ°O(1) 맨 처음 top κ°’ λΉΌκΈ°
DestructorO(1) λ°°μ—΄ ν• λ‹Ή ν•΄μ œO(N) μˆœνšŒν•˜λ©° ν• λ‹Ή ν•΄μ œ
  • Why is a destructor needed? 동적 ν• λ‹Ή(new)λ₯Ό 톡해 ν• λ‹Ήλœ heap의 λ©”λͺ¨λ¦¬λŠ” main ν•¨μˆ˜κ°€ λλ‚˜λ”λΌλ„ μžλ™μœΌλ‘œ ν• λ‹Ή ν•΄μ œλ˜μ§€ μ•ŠλŠ”λ‹€. λͺ…μ‹œμ μœΌλ‘œ 할당을 ν•΄μ œν•΄μ•Ό ν•œλ‹€. λ§Œμ•½ λͺ…μ‹œμ μœΌλ‘œ ν• λ‹Ή ν•΄μ œν•˜μ§€ μ•Šκ³  ν•¨μˆ˜κ°€ λλ‚˜κ²Œ λ˜λŠ” 경우 포인터가 κ°€λ¦¬ν‚€λŠ” λ…Έλ“œλŠ” 할당이 된 μ±„λ‘œ λ‚­λΉ„λœλ‹€.

Queue Implementation(Linked List)

class QueType{
public:
	QueType();
	~QueType();
	bool IsEmpty();
	bool IsFull();
	void Enqueue(ItemType newItem);
	void Dequeue(ItemType& item);
	void MakeEmpty();
private:
	NodeType<ItemType>* qFront = NULL;
	NodeType<ItemType>* qRear = NULL;
}
 
// Enqueue
NodeType<ItemType>* ptr;
ptr = new NodeType<ItemType>;
ptr->info = newItem;
ptr->next = NULL;
if(qRear == NULL)      // 큐가 λΉ„μ–΄μžˆμ„ λ•Œ 
	qFront = ptr;      // Front μ„€μ •
else
	qRear->next = ptr; // μ•ˆ λΉ„μ–΄ 있으면 이전값을 λ“€μ–΄μ˜¨ κ°’κ³Ό μ—°κ²°
qRear = ptr;           // qRear μˆ˜μ •
 
// Dequeue
NodeType<ItemType>* tempPtr;
tempPtr = qFront;
item = qFront->info;
qFront = qFront->next;
if(qFront == NULL)
	qRear = NULL;
delete tempPtr;
 
  • Comparing queue implementations
OperationArray ImplementationLinked Implementation
Class constructorO(1) front, rear, items μ„€μ •O(1) front, rear Null μ§€μ •
Make EmptyO(1) front, rear μ΄ˆκΈ°ν™”O(N) front == Null이 될 λ•ŒκΉŒμ§€ μˆœνšŒν•˜λ©° ν•΄μ œ
IsFullO(1) ((rear+1)%max == front) 확인O(1) heap λ©”λͺ¨λ¦¬ 였λ₯˜ 확인
IsEmptyO(1) rear == front 확인O(1) frontκ°€ Null인지 확인
EnqueueO(1) rear index μ„€μ •, μž…λ ₯O(1) μƒˆλ‘œμš΄ λ…Έλ“œ 생성, rear λ…Έλ“œλ‘œ μ—°κ²°
DequeueO(1) front index μ„€μ •, λ°˜ν™˜O(1) front κ°’ λΉΌκΈ°
DestructorO(1) λ°°μ—΄ ν• λ‹Ή ν•΄μ œO(N) μˆœνšŒν•˜λ©° ν• λ‹Ή ν•΄μ œ

Static vs Dynamic

  • 두 κ°€μ§€ λ‹€λ₯Έ λ§₯λ½μ—μ„œ dynamic, static μš©μ–΄λ₯Ό μ‚¬μš©ν•œλ‹€.

  • 이 두 μš©μ–΄λ₯Ό ꡬ뢄할 ν•„μš”κ°€ μžˆλ‹€.

  • Memory allocation 관점

    • Static memory allocation : new/malloc을 μ‚¬μš©ν•˜μ§€ μ•Šμ€ μ§€μ—­ λ³€μˆ˜κ°€ μƒμ„±ν•œ λ©”λͺ¨λ¦¬λŠ” stack segment에 μœ„μΉ˜ν•˜κ³  정적 λ©”λͺ¨λ¦¬ 할당을 ν•œλ‹€.
    • Dynamic memory allocation : new/malloc을 μ‚¬μš©ν•˜μ—¬ μ΄ˆκΈ°ν™”λœ λ³€μˆ˜κ°€ μƒμ„±ν•œ λ©”λͺ¨λ¦¬λŠ” heap segment에 μœ„μΉ˜ν•˜κ³  동적 λ©”λͺ¨λ¦¬ 할당을 ν•œλ‹€.
  • Data structure 관점

    • Static data structure : 자료 ꡬ쑰의 크기가 κ³ μ •λ˜μ–΄ μžˆλŠ” 경우
      • ex) Array-based stack/queue
    • Dynamic data structure : 자료 ꡬ쑰의 크기가 κ³ μ •λ˜μ–΄ μžˆμ§€ μ•Šμ€ 경우 λ³€ν•˜λŠ” 경우
      • ex) Linked structure-based stack/queue

Unsorted List Implementation(Linked List)

  • Unsorted
  • InsertItem : 정렬없이 맨 μ²˜μŒμ— 뢙인닀.
  • RetrieveItem : μˆœνšŒν•˜λ©΄μ„œ 값을 μ°ΎλŠ”λ‹€.
  • DeleteItem : μˆœνšŒν•˜λ©° 값을 μ°Ύκ³  ν• λ‹Ή ν•΄μ œλ₯Ό ν•˜κ³ , 이전 λ…Έλ“œμ˜ next값을 ν•΄μ œν•˜λŠ” λ…Έλ“œ λ‹€μŒμœΌλ‘œ μ—°κ²°μ‹œμΌœμ€˜μ•Ό ν•œλ‹€.
    • ν•˜μ§€λ§Œ 단방ν–₯ linked list이기 λ•Œλ¬Έμ— μ›ν•˜λŠ” 값에 λ„λ‹¬ν•˜λŠ” 경우 이전 값을 μ•Œ 수 μ—†λ‹€.
    • λ•Œλ¬Έμ— 2단계 μ•žμ˜ 정보λ₯Ό ν™•μΈν•˜λŠ” 방식을 μ‚¬μš©ν•œλ‹€.
class UnsortedType{
public :
	UnsortedType();
	~UnsortedType();
	void MakeEmpty();
	bool IsFull() const;
	int LengthIs() const;
	void RetrieveItem(ItemType& item, bool& found);
	void InsertItem(ItemType item);
	void DeleteItem(ItemType item);
	void ResetList();
	void GetNextItem(ItemType& item);
private :
	NodeType<ItemType>* listData;
	int length;
	NodeType<ItemType>* currentPos;
} ;
 
// InsertItem
NodeType<ItemType>* location;
location = new NodeType<ItemType>;
location->info = item;
location->next = listData;
listData = location;
length++;
 
// RetrieveItem
bool moreToSearch;
NodeType<ItemType>* location;
location = listData;
found = false;
moreToSearch = (location != NULL);
while(moreToSearch && !found){
	if(item == location->info){
		found = true;
	}
	else{
		location = location->next;
		moreToSearch = location != NULL);
	}
}
 
// DeleteItem
NodeType<ItemType>* location = listData;
NodeType<ItemType>* tempLocation;
 
if(item == listData->info){
	tempLocation = location;
	listData = listData->next;
}
else{
	while(!(item == location->next->info))
		location = location->next;
	tempLocation = location;
	location->next = location->next->next;
}
delete tempLocation;
length--;
  • Comparison unsorted list implementations
OperationArray ImplementationLinked Implementation
Class constructorO(1) length μ§€μ •O(1) listData = NULL μ§€μ •
DestructorO(1) 동적할당XO(N) NULL κΉŒμ§€ μˆœνšŒν•˜λ©° ν•΄μ œ
MakeEmptyO(1) length = 0O(N) μˆœνšŒν•˜λ©° ν•΄μ œ
IsFullO(1) length == MAX 확인O(1) heap λ©”λͺ¨λ¦¬ 였λ₯˜ λ©”μ‹œμ§€ 확인
LengthIsO(1) length λ°˜ν™˜O(1) length λ°˜ν™˜
ResetListO(1) currentPos = -1O(1) currentPos = listData
GetNextItemO(1) currentPos++ μΆ”μΆœO(1) currentPosβ†’nextβ†’info
RetrieveNextItemO(N) or O(logN) 탐색 ν•„μš”ν•˜κΈ° λ•Œλ¬ΈO(N) μˆœνšŒν•˜μ—¬ 탐색
InsertItemO(1) 맨 뒀에 μ‚½μž…O(1) 맨 μ•žμ— μ‚½μž…
DeleteItemO(N) 탐색해 μ§€μš°κ³  빈자리 μ΄λ™μ‹œμΌœμ•Ό 함O(N) 탐색해 μ§€μš°κΈ°

Sorted List Implementation(Linked List)

1 β†’ 3 β†’ 7 β†’ 9

  • Sorted
  • InsertItem : μœ„μΉ˜λ₯Ό νƒμƒ‰ν•˜κ³  ν•΄λ‹Ή μœ„μΉ˜μ— μ‚½μž…ν•΄μ•Ό ν•œλ‹€. 4λ₯Ό μ‚½μž…ν•˜λŠ” 경우 3κ³Ό 7 사이에 μ‚½μž…ν•΄μ•Ό ν•˜κΈ° λ•Œλ¬Έμ— info == 3인 λ…Έλ“œμ˜ nextλ₯Ό 4둜 μ§€μ •ν•΄μ€˜μ•Ό ν•œλ‹€. Unsorted와 같이 2단계 λ’€λ₯Ό μƒκ°ν•˜λŠ” 방식이 μžˆμœΌλ‚˜, 10을 μ‚½μž…ν•˜λŠ” 경우 즉, 맨 λ§ˆμ§€λ§‰μ— μ‚½μž…ν•˜λŠ” 경우 2단계 λ’€μ˜ infoλ₯Ό ν™•μΈν•˜λŠ” 방식은 NULL값을 λ°›κΈ° λ•Œλ¬Έμ— μ‚¬μš©ν•  수 μ—†λ‹€. λ•Œλ¬Έμ— 포인터λ₯Ό 2개λ₯Ό μ‚¬μš©ν•˜μ—¬ ν•΄κ²°ν•œλ‹€.
  • RetrieveItem : 이미 μ •λ ¬λ˜μ–΄ 있기 λ•Œλ¬Έμ— item보닀 큰지, μž‘μ€μ§€, 같은지λ₯Ό νŒλ‹¨ν•˜λ©΄ λœλ‹€. 이진 탐색을 μ‚¬μš©ν•  μˆ˜λŠ” μžˆμœΌλ‚˜, 이진 탐색을 μ‚¬μš©ν•˜λ”λΌλ„ λ³΅μž‘λ„κ°€ O(logN)이 λ˜μ§€λŠ” μ•ŠλŠ”λ‹€.(단방ν–₯μ΄λ―€λ‘œ)
class SortedType{
public :
	SortedType();
	~SortedType();
	void MakeEmpty();
	bool IsFull() const;
	int LengthIs() const;
	void RetrieveItem(ItemType& item, bool& found);
	void InsertItem(ItemType item);
	void DeleteItem(ItemType item);
	void ResetList();
	void GetNextItem(ItemType& item);
private :
	NodeType<ItemType>* listData;
	int length;
	NodeType<ItemType>* currentPos;
} ;
 
// RetrieveItem
NodeType<ItemType>* location;
location = listData;
found = false;
while((location!=NULL) && !found){
	if (location->info < item)
		location = location->next;
	else if (location->info == item)
		found = true;
	else
		location = NULL;
}
 
// InsertItem
bool isFindLoc;
NodeType<ItemType>* predLoc = NULL;
NodeType<ItemType>* location = listData;
NodeType<ItemType>* newNode;
 
newNode = new NodeType<ItemType>;
newNode->info = item;
 
if(listData == NULL){
	newNode->next = NULL;
	listData = newNode;
	length++;
}
else{
    while (true){                   // for searching.
        if(predLoc == NULL)         // Add item at the first index.
            isFindLoc = (item <= location->info);
        else if(location == NULL)   // Add item at the last index.
            isFindLoc = (item >= predLoc->info);
        else 
            isFindLoc = (predLoc->info <= item && item <= location->info);
 
        if(isFindLoc){
            newNode->next = location;
            length++;
            if(predLoc == NULL){
                listData = newNode;
                break;
            }
            predLoc->next = newNode;
            break;
        }
        else{
            predLoc = location;
            location = location->next;
        }
    }
}
  • Comparing sorted list implementations
OperationArray ImplementationLinked Implementation
Class constructorO(1) length μ§€μ •O(1) listData = NULL μ§€μ •
DestructorO(1) 동적할당XO(N) NULL κΉŒμ§€ μˆœνšŒν•˜λ©° ν•΄μ œ
MakeEmptyO(1) length = 0O(N) μˆœνšŒν•˜λ©° ν•΄μ œ
IsFullO(1) length == MAX 확인O(1) heap λ©”λͺ¨λ¦¬ 였λ₯˜ λ©”μ‹œμ§€ 확인
LengthIsO(1) length λ°˜ν™˜O(1) length λ°˜ν™˜
ResetListO(1) currentPos = -1O(1) currentPos = listData
GetNextItemO(1) currentPos++ μΆ”μΆœO(1) currentPosβ†’nextβ†’info
RetrieveNextItemO(N) or O(logN) 탐색 ν•„μš”ν•˜κΈ° λ•Œλ¬ΈO(N) μˆœνšŒν•˜μ—¬ 탐색
InsertItemO(N) μˆœνšŒν•˜μ—¬ μœ„μΉ˜ 탐색, 자리 이동, μ‚½μž…O(N) μˆœνšŒν•˜μ—¬ μ‚½μž…
DeleteItemO(N) 탐색해 μ§€μš°κ³  빈자리 μ΄λ™μ‹œμΌœμ•Ό 함O(N) 탐색해 μ§€μš°κΈ°