C++/Fundamentals of Data Structures in C++(Horowitz) 47

C++ Fundamentals of Data Structures(C++ 자료구조론) 4.10 이중연결리스트(Doubly Linked List)

/*이중 연결리스트 Doubly LinkedList*/#include using namespace std; class DblList; class DblListNode{private: int data; DblListNode *left, *right;public: DblListNode(int element = 0, DblListNode *lNode = NULL, DblListNode *rNode = NULL) :data(element), left(lNode), right(rNode) { } friend class DblList; friend ostream &operatorright = first; //더미노드 } ~DblList() { DblListNode *delNode = first; //위치를 기억해두..

C++ Fundamentals of Data Structures(C++ 자료구조론) 4.9 희소행렬(Sparse Matrix)

/*희소행렬 SparseMatrix*/#include using namespace std; class Matrix; struct Triple{ int row; int col; int val;}; class MatrixNode{private: MatrixNode *right; //행 MatrixNode *down; //열 bool isHead; union { MatrixNode *next; Triple triple; //row, col, val };public: MatrixNode() //디폴트 생성자 { } MatrixNode(bool b, Triple *t) { isHead = b; if (isHead) { right = down = next = this; } else { triple = *t; } }..

C++ Fundamentals of Data Structures(C++ 자료구조론) 4.8 동치(Equivalence)

/*동치 Equivalence*/#include #include using namespace std; class ENode{private: int data; ENode *link; ENode *link2; //link와 똑같은 역할을 하는 link2 //link2가 없을 경우 동적할당한 메모리를 반환할 때 문제가 생긴다(몇몇 데이터들이 제거되지 않는다)public: ENode(int d = 0) //생성자 { data = d; link = 0; } friend void Equivalence();}; void Equivalence() //동치{ ifstream inFile("equiv.txt", ios::in); if (!inFile) { cout > i >> j; ENode *x = new ENode(j..

C++ Fundamentals of Data Structures(C++ 자료구조론) 4.7 다항식(Polynomial)

/*기존에 정의한 ChainNode를 이용한 다항식*/#include using namespace std; template class Chain; template class ChainNode{private: T data; ChainNode *link;public: ChainNode(T element = 0, ChainNode *next = NULL) :data(element), link(next) { } T getData() { return data; } ChainNode *Link() { return link; } friend class Chain; template friend ostream &operatorlink = temp; //x가 temp를 가르키도록 return x->link; //위치를 반..

C++ Fundamentals of Data Structures(C++ 자료구조론) 4.6 링크드 스택, 링크드 큐

[linkedStack]/*링크드리스트를 이용해서 스택을 구현했습니다*/#include using namespace std; template class LinkedStack; template class ChainNode //기존에 정의한 ChainNode 클래스 그대로 사용함{private: T data; ChainNode *link;public: ChainNode(T element = 0, ChainNode *next = NULL) :data(element), link(next) { } /* T getData() { return data; } ChainNode *Link() { return link; } */ friend LinkedStack; template friend ostream &operato..

C++ Fundamentals of Data Structures(C++ 자료구조론) 4.4 CircularList(원형리스트)

/*기존에 풀었던 문제를 원형리스트로 다시 푸는 문제였기 때문에간단하게 원형리스트(CircularList) 예제를 작성했습니다*/#include using namespace std; template class CircularList; template class ChainNode //기존에 정의한 ChainNode 클래스 그대로 사용함{private: T data; ChainNode *link;public: ChainNode(T element = 0, ChainNode *next = NULL) :data(element), link(next) { } /* T getData() { return data; } ChainNode *Link() { return link; } */ friend CircularList..

C++ Fundamentals of Data Structures(C++ 자료구조론) 4.3 연습문제

[Exercises 1]/*Write a C++ template function to output all elements of a chain by overloading the output operatorlink = x->link; //x 다음을 temp와 연결시켜 자연스럽게 x가 없어지도록 한다 } delete x; } void InsertBack(const T &item) { if (first) { last->link = new ChainNode(item); last = last->link; last->link = NULL; } else { first = last = new ChainNode(item); first->link = NULL; } } void Concatenate(Chain &b) //합치..

C++ Fundamentals of Data Structures(C++ 자료구조론) 4.2 연습문제

[Exercises 1]/*Write a C++ function length to count the number of nodes in a chain.체인에 있는 노드의 갯수를 세는 함수를 작성한다*/#include using namespace std; class ChainNode{private: int data; ChainNode *link;public: ChainNode(int element = 0, ChainNode *next = NULL) :data(element), link(next) { } int getData() { return data; } friend class Chain; friend ostream &operatorlink = temp; //x가 temp를 가르키도록 return x->l..

C++ Fundamentals of Data Structures(C++ 자료구조론) 3.5 미로 예제

미로 코드가 제시되어있지 않고 딱히 연습문제에서도 풀라는 문제가 없었기 때문에 직접 작성해봤습니다! #include #include using namespace std; //앞서 정의한 Stacktemplate class Stack{private: T *stack; //스택배열 int top; //제일 마지막에 삽입된 원소 int capacity; //배열 크기public: Stack(int stackCapacity = 10) :capacity(stackCapacity) { if (capacity 0"; stack = new T[capacity]; top = -1; } ~Stack() { delete[]stack; } bool IsEmpty..