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

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

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

[Exercises 1]/*Extend the stack ADT by adding functions to output a stack;스택 추상자료형을 출력하도록 확장시킨다;split a stack into two stacks with one containing the bottom half elements and the second the remaining elements;스택을 두개로 나누어서 하나는 밑에 절반을 소장하고 다른 하나는 나머지를 소장하도록 한다;and to combine two stacks into one by placing all elements of the second stack on top of those of the first stack.그리고 두번째 스택을 첫번째 스택 위에 위치시..

C++ Fundamentals of Data Structures(C++ 자료구조론) 2.8 연습문제 5~9

[Exercises 5]/*Another kind of sparse matrix that arises often in practice is the tridiagonal matrix.3중 대각행렬은 희소행렬을 연습하는 와중에 자주 등장한다.In this square matrix, all elements other than those on the major diagonal and on the diagonals immediately above and below this one are zero.3중 대각행렬에서는 중심 대각선과 중심 대각선을 기준으로 +1 위 아래로 있는 대각선을 제외하고는 다 0이다.If the elements in the band formed by these three diagonals ar..

C++ Fundamentals of Data Structures(C++ 자료구조론) 2.8 연습문제 1~4

[Exercises 1]/*Write a C++ function to make an in-place reversal of the order of elements in the array list.list라는 배열의 요소를 거꾸로 뒤집는 함수를 작성한다.That is, the function should transform list such that following the transformation, list[i] contaions the element originally in list[n-i-1]th즉, 이 함수는 list[i]가 list[n-i-1]번째의 요소를 갖도록 해야한다.where n is the total number of elements in list.n은 list 배열의 전체크기이다.The ..

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

[Exercises 1]/*Write a function String::Frequency that determines the frequency of occurence of each of the distinct characters in the string.문자열의 알파벳들의 출현빈도를 가늠하는 String::Frequency 함수를 작성한다.*/#include using namespace std; class String{private: char *str; int len; int *f; //실패 함수용public: String(char *init, int m) :len(m) { str = new char[m + 1]; f = new int[m]; for (int i = 0; i < m; i++) { str[..

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

[Exercises 1]/*Even though the multidimensional array is provided as a standard data object in C++,다차원 배열이 C++ 표준 데이터 객체로 제공되고 있지만,it is often useful to define your own class for multidimensional arrays.다차원 배열을 직접 정의하는 것이 실용적일 때가 많다.This gives a more robust class that:직접 정의할 경우 아래와 같은 기능을 제공하는 탄탄한 클래스가 된다:(a) Performs range checking(a) 범위를 확인한다(b) Does not require the index set in each dimension..

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

[Exercises 1]/*How much time does it take to locate an arbitrary element A[i][j] in the representation of this section and to change its value이번 섹션에서 나타낸 이차행렬의 임의 원소인 A[i][j]를 찾고 값을 변경하는데 걸리는 시간은?*/일반적인 방법을 쓸 경우 : O(rows*cols) FastTranspose를 쓸 경우 : O(cols + terms)[Exercises 2]/*Analyze carefully the computing time and storage requirements of function FastTransposeFastTranspose 함수의 작동시간과 저장 크기 조건..

C++ Fundamentals of Data Structures(C++ 자료구조론) 2.3 연습문제 7~9

[Exercises 7]/*The polynomials a(x)=x^(2n)+x^(2n-2)+...+x^(2)+x^(0) and b(x)=x^(2n+1)+x^(2n-1)+...+x^(3)+x cause Add to work very harda(x)=x^(2n)+x^(2n-2)+...+x^(2)+x^(0) 와 b(x)=x^(2n+1)+x^(2n-1)+...+x^(3)+x 다항식은 Add 함수가 작동하기 매우 힘들게 한다For these polynomials, determine the exact number of times each statement will be executed이러한 다항식들에 대해서는, 각 문장이 몇번 실행되는지 구해본다*//*Write a c++ function that evaluate..

C++ Fundamentals of Data Structures(C++ 자료구조론) 2.3 연습문제 1~6

[Exercises 1]/*Use the six operations defined in this section for an ordered list to arrive at an ADT specification for such a list이번 섹션에 언급된 여섯가지의 연산을 이용해 정렬된 리스트의 ADT를 작성해본다*/ //ADT를 작성할 때는 추상적으로 작성해야하지만 어쩌다보니 클래스를 정의하였다.(물론 문법적으로 틀린부분이 있을 수 있습니다)class orderedList{private: int *arr; int length;public: orderedList(int n):length(n) { arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = 0; //0으..