C++ 136

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..

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

[Exercises 1]/*Implement Stack as a publicly derived class of Bag using templates템플릿을 이용하여 Bag 클래스를 상속하는 Stack 클래스를 구현한다*/#include using namespace std; template class Bag{protected: T *array; int capacity; int top;public: Bag(int bagCapacity = 10) :capacity(bagCapacity) { array = new T[capacity]; top = -1; } virtual ~Bag() { delete[]array; } virtual void Push(const T &item) { if (IsFull()) { cout

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

[Exercises 1]/*Rewrite functions Push and Pop using the variable lastOp as discussed in this section이번 섹션에서 언급한 lastOp 변수를 사용하도록 Push, Pop 함수를 수정한다.The queue should now be ble to hold up to capacity elements.이제 큐는 배열의 크기만큼 요소를 가지고 있을 수 있을 것이다.*/#include #include using namespace std; template class Queue{private: T *queue; int front; int rear; int capacity; int lastOp;public: Queue(int queueCapac..