├── Chapter-07 ├── Supplemenary Problems │ ├── 7-33.png │ ├── 7-36.png │ ├── 7-26.cpp │ ├── 7-19.cpp │ └── 7-33.cpp ├── Solved Problem │ ├── 7-1.cpp │ ├── 7-7.cpp │ ├── 7-8.cpp │ └── 7-2.cpp ├── ALGORITHM WITH POINTER │ ├── Algorithm-7-5.cpp │ ├── Algorithm-7-2.cpp │ ├── Algorithm-7-1.cpp │ └── Algorithm-7-3.cpp ├── ALGORITHM │ ├── Algorithm-7-6.cpp │ ├── Algorithm-7-1.cpp │ ├── Algorithm-7-2.cpp │ ├── Algorithm-7-9.cpp │ └── Algorithm-7-3.cpp └── Programming Problems │ └── 7-43.cpp ├── Chapter-06 ├── SUPPLEMENTARY PROBLEMS │ ├── 6-33.cpp │ ├── 6-32.CPP │ ├── 6-43.cpp │ ├── 6-38.cpp │ ├── 6-40.cpp │ ├── 6-42.cpp │ ├── 6-45.cpp │ ├── 6-39.cpp │ ├── 6-41.cpp │ ├── 6-31.CPP │ ├── 6-37.cpp │ └── 6-44.cpp ├── SOLVED PROBLEMS │ ├── 6-15.cpp │ ├── 6-14.cpp │ ├── 6-19.cpp │ ├── 6-16.cpp │ └── 6-17.cpp ├── ALGORITHM │ ├── Algorithm-6-7.cpp │ ├── Algorithm-6-8.cpp │ ├── Algorithm-6-9.cpp │ ├── Algorithm-6-1.cpp │ ├── Algorithm-6-11.cpp │ └── Algorithm-6-15.cpp ├── PROGRAMMING PROBLEMS │ ├── 6-52.cpp │ └── 6-53.cpp ├── STACK.cpp └── EXAMPLE │ └── 6-2.cpp ├── Chapter 01 ├── Problem Solve │ └── Tamplate.txt └── Example │ ├── Example01-05.cpp │ ├── Example01-02.cpp │ ├── Example01-03.cpp │ └── Example01-04.cpp ├── Tree ├── Height.h ├── MAX.h ├── MIN.h ├── Search.h ├── BFS.h ├── DFS.h ├── Insert.h ├── Check_BST.h ├── Inroder_Succsesor.h ├── Recursive_Tree.cpp └── Delete.h ├── Chapter-05 ├── LINKED LIST │ ├── SUB_ITEM.h │ ├── ELEMENTS.h │ ├── OCARANCE.h │ ├── LINKED_LIST.cpp │ ├── INTERCHANGE.h │ ├── REPLACE.h │ ├── SORT.h │ ├── COMBINE.h │ ├── SEARCH.h │ ├── DISPLAY.h │ ├── HELP.h │ └── START.h ├── SOLVED PROBLEMS │ ├── 5-2.cpp │ └── 5-7.cpp ├── Algorithm │ └── Algorithm-5-11.cpp └── Supplementary Problems │ └── 5-26.cpp ├── Chapter-04 ├── Example │ ├── 4-1.cpp │ └── 4-7.cpp ├── Solved Problems │ ├── 4-17.cpp │ ├── 4-16.cpp │ ├── 4-18.cpp │ ├── 4-7.cpp │ ├── 4-3.cpp │ └── 4-9.cpp ├── Supplementary Problems │ ├── 4-21.cpp │ ├── 4-22.cpp │ ├── 4-26.cpp │ ├── 4-24.cpp │ └── 4-29.cpp ├── Programming Problems │ ├── 4-32.cpp │ └── 4-33.cpp ├── Algorithm │ └── Algorithm-4-1.cpp └── MISCELLANEOUS │ ├── 4-46.cpp │ └── 4-45.cpp ├── Chapter-03 ├── Example │ └── Example3-1.cpp ├── 3-26.cpp ├── 3-9.cpp ├── 3-25.cpp ├── 3-22.cpp ├── 3-27.cpp ├── Algorithm │ ├── Algorithm3-1.cpp │ ├── Algorithm3-2.cpp │ └── Algorithm3-3.cpp ├── 3-32.CPP ├── 3-30.cpp ├── 3-8.cpp ├── 3-31.cpp ├── 3-29.cpp ├── 3-33.cpp ├── 3-10.cpp ├── 3-13.cpp ├── 3-12.cpp ├── 3-23.cpp ├── 3-18.cpp ├── 3-24.CPP ├── 3-16.cpp ├── 3-20.cpp ├── 3-1.cpp ├── 3-11.cpp ├── 3-17.cpp ├── 3-28.cpp └── Programming Problems │ ├── 3-44.cpp │ ├── 3-40.cpp │ └── 3-43.cpp ├── Chapter-08 ├── PROGRAMMING PROBLEMS │ └── 8-44.cpp ├── SOLVED PROBLEMS │ ├── 8-13.cpp │ └── 8-9.cpp ├── SUPPLEMENTARY PROBLEMS │ ├── 8-27.cpp │ └── 8-26.cpp └── ALGORITHM │ └── Algorithm-8-1.cpp ├── Chapter-09 ├── SUPPLEMENTARY PROBLEMS │ ├── 9-1.CPP │ ├── 9-7.cpp │ ├── 9-2.cpp │ ├── 9-3.cpp │ ├── 9-8.cpp │ └── 9-4.cpp ├── ALGORITHM │ ├── Algorithm-9-1.cpp │ ├── Algorithm-9-3.cpp │ ├── Algorithm-9-2.cpp │ ├── Algorithm-9-6.cpp │ └── Algorithm-9-5.cpp └── Hashing │ └── Code_of_Hashing.cpp └── Chapter-02 ├── Programming Problems ├── 2-23.cpp ├── 2-24.cpp └── 2-25.cpp ├── Solved Problem ├── 2-4.cpp ├── 2-1.cpp ├── 2-3.cpp ├── 2-8.cpp └── 2-2.cpp ├── Supplementary Problems ├── 2-15.cpp ├── 2-17.cpp └── 2-18.cpp └── Chapter Algorithm ├── Algorithm2-1.cpp ├── Algorithm2-3.cpp ├── Algorithm2-2.cpp └── Algorithm2-4.cpp /Chapter-07/Supplemenary Problems/7-33.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Feroz455/DATA-STRUCTURE-By-Seymour-Lipschutz/HEAD/Chapter-07/Supplemenary Problems/7-33.png -------------------------------------------------------------------------------- /Chapter-07/Supplemenary Problems/7-36.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Feroz455/DATA-STRUCTURE-By-Seymour-Lipschutz/HEAD/Chapter-07/Supplemenary Problems/7-36.png -------------------------------------------------------------------------------- /Chapter-06/SUPPLEMENTARY PROBLEMS/6-33.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | page 211 3 | 6.8 Evaluate each of the following parenthesis-free arithmetic expressions: 4 | (a) 5 + 3 ↑ 2 – 8 / 4 * 3 + 6 5 | (b) 6 + 2 ↑ 3 + 9 / 3 – 4 * 5 6 | */ 7 | confusing 8 | -------------------------------------------------------------------------------- /Chapter 01/Problem Solve/Tamplate.txt: -------------------------------------------------------------------------------- 1 | /* 2 | Program 3 | 4 | */ 5 | #include 6 | using namespace std; 7 | 8 | 9 | 10 | //main begin 11 | int main() 12 | { 13 | getchar(); 14 | return 0; 15 | } 16 | //main end 17 | ///Program end 18 | 19 | /* 20 | output 21 | */ 22 | -------------------------------------------------------------------------------- /Tree/Height.h: -------------------------------------------------------------------------------- 1 | int MAX(int x, int y) 2 | { 3 | return (x > y ? x : y); 4 | } 5 | int FindHeight(TreeNode *node) 6 | { 7 | 8 | if (node == NULL) 9 | { 10 | return -1; 11 | } 12 | else 13 | return MAX(FindHeight(node->lchild), FindHeight(node->rchild) + 1); 14 | } -------------------------------------------------------------------------------- /Chapter-05/LINKED LIST/SUB_ITEM.h: -------------------------------------------------------------------------------- 1 | void PRINT_SUB_STR(LINKED **START , int K, int N) 2 | { 3 | LINKED *PTR = (*START); 4 | while (K--) 5 | { 6 | PTR = PTR->next; 7 | } 8 | while (N--) 9 | { 10 | cout << PTR->CHAR << " "; 11 | PTR = PTR->next; 12 | } 13 | } -------------------------------------------------------------------------------- /Tree/MAX.h: -------------------------------------------------------------------------------- 1 | bool MAX(TreeNode *root, int &item) 2 | { 3 | if(root == NULL) 4 | { 5 | cout << "ROOT is Empty\n"; 6 | return false; 7 | } 8 | TreeNode *current = root; 9 | while (current->rchild != NULL) 10 | { 11 | current = current->rchild; 12 | } 13 | item = current->DATA; 14 | return true; 15 | } -------------------------------------------------------------------------------- /Tree/MIN.h: -------------------------------------------------------------------------------- 1 | bool MIN(TreeNode *root, int &item) 2 | { 3 | if(root == NULL) 4 | { 5 | cout << "ROOT is Empty\n"; 6 | return false; 7 | } 8 | TreeNode *current = root; 9 | while (current->lchild != NULL) 10 | { 11 | current = current->lchild; 12 | } 13 | item = current->DATA; 14 | return true; 15 | } -------------------------------------------------------------------------------- /Chapter-06/SUPPLEMENTARY PROBLEMS/6-32.CPP: -------------------------------------------------------------------------------- 1 | /* 2 | 6.7 Translate, by inspection and hand, each infix expression in 3 | Supplementary Problem 6.31 into its equivalent prefix expression. 4 | 5 | (A-B)/((D+E)*F) 6 | -AB/(+DE * F) 7 | -AB/*+DEF 8 | /-AB*DEF 9 | 10 | 11 | ((A + B)/D) ^ ((E - F) * G) 12 | (+AB/D)^(-EF * G) 13 | /+ABD ^ *-EFG 14 | ^+ABD*-EFG 15 | 16 | 17 | */ -------------------------------------------------------------------------------- /Chapter-05/LINKED LIST/ELEMENTS.h: -------------------------------------------------------------------------------- 1 | void ELEMENT(LINKED **START) 2 | { 3 | int COUNT = 0 ; 4 | LINKED *PTR = (*START); 5 | if(PTR == NULL) 6 | { 7 | cout << "LIST IS EMPTY\n"; 8 | } 9 | while (PTR->next != *START) 10 | { 11 | COUNT++; 12 | PTR = PTR->next; 13 | } 14 | cout << "NUMBER OF ELEMENT -> " << COUNT+1 << "\n"; 15 | } -------------------------------------------------------------------------------- /Chapter-05/LINKED LIST/OCARANCE.h: -------------------------------------------------------------------------------- 1 | void OCARANCE(LINKED **START, int NUM) 2 | { 3 | int COUNT = 0; 4 | LINKED *PTR = (*START); 5 | while(PTR->next != (*START)) 6 | { 7 | if(PTR->DATA == NUM) 8 | COUNT++; 9 | PTR = PTR->next; 10 | } 11 | if(PTR->DATA == NUM) 12 | COUNT++; 13 | cout << "NUMBER OF OCARENCE -> " << COUNT+1 << "\n"; 14 | } -------------------------------------------------------------------------------- /Chapter-04/Example/4-1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | page 68 3 | program 4 | Example 4-1 5 | */ 6 | #include 7 | using namespace std; 8 | int main() 9 | { 10 | int DATA[6] = {247,56,429,135,87,156}; 11 | for(int i = 0; i < sizeof(DATA)/sizeof(int); i++) 12 | cout << DATA[i] << endl; 13 | return 0; 14 | } 15 | /* 16 | output 17 | 18 | 247 19 | 56 20 | 429 21 | 135 22 | 87 23 | 156 24 | */ -------------------------------------------------------------------------------- /Tree/Search.h: -------------------------------------------------------------------------------- 1 | bool FIND(TreeNode **root, int key) 2 | { 3 | TreeNode *temp = *root; 4 | if (key == temp->DATA) 5 | { 6 | return true; 7 | } 8 | while ((temp != NULL) && temp->DATA != key) 9 | { 10 | temp = (key > temp->DATA) ? temp->rchild : temp->lchild; 11 | } 12 | if (temp == NULL) 13 | { 14 | return false; 15 | } 16 | return true; 17 | } -------------------------------------------------------------------------------- /Chapter-03/Example/Example3-1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | page 42 3 | Program 3-1 4 | */ 5 | 6 | #include 7 | #include 8 | using namespace std; 9 | int main() 10 | { 11 | char *c = new char[100]; 12 | int i = 0; 13 | cin.get(c[i]); 14 | while(c[i] != EOF) 15 | { 16 | cin.get(c[++i]); 17 | } 18 | c[i] = '\0'; 19 | cout << c << endl; 20 | 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Chapter 01/Example/Example01-05.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Program 3 | 4 | */ 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | 10 | //main begin 11 | int main() 12 | { 13 | double x, y , a, b; 14 | 15 | x = 2; 16 | y = 2; 17 | a = 2; 18 | b = 2; 19 | 20 | cout << (2*x+y)*pow((a - 7*b),3); 21 | getchar(); 22 | return 0; 23 | } 24 | //main end 25 | ///Program end 26 | 27 | /* 28 | output 29 | */ 30 | -------------------------------------------------------------------------------- /Chapter-06/SUPPLEMENTARY PROBLEMS/6-43.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 6.18 Let A be an integer array with N elements. Suppose X is an integer function defined by 3 | 4 | 0 if(K = 0) 5 | X(K) = X(A, N, k) = X(K - 1)+A(K) if (0 < K && k <= N) 6 | X(K - 1) if(K > N) 7 | 8 | Find X(5) for each of the following arrays: 9 | 10 | (a) N = 8, A: 3, 7, –2, 5, 6, –4, 2, 7 (b) N = 3, A: 2, 7, –4 11 | 12 | What does this function do? 13 | 14 | */ -------------------------------------------------------------------------------- /Chapter-05/LINKED LIST/LINKED_LIST.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | */ 3 | typedef struct _link 4 | { 5 | int DATA; 6 | _link *next; 7 | _link *prev; 8 | }LINKED; 9 | //FUNCTION PROTOTYRE 10 | /*******************************/ 11 | /*******************************/ 12 | #include"HELP.h" 13 | #include"INSERT.h" 14 | #include"DISPLAY.h" 15 | #include"DELETE.h" 16 | #include"REPLACE.h" 17 | #include"SEARCH.h" 18 | #include"START.h" 19 | 20 | using namespace std; 21 | int main() 22 | { 23 | START(); 24 | } -------------------------------------------------------------------------------- /Chapter-08/PROGRAMMING PROBLEMS/8-44.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | page 317 3 | 8.4 Write a procedure with input CITYA and CITYB which finds 4 | the flight number and cost of the flight from city A to city B, 5 | if a flight exists. Test the procedure using 6 | (a) CITYA = Chicago, CITYB = Boston; 7 | (b) CITYA = Washington, CITYB = Denver; and 8 | (c) CITYA = New York, CITYB = Philadelphia. 9 | */ 10 | #include 11 | #include 12 | using namespace std; 13 | int main() 14 | { 15 | cout << "hello\n"; 16 | } -------------------------------------------------------------------------------- /Chapter-03/3-26.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 65 3 | Program 3-26 4 | In Supplementary Problems 3.5 to 3.10, let S and T be character variables such that 5 | 6 | S = 'WE THE PEOPLE'    and    T = 'OF THE UNITED STATES' 7 | 8 | 3.5 Find the length of S and T 9 | */ 10 | #include 11 | #include 12 | using namespace std; 13 | int main() 14 | { 15 | string S("WE THE PEOPLE"), T("OF THE UNITED STATES"); 16 | cout << S.length() << " " << T.length(); 17 | 18 | return 0; 19 | } 20 | /* 21 | Output 22 | 23 | */ -------------------------------------------------------------------------------- /Tree/BFS.h: -------------------------------------------------------------------------------- 1 | void LeverOrder(TreeNode *root) 2 | { 3 | if (root == NULL) 4 | { 5 | return; 6 | } 7 | queue Q; 8 | Q.push(root); 9 | TreeNode *current; 10 | while (!Q.empty()) 11 | { 12 | current = Q.front(); 13 | Q.pop(); 14 | cout << current->DATA << " "; 15 | if (current->lchild != NULL) 16 | { 17 | Q.push(current->lchild); 18 | } 19 | if (current->rchild != NULL) 20 | Q.push(current->rchild); 21 | } 22 | } -------------------------------------------------------------------------------- /Chapter-03/3-9.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | char *SUBSTR(char const *S, int from, int to) 5 | { int i; 6 | char *str = new char[to + 1]; 7 | for( i = 0; i < to && S[i+from] != '\0'; i++) 8 | { 9 | str[i] = S[i+from]; 10 | } 11 | str[i] = '\0'; 12 | return str; 13 | 14 | } 15 | int main() 16 | { 17 | char const *S = "JOHN PAUL JONES", *T = "A THING OF BEAUTY IS A JOY FOREVER"; 18 | cout << S << endl; 19 | cout << T << endl; 20 | char *s = SUBSTR(S,4,8); 21 | cout << s << endl; 22 | s = SUBSTR(T,10,5); 23 | cout << s << endl; 24 | } -------------------------------------------------------------------------------- /Tree/DFS.h: -------------------------------------------------------------------------------- 1 | void Pre_order(TreeNode *root) 2 | { 3 | if (root != NULL) 4 | { 5 | cout << root->DATA << " "; 6 | Pre_order(root->lchild); 7 | Pre_order(root->rchild); 8 | } 9 | } 10 | void In_order(TreeNode *root) 11 | { 12 | if (root != NULL) 13 | { 14 | In_order(root->lchild); 15 | cout << root->DATA << " "; 16 | In_order(root->rchild); 17 | } 18 | } 19 | void Post_order(TreeNode *root) 20 | { 21 | if (root != NULL) 22 | { 23 | Post_order(root->lchild); 24 | Post_order(root->rchild); 25 | cout << root->DATA << " "; 26 | } 27 | } -------------------------------------------------------------------------------- /Chapter-06/SUPPLEMENTARY PROBLEMS/6-38.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 6.13 Let J and K be integers and suppose Q(J, K) is recursively defined by 3 | 4 | 5 if j < k 5 | GCD(A,B) = 6 | Q(j-k, k+2) + j if j >= k 7 | Find Q(2, 7), Q(5, 3) and Q(15, 2) 8 | */ 9 | #include 10 | using namespace std; 11 | int Q(int J, int K) 12 | { 13 | if(J < K) 14 | { 15 | return 5; 16 | } 17 | return (Q(J - K, K+2)+J); 18 | } 19 | int main() 20 | { 21 | cout << Q(2,7) << endl; 22 | cout << Q(5,3) << endl; 23 | cout << Q(15,2) << endl; 24 | 25 | } 26 | /* 27 | 5 28 | 10 29 | 42 30 | */ -------------------------------------------------------------------------------- /Chapter-03/3-25.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Suppose STATE is a character variable with fixed length 12. 3 | Describe the contents of STATE after the assignment 4 | (a) STATE := 'NEW YORK', 5 | (b) STATE := 'SOUTH CAROLINA' and 6 | (c) STATE := 'PENNSYLVANIA'. 7 | 8 | Page 9 | Program 10 | */ 11 | #include 12 | #include 13 | using namespace std; 14 | int main() 15 | { 16 | char s[12]; 17 | strcpy(s,"NEW YORK"); 18 | cout << s << endl; 19 | strcpy(s,"SOUTH CAROLINA"); 20 | cout << s << endl; 21 | strcpy(s,"PENNSYLVANIA"); 22 | cout << s << endl; 23 | 24 | 25 | 26 | return 0; 27 | } 28 | /* 29 | Output 30 | 31 | */ -------------------------------------------------------------------------------- /Chapter-05/LINKED LIST/INTERCHANGE.h: -------------------------------------------------------------------------------- 1 | void MAKE_FIRST_ELEMENT_LAST_ELEMENT(LINKED **START) 2 | { 3 | (*START) = (*START)->next; 4 | } 5 | void INTERCHANGE(LINKED **START, int lOC) 6 | { 7 | LINKED *PTR = (*START); 8 | if(lOC == 0) 9 | { 10 | MAKE_FIRST_ELEMENT_LAST_ELEMENT(START); 11 | return; 12 | } 13 | while (lOC-- != 1) 14 | { 15 | PTR = PTR->next; 16 | } 17 | int TEMP = PTR->DATA; 18 | PTR->DATA = PTR->next->DATA; 19 | PTR->next->DATA = TEMP; 20 | } 21 | void SWAP(LINKED *a, LINKED *b) 22 | { 23 | int temp = a->DATA; 24 | a->DATA = b->DATA; 25 | b->DATA = temp; 26 | } -------------------------------------------------------------------------------- /Chapter-06/SUPPLEMENTARY PROBLEMS/6-40.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 6.15 Let N be an integer and suppose H(N) is recursively defined by 3 | 3 * N if N < 5 4 | H(N) = 5 | 2 * H (N - 5) + 7 otherwise 6 | 7 | (a) Find the base criteria of H and 8 | 9 | (b) find H(2), H(8) and H(24). 10 | */ 11 | #include 12 | using namespace std; 13 | int H(int N) 14 | { 15 | if(N < 5) 16 | { 17 | return (3 * N); 18 | } 19 | return (2*H(N-5))+7; 20 | } 21 | int main() 22 | { 23 | cout << H(2) << endl; 24 | cout << H(8) << endl; 25 | cout << H(24) << endl; 26 | } 27 | /* 28 | 6 29 | 25 30 | 297 31 | */ -------------------------------------------------------------------------------- /Chapter-04/Solved Problems/4-17.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 106 3 | Program 4 | 4.17 A professor keeps the following data for each student in a class of 20 students: 5 | 6 | Name (Last, First, MI), Three Tests, Final, Grade 7 | */ 8 | #include 9 | #include 10 | using namespace std; 11 | struct Student 12 | { 13 | struct NAME 14 | { 15 | string Last; 16 | string First; 17 | string Middle_Initial; 18 | }Name; 19 | int Test[3]; 20 | int FINAL[3]; 21 | char GRADE[2]; 22 | }; 23 | 24 | //Main from Here 25 | int main() 26 | { 27 | Student STU[20]; 28 | } 29 | 30 | /* 31 | Output 32 | 33 | */ -------------------------------------------------------------------------------- /Chapter-05/LINKED LIST/REPLACE.h: -------------------------------------------------------------------------------- 1 | void REPLACE(LINKED **START, int DATA, int RDATA) 2 | { 3 | LINKED *PTR = (*START); 4 | if(PTR->DATA == DATA) 5 | { 6 | PTR->DATA = RDATA; 7 | } 8 | else 9 | if(PTR->prev->DATA == DATA) 10 | { 11 | PTR->prev->DATA = RDATA; 12 | } 13 | else 14 | { 15 | while(PTR->next != (*START) && PTR->DATA != DATA) 16 | { 17 | PTR = PTR->next; 18 | } 19 | if(PTR->next == (*START)) 20 | { 21 | cout << DATA << " NOT FOUND\n"; 22 | return; 23 | } 24 | PTR->DATA = RDATA; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Chapter-09/SUPPLEMENTARY PROBLEMS/9-1.CPP: -------------------------------------------------------------------------------- 1 | /* 2 | page 9-1 3 | 9.1   Write a subprogram RANDOM(DATA, N, K) which assigns N random integers between 1 and K to the array DATA. 4 | */ 5 | #include 6 | #include 7 | using namespace std; 8 | void RANDOM(int ARRAY[], int N, int K) 9 | { 10 | srand(time(0)); 11 | for(int i = 0; i < K; i++) 12 | { 13 | ARRAY[i] = rand()%N; 14 | } 15 | return; 16 | 17 | } 18 | int main() 19 | { 20 | int K = 10 , N = 10; 21 | int ARRAY[K]; 22 | RANDOM(ARRAY, N, K); 23 | for(int i: ARRAY) 24 | { 25 | cout << i << " "; 26 | } 27 | } 28 | /* 29 | 6 6 7 4 5 2 0 7 7 2 30 | */ -------------------------------------------------------------------------------- /Chapter-03/3-22.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 64 3 | Program 3.22 4 | 5 | */ 6 | #include 7 | #include 8 | using namespace std; 9 | struct TEST 10 | { 11 | string s; 12 | int num; 13 | }; 14 | 15 | int main() 16 | { 17 | TEST T[12]; 18 | for(int i = 0 ; i <= 12; i++) 19 | { 20 | cout << "Enter text\n"; 21 | getline(cin,T[i].s); 22 | cout << "Enter number\n"; 23 | cin >> T[i].num; 24 | getchar(); 25 | } 26 | int j = 5; 27 | while(T[j].num != 0) 28 | { 29 | cout << T[j].s; 30 | j = T[j].num; 31 | } 32 | 33 | 34 | return 0; 35 | } 36 | /* 37 | Output 38 | 39 | */ -------------------------------------------------------------------------------- /Chapter-03/3-27.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 65 3 | Program 3-27 4 | In Supplementary Problems 3.5 to 3.10, let S and T be character variables such that 5 | 6 | S = 'WE THE PEOPLE'    and    T = 'OF THE UNITED STATES' 7 | 8 | 3.5 Find the length of S and T 9 | 10 | 3.6 Find (a) SUBSTRING(S, 4, 8) and (b) SUBSTRING(T, 10, 5). 11 | */ 12 | #include 13 | #include 14 | using namespace std; 15 | int main() 16 | { 17 | string S("WE THE PEOPLE"), T("OF THE UNITED STATES"); 18 | cout << S.length() << " " << T.length() << endl; 19 | cout << S.substr(4,8) << endl; 20 | cout << " " << T.substr(10,5) << endl; 21 | 22 | return 0; 23 | } 24 | /* 25 | Output 26 | 27 | */ -------------------------------------------------------------------------------- /Chapter-03/Algorithm/Algorithm3-1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Program 3 | 4 | */ 5 | #include 6 | #include 7 | using namespace std; 8 | //main begin 9 | int main() 10 | { 11 | string T, P; 12 | 13 | cout << "Enter your Text\n"; 14 | getline(cin,T); 15 | cout << "Enter your keyword\n"; 16 | cin >> P; 17 | size_t K = T.find(P); 18 | while(K != string::npos) 19 | { 20 | T.erase(K, P.length()); 21 | K = T.find(P); 22 | } 23 | cout << T << endl; 24 | return 0; 25 | } 26 | //main end 27 | ///Program end 28 | 29 | /* 30 | output 31 | Enter your Text 32 | ZAAABBBY 33 | Enter your keyword 34 | AB 35 | ZY 36 | */ 37 | -------------------------------------------------------------------------------- /Chapter-09/ALGORITHM/Algorithm-9-1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | template 4 | void insertion_sort(T A[], int N) 5 | { 6 | T temp; 7 | int hole; 8 | for (int i = 1; i < N - 1; i++) 9 | { 10 | temp = A[i]; 11 | hole = i; 12 | while(hole>0 && A[hole-1] > temp) 13 | { 14 | A[hole] = A[hole-1]; 15 | hole = hole -1; 16 | } 17 | A[hole] = temp; 18 | } 19 | } 20 | int main() 21 | { 22 | int A[] = {2, 7, 4, 1, 5, 3}; 23 | insertion_sort(A, 6); 24 | for (int i : A) 25 | { 26 | cout << i << " "; 27 | } 28 | } 29 | /* 30 | 1 2 3 4 5 7 31 | */ -------------------------------------------------------------------------------- /Chapter-06/SOLVED PROBLEMS/6-15.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 6.16 Let n denote a positive integer. Suppose a function L is defined. recursively as follows: 3 | 4 | 0 if(n = 1) 5 | L(n) = 6 | L(floor(n/2))+1 if(n > 1) 7 | 8 | (Here [k] denotes the “floor” of k, that is, the greatest integer which does not exceed k. See Sec. 2.2.) 9 | 10 | (a) Find L(25). 11 | 12 | (b) What does this function do? 13 | */ 14 | #include 15 | #include 16 | using namespace std; 17 | int L(int N) 18 | { 19 | if( N == 1) 20 | { 21 | return 1; 22 | } 23 | return (L(floor(N/2))+1); 24 | } 25 | int main() 26 | { 27 | cout << L(12) << endl; 28 | } 29 | /* 30 | 3 31 | */ -------------------------------------------------------------------------------- /Tree/Insert.h: -------------------------------------------------------------------------------- 1 | TreeNode *NewNode(int DATA) 2 | { 3 | TreeNode *node = new TreeNode; 4 | node->DATA = DATA; 5 | node->lchild = node->rchild = nullptr; 6 | return node; 7 | } 8 | void INSERT(TreeNode **root, int DATA) 9 | { 10 | if (*root == NULL) 11 | { 12 | *root = NewNode(DATA); 13 | return; 14 | } 15 | TreeNode *temp = *root, *parentNode = NULL; 16 | while (temp != NULL) 17 | { 18 | parentNode = temp; 19 | temp = (DATA > temp->DATA) ? temp->rchild : temp->lchild; 20 | } 21 | if (DATA > parentNode->DATA) 22 | { 23 | parentNode->rchild = NewNode(DATA); 24 | } 25 | else 26 | parentNode->lchild = NewNode(DATA); 27 | } -------------------------------------------------------------------------------- /Chapter-04/Supplementary Problems/4-21.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 109 3 | 4 | Program 5 | Consider the linear arrays XXX(−10 : 10), YYY(1935 : 1985), ZZZ(35). 6 | (a) Find the number of elements in each array. 7 | (b) Suppose Base(YYY) = 400 and w = 4 words per memory cell for YYY. 8 | Find the address of YYY[1942], YYY[1977] and YYY[1988]. 9 | */ 10 | #include 11 | #include 12 | using namespace std; 13 | //Main from Here 14 | int main() 15 | { 16 | int J, K; 17 | J = -10; 18 | K = 10; 19 | int XXX[K-J]; 20 | J = 1935; 21 | K = 1985; 22 | int YYY[K - J]; 23 | int ZZZ[35]; 24 | 25 | //LOC(LA[K]) = BASE(LA) + W(K - 1); 26 | return ; 27 | } 28 | 29 | /* 30 | Output 31 | 32 | */ -------------------------------------------------------------------------------- /Chapter-02/Programming Problems/2-23.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 40 3 | Program 4 | Write a function subprogram DIV(J, K), where J and K are 5 | positive integers such that DIV(J, K) = 1 if J divides K 6 | but otherwise DIV(J, K) = 0. (For example, DIV(3, 15) = 1 7 | but DIV(3, 16) = 0.) 8 | 9 | */ 10 | #include 11 | using namespace std; 12 | 13 | template 14 | T DIV(T J, T K) 15 | { 16 | if(K % J== 0) 17 | return 1; 18 | else 19 | return 0; 20 | } 21 | 22 | //main begin 23 | 24 | int main() 25 | { 26 | cout << DIV(3,15) << endl; 27 | cout << DIV(3,16) << endl; 28 | getchar(); 29 | return 0; 30 | } 31 | //main end 32 | ///Program end 33 | 34 | /* 35 | output 36 | */ -------------------------------------------------------------------------------- /Chapter-03/3-32.CPP: -------------------------------------------------------------------------------- 1 | /* 2 | Page 65 3 | Program 4 | Find 5 | (a) INSERT('AAA', 2, 'BBB'), 6 | (b) INSERT('ABCDE', 3, 'XYZ') and 7 | (c) INSERT ('THE BOY', 5, 'BIG'). 8 | */ 9 | #include 10 | #include 11 | using namespace std; 12 | string INSERT(string T, int i , string R) 13 | { 14 | T.insert(i,R); 15 | return T; 16 | } 17 | int main() 18 | { 19 | string R; 20 | string S("WE THE PEOPLE"); 21 | string T("OF THE UNITED STATES"); 22 | cout << INSERT("AAA", 1, "BBB") << endl; 23 | cout << INSERT("ABCDE", 2, "XYZ") << endl; 24 | cout << INSERT("THE BOY", 4, "BIG ") << endl; 25 | 26 | 27 | return 0; 28 | } 29 | /* 30 | Output 31 | ABBBAA 32 | ABXYZCDE 33 | THE BIG BOY 34 | */ -------------------------------------------------------------------------------- /Chapter-06/SOLVED PROBLEMS/6-14.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 6.14 3 | 6.15 Let a and b denote positive integers. Suppose a function Q is defined recursively as follows: 4 | 5 | 0 if( a < b) 6 | Q(a,b) = 7 | Q(a - b, b)+1 if(b 14 | using namespace std; 15 | 16 | int Q(int a, int b) 17 | { 18 | if(a < b) 19 | { 20 | return 0; 21 | } 22 | return (Q(a - b, b)+1); 23 | } 24 | int main() 25 | { 26 | cout << Q(2,3) << endl; 27 | cout << Q(14,3) << endl; 28 | cout << Q(5861,7) << endl; 29 | } 30 | /* 31 | output 32 | 0 33 | 4 34 | 837 35 | */ -------------------------------------------------------------------------------- /Chapter-05/LINKED LIST/SORT.h: -------------------------------------------------------------------------------- 1 | void SWAP(LINKED *FIRST, LINKED *SECOND) 2 | { 3 | int TEMP = FIRST->DATA; 4 | FIRST->DATA = SECOND->DATA; 5 | SECOND->DATA = TEMP; 6 | } 7 | void BUBBLE_SORT(LINKED *START) 8 | { 9 | int SWAPPED, i; 10 | LINKED *PTR, *LAST = START; 11 | if(START == NULL) 12 | { 13 | return; 14 | } 15 | do 16 | { 17 | SWAPPED = 0; 18 | PTR = START; 19 | while(PTR->next != LAST) 20 | { 21 | if(PTR->DATA > PTR->next->DATA) 22 | { 23 | SWAP(PTR, PTR->next); 24 | SWAPPED = 1; 25 | } 26 | PTR = PTR->next; 27 | } 28 | LAST = PTR; 29 | }while (SWAPPED); 30 | } 31 | -------------------------------------------------------------------------------- /Chapter-06/SUPPLEMENTARY PROBLEMS/6-42.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 6.17 Let M and N be integers and suppose F(M, N) is recursively defined by 3 | 4 | 1 if(M == 0 || (M >= N && N >= 1)) 5 | F(M, N) = 6 | F(M -1 , N) + F (M - 1, N - 1); otherwise 7 | 8 | (a) Find F(4, 2), F(1, 5) and F(2, 4). 9 | 10 | (b) When is F(M, N) undefined? 11 | */ 12 | #include 13 | using namespace std; 14 | int F(int M,int N) 15 | { 16 | if(M == 0 || (M >= N && N >= 1)) 17 | { 18 | return 1; 19 | } 20 | return (F(M -1 , N) + F (M - 1, N - 1)); 21 | } 22 | int main() 23 | { 24 | cout << F(4, 2) << endl; 25 | cout << F(1, 5) << endl; 26 | cout << F(2, 4) << endl; 27 | } 28 | /* 29 | 1 30 | 2 31 | 4 32 | */ -------------------------------------------------------------------------------- /Chapter-06/ALGORITHM/Algorithm-6-7.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Procedure 6.9B: FACTORIAL(FACT, N) 3 | 4 | This procedure calculates N! and returns the value in the variable FACT. 5 | 6 | 1. If N = 0, then: Set FACT := 1, and Return. 7 | 8 | 2. Call FACTORIAL(FACT, N – 1). 9 | 10 | 3. Set FACT := N*FACT. 11 | 12 | 4. Return. 13 | */ 14 | #include 15 | #include 16 | using namespace std; 17 | int FACTORIAL(int n) 18 | { 19 | if (n <= 1) 20 | return n; 21 | return (n * FACTORIAL(n-1)); 22 | } 23 | 24 | int main () 25 | { 26 | int n; 27 | cout << "Enter an integer number \n"; 28 | cin >> n; 29 | cout<< n <<"'s FACTORIAL is = " << FACTORIAL(n); 30 | getchar(); 31 | return 0; 32 | } 33 | /* 34 | Enter an integer number 35 | 12 36 | 12's FACTORIAL is = 479001600 37 | */ -------------------------------------------------------------------------------- /Chapter-04/Programming Problems/4-32.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 110 3 | Program 4 | rch algorithm into a subprogram LINEAR(ARRAY, LB, UB, ITEM, LOC) 5 | which either finds the location LOC where ITEM appears in ARRAY or returns LOC = 0. 6 | */ 7 | #include 8 | #include 9 | using namespace std; 10 | /****************Linear Search*********************/ 11 | template 12 | int LINEAR(T DATA[],int LB, int UB, T ITEM) 13 | { 14 | int K = LB; 15 | int LOC = -1; 16 | while(LOC == -1 && K <= UB) 17 | { 18 | if(ITEM == DATA[K]) 19 | LOC = K ; 20 | K++; 21 | } 22 | if(LOC == -1) 23 | { 24 | INSERT(DATA,N,N,ITEM); 25 | } 26 | return LOC+1; 27 | } 28 | //Main from Here 29 | int main() 30 | { 31 | 32 | } 33 | 34 | /* 35 | Output 36 | 37 | */ -------------------------------------------------------------------------------- /Chapter-03/3-30.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 3 | Program 4 | Find 5 | (a) DELETE('AAABBB', 3, 3), 6 | (b) DELETE('AAABBB', 1, 4), 7 | (c) DELETE(S, 1, 3) and 8 | (d) DELETE(T, 1, 7). 9 | */ 10 | #include 11 | #include 12 | using namespace std; 13 | string DELETE(string S, int i, int j) 14 | { 15 | string Q; 16 | S.erase(i,j); 17 | return S; 18 | } 19 | int main() 20 | { 21 | 22 | string R; 23 | string S("WE THE PEOPLE"); 24 | string T("OF THE UNITED STATES"); 25 | R = DELETE("AAABBB", 2,3); 26 | cout << R << endl; 27 | R = DELETE("AAABBB", 0,4); 28 | cout << R << endl; 29 | R = DELETE(S, 0, 3); 30 | cout << R << endl; 31 | R = DELETE(T, 0, 7); 32 | cout << R << endl; 33 | return 0; 34 | } 35 | /* 36 | Output 37 | AAB 38 | BB 39 | THE PEOPLE 40 | UNITED STATES 41 | */ -------------------------------------------------------------------------------- /Chapter-06/ALGORITHM/Algorithm-6-8.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 181 3 | Procedure 6.10: FIBONACCI(FIB, N) 4 | 5 | This procedure calculates FN and returns the value in the first parameter FIB. 6 | 7 | 1. If N = 0 or N = 1, then: Set FIB := N, and Return. 8 | 9 | 2. Call FIBONACCI(FIBA, N – 2). 10 | 11 | 3. Call FIBONACCI(FIBB, N – 1). 12 | 13 | 4. Set FIB := FIBA + FIBB. 14 | 15 | 5. Return. 16 | */ 17 | #include 18 | using namespace std; 19 | int FIBONACCHI(int N) 20 | { 21 | if(N <= 1) 22 | { 23 | return N; 24 | } 25 | return FIBONACCHI(N-1)+FIBONACCHI(N-2); 26 | } 27 | int main() 28 | { 29 | int N; 30 | cout << "Enter a number\n"; 31 | cin >> N; 32 | cout<< N << "'s fibonacchi is = " << FIBONACCHI(N); 33 | return 0; 34 | } 35 | /* 36 | Enter a number 37 | 30 38 | 30's fibonacchi is = 832040 39 | */ -------------------------------------------------------------------------------- /Chapter-03/3-8.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | page 60 3 | program 3.9 4 | 5 | */ 6 | #include 7 | #include 8 | using namespace std; 9 | int LENGTH(char const *S) 10 | { 11 | int i = 0; 12 | while(S[i++] != '\0'); 13 | return i; 14 | } 15 | char *SUBSTR(char const *S, int from, int to) 16 | { int i; 17 | char *str = new char[to + 1]; 18 | for( i = 0; i < to && S[i+from] != '\0'; i++) 19 | { 20 | str[i] = S[i+from]; 21 | } 22 | str[i] = '\0'; 23 | return str; 24 | 25 | } 26 | int main() 27 | { 28 | char const *S = "JOHN PAUL JONES", *T = "A THING OF BEAUTY IS A JOY FOREVER"; 29 | cout << S << endl; 30 | cout << T << endl; 31 | cout << LENGTH(S) << endl; 32 | cout << LENGTH(T) << endl; 33 | } 34 | 35 | /* 36 | JOHN PAUL JONES 37 | A THING OF BEAUTY IS A JOY FOREVER 38 | 16 39 | 35 40 | */ -------------------------------------------------------------------------------- /Chapter-09/SUPPLEMENTARY PROBLEMS/9-7.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | page 338 3 | 9.7   Translate Procedure 9.2 into a subprogram MIN(A, LB, UB, LOC) 4 | which finds the location LOC of the smallest elements among A[LB], A[LB + 1], …, A[UB]. 5 | */ 6 | #include 7 | using namespace std; 8 | template 9 | int MIN(T A[],int LB, int UB, int N) 10 | { 11 | T temp; 12 | int hole; 13 | for (int i = LB; i < UB-1; i++) 14 | { 15 | temp = A[i]; 16 | hole = i; 17 | while (hole > 0 && A[hole - 1] > temp) 18 | { 19 | A[hole] = A[hole - 1]; 20 | hole = hole - 1; 21 | } 22 | A[hole] = temp; 23 | } 24 | return A[LB]; 25 | } 26 | int main() 27 | { 28 | int A[] = {44, 33, 11, 55, 77, 90, 40, 60, 99, 22, 88, 66}; 29 | cout << MIN(A, 0, 12, 13) << " "; 30 | } 31 | /* 32 | 11 33 | */ -------------------------------------------------------------------------------- /Chapter-03/3-31.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 65 3 | Program 4 | Find 5 | (a) REPLACE('ABABAB', 'B', 'BAB'), 6 | (b) REPLACE(S, 'WE', 'ALL') and 7 | (c) REPLACE(T, 'THE', 'THESE'). 8 | 9 | */ 10 | #include 11 | #include 12 | using namespace std; 13 | string REPLACE(string S, string Q, string Z) 14 | { 15 | int i = S.find(Q); 16 | S.replace(S.find(Q),Q.length(),Z); 17 | return S; 18 | 19 | } 20 | int main() 21 | { 22 | string R; 23 | string S("WE THE PEOPLE"); 24 | string T("OF THE UNITED STATES"); 25 | R = REPLACE("ABABAB", "B", "BAB"); 26 | cout << R << endl; 27 | R = REPLACE(S, "WE", "ALL"); 28 | cout << R << endl; 29 | R = REPLACE(T, "THE", "THESE"); 30 | cout << R << endl; 31 | 32 | 33 | return 0; 34 | } 35 | /* 36 | Output 37 | ABABABAB 38 | ALL THE PEOPLE 39 | OF THESE UNITED STATES 40 | 41 | */ -------------------------------------------------------------------------------- /Chapter-06/SUPPLEMENTARY PROBLEMS/6-45.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | page 212 3 | 6.20 Suppose S is a string with N characters. Let SUB(S, J, L) 4 | denote the substring of S beginning in the position J and having 5 | length L. Let A//B denote the concatenation of strings A and B. 6 | Suppose REV(S, N) is recursively defined by 7 | 8 | 9 | 10 | (a) Find REV(S, N) when 11 | (i) N = 3, S = abc and (ii) N = 5, S = ababc. 12 | (b) What does this function do? 13 | */ 14 | #include 15 | #include 16 | using namespace std; 17 | string REV(string S, int N) 18 | { 19 | if(N == 1) 20 | { 21 | return S; 22 | } 23 | return (S.substr(N,1)+REV(S.substr(1,N-1),N-1)); 24 | } 25 | int main() 26 | { 27 | string S = "ABC"; 28 | 29 | cout << REV(S,2) << endl; 30 | S = "ababc"; 31 | cout << REV(S,3) << endl; 32 | } 33 | /* 34 | CB 35 | ba 36 | */ -------------------------------------------------------------------------------- /Tree/Check_BST.h: -------------------------------------------------------------------------------- 1 | bool IsSubtreeLesser(TreeNode *root, int value) 2 | { 3 | if (root == NULL) 4 | return true; 5 | if (root->DATA <= value && IsSubtreeLesser(root->lchild, value) && IsSubtreeLesser(root->rchild, value)) 6 | return true; 7 | else 8 | return false; 9 | } 10 | bool IsSubtreeGreater(TreeNode *root, int value) 11 | { 12 | if (root == NULL) 13 | return true; 14 | if (root->DATA > value && IsSubtreeGreater(root->lchild, value) && IsSubtreeGreater(root->rchild, value)) 15 | return true; 16 | else 17 | return false; 18 | } 19 | bool IsBinarySearchTree(TreeNode *root) 20 | { 21 | if (IsSubtreeLesser(root->lchild, root->DATA) && IsSubtreeGreater(root->rchild, root->DATA) && IsBinarySearchTree(root->lchild) && IsBinarySearchTree(root->rchild)) 22 | return true; 23 | else 24 | return false; 25 | } -------------------------------------------------------------------------------- /Chapter-02/Solved Problem/2-4.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | page 34 3 | Program 4 | 2.4 Find: (a) 2^-5, 8^(2/3), 25^(−3/2); (b)log2 32, log10 (1000), log2 (1/16); (c) log2 (1000), log2 (0.01). 5 | 6 | */ 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | //main begin 12 | int main() 13 | { 14 | //((a) 2^-5) ,8^(2/3), 25^(−3/2); 15 | cout << pow(2,(-5)) << endl; 16 | cout << 1/pow(2,5) << endl; 17 | cout << pow((cbrt(8)),2) << endl; 18 | cout << float(1/125) << endl; 19 | cout << "*********************************\n"; 20 | cout << log2(32) << endl; 21 | cout << log10(1000) << endl; 22 | cout << log2(1/16) << endl; 23 | 24 | cout << "*********************************\n"; 25 | cout << floor(log2(1000)) << endl; 26 | cout << floor(log2(0.01)) << endl; 27 | 28 | return 0; 29 | } 30 | //main end 31 | ///Program end 32 | 33 | /* 34 | output 35 | */ -------------------------------------------------------------------------------- /Chapter-03/3-29.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 65 3 | Program 4 | Using S1//S2 to stand for the concatenation of S1 and S2, find 5 | (a) 'NO'//'EXIT', 6 | (b) 'NO'// '' // 'EXIT' and 7 | (c) SUBSTRING(S, 4, 10)//'ARE' //SUBSTRING(T, 8, 6). 8 | */ 9 | #include 10 | #include 11 | using namespace std; 12 | string STRINGCAT(string S, string O) 13 | { 14 | string X = S + O; 15 | return X; 16 | } 17 | string STRINGCAT(string S, string O, string T) 18 | { 19 | string X = S + O + T; 20 | return X; 21 | } 22 | int main() 23 | { 24 | string S("WE THE PEOPLE"); 25 | string T("OF THE UNITED STATES"); 26 | string S1("NO"), S2("EXIT"); 27 | cout << STRINGCAT(S1, S2) << endl; 28 | cout << STRINGCAT(S1, " ", S2) << endl; 29 | cout << STRINGCAT(S.substr(4-1,10), " ARE ", T.substr(8-1,6)); 30 | return 0; 31 | } 32 | /* 33 | Output 34 | 35 | NOEXIT 36 | NO EXIT 37 | THE PEOPLE ARE UNITED 38 | */ -------------------------------------------------------------------------------- /Chapter-03/3-33.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 65 3 | Program 3.33 4 | Suppose U is the text 'MARC STUDIES MATHEMATICS'. 5 | Use INSERT to change U so that it reads: 6 | (a) MARC STUDIES ONLY MATHEMATICS. 7 | (b) MARC STUDIES MATHEMATICS AND PHYSICS. 8 | (c) MARC STUDIES APPLIED MATHEMATICS. 9 | */ 10 | #include 11 | #include 12 | using namespace std; 13 | string INSERT(string T, int i , string R) 14 | { 15 | T.insert(i,R); 16 | return T; 17 | } 18 | int main() 19 | { 20 | string U("MARC STUDIES MATHEMATICS."); 21 | cout << INSERT(U, U.find("MATHEMATICS."), "ONLY ") << endl; 22 | cout << INSERT(U, U.length()-1, " AND PHYSICS") << endl; 23 | cout << INSERT(U, U.find("MATHEMATICS"), "APPLY ") << endl; 24 | 25 | 26 | return 0; 27 | } 28 | /* 29 | Output 30 | MARC STUDIES ONLY MATHEMATICS. 31 | MARC STUDIES MATHEMATICS AND PHYSICS. 32 | MARC STUDIES APPLY MATHEMATICS. 33 | */ -------------------------------------------------------------------------------- /Chapter-04/Solved Problems/4-16.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 106 3 | Program 4 | 4.16 The following is a list of entries, with level numbers, in a student’s record: 5 | 1 Student 2 Number 2 Name 3 Last 3 First 3 MI (Middle Initial) 2 Sex 6 | 2 Birthday 3 Day 3 Month 3 Year 2 SAT 3 Math 3 Verbal 7 | (a) Draw the corresponding hierarchical structure. 8 | (b) Which of the items are elementary items? 9 | */ 10 | #include 11 | #include 12 | using namespace std; 13 | struct Student 14 | { 15 | int Number; 16 | struct NAME 17 | { 18 | string Last; 19 | string First; 20 | string Middle_Initial; 21 | }Name; 22 | char Sex; 23 | struct Birthday 24 | { 25 | int Day; 26 | int Month; 27 | int Year; 28 | }; 29 | struct S 30 | { 31 | string Math; 32 | string Verbal; 33 | }SAT; 34 | }; 35 | 36 | /* 37 | Output 38 | 39 | */ -------------------------------------------------------------------------------- /Chapter-06/SUPPLEMENTARY PROBLEMS/6-39.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 6.14 Let A and B be nonnegative integers. Suppose a 3 | function GCD is recursively defined as follows: 4 | 5 | GCD(B,A) if(A < B) 6 | GCD(A,B) = A if B = 0 7 | GCD(B, MOD(A,B)) otherwise 8 | 9 | (Here MOD(A, B), read “A modulo B,” denotes the 10 | remainder when A is divided by B.) (a) Find GCD(6, 15), 11 | GCD(20, 28) and GCD(540, 168). (b) What does this function do? 12 | 13 | */ 14 | #include 15 | #include 16 | using namespace std; 17 | int GCD(int A, int B) 18 | { 19 | if(ADATA); 9 | SPTR = SPTR->next; 10 | } while (SPTR->next != START); 11 | INSERT_END(&TEMP, SPTR->DATA); 12 | SPTR = SPTR->next; 13 | do 14 | { 15 | INSERT_END(&TEMP, LPTR->DATA); 16 | LPTR = LPTR->next; 17 | } while (LPTR->next != LAST); 18 | INSERT_END(&TEMP, LPTR->DATA); 19 | LPTR = LPTR->next; 20 | return TEMP; 21 | } 22 | 23 | void ADD_TWO_LIST(LINKED **START, LINKED *LAST) 24 | { 25 | LINKED *SFIRST, *SLAST, *LFIRT, *LLAST; 26 | SFIRST = (*START); 27 | SLAST = (*START)->prev; 28 | LFIRT = (LAST); 29 | LLAST = (LAST->prev); 30 | 31 | (*START)->prev->next = LAST; 32 | LAST->prev = (*START)->prev; 33 | LLAST->next = (*START); 34 | (*START)->prev = LLAST; 35 | } -------------------------------------------------------------------------------- /Chapter-04/Supplementary Problems/4-22.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 109 3 | Program 4 | Consider the following multidimensional arrays: 5 | 6 | X(−5 : 5, 3:33)    Y(3 : 10, 1 : 15, 10 : 20) 7 | 8 | (a) Find the length of each dimension and the number of elements in X and Y. 9 | 10 | (b) Suppose Base(Y) = 400 and there are w = 4 words per memory location. 11 | Find the effective indices E1, E2, E3 and the address of Y[5, 10, 15] 12 | assuming (i) Y is stored in row-major order and (ii) Y is stored in column-major order. 13 | */ 14 | #include 15 | #include 16 | using namespace std; 17 | //Main from Here 18 | int main() 19 | { 20 | int J , K, X, Y, Z, N; 21 | J = -5; 22 | K = 5; 23 | X = 3; 24 | Y = 33; 25 | N = K - J; 26 | Z = Y - X; 27 | int Ar[N][X]; 28 | J = 3; 29 | K = 10; 30 | X = 1; 31 | Y = 15; 32 | Z = 10; 33 | N = 20; 34 | int YYY[K - J][Y - X][N-Z]; 35 | } 36 | 37 | /* 38 | Output 39 | 40 | */ -------------------------------------------------------------------------------- /Chapter-03/3-10.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int INDEX(string &T, string P) 5 | { 6 | size_t K, MAX, S, R, INDEX = 0; 7 | 8 | S = T.length(); //length of Main string 9 | R = P.length(); // Length of sub string 10 | K = 0; 11 | MAX = S - R + 1; 12 | while(K <= MAX) 13 | { 14 | for(int L = 0; L < R; L++) 15 | { 16 | if(P[L] != T[K + L -1]) 17 | goto Lable; 18 | } 19 | INDEX = K; 20 | break; 21 | Lable: 22 | K++; 23 | } 24 | return INDEX; 25 | 26 | } 27 | int main() 28 | { 29 | string S("JOHN PAUL JONES"), T("A THING OF BEAUTY IS A JOY FOREVER"); 30 | cout << INDEX(S,"JO") << endl; 31 | cout << INDEX(S, "JOY") << endl; 32 | cout << INDEX(S, " JO") << endl; 33 | cout << INDEX(T, "A") << endl; 34 | cout << INDEX(T, " A ") << endl; 35 | cout << INDEX(T, "THE") << endl; 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Chapter-04/Solved Problems/4-18.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 107 3 | Program 4 | 4.18 A college uses the following structure for a graduating class: 5 | 1.Student(200) 6 | 2. Name 7 | 3. Last 8 | 3. First 9 | 3. Middle Initial 10 | 2. Major 11 | 2. SAT 12 | 3. Verbal 13 | 3. Math 14 | 2. GPA(4) 15 | 2. CUM 16 | */ 17 | #include 18 | #include 19 | struct Student 20 | { 21 | struct NAME 22 | { 23 | string Last; 24 | string First; 25 | string Middle_Initial; 26 | }Name; 27 | string Major; 28 | struct S 29 | { 30 | string Math; 31 | string Verbal; 32 | }SAT; 33 | float GPA[4]; 34 | float CUM; 35 | }; 36 | using namespace std; 37 | //Main from Here 38 | int main() 39 | { 40 | /* 41 | Student STU[200]; 42 | STU[8].Major; 43 | STU[45].GPA[2]; 44 | */ 45 | 46 | 47 | } 48 | 49 | /* 50 | Output 51 | 52 | */ -------------------------------------------------------------------------------- /Chapter-03/3-13.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 59 3 | Program 4 | (a) DELETE('AAABBB', 2, 2) and DELETE('JOHN PAUL JONES', 6, 5) 5 | 6 | (b) REPLACE('AAABBB', 'AA', 'BB') and REPLACE('JOHN PAUL JONES', 'PAUL', 'DAVID') 7 | */ 8 | #include 9 | #include 10 | #include 11 | using namespace std; 12 | string DELETE(string S, int i, int j) 13 | { 14 | string Q; 15 | S.erase(i-1,j); 16 | return S; 17 | } 18 | 19 | string REPLACE(string S, string Q, string Z) 20 | { 21 | int i = S.find(Q); 22 | S.replace(S.find(Q),Q.length(),Z); 23 | return S; 24 | 25 | } 26 | int main() 27 | { 28 | string R; 29 | R = DELETE("AAABBB", 2,2); 30 | cout << R << endl; 31 | R = DELETE("JOHN PAUL JONES", 6, 5); 32 | cout << R << endl; 33 | R = REPLACE("AAABBB", "AA", "BB"); 34 | cout << R << endl; 35 | R = REPLACE("JOHN PAUL JONES", "PAUL", "DEVID"); 36 | cout << R << endl; 37 | 38 | return 0; 39 | } 40 | /* 41 | ABBB 42 | JOHN JONES 43 | BBABBB 44 | JOHN DEVID JONES 45 | */ -------------------------------------------------------------------------------- /Chapter-06/ALGORITHM/Algorithm-6-9.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | page 183 3 | Procedure 6.11: TOWER(N, BEG, AUX, END) 4 | 5 | This procedure gives a recursive solution to the Towers of Hanoi problem for N disks. 6 | 7 | 1. If N = 1, then: 8 | 9 | (a) Write: BEG → END. 10 | 11 | (b) Return. 12 | 13 | [End of If structure.] 14 | */ 15 | #include 16 | #include 17 | using namespace std; 18 | void TOWER(int N, string BEG, string AUX, string END) 19 | { 20 | if(N > 0) 21 | { 22 | TOWER(N-1, BEG, END, AUX); 23 | cout << "Move top disk form pag " << BEG << " to pag " << END << endl; 24 | TOWER(N-1, AUX, BEG, END); 25 | } 26 | } 27 | int main() 28 | { 29 | TOWER(3, "A", "B", "C"); 30 | return 0; 31 | } 32 | /* 33 | Move top disk form pag A to pag C 34 | Move top disk form pag A to pag B 35 | Move top disk form pag C to pag B 36 | Move top disk form pag A to pag C 37 | Move top disk form pag B to pag A 38 | Move top disk form pag B to pag C 39 | Move top disk form pag A to pag C 40 | */ -------------------------------------------------------------------------------- /Chapter 01/Example/Example01-02.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Program example 1.2 3 | A linear array STUDENT consisting of the names of six students in picture 4 | in Fig 1.1, Here STUDENT[1] denotes John Brown STUDENT[2] Sandra Gold 5 | STUDENT[3] Tom Jones STUDENT[4]June Kelly 6 | and so on. 7 | 8 | */ 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | //main begin 14 | int main() 15 | { 16 | string STUDENT[5]; 17 | cout <<"Enter Studet name " << "\n"; 18 | for(int i = 0; i < 5; i++) 19 | { 20 | getline(cin, STUDENT[i]); 21 | } 22 | cout << "\n"; 23 | for (int i = 0; i < 5; i++) 24 | { 25 | cout << STUDENT[i] << "\n"; 26 | } 27 | cout << "\n"; 28 | getchar(); 29 | return 0; 30 | } 31 | //main end 32 | ///Program end 33 | 34 | /* 35 | output 36 | Enter Studet name 37 | John Brown 38 | Sandra Gold 39 | Tom Jones 40 | June Kelly 41 | Marry Reed 42 | 43 | John Brown 44 | Sandra Gold 45 | Tom Jones 46 | June Kelly 47 | Marry Reed 48 | */ 49 | -------------------------------------------------------------------------------- /Tree/Inroder_Succsesor.h: -------------------------------------------------------------------------------- 1 | TreeNode *Find(TreeNode *root, int data) 2 | { 3 | if (root == NULL) 4 | return NULL; 5 | else if (root->DATA == data) 6 | return root; 7 | else if (root->DATA < data) 8 | return Find(root->rchild, data); 9 | else 10 | return Find(root->lchild, data); 11 | } 12 | TreeNode *GetSuccessor(TreeNode *root, int data) 13 | { 14 | TreeNode *current = Find(root, data); 15 | if (current == NULL) 16 | return NULL; 17 | if (current->rchild != NULL) 18 | { 19 | return FindMin(current->rchild); 20 | } 21 | else 22 | { 23 | TreeNode *successor = NULL, *ancestor = root; 24 | while (ancestor != current) 25 | { 26 | if (current->DATA < ancestor->DATA) 27 | { 28 | successor = ancestor; 29 | ancestor = ancestor->lchild; 30 | } 31 | else 32 | ancestor = ancestor->rchild; 33 | } 34 | return successor; 35 | } 36 | } -------------------------------------------------------------------------------- /Chapter-05/LINKED LIST/SEARCH.h: -------------------------------------------------------------------------------- 1 | int SEARCH(LINKED **START, int DATA) 2 | { 3 | LINKED *PTR = (*START); 4 | int count = 1; 5 | if(PTR->DATA == DATA) 6 | { 7 | return count; 8 | } 9 | else 10 | { 11 | while(PTR->next != (*START) && PTR->DATA != DATA) 12 | { 13 | PTR = PTR->next; 14 | count++; 15 | } 16 | if(PTR->DATA != DATA && PTR->next == (*START)) 17 | return 0; 18 | return count++; 19 | } 20 | } 21 | int SEARCH_SORTED_LIST(LINKED **START, int DATA) 22 | { 23 | LINKED *PTR = (*START); 24 | int count = 1; 25 | if(PTR->DATA == DATA) 26 | { 27 | return count; 28 | } 29 | else 30 | { 31 | while(PTR->next != (*START) && PTR->DATA != DATA) 32 | { 33 | if(PTR->DATA > DATA); 34 | { 35 | return 0; 36 | } 37 | PTR = PTR->next; 38 | count++; 39 | } 40 | if(PTR->DATA != DATA && PTR->next == (*START)) 41 | return 0; 42 | return count++; 43 | } 44 | } -------------------------------------------------------------------------------- /Chapter-05/LINKED LIST/DISPLAY.h: -------------------------------------------------------------------------------- 1 | 2 | void DISPLAY_FORWORD( LINKED **START) 3 | { 4 | if((*START) == NULL) 5 | { 6 | cout << "LIST IS EMPTY\n"; 7 | return; 8 | } 9 | LINKED *PTR = (*START); 10 | while (PTR->next != *START) 11 | { 12 | 13 | cout << PTR->DATA << "-> " ; 14 | PTR = PTR->next; 15 | } 16 | cout << PTR->DATA ; 17 | } 18 | void DISPLAY_REVERSE( LINKED **START) 19 | { 20 | if((*START) == NULL) 21 | { 22 | cout << "LIST IS EMPTY\n"; 23 | return; 24 | } 25 | LINKED *PTR = (*START)->prev; 26 | while (PTR != *START) 27 | { 28 | cout << PTR->DATA << "-> " ; 29 | PTR = PTR->prev; 30 | } 31 | cout << PTR->DATA ; 32 | } 33 | 34 | 35 | void NON_EMPTY(LINKED **START) 36 | { 37 | int COUNT = 0; 38 | LINKED *PTR = (*START); 39 | while(PTR->next != (*START)) 40 | { 41 | if(PTR->DATA != 0) 42 | COUNT++; 43 | PTR = PTR->next; 44 | } 45 | if(PTR->DATA != 0) 46 | COUNT++; 47 | cout << "\nNUMBER OF OCARENCE -> " << COUNT+1 << "\n"; 48 | } 49 | -------------------------------------------------------------------------------- /Chapter-09/Hashing/Code_of_Hashing.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define MAX 1000 5 | bool HASH[MAX + 1][2]; 6 | bool search(int x) 7 | { 8 | if (x >= 0) 9 | { 10 | if (HASH[x][0] == true) 11 | { 12 | return true; 13 | } 14 | } 15 | else 16 | { 17 | x = abs(x); 18 | if (x >= 0) 19 | { 20 | if (HASH[x][1] == true) 21 | { 22 | return true; 23 | } 24 | } 25 | } 26 | 27 | return false; 28 | } 29 | void insert(int a[], int n) 30 | { 31 | for (int i = 0; i < n; i++) 32 | { 33 | if (a[i] >= 0) 34 | { 35 | HASH[a[i]][0] = true; 36 | } 37 | else 38 | { 39 | HASH[abs(a[i])][1] = true; 40 | } 41 | } 42 | } 43 | int main() 44 | { 45 | int arr[] = {-1, 9, 2, -5, 3, 1}; 46 | int n = sizeof(arr) / sizeof(int); 47 | insert(arr, n); 48 | if (search(3)) 49 | { 50 | cout << "Found "; 51 | } 52 | else 53 | cout << "Result not found\n"; 54 | return 0; 55 | } -------------------------------------------------------------------------------- /Chapter-04/Supplementary Problems/4-26.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 109 3 | Program 4 | 4.26 5 | 4.6 Consider the data in Fig. 4.26(c). 6 | (a) Write a procedure which prints the list of clients belonging to LAWYER[K]. 7 | (b) Assuming CLIENT has space for 400 elements, define an array FREE such that 8 | FREE[K] contains the number of empty cells following the list of clients belonging 9 | to LAWYER[K]. 10 | */ 11 | #include 12 | #include 13 | using namespace std; 14 | struct LAWYER 15 | { 16 | string Name; 17 | //Number of clint 18 | int NUMB; 19 | string *PTR; 20 | //number of free space in Clint array 21 | int FREE; 22 | }; 23 | void CLIENT_PRINT(LAWYER L[],int M) 24 | { 25 | 26 | string *LAST = (L[M].PTR) - 1; 27 | cout << L[M-1].Name << "'s Clint are\n"; 28 | for( string *FIRST = L[M-1].PTR; FIRST < LAST; FIRST++) 29 | { 30 | cout << *FIRST << endl; 31 | } 32 | 33 | } 34 | //Main from Here 35 | int main() 36 | { 37 | string CLIENT[400]; 38 | LAWYER L[3]; 39 | L[0].PTR = &CLIENT[0]; 40 | L[1].PTR = &CLIENT[150]; 41 | L[2].PTR = &CLIENT[300]; 42 | } 43 | 44 | /* 45 | Output 46 | 47 | */ -------------------------------------------------------------------------------- /Chapter-04/Solved Problems/4-7.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Algorithm P4.7: (Binary Search and Insertion) 3 | DATA is a sorted array with N elements, and 4 | ITEM is a given item of information. 5 | This algorithm finds the location LOC of 6 | ITEM in DATA or inserts ITEM in its proper place in DATA. 7 | 8 | Steps 1 through 4. Same as in Algorithm 4.6. 9 | 5. If ITEM < DATA[MID), then: 10 | Set LOC := MID. 11 | Else: 12 | Set LOC := MID + 1. 13 | [End of If structure.] 14 | 6. Insert ITEM into DATA[LOC] using Algorithm 4.2. 15 | 7. Exit 16 | */ 17 | template 18 | int BINARY(T DATA[], int LB, int UB, T ITEM, int LOC) 19 | { 20 | int BEG, END, MID; 21 | BEG = LB; 22 | END = UB; 23 | MID = (int)((LB+UB)/2); 24 | while(BEG <= END && DATA[MID] != ITEM) 25 | { 26 | if(ITEM < DATA[MID]) 27 | { 28 | END = MID - 1; 29 | } 30 | else 31 | BEG = MID + 1; 32 | MID = (int)((BEG + END) / 2); 33 | } 34 | if(DATA[MID] == ITEM) 35 | LOC = MID; 36 | else 37 | if(ITEM < DATD[MID]) 38 | LOC = MID; 39 | else 40 | LOC = MID + 1; 41 | INSERT(DATA, LOC, ITEM); 42 | return LOC; 43 | } -------------------------------------------------------------------------------- /Chapter-09/ALGORITHM/Algorithm-9-3.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | page 325 3 | Algorithm 9.3: (Selection Sort) SELECTION(A, N) 4 | 5 | This algorithm sorts the array A with N elements. 6 | 7 | 1.    Repeat Steps 2 and 3 for K = 1, 2, …, N – 1: 8 | 9 | 2.           Call MIN(A, K, N, LOC). 10 | 11 | 3.           [Interchange A[K] and A[LOC].] 12 | 13 | Set TEMP := A[K], A[K] := A[LOC] and A[LOC] := TEMP. 14 | 15 | [End of Step 1 loop.] 16 | 17 | 4.    Exit. 18 | */ 19 | 20 | #include 21 | using namespace std; 22 | void MIN(int A[], int K, int N, int &LOC) 23 | { 24 | int min = A[K]; 25 | LOC = K; 26 | for (int i = K + 1; i < N; i++) 27 | { 28 | if (min > A[i]) 29 | { 30 | min = A[i]; 31 | LOC = i; 32 | } 33 | } 34 | } 35 | void selection(int A[], int N) 36 | { 37 | int LOC = 0; 38 | int temp; 39 | for (int i = 0; i < N - 1; i++) 40 | { 41 | MIN(A, i, N, LOC); 42 | temp = A[i]; 43 | A[i] = A[LOC]; 44 | A[LOC] = temp; 45 | } 46 | } 47 | int main() 48 | { 49 | int A[] = {2, 7, 4, 1, 5, 3}; 50 | selection(A, 6); 51 | for (int i : A) 52 | { 53 | cout << i << " "; 54 | } 55 | } 56 | /* 57 | 1 2 3 4 5 7 58 | */ 59 | -------------------------------------------------------------------------------- /Chapter-04/Supplementary Problems/4-24.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 109 3 | 4 | Program 5 | 4.4 Suppose A is a linear array with n numeric values. Write a procedure 6 | 7 | MEAN(A, N, AVE) 8 | 9 | which finds the average AVE of the values in A. 10 | The arithmetic mean or average of the values x1, x2, …, xn is defined by 11 | AVE = (x1+x2+x3....+xn)/n 12 | */ 13 | #include 14 | #include 15 | #include 16 | #include 17 | using namespace std; 18 | int MEAN(int A[], int N, int AVE) 19 | { 20 | 21 | for(int i = 0; i < N; i++) 22 | { 23 | AVE += A[i]; 24 | } 25 | return (AVE / N); 26 | } 27 | //Main from Here 28 | int main() 29 | { 30 | srand(time(0)); 31 | int A[25]; 32 | for(int i = 0; i < 25; i++) 33 | { 34 | A[i] = rand() % 15 + 1; 35 | } 36 | for(int i = 0; i < 25; i++) 37 | { 38 | cout << setw(5) << A[i] ; 39 | } 40 | cout << "\n"; 41 | cout << "Average of all Array A is = " << MEAN(A, 25, 0) << endl; 42 | 43 | } 44 | 45 | /* 46 | Output 47 | 10 2 10 4 48 | 1 10 15 4 49 | 2 3 7 4 50 | 14 11 15 9 51 | 10 6 7 5 52 | 9 7 8 3 1 53 | Average of all Array A is = 7 54 | */ -------------------------------------------------------------------------------- /Chapter-09/ALGORITHM/Algorithm-9-2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | page 325 3 | Procedure 9.2: MIN(A, K, N, LOC) 4 | 5 | An array A is in memory. This procedure finds the location LOC of the 6 | smallest element among A[K], A[K + 1], …, A[N]. 7 | 8 | 1.    Set MIN := A[K] and LOC := K. [Initializes pointers.] 9 | 10 | 2.    Repeat for J = K + 1, K + 2, …, N: 11 | 12 | If MIN > A[J], then: Set MIN := A[J] and LOC := A[J] and LOC := J. 13 | 14 | [End of loop.] 15 | 16 | 3.    Return. 17 | */ 18 | #include 19 | using namespace std; 20 | void MIN(int A[], int K, int N, int &LOC) 21 | { 22 | int min = A[K]; 23 | LOC = K; 24 | for (int i = K + 1; i < N; i++) 25 | { 26 | if (min > A[i]) 27 | { 28 | min = A[i]; 29 | LOC = i; 30 | } 31 | } 32 | } 33 | void selection(int A[], int N) 34 | { 35 | int LOC = 0; 36 | int temp; 37 | for (int i = 0; i < N - 1; i++) 38 | { 39 | MIN(A, i, N, LOC); 40 | temp = A[i]; 41 | A[i] = A[LOC]; 42 | A[LOC] = temp; 43 | } 44 | } 45 | int main() 46 | { 47 | int A[] = {2, 7, 4, 1, 5, 3}; 48 | selection(A, 6); 49 | for (int i : A) 50 | { 51 | cout << i << " "; 52 | } 53 | } 54 | /* 55 | 1 2 3 4 5 7 56 | */ 57 | -------------------------------------------------------------------------------- /Tree/Recursive_Tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node{ 5 | int data; 6 | Node *left,*right; 7 | }; 8 | 9 | 10 | Node *newNode(int key) 11 | { 12 | Node *node = new Node; 13 | node->data = key; 14 | node->left = node->right = nullptr; 15 | 16 | return node; 17 | } 18 | 19 | void insert(Node* &travptr, int data){ 20 | 21 | if (travptr == nullptr) 22 | { 23 | travptr = newNode(data); 24 | return; 25 | } 26 | (data > travptr->data)? insert(travptr->right,data) : insert(travptr->left, data); 27 | 28 | } 29 | 30 | bool contains(Node* travptr, int data){ 31 | 32 | if (travptr == nullptr) 33 | { 34 | return false; 35 | } 36 | if (data == travptr->data) 37 | { 38 | return true; 39 | } 40 | 41 | return (data > travptr->data) ? contains(travptr->right, data) : contains(travptr->left, data); 42 | 43 | } 44 | 45 | int main() 46 | { 47 | Node* root = nullptr; 48 | 49 | insert(root, 1); 50 | insert(root, 2); 51 | insert(root, 3); 52 | insert(root, 4); 53 | insert(root, 5); 54 | 55 | cout << contains(root, 3) << "\t" << contains(root, 10) << "\t" << contains(root, 5); 56 | 57 | return 0; 58 | } -------------------------------------------------------------------------------- /Chapter-09/SUPPLEMENTARY PROBLEMS/9-2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | page 338 3 | 9.2   Translate insertion sort into a subprogram INSERTSORT(A, N) 4 | which sorts the array A with N elements. Test the program using: 5 | (a)  44, 33, 11, 55, 77, 90, 40, 60, 99, 22, 88, 66 6 | (b)  D, A, T, A, S, T, R, U, C, T, U, R, E, S 7 | */ 8 | #include 9 | using namespace std; 10 | template 11 | void insertion_sort(T A[], int N) 12 | { 13 | T temp; 14 | int hole; 15 | for (int i = 1; i < N - 1; i++) 16 | { 17 | temp = A[i]; 18 | hole = i; 19 | while (hole > 0 && A[hole - 1] > temp) 20 | { 21 | A[hole] = A[hole - 1]; 22 | hole = hole - 1; 23 | } 24 | A[hole] = temp; 25 | } 26 | } 27 | int main() 28 | { 29 | int A[] = {44, 33, 11, 55, 77, 90, 40, 60, 99, 22, 88, 66}; 30 | insertion_sort(A, 13); 31 | for (int i : A) 32 | { 33 | cout << i << " "; 34 | } 35 | cout << "\n"; 36 | char B[] = {'D', 'A', 'T', 'A', 'S', 'T', 'R', 'U', 'C', 'T', 'U', 'R', 'E', 'S'}; 37 | insertion_sort(B, 15); 38 | for (char i : B) 39 | { 40 | cout << i << " "; 41 | } 42 | } 43 | /* 44 | 11 22 33 40 44 55 60 66 77 88 90 99 45 | A A C D E R R S S T T T U U 46 | */ -------------------------------------------------------------------------------- /Chapter-03/3-12.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | page 60 3 | Pagogram 4 | 3.12 Recall that we use INSERT(text, position, string) to denote inserting a 5 | string S in a given text T beginning in position K. 6 | 7 | (a) Find 8 | (i) INSERT('AAAAA', 1, 'BBB'), 9 | (ii) INSERT('AAAAA', 3, 'BBB') and 10 | (iii) INSERT('AAAAA', 6, 'BBB'). 11 | 12 | (b) Suppose T is the text 'THE STUDENT IS ILL. ' 13 | Use INSERT to change T so that it reads: 14 | (i) The student is very ill. 15 | (ii) The student is ill today. 16 | (iii) The student is very ill today. 17 | */ 18 | #include 19 | #include 20 | #include 21 | using namespace std; 22 | string INSERT(string T, int i , string R) 23 | { 24 | T.insert(i,R); 25 | return T; 26 | } 27 | int main() 28 | { 29 | string test("The STUDENT IS ILL"); 30 | cout << test << endl; 31 | test.insert(test.find("ILL"), "VERY "); 32 | cout << test << endl; 33 | test.insert(test.length(), " TODAY"); 34 | cout << test << endl; 35 | cout << INSERT("AAAAA", 1-0, "BBB") << endl; 36 | cout << INSERT("AAAAA", 3-1, "BBB") << endl; 37 | cout << INSERT("AAAAA", 6-1, "BBB") << endl; 38 | 39 | 40 | } 41 | /* 42 | The STUDENT IS ILL 43 | The STUDENT IS VERY ILL 44 | The STUDENT IS VERY ILL TODAY 45 | ABBBAAAA 46 | AABBBAAA 47 | AAAAABBB 48 | */ -------------------------------------------------------------------------------- /Chapter-04/Algorithm/Algorithm-4-1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | page 71 3 | program 4 | Algortithm 4.1 5 | 6 | */ 7 | #include 8 | using namespace std; 9 | void TRAVESING(int LA[], int UB, int LB) 10 | { 11 | cout << "TRAVESING\n"; 12 | for(int i = LB; i < UB; i++) 13 | { 14 | cout << LA[i] << endl; 15 | } 16 | cout << "\n\n\n"; 17 | return; 18 | } 19 | void RAVESING(int LA[], int UB, int LB) 20 | { 21 | cout << "RAVESING\n"; 22 | for(int i = UB; i >= LB; i--) 23 | { 24 | cout << LA[i] << endl; 25 | } 26 | cout << "\n\n\n"; 27 | return; 28 | } 29 | int main() 30 | { 31 | int LA[100]; 32 | cout << "Enter 10 integer number\n"; 33 | for(int i = 0; i <= 10; i++) 34 | { 35 | cin >> LA[i]; 36 | } 37 | cout << "\n\n\n"; 38 | TRAVESING(LA, 10, 0); 39 | RAVESING(LA, 10, 0); 40 | 41 | return 0; 42 | } 43 | /* 44 | output 45 | Enter 10 integer number 46 | 201 47 | 202 48 | 203 49 | 204 50 | 205 51 | 206 52 | 207 53 | 208 54 | 209 55 | 210 56 | 211 57 | 58 | 59 | 60 | TRAVESING 61 | 201 62 | 202 63 | 203 64 | 204 65 | 205 66 | 206 67 | 207 68 | 208 69 | 209 70 | 210 71 | 72 | 73 | 74 | RAVESING 75 | 211 76 | 210 77 | 209 78 | 208 79 | 207 80 | 206 81 | 205 82 | 204 83 | 203 84 | 202 85 | 201 86 | */ -------------------------------------------------------------------------------- /Chapter-04/Solved Problems/4-3.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 99 3 | Program 4 | 4.3 Suppose a 10-element array A contains the values a1, a2 , …, a10. 5 | Find the values in A after each loop. 6 | 7 | (a) Repeat for K = 1 to 9: 8 | 9 | Set A[K + 1] := A[K]. 10 | 11 | [End of loop.] 12 | 13 | (b) Repeat for K = 9 to 1 by −1: 14 | 15 | Set A[K + 1] := A[9]. 16 | 17 | [End of loop.] 18 | 19 | Note that the index K runs from 1 to 9 in part 20 | (a) but in reverse order from 9 back to 1 in part (b) 21 | 22 | */ 23 | #include 24 | #include 25 | using namespace std; 26 | //Main from Here 27 | int main() 28 | { 29 | int A[10] = {1,2,3,4,5,6,7,8,9,10}; 30 | for(int i = 0; i < 10; i++) 31 | { 32 | cout << A[i] << " "; 33 | } 34 | 35 | for(int K = 0; K < 10; K++) 36 | { 37 | A[K+1] = A[K]; 38 | } 39 | 40 | cout << "\n\n\n"; 41 | for(int i = 0; i < 10; i++) 42 | { 43 | cout << A[i] << " "; 44 | } 45 | for(int K = 10; K > 0; K--) 46 | { 47 | A[K+1] = A[K]; 48 | } 49 | 50 | 51 | cout << "\n\n\n"; 52 | for(int i = 0; i < 10; i++) 53 | { 54 | cout << A[i] << " "; 55 | } 56 | } 57 | 58 | /* 59 | Output 60 | 1 2 3 4 5 6 7 8 9 10 61 | 62 | 63 | 1 1 1 1 1 1 1 1 1 1 64 | 65 | 66 | 1 1 1 1 1 1 1 1 1 1 67 | */ -------------------------------------------------------------------------------- /Chapter-02/Solved Problem/2-1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 33 3 | Program 4 | 2.1 Find (a) 7.5, −7.5, −18, , , π; and (b) 7.5, −7.5, −18, , , π. 5 | 6 | */ 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | //main begin 12 | int main() 13 | { 14 | cout <<"floor(7.5) " < 22 | #include 23 | using namespace std; 24 | int main() 25 | { 26 | string T, P, Q; 27 | size_t K; 28 | cout << "Enter your Text\n"; 29 | getline(cin,T); 30 | cout << "Enter your keyword to replace\n"; 31 | cin >> P; 32 | cout << "Enter replace keyword\n"; 33 | cin >> Q; 34 | cout << T << endl; 35 | cout << P << endl; 36 | cout << Q << endl; 37 | K = T.find(P); 38 | while(K != string::npos) 39 | { 40 | T.replace(K,P.length(),Q); 41 | K = T.find(P); 42 | } 43 | cout << T << endl; 44 | cout << P << endl; 45 | cout << Q << endl; 46 | return 0; 47 | 48 | } 49 | /* 50 | output 51 | Enter your Text 52 | XABYABZAB 53 | Enter your keyword to replace 54 | AB 55 | Enter replace keyword 56 | PQR 57 | XABYABZAB 58 | AB 59 | PQR 60 | XPQRYPQRZPQR 61 | AB 62 | PQR 63 | */ 64 | -------------------------------------------------------------------------------- /Tree/Delete.h: -------------------------------------------------------------------------------- 1 | TreeNode *FindMin(TreeNode *root) 2 | { 3 | while (root->lchild != NULL) 4 | { 5 | root = root->lchild; 6 | } 7 | return root; 8 | } 9 | TreeNode *Delete(TreeNode *root, int data) 10 | { 11 | if (root == NULL) 12 | { 13 | return NULL; 14 | } 15 | else if (data < root->DATA) 16 | { 17 | root->lchild = Delete(root->lchild, data); 18 | } 19 | else if (data > root->DATA) 20 | { 21 | root->rchild = Delete(root->rchild, data); 22 | } 23 | else 24 | { 25 | // case 1: NO child 26 | if (root->lchild == NULL && root->rchild == NULL) 27 | { 28 | delete root; 29 | root == NULL; 30 | } 31 | // Case 2: one child 32 | else if (root->lchild == NULL) 33 | { 34 | TreeNode *temp = root; 35 | root = root->rchild; 36 | delete temp; 37 | } 38 | else if (root->rchild == NULL) 39 | { 40 | TreeNode *temp = root; 41 | root = root->lchild; 42 | delete temp; 43 | } 44 | else 45 | { 46 | TreeNode *temp = FindMin(root->rchild); 47 | root->DATA = temp->DATA; 48 | root->rchild = Delete(root->rchild, temp->DATA); 49 | } 50 | } 51 | return root; 52 | } -------------------------------------------------------------------------------- /Chapter-05/SOLVED PROBLEMS/5-2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 149 3 | 5.2 The following list of names is assigned (in order) to a linear array INFO: 4 | Mary, June, Barbara, Paula, Diana, Audrey, Karen, Nancy, Ruth, Eileen, Sandra, Helen 5 | That is, INFO[1] = Mary, INFO[2] = June,…, INFO[12] = Helen. Assign values to an array 6 | LINK and a variable START so that INFO, LINK and START form an alphabetical listing of 7 | the names 8 | */ 9 | #include 10 | #include 11 | using namespace std; 12 | int main() 13 | { 14 | string INFO[] = {"Mary", "June", "Barbara", "Paula", "Diana", "Audrey", "Karen", "Nancy", "Ruth", "Eileen", "Sandra", "Helen"}; 15 | 16 | int START = 5; 17 | int LINK[12] = {7, 6, 4, 8, 9, 2, 0, 3, 10, 11, -1, 1}; 18 | while (START != -1) 19 | { 20 | cout << INFO[START] << endl; 21 | START = LINK[START]; 22 | } 23 | 24 | 25 | } 26 | /* 27 | Mary 28 | June 29 | Barbara, 30 | Paula 31 | Diana 32 | Audrey 33 | Karen 34 | Nancy 35 | Ruth 36 | Eileen 37 | Sandra 38 | Helen 39 | 40 | 8 41 | 7 42 | 5 43 | 9 44 | 10 45 | 3 46 | 1 47 | 4 48 | 11 49 | 12 50 | 0 51 | 2 52 | 53 | OUTPUT 54 | Audrey 55 | Barbara 56 | Diana 57 | Eileen 58 | Helen 59 | June 60 | Karen 61 | Mary 62 | Nancy 63 | Paula 64 | Ruth 65 | Sandra 66 | */ -------------------------------------------------------------------------------- /Chapter-06/ALGORITHM/Algorithm-6-1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 167 3 | Procedure 6.1: PUSH(STACK, TOP, MAXSTK, ITEM) 4 | 5 | This procedure pushes an ITEM onto a stack. 6 | 7 | 1. [Stack already filled?] 8 | If TOP = MAXSTK, then: Print: OVERFLOW, and Return. 9 | 10 | 2. Set TOP := TOP + 1. [Increases TOP by 1.] 11 | 12 | 3. Set STACK[TOP] := ITEM. [Inserts ITEM in new TOP position.] 13 | 14 | 4. Return. 15 | */ 16 | #include 17 | #include 18 | using namespace std; 19 | typedef struct _link 20 | { 21 | int DATA; 22 | _link *next; 23 | }STACK; 24 | void PUSH(STACK **TOP, int DATA) 25 | { 26 | if(*TOP == NULL) 27 | { 28 | STACK *PTR = new STACK; 29 | PTR->DATA = DATA; 30 | PTR->next = NULL; 31 | (*TOP) = PTR; 32 | return; 33 | } 34 | STACK *PTR = new STACK; 35 | PTR->DATA = DATA; 36 | PTR->next = (*TOP); 37 | (*TOP) = PTR; 38 | } 39 | int POP(STACK **TOP) 40 | { 41 | if(*TOP == NULL) 42 | { 43 | return 0; 44 | } 45 | int temp = (*TOP)->DATA; 46 | (*TOP) = (*TOP)->next; 47 | return temp; 48 | } 49 | int main() 50 | { 51 | STACK *TOP = NULL; 52 | for(int i = 1; i <= 10; i++) 53 | { 54 | PUSH(&TOP,i); 55 | } 56 | int temp = POP(&TOP); 57 | while(temp) 58 | { 59 | cout << temp << " "; 60 | temp = POP(&TOP); 61 | } 62 | 63 | } -------------------------------------------------------------------------------- /Chapter-07/Solved Problem/7-1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 260 3 | 7.1 Suppose T is the binary tree stored in memory as in Fig. 7.72. Draw the diagram of T. 4 | */ 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | typedef struct _tree 10 | { 11 | int data; 12 | _tree *left; 13 | _tree *right; 14 | } TREE; 15 | 16 | TREE *insert(TREE *root, int data) 17 | { 18 | if (root == NULL) 19 | { 20 | TREE *ptr = new TREE; 21 | ptr->data = data; 22 | ptr->left = NULL; 23 | ptr->right = NULL; 24 | root = ptr; 25 | } 26 | else if (root->data > data) 27 | { 28 | root->right = insert(root->right, data); 29 | } 30 | else 31 | root->left = insert(root->left, data); 32 | return root; 33 | } 34 | void inorder(TREE *root) 35 | { 36 | 37 | if(root != NULL) 38 | { 39 | inorder(root->left); 40 | cout << root->data << " "; 41 | inorder(root->right); 42 | } 43 | 44 | } 45 | int main() 46 | { 47 | int AVAIL = 11, temp; 48 | TREE *root = NULL; 49 | for(int i = 0; i < AVAIL; i++) 50 | { 51 | cin >> temp; 52 | root = insert(root, temp); 53 | } 54 | inorder(root); 55 | 56 | } 57 | /* 58 | 60 59 | 30 60 | 70 61 | 20 62 | 55 63 | 35 64 | 45 65 | 40 66 | 50 67 | 90 68 | 80 69 | 90 80 70 60 55 50 45 40 35 30 20 70 | */ -------------------------------------------------------------------------------- /Chapter-09/SUPPLEMENTARY PROBLEMS/9-3.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | page 338 3 | 9.3   Translate insertion sort into a subprogram INSERTCOUNT(A, N, NUMB) 4 | which sorts the array A with N elements and which also counts the number NUMB of comparisons. 5 | */ 6 | #include 7 | using namespace std; 8 | template 9 | int insertion_sort(T A[], int N) 10 | { 11 | int count = 0; 12 | T temp; 13 | int hole; 14 | for (int i = 1; i < N - 1; i++) 15 | { 16 | temp = A[i]; 17 | hole = i; 18 | while (hole > 0 && A[hole - 1] > temp) 19 | { 20 | A[hole] = A[hole - 1]; 21 | hole = hole - 1; 22 | count++; 23 | } 24 | A[hole] = temp; 25 | } 26 | return count; 27 | } 28 | int main() 29 | { 30 | int A[] = {44, 33, 11, 55, 77, 90, 40, 60, 99, 22, 88, 66}; 31 | cout << "Number of Comparisons = "<< insertion_sort(A, 13)<<"\n"; 32 | for (int i : A) 33 | { 34 | cout << i << " "; 35 | } 36 | cout << "\n"; 37 | char B[] = {'D', 'A', 'T', 'A', 'S', 'T', 'R', 'U', 'C', 'T', 'U', 'R', 'E', 'S'}; 38 | cout << "Number of Comparisons = "<< insertion_sort(B, 15) << endl; 39 | for (char i : B) 40 | { 41 | cout << i << " "; 42 | } 43 | } 44 | /* 45 | Number of Comparisons = 23 46 | 11 22 33 40 44 55 60 66 77 88 90 99 47 | Number of Comparisons = 33 48 | A A C D E R R S S T T T U U 49 | */ -------------------------------------------------------------------------------- /Chapter-07/ALGORITHM WITH POINTER/Algorithm-7-5.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 236 3 | TreeNode* Insert(TreeNode *root,int data) 4 | { 5 | if(root == NULL) { 6 | root = new TreeNode(); 7 | root->data = data; 8 | root->left = root->right = NULL; 9 | } 10 | else if(data <= root->data) 11 | root->left = Insert(root->left,data); 12 | else 13 | root->right = Insert(root->right,data); 14 | return root; 15 | } 16 | */ 17 | #include 18 | #include 19 | using namespace std; 20 | typedef struct _tree 21 | { 22 | int data; 23 | _tree *left; 24 | _tree *right; 25 | }TreeNode; 26 | 27 | TreeNode* Insert(TreeNode *root,int data) 28 | { 29 | if(root == NULL) { 30 | root = new TreeNode(); 31 | root->data = data; 32 | root->left = root->right = NULL; 33 | } 34 | else if(data <= root->data) 35 | root->left = Insert(root->left,data); 36 | else 37 | root->right = Insert(root->right,data); 38 | return root; 39 | } 40 | TreeNode pre_order(TreeNode *root) 41 | { 42 | if(root != NULL) 43 | { 44 | cout << root->data << " "; 45 | pre_order(root->left); 46 | pre_order(root->right); 47 | } 48 | } 49 | int main() 50 | { 51 | srand(time(0)); 52 | TreeNode *root = NULL; 53 | for(int i = 0; i < 10; i++) 54 | { 55 | root = Insert(root, rand()%100); 56 | } 57 | pre_order(root); 58 | return 0; 59 | } 60 | /* 61 | 80 70 42 38 16 42 69 67 78 74 62 | */ -------------------------------------------------------------------------------- /Chapter-02/Programming Problems/2-24.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 40 3 | Program 2.24 4 | Write a program using DIV(J, K) which reads a positive integer N > 10 5 | and determines whether or not N is a prime number. (Hint: N is prime 6 | if (i) DlV(2, N) = 0 (i.e., N is odd) and (ii) DIV(K, N) = 0 for all 7 | odd integers K where 1 < K2 ≤ N.) 8 | */ 9 | #include 10 | using namespace std; 11 | 12 | // Take Divisor and Dividend 13 | //if reminder is 0 then retrun true 14 | //else retrun 1 15 | template 16 | T DIV(T J, T K) 17 | { 18 | if(K % J== 0) 19 | return 1; 20 | else 21 | return 0; 22 | } 23 | //This function get a input to check its prime or not 24 | 25 | int Prime(int N) 26 | { 27 | if(DIV(2,N) == 0) 28 | { 29 | for(int K = 3 ; K < N ; K += 2) 30 | { 31 | if(DIV(K,N) == 1) 32 | { 33 | return 1; 34 | } 35 | } 36 | return 0; 37 | } 38 | else 39 | return 1; 40 | } 41 | int main() 42 | { 43 | int N; 44 | cout << "Enter a positive integer greater then 10\n"; 45 | cin >> N ; 46 | if(Prime(N) == 0) 47 | cout << N << " is Prime\n"; 48 | else 49 | cout << N << " is not Prime\n"; 50 | 51 | 52 | getchar(); 53 | return 0; 54 | } 55 | 56 | /* 57 | Output 58 | Enter a positive integer greater then 10 59 | 111 60 | 111 is not Prime 61 | */ -------------------------------------------------------------------------------- /Chapter-06/SOLVED PROBLEMS/6-19.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | page 183 3 | Procedure 6.11: TOWER(N, BEG, AUX, END) 4 | 5 | This procedure gives a recursive solution to the Towers of Hanoi problem for N disks. 6 | 7 | 1. If N = 1, then: 8 | 9 | (a) Write: BEG → END. 10 | 11 | (b) Return. 12 | 13 | [End of If structure.] 14 | */ 15 | #include 16 | #include 17 | using namespace std; 18 | void TOWER(int N, string BEG, string AUX, string END) 19 | { 20 | string temp; 21 | if(N == 0) 22 | { 23 | return; 24 | } 25 | for(int K = N ; K >= 1; K--) 26 | { 27 | TOWER(N-1, BEG, END, AUX); 28 | cout << "Move top disk form pag " << BEG << " to pag " << END << endl; 29 | temp = BEG ,BEG = AUX, AUX = temp; 30 | } 31 | 32 | } 33 | int main() 34 | { 35 | TOWER(3, "A", "B", "C"); 36 | return 0; 37 | } 38 | /* 39 | Move top disk form pag A to pag C 40 | Move top disk form pag A to pag B 41 | Move top disk form pag C to pag A 42 | Move top disk form pag C to pag B 43 | Move top disk form pag A to pag C 44 | Move top disk form pag B to pag C 45 | Move top disk form pag B to pag A 46 | Move top disk form pag C to pag B 47 | Move top disk form pag C to pag A 48 | Move top disk form pag B to pag C 49 | Move top disk form pag A to pag C 50 | Move top disk form pag A to pag B 51 | Move top disk form pag C to pag A 52 | Move top disk form pag C to pag B 53 | Move top disk form pag A to pag C 54 | */ -------------------------------------------------------------------------------- /Chapter-08/SOLVED PROBLEMS/8-13.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | page 308 3 | problem 8-13 4 | */ 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | void PRINT(list A, list X[]) 10 | { 11 | list::iterator ptr = A.begin(); 12 | cout << "\n==========GRAPH=============\n"; 13 | while (ptr != A.end()) 14 | { 15 | list::iterator ptr1 = X[*ptr].begin(); 16 | cout << char((*ptr)+65) << " -> "; 17 | while (ptr1 != X[*ptr].end()) 18 | { 19 | cout << char((*ptr1++)+65) << " "; 20 | } 21 | cout << "\n"; 22 | ptr++; 23 | } 24 | 25 | cout << "=======================\n"; 26 | } 27 | int main() 28 | { 29 | list A; 30 | list X[2000]; 31 | int EDGE, NODE, SRC, DEST; 32 | char n, m; 33 | cout << "Enter Nodes and EDGE\n"; 34 | cin >> NODE >> EDGE; 35 | 36 | for (int i = 0; i < EDGE; i++) 37 | { 38 | cin >> n >> m; 39 | SRC = n - 65; 40 | DEST = m - 65; 41 | X[SRC].push_back(DEST); 42 | } 43 | for (int i = 0; i < NODE; i++) 44 | { 45 | A.push_back(i); 46 | } 47 | PRINT(A, X); 48 | } 49 | /* 50 | Enter Nodes and EDGE 51 | 5 8 52 | A B 53 | A D 54 | B D 55 | B C 56 | B E 57 | C E 58 | D E 59 | E D 60 | 61 | ==========GRAPH============= 62 | A -> B D 63 | B -> D C E 64 | C -> E 65 | D -> E 66 | E -> D 67 | ======================= 68 | */ -------------------------------------------------------------------------------- /Chapter-09/SUPPLEMENTARY PROBLEMS/9-8.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | page 338 3 | 9.8   Translate selection sort into a subprogram SELECTSORT(A, N) 4 | which sorts the array with N elements. Test the program using: 5 | 6 | (a)  44, 33, 11, 55, 77, 90, 40, 60, 99, 22, 88, 66 7 | 8 | (b)  D, A, T, A, S, T, R, U, C, T, U, R, E, S 9 | */ 10 | #include 11 | using namespace std; 12 | template 13 | void MIN(T A[], int K, int N, int &LOC) 14 | { 15 | T min = A[K]; 16 | LOC = K; 17 | for (int i = K + 1; i < N; i++) 18 | { 19 | if (min > A[i]) 20 | { 21 | min = A[i]; 22 | LOC = i; 23 | } 24 | } 25 | } 26 | template 27 | void selection(T A[], int N) 28 | { 29 | int LOC = 0; 30 | T temp; 31 | for (int i = 0; i < N - 1; i++) 32 | { 33 | MIN(A, i, N, LOC); 34 | temp = A[i]; 35 | A[i] = A[LOC]; 36 | A[LOC] = temp; 37 | } 38 | } 39 | int main() 40 | { 41 | int A[] = {44, 33, 11, 55, 77, 90, 40, 60, 99, 22, 88, 66}; 42 | selection(A, 12); 43 | for (int i : A) 44 | { 45 | cout << i << " "; 46 | } 47 | cout << "\n"; 48 | char B[] = {'D', 'A', 'T', 'A', 'S', 'T', 'R', 'U', 'C', 'T', 'U', 'R', 'E', 'S'}; 49 | selection(B, 14); 50 | for (char i : B) 51 | { 52 | cout << i << " "; 53 | } 54 | } 55 | /* 56 | 11 22 33 40 44 55 60 66 77 88 90 99 57 | A A C D E R R S S T T T U U 58 | */ -------------------------------------------------------------------------------- /Chapter-06/SOLVED PROBLEMS/6-16.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 181 3 | Procedure 6.10: FIBONACCI(FIB, N) 4 | 5 | This procedure calculates FN and returns the value in the first parameter FIB. 6 | 7 | 1. If N = 0 or N = 1, then: Set FIB := N, and Return. 8 | 9 | 2. Call FIBONACCI(FIBA, N – 2). 10 | 11 | 3. Call FIBONACCI(FIBB, N – 1). 12 | 13 | 4. Set FIB := FIBA + FIBB. 14 | 15 | 5. Return. 16 | */ 17 | #include 18 | using namespace std; 19 | int FIBONACCHI(int N) 20 | { 21 | if(N <= 1) 22 | { 23 | return N; 24 | } 25 | return FIBONACCHI(N-1)+FIBONACCHI(N-2); 26 | } 27 | void FIB(int F[], int N) 28 | { 29 | F[0] = 1; 30 | cout << F[0] << ' '; 31 | F[1] = 1; 32 | cout << F[1] << ' '; 33 | for(int L = 2; L < N; L++) 34 | { 35 | F[L] = F[L-1] + F[L-2]; 36 | cout << F[L] << ' '; 37 | } 38 | } 39 | int main() 40 | { 41 | int N, F[N]; 42 | cout << "Enter a number\n"; 43 | cin >> N; 44 | cout<< N << "'s fibonacchi is = " << FIBONACCHI(N) << "\n"; 45 | FIB(F, N); 46 | return 0; 47 | } 48 | /* 49 | Enter a number 50 | 12 51 | 12's fibonacchi is = 144 52 | 53 | Enter a number 54 | 13 55 | 13's fibonacchi is = 233 56 | 57 | Enter a number 58 | 14 59 | 14's fibonacchi is = 377 60 | 61 | Enter a number 62 | 15 63 | 15's fibonacchi is = 610 64 | 65 | 66 | 67 | Enter a number 68 | 16 69 | 16's fibonacchi is = 987 70 | 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 71 | */ -------------------------------------------------------------------------------- /Chapter-04/Programming Problems/4-33.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 110 3 | Program 4 | 4.3 Translate the binary search and insertion algorithm into a subprogram 5 | BINARY(ARRAY, LB, UB, ITEM, LOC) which finds either the location LOC where 6 | ITEM appears in ARRAY or the location LOC where ITEM should be inserted into ARRAY. 7 | */ 8 | #include 9 | #include 10 | using namespace std; 11 | /*****************************************/ 12 | template 13 | int BINARY(T DATA[], int LB, int UB, T ITEM, int LOC) 14 | { 15 | int BEG, END, MID; 16 | BEG = LB; 17 | END = UB; 18 | MID = (int)((LB+UB)/2); 19 | while(BEG <= END && DATA[MID] != ITEM) 20 | { 21 | if(ITEM < DATA[MID]) 22 | { 23 | END = MID - 1; 24 | } 25 | else 26 | BEG = MID + 1; 27 | MID = (int)((BEG + END) / 2); 28 | } 29 | if(DATA[MID] == ITEM) 30 | LOC = MID; 31 | else 32 | if(ITEM < DATD[MID]) 33 | LOC = MID; 34 | else 35 | LOC = MID + 1; 36 | INSERT(DATA, LOC, ITEM); 37 | return LOC; 38 | } 39 | 40 | /****************************/ 41 | template 42 | void INSERT(T LA[],int N, int K, T ITEM) 43 | { 44 | int J = N; 45 | while(J >= K) 46 | { 47 | LA[J+1] = LA[J]; 48 | J--; 49 | } 50 | LA[K] = ITEM; 51 | Global_J++; 52 | } 53 | //Main from Here 54 | int main() 55 | { 56 | 57 | } 58 | 59 | /* 60 | Output 61 | 62 | */ -------------------------------------------------------------------------------- /Chapter-09/SUPPLEMENTARY PROBLEMS/9-4.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 9.4   Write a program TESTINSERT(N, AVE) which repeats 500 times the procedure INSERTCOUNT(A, N, NUMB) 3 | and which finds the average AVE of the 500 values of NUMB. (Theoretically, AVE ≈ N2/4.) 4 | Use RANDOM(A, N, 5*N) from Problem 9.1 as each input. Test the program using N = 100 5 | (so, theoretically, AVE ≈ N2/4 = 2500). 6 | */ 7 | 8 | #include 9 | #include 10 | using namespace std; 11 | void RANDOM(int ARRAY[], int N, int K) 12 | { 13 | srand(time(0)); 14 | for (int i = 0; i < K; i++) 15 | { 16 | ARRAY[i] = rand() % N; 17 | } 18 | return; 19 | } 20 | int insert_count(int A[], int N) 21 | { 22 | int count = 0; 23 | int temp; 24 | int hole; 25 | for (int i = 1; i < N - 1; i++) 26 | { 27 | temp = A[i]; 28 | hole = i; 29 | while (hole > 0 && A[hole - 1] > temp) 30 | { 31 | A[hole] = A[hole - 1]; 32 | hole = hole - 1; 33 | count++; 34 | } 35 | A[hole] = temp; 36 | } 37 | return count; 38 | } 39 | void TESTINSERT(int N, int &AVE) 40 | { 41 | int ARRAY[N * 5]; 42 | int SUM = 0; 43 | for (int i = 0; i < 500; i++) 44 | { 45 | RANDOM(ARRAY, N, N * 5); 46 | SUM += insert_count(ARRAY, N * 5); 47 | } 48 | AVE = SUM / 500; 49 | cout << "AVE = " << AVE << "\n"; 50 | } 51 | int main() 52 | { 53 | int N = 100, AVE = 0; 54 | TESTINSERT(N, AVE); 55 | } -------------------------------------------------------------------------------- /Chapter-02/Solved Problem/2-3.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 34 3 | Program 4 | List all the permutations of the numbers 1, 2, 3, 4. 5 | */ 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | /* Function to swap values at two pointers */ 11 | void swap(char &a, char &b) 12 | { 13 | char temp; 14 | temp = a; 15 | a = b; 16 | b = temp; 17 | return; 18 | } 19 | /* Function to print permutations of string 20 | This function takes three parameters: 21 | 1. String 22 | 2. Starting index of the string 23 | 3. Ending index of the string. */ 24 | void permutation(char str[], int x, int y) 25 | { 26 | if(x == y) 27 | { 28 | cout << str << " \n" ; 29 | } 30 | else 31 | { 32 | 33 | for(int i = x; i < y; i++) 34 | { 35 | 36 | swap(str[x], str[i]); 37 | permutation(str, x+1, y); 38 | swap(str[i], str[x]);//backtrack 39 | } 40 | } 41 | } 42 | /* Driver program to test above functions */ 43 | int main() 44 | { 45 | 46 | char String[20]; 47 | cout << "Enter your string\n"; 48 | cin >> String; 49 | int n = strlen(String); 50 | permutation(String, 0, n); 51 | return 0; 52 | } 53 | /* 54 | Enter your string 55 | 1234 56 | 1234 57 | 1243 58 | 1324 59 | 1342 60 | 1432 61 | 1423 62 | 2134 63 | 2143 64 | 2314 65 | 2341 66 | 2431 67 | 2413 68 | 3214 69 | 3241 70 | 3124 71 | 3142 72 | 3412 73 | 3421 74 | 4231 75 | 4213 76 | 4321 77 | 4312 78 | 4132 79 | 4123 80 | */ -------------------------------------------------------------------------------- /Chapter-03/3-23.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Let W be the string ABCD. 3 | (a) Find the length of W. 4 | (b) List all substrings of W. 5 | (c) List all the initial substrings of W. 6 | */ 7 | #include 8 | #include 9 | using namespace std; 10 | int main() 11 | { 12 | string W, R; 13 | cout << "Enter your String \n"; 14 | cin >> W; 15 | //(a) Find the length of W. 16 | cout << "Length of String = " << W.length() << endl; 17 | 18 | int k = W.length(); 19 | //(b) List all substrings of W. 20 | cout << "(b) List all substrings of W.\n"; 21 | for(int i = 0; i <= W.length(); i++,k--) 22 | { 23 | for(int j = 0; j <= i; j++) 24 | { 25 | R = W.substr(j,k); 26 | cout << R << " " ; 27 | } 28 | cout << "\n"; 29 | } 30 | k = W.length(); 31 | //(c) List all the initial substrings of W. 32 | cout << "(c)List all the initial substrings of W\n"; 33 | for(int i = 0; i <= W.length(); i++,k--) 34 | { 35 | for(int j = 0; j < 1; j++) 36 | { 37 | R = W.substr(j,k); 38 | cout << R << " " ; 39 | } 40 | cout << "\n"; 41 | } 42 | // 43 | return 0; 44 | } 45 | /* 46 | Enter your String 47 | XYZST 48 | Length of String = 5 49 | (b) List all substrings of W. 50 | XYZST 51 | XYZS YZST 52 | XYZ YZS ZST 53 | XY YZ ZS ST 54 | X Y Z S T 55 | 56 | (c)List all the initial substrings of W 57 | XYZST 58 | XYZS 59 | XYZ 60 | XY 61 | X 62 | */ -------------------------------------------------------------------------------- /Chapter-06/ALGORITHM/Algorithm-6-11.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Procedure 6.13: QINSERT(QUEUE, N, FRONT, REAR, ITEM) 3 | 4 | This procedure inserts an element ITEM into a queue. 5 | 6 | 1. [Queue already filled?] 7 | If FRONT = 1 and REAR = N, or if FRONT = REAR + 1, then: 8 | 9 | Write: OVERFLOW, and Return. 10 | 11 | 2. [Find new value of REAR.] 12 | If FRONT := NULL, then: [Queue initially empty.] 13 | 14 | Set FRONT := 1 and REAR := 1. 15 | 16 | Else if REAR = N, then: 17 | 18 | Set REAR := 1. 19 | 20 | Else: 21 | 22 | Set REAR := REAR + 1. 23 | 24 | [End of If structure.] 25 | 26 | 3. Set QUEUE[REAR] := ITEM. [This inserts new element.] 27 | 28 | 4. Return. 29 | */ 30 | #include 31 | #include 32 | using namespace std; 33 | void QINSERT(int QUEUE[], int N, int &FRONT, int &REAR, int ITEM) 34 | { 35 | if(((FRONT == 0) && (REAR == N-1)) || FRONT == REAR+1) 36 | { 37 | cout << "OVERFLOW\n"; 38 | return; 39 | } 40 | if(FRONT == -1) 41 | { 42 | FRONT = 0; 43 | REAR = 0; 44 | } 45 | else 46 | if(REAR == N-1) 47 | { 48 | REAR = 0; 49 | } 50 | else 51 | REAR = REAR+1; 52 | QUEUE[REAR] = ITEM; 53 | return; 54 | } 55 | int main() 56 | { 57 | int N = 5; 58 | int QUEUE[N]; 59 | int FRONT, REAR, ITEM; 60 | FRONT = REAR = -1; 61 | for(int i = 0; i < 5; i++) 62 | { 63 | QINSERT(QUEUE, N, FRONT, REAR, i); 64 | } 65 | cout << QUEUE[FRONT]; 66 | } 67 | /* 68 | 0 69 | */ -------------------------------------------------------------------------------- /Chapter-02/Supplementary Problems/2-15.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 39 3 | Program 4 | 2.1 Find (a)floor (3.4),floor(–3.4), floor(–7),floor(sqrt(75)) ,floor(cbrt(75)) , floor(e); 5 | (b) ceil(3.4), ceil(–3.4), ceil(–7),ceil(sqrt(75)) ,ceil(cbrt(75)) ,ceil(e); 6 | */ 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | //main begin 12 | int main() 13 | { 14 | cout << "floor (3.4) = " << floor (3.4) << endl; 15 | cout << "floor(-3.4) = " << floor(-3.4) << endl; 16 | cout << "floor(-7) = " << floor(-7) << endl; 17 | cout << "floor(sqrt(75)) = " << floor(sqrt(75)) << endl; 18 | cout << "floor(cbrt(75)) = " << floor(cbrt(75)) << endl; 19 | cout << "floor(e)= " << floor(exp(1.0)) << endl; 20 | 21 | 22 | cout << "\n*****************\n"; 23 | cout << "ceil (3.4) = " << ceil(3.4) << endl; 24 | cout << "ceil(-3.4) = " << ceil(-3.4) << endl; 25 | cout << "ceil(-7) = " << ceil(-7) << endl; 26 | cout << "ceil(sqrt(75)) = " << ceil(sqrt(75)) << endl; 27 | cout << "ceil(cbrt(75)) = " << ceil(cbrt(75)) << endl; 28 | cout << "ceil(e)= " << ceil(exp(1.0)); 29 | return 0; 30 | } 31 | //maiceil 32 | ///Program end 33 | 34 | /* 35 | output 36 | floor (3.4) = 3 37 | floor(-3.4) = -4 38 | floor(-7) = -7 39 | floor(sqrt(75)) = 8 40 | floor(cbrt(75)) = 4 41 | floor(e)= 2 42 | 43 | ***************** 44 | ceil (3.4) = 4 45 | ceil(-3.4) = -3 46 | ceil(-7) = -7 47 | ceil(sqrt(75)) = 9 48 | ceil(cbrt(75)) = 5 49 | ceil(e)= 3 50 | */ -------------------------------------------------------------------------------- /Chapter-07/Solved Problem/7-7.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 260 3 | 7.7 Consider the binary search tree T in Fig. 7.73(b), 4 | which is stored in memory as in Fig. 7.72. Suppose ITEM = 33 5 | is added to the tree T. 6 | (a) Find the new tree T. 7 | (b) Which changes occur in Fig. 7.72? 8 | */ 9 | #include 10 | #include 11 | #include 12 | using namespace std; 13 | typedef struct _tree 14 | { 15 | int data; 16 | _tree *left; 17 | _tree *right; 18 | } TREE; 19 | 20 | TREE *insert(TREE *root, int data) 21 | { 22 | if (root == NULL) 23 | { 24 | TREE *ptr = new TREE; 25 | ptr->data = data; 26 | ptr->left = NULL; 27 | ptr->right = NULL; 28 | root = ptr; 29 | } 30 | else if (root->data > data) 31 | { 32 | root->right = insert(root->right, data); 33 | } 34 | else 35 | root->left = insert(root->left, data); 36 | return root; 37 | } 38 | void inorder(TREE *root) 39 | { 40 | 41 | if(root != NULL) 42 | { 43 | inorder(root->left); 44 | cout << root->data << " "; 45 | inorder(root->right); 46 | } 47 | 48 | } 49 | int main() 50 | { 51 | int AVAIL = 12, temp; 52 | TREE *root = NULL; 53 | for(int i = 0; i <= AVAIL; i++) 54 | { 55 | cin >> temp; 56 | root = insert(root, temp); 57 | } 58 | inorder(root); 59 | 60 | } 61 | /* 62 | 60 63 | 30 64 | 70 65 | 20 66 | 55 67 | 35 68 | 45 69 | 40 70 | 50 71 | 90 72 | 80 73 | 95 74 | 33 75 | 95 90 80 70 60 55 50 45 40 35 33 30 20 76 | */ -------------------------------------------------------------------------------- /Chapter-03/3-18.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 54 3 | Algorithm 3.3: (Pattern Matching) P and T are strings with lengths R and S, 4 | respectively, and are stored as arrays with one character per element. This 5 | algorithm finds the INDEX of P in T. 6 | 7 | 1. [Initialize.] Set K := 1 and MAX : = S − R + 1. 8 | 2. Repeat Steps 3 to 5 while K ≤ MAX: 9 | 3.    Repeat for L = 1 to R: [Tests each character of P.] 10 | If P[L] ≠ T[K + L − 1], then: Go to Step 5. 11 | [End of inner loop.] 12 | 4.    [Success.] Set INDEX = K, and Exit. 13 | 5.    Set K := K + 1. 14 | [End of Step 2 outer loop.] 15 | 6. [Failure.] Set INDEX = 0. 16 | 7. Exit. 17 | */ 18 | #include 19 | #include 20 | using namespace std; 21 | int main() 22 | { 23 | string T, P; 24 | size_t K, MAX, S, R, INDEX = 0; 25 | 26 | 27 | cout << "Enter your Text\n"; 28 | getline(cin,T); 29 | cout << "Enter your keyword\n"; 30 | cin >> P; 31 | S = T.length(); //length of Main string 32 | R = P.length(); // Length of sub string 33 | K = 0; 34 | MAX = S - R + 1; 35 | while(K <= MAX) 36 | 37 | { 38 | for(int L = 0; L < R; L++) 39 | { 40 | if(P[L] != T[K + L -1]) 41 | goto Lable; 42 | } 43 | INDEX = K; 44 | break; 45 | Lable: 46 | K++; 47 | 48 | } 49 | cout << "Index = " << INDEX << endl; 50 | return 0; 51 | } 52 | /* 53 | output 54 | Enter your Text 55 | stored as arrays with one character per element 56 | Enter your keyword 57 | ent 58 | Index = 45 59 | */ -------------------------------------------------------------------------------- /Chapter-06/SOLVED PROBLEMS/6-17.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 6.18 Use the definition of the Ackermann function (Definition 6.3) to find A(l, 3). 3 | 4 | We have the following 15 steps: 5 | 6 | 7 | 8 | The forward indention indicates that we are postponing 9 | an evaluation and are recalling the definition, and the 10 | backward indention indicates that we are backtracking. 11 | 12 | Observe that the first formula in Definition 6.3 is used 13 | in Steps 5, 8, 11 and 14, the second formula in Step 4 and 14 | the third formula in Steps 1, 2 and 3. In the other Steps 15 | we are backtracking with substitutions. 16 | */ 17 | #include 18 | #include 19 | using namespace std; 20 | int ACKERMANN(int m, int n) 21 | { 22 | if(m == 0) 23 | { 24 | cout << "ACKERMANN( " << m << " , " << n << ") " << endl; 25 | return(n+1); 26 | } 27 | else 28 | if( (m > 0) && (n == 0)) 29 | { 30 | cout << "ACKERMANN(" << m-1 << "," << 1 << ")\n"; 31 | return ACKERMANN(m-1,1); 32 | } 33 | else 34 | if ((m > 0) && (n > 0)) 35 | { 36 | cout << "ACKERMANN( "<< m - 1 << ", ACKERMANN(" << m << "," << n - 1 << "))\n"; 37 | return ACKERMANN(m - 1 ,ACKERMANN( m , n - 1)); 38 | } 39 | 40 | return 0; 41 | } 42 | int main() 43 | { 44 | 45 | cout << ACKERMANN(1,3) < 10 | #include 11 | #include 12 | using namespace std; 13 | typedef struct _tree 14 | { 15 | char data; 16 | _tree *left; 17 | _tree *right; 18 | } TREE; 19 | 20 | TREE *insert(TREE *root, char data) 21 | { 22 | if (root == NULL) 23 | { 24 | TREE *ptr = new TREE; 25 | ptr->data = data; 26 | ptr->left = NULL; 27 | ptr->right = NULL; 28 | root = ptr; 29 | } 30 | else if (root->data < data) 31 | { 32 | root->right = insert(root->right, data); 33 | } 34 | else 35 | root->left = insert(root->left, data); 36 | return root; 37 | } 38 | void inorder(TREE *root) 39 | { 40 | 41 | if(root != NULL) 42 | { 43 | inorder(root->left); 44 | cout << root->data << " "; 45 | inorder(root->right); 46 | } 47 | 48 | } 49 | int main() 50 | { 51 | int AVAIL = 12; 52 | char temp; 53 | TREE *root = NULL; 54 | for(int i = 0; i < AVAIL; i++) 55 | { 56 | cin >> temp; 57 | root = insert(root, temp); 58 | } 59 | inorder(root); 60 | 61 | } 62 | /* 63 | g++ 7-8.cpp -o 7-8 } ; if ($?) { .\7-8 } 64 | J 65 | R 66 | D 67 | G 68 | T 69 | E 70 | M 71 | H 72 | P 73 | A 74 | F 75 | Q 76 | A D E F G H J M P Q R T 77 | */ -------------------------------------------------------------------------------- /Chapter-03/Algorithm/Algorithm3-3.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 54 3 | Algorithm 3.3: (Pattern Matching) P and T are strings with lengths R and S, 4 | respectively, and are stored as arrays with one character per element. This 5 | algorithm finds the INDEX of P in T. 6 | 7 | 1. [Initialize.] Set K := 1 and MAX : = S − R + 1. 8 | 2. Repeat Steps 3 to 5 while K ≤ MAX: 9 | 3.    Repeat for L = 1 to R: [Tests each character of P.] 10 | If P[L] ≠ T[K + L − 1], then: Go to Step 5. 11 | [End of inner loop.] 12 | 4.    [Success.] Set INDEX = K, and Exit. 13 | 5.    Set K := K + 1. 14 | [End of Step 2 outer loop.] 15 | 6. [Failure.] Set INDEX = 0. 16 | 7. Exit. 17 | */ 18 | #include 19 | #include 20 | using namespace std; 21 | int main() 22 | { 23 | string T, P; 24 | size_t K, MAX, S, R, INDEX = 0; 25 | 26 | 27 | cout << "Enter your Text\n"; 28 | getline(cin,T); 29 | cout << "Enter your keyword\n"; 30 | cin >> P; 31 | S = T.length(); //length of Main string 32 | R = P.length(); // Length of sub string 33 | K = 0; 34 | MAX = S - R + 1; 35 | while(K <= MAX) 36 | { 37 | for(int L = 0; L < R; L++) 38 | { 39 | if(P[L] != T[K + L -1]) 40 | goto Lable; 41 | } 42 | INDEX = K; 43 | break; 44 | Lable: 45 | K++; 46 | 47 | } 48 | cout << "Index = " << INDEX << endl; 49 | return 0; 50 | } 51 | /* 52 | output 53 | Enter your Text 54 | stored as arrays with one character per element 55 | Enter your keyword 56 | ent 57 | Index = 45 58 | */ -------------------------------------------------------------------------------- /Chapter-06/PROGRAMMING PROBLEMS/6-52.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | page 213 3 | 6.2  Write a program which gives the solution to the Towers 4 | of Hanoi problem for n disks. Test the program using (a) n = 3 and (b) n = 4 5 | */ 6 | #include 7 | #include 8 | using namespace std; 9 | void TOWER(int N, string BEG, string AUX, string END) 10 | { 11 | if(N > 0) 12 | { 13 | TOWER(N-1, BEG, END, AUX); 14 | cout << "Move top disk form pag " << BEG << " to pag " << END << endl; 15 | TOWER(N-1, AUX, BEG, END); 16 | } 17 | } 18 | int main() 19 | { 20 | TOWER(3, "A", "B", "C"); 21 | 22 | cout << "\n\n" << endl; 23 | 24 | TOWER(4, "A", "B", "C"); 25 | return 0; 26 | } 27 | /* 28 | Move top disk form pag A to pag C 29 | Move top disk form pag A to pag B 30 | Move top disk form pag C to pag B 31 | Move top disk form pag A to pag C 32 | Move top disk form pag B to pag A 33 | Move top disk form pag B to pag C 34 | Move top disk form pag A to pag C 35 | 36 | 37 | 38 | Move top disk form pag A to pag B 39 | Move top disk form pag A to pag C 40 | Move top disk form pag B to pag C 41 | Move top disk form pag A to pag B 42 | Move top disk form pag C to pag A 43 | Move top disk form pag C to pag B 44 | Move top disk form pag A to pag B 45 | Move top disk form pag A to pag C 46 | Move top disk form pag B to pag C 47 | Move top disk form pag B to pag A 48 | Move top disk form pag C to pag A 49 | Move top disk form pag B to pag C 50 | Move top disk form pag A to pag B 51 | Move top disk form pag A to pag C 52 | Move top disk form pag B to pag C 53 | */ -------------------------------------------------------------------------------- /Chapter-06/SUPPLEMENTARY PROBLEMS/6-41.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 6.16 Use Definition 6.3 (of the Ackermann function) to find A(2, 2). 3 | */ 4 | #include 5 | #include 6 | using namespace std; 7 | int ACKERMANN(int m, int n) 8 | { 9 | if(m == 0) 10 | { 11 | cout << "ACKERMANN( " << m << " , " << n << ") " << endl; 12 | return(n+1); 13 | } 14 | else 15 | if( (m > 0) && (n == 0)) 16 | { 17 | cout << "ACKERMANN(" << m-1 << "," << 1 << ")\n"; 18 | return ACKERMANN(m-1,1); 19 | } 20 | else 21 | if ((m > 0) && (n > 0)) 22 | { 23 | cout << "ACKERMANN( "<< m - 1 << ", ACKERMANN(" << m << "," << n - 1 << "))\n"; 24 | return ACKERMANN(m - 1 ,ACKERMANN( m , n - 1)); 25 | } 26 | 27 | return 0; 28 | } 29 | int main() 30 | { 31 | 32 | cout << ACKERMANN(2,2) < 47 | void CASEA(T INFO[], int LEFT[], int RIGHT[], int ROOT, int LOC, int PAR) 48 | { 49 | int CHILD; 50 | if((LEFT[LOC] == -1) && (RIGHT[LOC]== -1)) 51 | { 52 | CHILD = -1; 53 | } 54 | else 55 | if(LEFT[LOC] != -1) 56 | { 57 | CHILD = LEFT[LOC]; 58 | } 59 | else 60 | CHILD = RIGHT[LOC]; 61 | 62 | if(PAR != -1) 63 | { 64 | if(LOC == LEFT[PAR]) 65 | LEFT[PAR] = CHILD; 66 | else 67 | RIGHT[PAR] = CHILD; 68 | } 69 | else 70 | ROOT = CHILD; 71 | } -------------------------------------------------------------------------------- /Chapter-07/Supplemenary Problems/7-26.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | page 271 3 | 7.8  Suppose ROOTA points to a binary tree T1 in memory. Write a procedure which 4 | makes a copy T2 of the tree T1 using ROOTB as a pointer. 5 | 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | using namespace std; 12 | typedef struct _tree 13 | { 14 | int data; 15 | _tree *left; 16 | _tree *right; 17 | } TREE; 18 | 19 | TREE *insert(TREE *root, int data) 20 | { 21 | if (root == NULL) 22 | { 23 | TREE *ptr = new TREE; 24 | ptr->data = data; 25 | ptr->left = NULL; 26 | ptr->right = NULL; 27 | root = ptr; 28 | } 29 | else if (root->data > data) 30 | { 31 | root->right = insert(root->right, data); 32 | } 33 | else 34 | root->left = insert(root->left, data); 35 | return root; 36 | } 37 | void inorder(TREE *root) 38 | { 39 | 40 | if(root != NULL) 41 | { 42 | inorder(root->left); 43 | cout << root->data << " "; 44 | inorder(root->right); 45 | } 46 | 47 | } 48 | 49 | int main() 50 | { 51 | int AVAIL = 11, temp; 52 | TREE *rootA = NULL; 53 | for(int i = 0; i < AVAIL; i++) 54 | { 55 | cin >> temp; 56 | rootA = insert(rootA, temp); 57 | } 58 | inorder(rootA); 59 | cout << "\n"; 60 | TREE *rootB = rootA; 61 | inorder(rootB); 62 | cout << "\n"; 63 | 64 | } 65 | /* 66 | 60 67 | 30 68 | 70 69 | 20 70 | 55 71 | 35 72 | 45 73 | 40 74 | 50 75 | 90 76 | 80 77 | 90 80 70 60 55 50 45 40 35 30 20 78 | 90 80 70 60 55 50 45 40 35 30 20 79 | */ -------------------------------------------------------------------------------- /Chapter-07/ALGORITHM WITH POINTER/Algorithm-7-2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | page 227 3 | void in_order(tree_node *root) 4 | { 5 | if (root != NULL) 6 | { 7 | in_order(root->left); 8 | cout << root->data << " "; 9 | in_order(root->right); 10 | } 11 | } 12 | */ 13 | #include 14 | #include 15 | #include 16 | #include 17 | using namespace std; 18 | typedef struct _tree 19 | { 20 | int data; 21 | _tree *left; 22 | _tree *right; 23 | } tree_node; 24 | 25 | tree_node *new_node(int data) 26 | { 27 | tree_node *temp = new tree_node; 28 | temp->data = data; 29 | temp->left = NULL; 30 | temp->right = NULL; 31 | return temp; 32 | } 33 | //insert data into tree 34 | tree_node *insert(tree_node *root, int data) 35 | { 36 | if (root == NULL) 37 | { 38 | root = new_node(data); 39 | } 40 | else if (data <= root->data) 41 | { 42 | root->left = insert(root->left, data); 43 | } 44 | else 45 | { 46 | root->right = insert(root->right, data); 47 | } 48 | return root; 49 | } 50 | //INORDER 51 | void in_order(tree_node *root) 52 | { 53 | if (root != NULL) 54 | { 55 | in_order(root->left); 56 | cout << root->data << " "; 57 | in_order(root->right); 58 | } 59 | } 60 | int main() 61 | { 62 | srand(time(0)); 63 | int temp; 64 | tree_node *start = NULL; 65 | for(int i = 0; i < 10; i++) 66 | { 67 | temp = (rand()%20)-5; 68 | start = insert(start, temp); 69 | } 70 | in_order(start); 71 | 72 | } 73 | /* 74 | output 75 | -3 -1 1 4 6 8 8 8 12 12 76 | */ -------------------------------------------------------------------------------- /Chapter-07/ALGORITHM WITH POINTER/Algorithm-7-1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | page 226 3 | void pre_order(tree_node *root) 4 | { 5 | if(root != NULL) 6 | { 7 | cout << root->data << " "; 8 | pre_order(root->left); 9 | pre_order(root->right); 10 | } 11 | } 12 | */ 13 | #include 14 | #include 15 | #include 16 | #include 17 | using namespace std; 18 | typedef struct _tree 19 | { 20 | int data; 21 | _tree *left; 22 | _tree *right; 23 | } tree_node; 24 | 25 | tree_node *new_node(int data) 26 | { 27 | tree_node *temp = new tree_node; 28 | temp->data = data; 29 | temp->left = NULL; 30 | temp->right = NULL; 31 | return temp; 32 | } 33 | //insert data into tree 34 | tree_node *insert(tree_node *root, int data) 35 | { 36 | if (root == NULL) 37 | { 38 | root = new_node(data); 39 | } 40 | else if (data <= root->data) 41 | { 42 | root->left = insert(root->left, data); 43 | } 44 | else 45 | { 46 | root->right = insert(root->right, data); 47 | } 48 | return root; 49 | } 50 | //PRERDER 51 | void pre_order(tree_node *root) 52 | { 53 | if(root != NULL) 54 | { 55 | cout << root->data << " "; 56 | pre_order(root->left); 57 | pre_order(root->right); 58 | } 59 | } 60 | int main() 61 | { 62 | srand(time(0)); 63 | int temp; 64 | tree_node *start = NULL; 65 | for(int i = 0; i < 10; i++) 66 | { 67 | temp = (rand()%20)-5; 68 | start = insert(start, temp); 69 | } 70 | pre_order(start); 71 | 72 | } 73 | /* 74 | output 75 | 13 -4 -4 -1 -1 11 0 5 11 14 76 | */ -------------------------------------------------------------------------------- /Chapter-07/ALGORITHM/Algorithm-7-1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Algorithm 7.1: PREORD(INFO, LEFT, RIGHT, ROOT) 3 | A binary tree T is in memory. The algorithm does a preorder traversal of T, 4 | applying an operation PROCESS to each of its nodes. An array STACK is used 5 | to temporarily hold the addresses of nodes. 6 | 7 | 1. [Initially push NULL onto STACK, and initialize PTR.] 8 | Set TOP := 1, STACK[1] := NULL and PTR := ROOT. 9 | 10 | 2. Repeat Steps 3 to 5 while PTR ≠ NULL: 11 | 12 | 3. Apply PROCESS to INFO[PTR]. 13 | 14 | 4. [Right child?] 15 | 16 | If RIGHT[PTR] ≠ NULL, then: [Push on STACK.] 17 | 18 |   Set TOP := TOP + 1, and STACK[TOP] := RIGHT[PTR]. 19 | 20 | [End of If structure.] 21 | 22 | 5. [Left child?] 23 | 24 | If LEFT[PTR] ≠ NULL, then: 25 | 26 |   Set PTR := LEFT[PTR]. 27 | 28 | Else: [Pop from STACK.] 29 | 30 |   Set PTR := STACK[TOP] and TOP := TOP – 1. 31 | 32 | [End of If structure.] 33 | 34 | [End of Step 2 loop.] 35 | 36 | 6. Exit. 37 | */ 38 | #include 39 | using namespace std; 40 | template 41 | void PREORD(T INFO[], int LEFT[], int RIGHT[], int ROOT) 42 | { 43 | int STACK[10], PTR, TOP; 44 | STACK[0] = -1; 45 | PTR = ROOT; 46 | TOP = 0; 47 | while(PTR != -1) 48 | { 49 | cout << INFO[PTR]; 50 | if(RIGHT[PTR] != NULL) 51 | { 52 | TOP = TOP + 1; 53 | STACK[TOP] = RIGHT[PTR]; 54 | } 55 | if(LEFT[PTR] != -1) 56 | { 57 | PTR = LEFT[PTR]; 58 | } 59 | else 60 | { 61 | PTR = STACK[TOP]; 62 | TOP = TOP - 1; 63 | } 64 | } 65 | return ; 66 | 67 | } 68 | -------------------------------------------------------------------------------- /Chapter-07/ALGORITHM WITH POINTER/Algorithm-7-3.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | page 229 3 | void in_order(tree_node *root) 4 | { 5 | if (root != NULL) 6 | { 7 | in_order(root->left); 8 | in_order(root->right); 9 | cout << root->data << " "; 10 | } 11 | } 12 | */ 13 | #include 14 | #include 15 | #include 16 | #include 17 | using namespace std; 18 | typedef struct _tree 19 | { 20 | int data; 21 | _tree *left; 22 | _tree *right; 23 | } tree_node; 24 | 25 | tree_node *new_node(int data) 26 | { 27 | tree_node *temp = new tree_node; 28 | temp->data = data; 29 | temp->left = NULL; 30 | temp->right = NULL; 31 | return temp; 32 | } 33 | //insert data into tree 34 | tree_node *insert(tree_node *root, int data) 35 | { 36 | if (root == NULL) 37 | { 38 | root = new_node(data); 39 | } 40 | else if (data <= root->data) 41 | { 42 | root->left = insert(root->left, data); 43 | } 44 | else 45 | { 46 | root->right = insert(root->right, data); 47 | } 48 | return root; 49 | } 50 | //INORDER 51 | void post_order(tree_node *root) 52 | { 53 | if (root != NULL) 54 | { 55 | post_order(root->left); 56 | post_order(root->right); 57 | cout << root->data << " "; 58 | } 59 | } 60 | int main() 61 | { 62 | srand(time(0)); 63 | int temp; 64 | tree_node *start = NULL; 65 | for(int i = 0; i < 10; i++) 66 | { 67 | temp = (rand()%20)-5; 68 | start = insert(start, temp); 69 | } 70 | post_order(start); 71 | 72 | } 73 | /* 74 | output 75 | -5 -4 2 -1 -4 -5 12 3 14 2 76 | */ -------------------------------------------------------------------------------- /Chapter-03/3-24.CPP: -------------------------------------------------------------------------------- 1 | /* 2 | Let W be the string ABCD. 3 | (a) Find the length of W. 4 | (b) List all substrings of W. 5 | (c) List all the initial substrings of W. 6 | */ 7 | #include 8 | #include 9 | using namespace std; 10 | int main() 11 | { 12 | string W, R; 13 | int sn = 0; 14 | cout << "Enter your String \n"; 15 | cin >> W; 16 | //(a) Find the length of W. 17 | cout << "Length of String = " << W.length() << endl; 18 | 19 | int k = W.length(); 20 | //(b) List all substrings of W. 21 | cout << "(b) List all substrings of W.\n"; 22 | for(int i = 0; i <= W.length(); i++,k--) 23 | { 24 | for(int j = 0; j <= i; j++) 25 | { 26 | R = W.substr(j,k); 27 | cout << R << " " ; 28 | sn++; 29 | 30 | 31 | } 32 | cout << "\n"; 33 | } 34 | cout << "Number of sub string " << sn - W.length() << endl; 35 | k = W.length(); 36 | //(c) List all the initial substrings of W. 37 | cout << "(c)List all the initial substrings of W\n"; 38 | for(int i = 0; i <= W.length(); i++,k--) 39 | { 40 | for(int j = 0; j < 1; j++) 41 | { 42 | R = W.substr(j,k); 43 | cout << R << " " ; 44 | } 45 | cout << "\n"; 46 | } 47 | // 48 | return 0; 49 | } 50 | /* 51 | Enter your String 52 | XYZST 53 | Length of String = 5 54 | (b) List all substrings of W. 55 | XYZST 56 | XYZS YZST 57 | XYZ YZS ZST 58 | XY YZ ZS ST 59 | X Y Z S T 60 | 61 | Number of sub string 16 62 | (c)List all the initial substrings of W 63 | XYZST 64 | XYZS 65 | XYZ 66 | XY 67 | X 68 | */ -------------------------------------------------------------------------------- /Chapter-02/Solved Problem/2-8.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 36 3 | Program 4 | (a) Write a procedure FIND(DATA, N, LOC1, LOC2) which finds the location LOC1 5 | of the largest element and the location LOC2 of the second largest element in 6 | an array DATA with n > 1 elements. 7 | 8 | (b) Why not let FIND also find the values of the largest and second largest elements? 9 | 10 | */ 11 | #include 12 | using namespace std; 13 | 14 | 15 | template 16 | void FIND(T DATA[], int N, int &LOC1, int &LOC2) 17 | { 18 | T FIRST = DATA[1]; 19 | T SECOND = DATA[2]; 20 | if (FIRST < SECOND) 21 | { 22 | swap(FIRST, SECOND); 23 | LOC1 = 2; 24 | LOC2 = 1; 25 | } 26 | for (int i = 0; i < N; i++) 27 | { 28 | if (FIRST < DATA[i]) 29 | { 30 | SECOND = FIRST; 31 | FIRST = DATA[i]; 32 | LOC2 = LOC1; 33 | LOC1 = i; 34 | } 35 | else 36 | if(SECOND < DATA[i]) 37 | { 38 | SECOND = DATA[i]; 39 | LOC2 = i; 40 | } 41 | 42 | } 43 | } 44 | //main begin 45 | int main() 46 | { 47 | int LOC1, LOC2; 48 | int DATA[] = {2289 ,5827 ,10253 ,26079 ,12679 ,28840 ,30895 ,19785 ,4761 ,7614}; 49 | FIND (DATA,(sizeof(DATA)/sizeof(int)),LOC1,LOC2); 50 | cout << "LOC1 = " << LOC1 << " LOC2 = " << LOC2 << endl; 51 | cout << "Leargest number is " << DATA[LOC1] << "\nSecond leargest is " << DATA[LOC2] << endl; 52 | 53 | getchar(); 54 | return 0; 55 | } 56 | //main end 57 | ///Program end 58 | 59 | /* 60 | output 61 | LOC1 = 6 LOC2 = 5 62 | Leargest number is 30895 63 | Second leargest is 28840 64 | */ -------------------------------------------------------------------------------- /Chapter-04/Supplementary Problems/4-29.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 110 3 | Program 4 | 4.29 5 | 4.9 A class of student records is organized as follows: 6 | 7 | 1 Student(35), 2 Name, 3 Last, 3 First, 3 MI (Middle Initial), 2 Major 8 | 9 | 2 Test(4), 2 Final, 2 Grade 10 | 11 | (a) How many elementary items are there? 12 | 13 | (b) Describe a record structure—for example, a PL/1 structure 14 | or a Pascal record, to store the data. 15 | 16 | (c) Describe the output of each of the following Write statements: 17 | (i) Write: Final[15], 18 | (ii) Write: Name[15] and 19 | (iii) Write: Test[4]. 20 | 21 | */ 22 | #include 23 | #include 24 | using namespace std; 25 | struct STUDENT 26 | { 27 | struct 28 | { 29 | string FIRST; 30 | string LAST; 31 | string MI; 32 | }NAME; 33 | string Major; 34 | int Test[4]; 35 | int FINAL; 36 | int Grade; 37 | }; 38 | 39 | //Main from Here 40 | int main() 41 | { 42 | STUDENT student[35]; 43 | for(int i = 0; i < 35; i++) 44 | { 45 | cin >> student[i].NAME.FIRST; 46 | cin >> student[i].NAME.MI; 47 | cin >> student[i].NAME.LAST; 48 | cin >> student[i].Major; 49 | for(int k = 0; k < 4; k++) 50 | cin >> student[4].Test[k]; 51 | cin >> student[i].FINAL; 52 | cin >> student[i].Grade; 53 | 54 | } 55 | cout < N, then: 15 | 16 | Write: LOC, MAX, and Exit. 17 | 18 | Step 4. [Compare and update.] If MAX < DATA[K], then: 19 | 20 | Set LOC : = K and MAX : = DATA[K]. 21 | 22 | Step 5. [Repeat loop.] Go to Step 2 23 | */ 24 | 25 | #include 26 | using namespace std; 27 | int main() 28 | { 29 | int K , LOC, MAX, N; // Declaring Variable K< LOC , N and MAX 30 | N = 5; // Initialize N = 5 31 | int DATA[5] = { 45, 18, 65 ,78,14}; // Declar and Initialize at the same time 32 | 33 | LOC = 0; //Initialize LOC = 0 .. or First Position 34 | MAX = DATA[0]; //Initialize MAX == DATA[0] Or First Data int Data Array 35 | // Checking conditon if Number of comprison is greater 36 | while (K < N) // then Number of Data in Data Array then break the loop 37 | { 38 | K++; 39 | if(MAX < DATA[K]) // If MAX is less then DATA[K] Then Update MAX Value and Also LOC value 40 | { 41 | LOC = K; 42 | MAX = DATA[K]; 43 | } 44 | } 45 | cout << "LOC " << LOC + 1 << " MAX " << MAX << endl; 46 | return 0; 47 | } 48 | //main end 49 | ///Program end 50 | 51 | /* 52 | output 53 | LOC 4 MAX 78 54 | */ -------------------------------------------------------------------------------- /Chapter-02/Chapter Algorithm/Algorithm2-3.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Chapter 2 4 | Page 21 5 | 6 | Algorithm 2.1: (Largest Element in Array) A nonempty array DATA with N numerical values is given. 7 | This algorithm finds the location LOC and the value MAX of the largest element of DATA. 8 | The variable K is used as a counter. 9 | 10 | Step 1. [Initialize.] Set K : = 1, LOC : = 1 and MAX : = DATA[1]. 11 | 12 | Step 2. [Increment counter.] Set K : = K + 1. 13 | 14 | Step 3. [Test counter.] If K > N, then: 15 | 16 | Write: LOC, MAX, and Exit. 17 | 18 | Step 4. [Compare and update.] If MAX < DATA[K], then: 19 | 20 | Set LOC : = K and MAX : = DATA[K]. 21 | 22 | Step 5. [Repeat loop.] Go to Step 2 23 | */ 24 | 25 | #include 26 | using namespace std; 27 | int main() 28 | { 29 | int K , LOC, MAX, N; // Declaring Variable K< LOC , N and MAX 30 | N = 5; // Initialize N = 5 31 | int DATA[N] = { 45, 18, 65 ,78,14}; // Declar and Initialize at the same time 32 | 33 | LOC = 0; //Initialize LOC = 0 .. or First Position 34 | MAX = DATA[0]; //Initialize MAX == DATA[0] Or First Data int Data Array 35 | // Checking conditon if Number of comprison is greater 36 | while (K < N) // then Number of Data in Data Array then break the loop 37 | { 38 | K++; 39 | if(MAX < DATA[K]) // If MAX is less then DATA[K] Then Update MAX Value and Also LOC value 40 | { 41 | LOC = K; 42 | MAX = DATA[K]; 43 | } 44 | } 45 | cout << "LOC " << LOC + 1 << " MAX " << MAX << endl; 46 | return 0; 47 | } 48 | //main end 49 | ///Program end 50 | 51 | /* 52 | output 53 | LOC 4 MAX 78 54 | */ -------------------------------------------------------------------------------- /Chapter-04/MISCELLANEOUS/4-46.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 3 | Program 4 | 4.16 Consider the polynomial 5 | 6 | f (x) = a1xn + a2xn−1 + … + anx + an+1 7 | 8 | Evaluating the polynomial in the obvious way would require 9 | 10 | multiplications and n additions. However, one can rewrite the 11 | polynomial by successively factoring out x as follows: 12 | 13 | f (x) = ((… ((a1x + a2)x + a3)x + …)x + an)x + an+l 14 | 15 | This uses only n multiplications and n additions. This second 16 | way of evaluating a polynomial is called Horner’s method. 17 | 18 | (a) Rewrite the polynomial f (x) = 5x4 − 6x3 + 7x2 + 8x − 9 as 19 | it would be evaluated using Horner’s method. 20 | 21 | (b) Suppose the coefficients of a polynomial are in memory in a 22 | linear array A(N + 1). (That is, A[1] is the coefficient of xn, 23 | A[2] is the coefficient of xn–1, …, and A[N + 1] is the constant.) 24 | Write a procedure HORNER(A, N + 1, X, Y) which finds the value 25 | Y = F(X) for a given value X using Horner’s method. 26 | 27 | Test the program using X = 2 and f (x) from part (a). 28 | */ 29 | #include 30 | #include 31 | #include 32 | using namespace std; 33 | void HORNER(int A[], int N, int X, int &Y) 34 | { 35 | 36 | for(int i = 0; i < N; i++) 37 | { 38 | if(i == N-1 ) 39 | { 40 | Y = Y + A[i]; 41 | } 42 | else 43 | { 44 | Y = (Y+A[i])*X; 45 | } 46 | } 47 | 48 | } 49 | //Main from Here 50 | int main() 51 | { 52 | int sum = 0, X = 2, Y; 53 | int A[5] = {5,-6,7,8,-9}; 54 | HORNER(A, 5, X, Y); 55 | cout << "Horner's method = " << Y << " \n"; 56 | 57 | } 58 | 59 | /* 60 | Output 61 | Horner's method = 67 62 | */ -------------------------------------------------------------------------------- /Chapter-05/Algorithm/Algorithm-5-11.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 142 3 | 4 | Algorithm 5.11: (Traversing a Circular Header List) Let LIST be a circular header 5 | list in memory. This algorithm traverses LIST, applying an operation PROCESS to each node of LIST. 6 | 1. Set PTR := LINK[START]. [Initializes the pointer PTR.] 7 | 2. Repeat Steps 3 and 4 while PTR ≠ START: 8 | 3.       Apply PROCESS to INFO[PTR]. 9 | 4.       Set PTR := LINK[PTR]. [PTR now points to the next node.] 10 | [End of Step 2 loop.] 11 | 5. Exit. 12 | */ 13 | #include 14 | #include 15 | #include 16 | using namespace std; 17 | typedef struct _link 18 | { 19 | int DATA; 20 | _link *next; 21 | }LINKED; 22 | /****************************/ 23 | void ADD_TILE(LINKED **HEAD, int DATA) 24 | { 25 | LINKED *PTR = *HEAD , *NEW; 26 | if(*HEAD == NULL) 27 | { 28 | *HEAD = new LINKED; 29 | (*HEAD)->DATA = DATA; 30 | (*HEAD)->next = *HEAD; 31 | } 32 | else 33 | { 34 | while(PTR->next != *HEAD) 35 | { 36 | PTR = PTR->next; 37 | } 38 | NEW = new LINKED; 39 | NEW->DATA = DATA; 40 | NEW->next = *HEAD; 41 | PTR->next = NEW; 42 | } 43 | return; 44 | } 45 | void DISPLAY(LINKED *HEAD) 46 | { 47 | LINKED *PTR = HEAD; 48 | while(PTR->next != HEAD) 49 | { 50 | cout << PTR->DATA << " \n"; 51 | PTR = PTR->next; 52 | } 53 | cout << PTR->DATA << " \n"; 54 | } 55 | int main() 56 | { 57 | int DATA; 58 | LINKED *HEAD; 59 | HEAD = NULL; 60 | for(int i = 1; i < 11; i++) 61 | { 62 | ADD_TILE(&HEAD, i*10); 63 | //cout << "I = " << i*10 << endl; 64 | } 65 | DISPLAY(HEAD); 66 | 67 | } 68 | -------------------------------------------------------------------------------- /Chapter-08/SUPPLEMENTARY PROBLEMS/8-27.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 8.7 Consider the weighted graph G in Fig. 8.38. 3 | Suppose the nodes are stored in an array DATA as follows: 4 | 5 | DATA: X, Y, S, T 6 | 7 | 8 | 9 | Fig. 8.38 10 | 11 | (a) Find the weight matrix W of G. 12 | (b) Find the matrix Q of shortest paths using Warshall’s Algorithm 8.2. 13 | */ 14 | #include 15 | #include 16 | #include 17 | using namespace std; 18 | #define INF 99999 19 | int MIN(int A, int B) 20 | { 21 | return (A>B) ? B : A; 22 | } 23 | void SORTEST(int W[][4], int N) 24 | { 25 | int Q[N][N]; 26 | for(int i = 0; i < N; i++) 27 | { 28 | for(int j = 0; j < N; j++) 29 | { 30 | cout << W[i][j] << " "; 31 | if(W[i][j] == 0) 32 | Q[i][j] = INF; 33 | else 34 | Q[i][j] = W[i][j]; 35 | } 36 | cout << "\n"; 37 | } 38 | for(int k = 0; k < N; k++) 39 | { 40 | for(int i = 0; i < N; i++) 41 | { 42 | for(int j = 0; j < N; j++) 43 | { 44 | Q[i][j] = MIN(Q[i][j], Q[i][k] + Q[k][j]); 45 | } 46 | } 47 | } 48 | cout << "\n"; 49 | for(int i = 0; i < N; i++) 50 | { 51 | for(int j = 0; j < N; j++) 52 | { 53 | cout << Q[i][j] << " "; 54 | } 55 | cout << "\n"; 56 | } 57 | } 58 | int main() 59 | { 60 | int W[4][4] = { {7,5,0,0}, 61 | {7,0,0,2}, 62 | {0,3,0,0}, 63 | {4,0,1,0}}; 64 | SORTEST(W, 4); 65 | 66 | } 67 | /* 68 | 7 5 0 0 69 | 7 0 0 2 70 | 0 3 0 0 71 | 4 0 1 0 72 | 73 | 7 5 8 7 74 | 6 6 3 2 75 | 9 3 6 5 76 | 4 4 1 6 77 | */ -------------------------------------------------------------------------------- /Chapter-07/Solved Problem/7-2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 260 3 | 7.2 A binary tree T has 9 nodes. The inorder and preorder 4 | traversals of T yield the following sequences of nodes: 5 | 6 | Inorder: E A C K F H D B G 7 | Preorder: F A E K C D H G B 8 | Draw the tree T. 9 | */ 10 | #include 11 | #include 12 | #include 13 | using namespace std; 14 | typedef struct _tree 15 | { 16 | char data; 17 | _tree *left; 18 | _tree *right; 19 | } TREE; 20 | 21 | TREE *insert(TREE *root, char data) 22 | { 23 | if (root == NULL) 24 | { 25 | TREE *ptr = new TREE; 26 | ptr->data = data; 27 | ptr->left = NULL; 28 | ptr->right = NULL; 29 | root = ptr; 30 | } 31 | else if (root->data > data) 32 | { 33 | root->right = insert(root->right, data); 34 | } 35 | else 36 | root->left = insert(root->left, data); 37 | return root; 38 | } 39 | void inorder(TREE *root) 40 | { 41 | 42 | if(root != NULL) 43 | { 44 | inorder(root->left); 45 | cout << root->data << " "; 46 | inorder(root->right); 47 | } 48 | 49 | } 50 | void PreOrder(TREE *root) 51 | { 52 | if(root != NULL) 53 | { 54 | cout << root->data << " "; 55 | inorder(root->left); 56 | inorder(root->right); 57 | } 58 | } 59 | int main() 60 | { 61 | int AVAIL = 9; 62 | char temp; 63 | TREE *root = NULL; 64 | for(int i = 0; i < AVAIL; i++) 65 | { 66 | cin >> temp; 67 | root = insert(root, temp); 68 | } 69 | inorder(root); 70 | cout << "\n"; 71 | PreOrder(root); 72 | 73 | } 74 | /* 75 | A 76 | E 77 | C 78 | K 79 | F 80 | H 81 | D 82 | B 83 | G 84 | K H G F E D C B A 85 | */ -------------------------------------------------------------------------------- /Chapter-07/ALGORITHM/Algorithm-7-2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Algorithm 7.2: INORD(INFO, LEFT, RIGHT, ROOT) 3 | A binary tree is in memory. This algorithm does an inorder traversal of T, 4 | applying an operation PROCESS to each of its nodes. An array STACK is used 5 | to temporarily hold the addresses of nodes. 6 | 7 | 1. [Push NULL onto STACK and initialize PTR.] 8 | Set TOP := 1, STACK[1] := NULL and PTR := ROOT. 9 | 10 | 2. Repeat while PTR ≠ NULL: [Pushes left-most path onto STACK.] 11 | 12 | (a)   Set TOP := TOP + 1 and STACK[TOP] := PTR. [Saves node.] 13 | 14 | (b)    Set PTR := LEFT[PTR]. [Updates PTR.] 15 | 16 | [End of loop.] 17 | 18 | 3. Set PTR := STACK[TOP] and TOP := TOP – 1. [Pops node from STACK.] 19 | 20 | 4. Repeat Steps 5 to 7 while PTR ≠ NULL: [Backtracking.] 21 | 22 | 5. Apply PROCESS to INFO[PTR]. 23 | 24 | 6. [Right child?] If RIGHT[PTR] ≠ NULL, then: 25 | 26 | (a)   Set PTR := RIGHT[PTR]. 27 | 28 | (b) Go to Step 3. 29 | 30 | [End of If structure.] 31 | 32 | 7. Set PTR := STACK[TOP] and TOP := TOP –1. [Pops node.] 33 | 34 | [End of Step 4 loop.] 35 | 36 | 8. Exit. 37 | */ 38 | template 39 | void INORD(T INFO[], int LEFT[], int RIGHT[], int ROOT, int N) 40 | { 41 | int STACT[N] , PTR, TOP ; 42 | STACT[0] = -1; 43 | TOP = 0; 44 | PTR = ROOT; 45 | STEP2: 46 | while(PTR != -1) 47 | { 48 | TOP = TOP + 1; 49 | STACT[TOP] = PTR; 50 | PTR = LEFT[PTR]; 51 | } 52 | STEP3: 53 | PTR = STACT[TOP]; 54 | TOP = TOP - 1; 55 | while(PTR != -1) 56 | { 57 | cout << INFO[PTR] << endl; 58 | if(RIGHT[PTR] != -1) 59 | { 60 | PTR = RIGHT[PTR]; 61 | goto STEP2; 62 | } 63 | PTR = STACT[TOP]; 64 | TOP = TOP - 1; 65 | } 66 | } -------------------------------------------------------------------------------- /Chapter-02/Supplementary Problems/2-17.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | page 39 3 | Program 4 | Find 5 | (a) |3 + 8|, |3 − 8|, |−3 + 8|, |−3−8|; 6 | (b) 7!, 8!, 14!/12!, 15!/16!, 7 | */ 8 | #include 9 | using namespace std; 10 | template 11 | T fact(T x, T y = 0.0); 12 | int absoluteValue(int x, int y) 13 | { 14 | int temp = x = y; 15 | if(temp < 0) 16 | return(temp*(-1)); 17 | else 18 | return (temp); 19 | } 20 | template 21 | T fact(T x, T y) 22 | { 23 | if(x == y) 24 | return 1; 25 | else 26 | return(x*fact(x-1,y)); 27 | } 28 | template 29 | T factDc(T x, T y) 30 | { 31 | if(x < y) 32 | { 33 | return (1/fact(y,x)); 34 | } 35 | else 36 | return(fact(x,y)); 37 | } 38 | //main begin 39 | int main() 40 | { 41 | //a) |3 + 8|, |3 − 8|, |−3 + 8|, |−3−8|; 42 | cout << "|3 + 8| = " << absoluteValue(3,4) << endl; 43 | cout << "|3 - 8| = " << absoluteValue(3,-8) << endl; 44 | cout << "|3 + 8| = " << absoluteValue(-3,8) < DATA[PTR+1]) 20 | { 21 | CHANGE = DATA[PTR]; 22 | DATA[PTR] = DATA[PTR+1]; 23 | DATA[PTR+1] = CHANGE; 24 | } 25 | } 26 | } 27 | return; 28 | } 29 | int main() 30 | { 31 | int j = 52; 32 | int A[j]; 33 | for(int i = 0; i < j; i++) 34 | { 35 | cin >> A[i]; 36 | } 37 | PRINT(A,j); 38 | Bubble(A,j); 39 | PRINT(A,j); 40 | 41 | } 42 | /* 43 | 331 44 | 222 902 679 911 45 | 275 9 457 857 46 | 253 851 222 364 47 | 158 66 748 652 48 | 359 393 467 657 49 | 852 414 866 891 50 | 161 629 149 119 51 | 20 963 929 442 52 | 370 486 219 666 53 | 582 19 270 405 54 | 15 423 683 772 55 | 802 954 806 904 56 | 536 449 214 57 | 58 | ************************** 59 | 331 222 902 679 60 | 911 275 9 457 61 | 857 253 851 222 62 | 364 158 66 748 63 | 652 359 393 467 64 | 657 852 414 866 65 | 891 161 629 149 66 | 119 20 963 929 67 | 442 370 486 219 68 | 666 582 19 270 69 | 405 15 423 683 70 | 772 802 954 806 71 | 904 536 449 214 72 | ************************** 73 | 9 15 19 20 66 119 149 158 161 214 219 222 222 253 270 74 | 275 331 359 364 370 393 405 414 423 442 449 457 467 75 | 486 536 582 629 652 657 666 679 683 748 772 802 806 76 | 851 852 857 866 891 902 904 911 929 954 963 77 | 78 | */ -------------------------------------------------------------------------------- /Chapter-02/Solved Problem/2-2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 33 3 | Program 4 | 2.2 (a) Find 26 (mod 7), 34 (mod 8), 2345 (mod 6), 495 (mod 11). 5 | (b) Find −26 (mod 7), −2345 (mod 6), −371 (mod 8), −39 (mod 3). 6 | (c) Using arithmetic modulo 15, evaluate 9 + 13, 7 + 11, 4 − 9, 2 − 10. 7 | */ 8 | #include 9 | using namespace std; 10 | //main begin 11 | int main() 12 | { 13 | //(a) Find 26 (mod 7), 34 (mod 8), 2345 (mod 6), 495 (mod 11). 14 | cout << "26 (mod 7) " << 26 % 7 << endl; 15 | cout << "34 (mod 8) " << 34 % 8 << endl; 16 | cout << "2345 (mod 6)" << 2345 % 6 << endl; 17 | cout << "495 (mod 11) " << 495 % 11 << endl; 18 | 19 | cout << "******************************\n"; 20 | //(b) Find −26 (mod 7), −2345 (mod 6), −371 (mod 8), −39 (mod 3). 21 | cout << "-26 (mod 7) " << ((-26) % 7)+7 << endl; 22 | cout << "-2345 (mod 6) " << ((-2345)%6)+6 << endl; 23 | cout << "-371 (mod 8)" << ((-371)%8) + 8<< endl; 24 | cout << "-39 (mod 3) " << (-39) % 3 << endl; 25 | cout << "*******************************\n"; 26 | //(c) Using arithmetic modulo 15, evaluate 9 + 13, 7 + 11, 4 − 9, 2 − 10. 27 | 28 | cout << "9 + 13 = " << ( 9 + 13 ) << " # " << ( 9 + 13 ) - 15 << endl; 29 | cout << "7 + 11 = " << (7+11) << " # " << (7+11) - 15 << endl; 30 | cout << "4 - 9 = " << (4-9) << " # " << (4-9) + 15 << endl; 31 | cout << "2 - 10 = " << ( 2 - 10) << " # " << (4-9) + 15 << endl; 32 | 33 | 34 | return 0; 35 | } 36 | //main end 37 | ///Program end 38 | 39 | /* 40 | output 41 | 26 (mod 7) 5 42 | 34 (mod 8) 2 43 | 2345 (mod 6)5 44 | 495 (mod 11) 0 45 | ****************************** 46 | -26 (mod 7) 2 47 | -2345 (mod 6) 1 48 | -371 (mod 8)5 49 | -39 (mod 3) 0 50 | ******************************* 51 | 9 + 13 = 22 # 7 52 | 7 + 11 = 18 # 3 53 | 4 - 9 = -5 # 10 54 | 2 - 10 = -8 # 10 55 | */ -------------------------------------------------------------------------------- /Chapter-02/Supplementary Problems/2-18.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 39 3 | Program 4 | Find (a) 3^−4, 4^7/2, 27^2/3; (b) log2 64, log10 0.001, log2 (1/8); (c)floor(log 1000000), floor(log 0.001). 5 | 6 | */ 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | template 12 | T Power(T x, T y) 13 | { 14 | if(y < 0) 15 | { 16 | y = y*(-1); 17 | return (1/pow(x,y)); 18 | } 19 | else 20 | { 21 | return (pow(x,y)); 22 | } 23 | } 24 | template 25 | T Power(T x, T y, T z) 26 | { 27 | T temp; 28 | if(y < 0) 29 | { 30 | temp = (-1*y) / z; 31 | return (1/(pow(x,temp))); 32 | } 33 | else 34 | { 35 | temp = y / z; 36 | return (pow(x,temp)); 37 | } 38 | } 39 | 40 | //main begin 41 | int main() 42 | { 43 | //(a) 3^−4, 4^7/2, 27^2/3; 44 | cout << "3^-4 = " << Power(3.0, -4.0) << endl; 45 | cout << "4^7/2 = " << Power(4,7,2) << endl; 46 | cout << "27^(-2/3) = " << Power(27,-2,3) << endl; 47 | 48 | //(b) log2 64, log10 0.001, log2 (1/8); 49 | 50 | cout << "log2 64 = " <> key; 17 | 18 | string BEG(key + " "), END(" " + key), MID(" "+key+" "); 19 | 20 | for(int i = 0; i < N ; i++) 21 | { 22 | if( WORD[i].substr(0, BEG.length()) == BEG) 23 | { 24 | NUM += 1; 25 | } 26 | if(WORD[i].substr(WORD[i].length()- END.length(), WORD[i].length()) == END) 27 | { 28 | NUM += 1; 29 | } 30 | for(int K = 2; K < WORD[i].length()-3; K++) 31 | { 32 | if(WORD[i].substr(K,MID.length()) == MID) 33 | { 34 | NUM += 1; 35 | } 36 | } 37 | } 38 | 39 | return NUM; 40 | } 41 | 42 | int main() 43 | { 44 | 45 | string S[100]; 46 | int i; 47 | cout << "Enter your string\n"; 48 | for(i ; i < 100; i++) 49 | { 50 | getline(cin,S[i]); 51 | if(S[i].substr(0,5) == "$$$$$") 52 | break; 53 | } 54 | int NUM = COUNT(S,i,0); 55 | cout << "Number of ' the ' = " << NUM << " Times\n"; 56 | return 0; 57 | } 58 | /* 59 | Output 60 | Enter your string 61 | the We do not count the in mother, and we assume no sentence ends with the word the 62 | the We do not count the in mother, and we assume no sentence ends with the word the 63 | the We do not count the in mother, and we assume no sentence ends with the word the 64 | the We do not count the in mother, and we assume no sentence ends with the word the 65 | $$$$$ 66 | Enter your key 67 | we 68 | Number of ' the ' = 4 Times 69 | */ -------------------------------------------------------------------------------- /Chapter-03/3-20.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | page 63 3 | program 3.20 4 | 5 | */ 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | string p; 11 | string Q[100]; 12 | /////////////////////////////////////////////////////////// 13 | string F(string S, string C) 14 | { 15 | string ck = S + C; 16 | int i ; 17 | for(i = 0; i < ck.length(); i++) 18 | { 19 | if(ck.substr(i,ck.length()) == Q[(ck.length() - i)]) 20 | { 21 | //cout << " CK " << (ck.length() - i) << endl; 22 | break; 23 | 24 | } 25 | 26 | } 27 | // cout << " CK " << (ck.length() - i) << endl; 28 | return Q[(ck.length() - i)]; 29 | } 30 | 31 | 32 | ///////////////////////////////////////////////////////////// 33 | void INITIAL() 34 | { 35 | for(int i = 0; i <= p.length(); i++) 36 | { 37 | Q[i] = p.substr(0,i); 38 | // cout << "Q[" << i << "] = " << Q[i] << endl; 39 | } 40 | } 41 | 42 | int main() 43 | { 44 | string T, S[100]; 45 | int i, INDEX = 0; 46 | cout << "Enter your string\n"; 47 | cin >> T ; 48 | cout << "Enter your keyword\n"; 49 | cin >> p; 50 | INITIAL(); 51 | S[0] = Q[0]; 52 | for(i = 0; i < T.length(); i++) 53 | { 54 | 55 | S[i+1] = F(S[i],T.substr(i,1)); 56 | if(S[i+1] == p) 57 | { 58 | INDEX = i - p.length(); 59 | break; 60 | } 61 | } 62 | cout << S[i+1] << "INDEX = " << INDEX << endl; 63 | 64 | return 0; 65 | } 66 | /* 67 | output 68 | 69 | 70 | #First run 71 | 72 | Enter your string 73 | aabbaabbaabbaabb 74 | Enter your keyword 75 | aaa 76 | INDEX = 0 77 | 78 | 79 | 80 | #second run 81 | 82 | Enter your string 83 | abaabbaaabbbaaaabbbb 84 | Enter your keyword 85 | aaa 86 | aaaINDEX = 5 87 | */ -------------------------------------------------------------------------------- /Chapter-06/STACK.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | */ 4 | #include 5 | #include 6 | using namespace std; 7 | class STACK 8 | { 9 | private: 10 | typedef struct _node 11 | { 12 | int DATA; 13 | _node *next; 14 | }STK; 15 | int MAXSTK; 16 | int AVAIL; 17 | STK *START; 18 | 19 | public: 20 | STACK(int N); 21 | ~STACK(); 22 | void PUSH(int DATA); 23 | int POP(); 24 | void DISPLAY(); 25 | }; 26 | STACK::STACK(int N) 27 | { 28 | MAXSTK = 0; 29 | AVAIL = N; 30 | START = nullptr; 31 | } 32 | //INSERT DATA INTO THE STACK 33 | void STACK::PUSH(int DATA) 34 | { 35 | 36 | if(START == nullptr) 37 | { 38 | STK *PTR = new STK; 39 | PTR->DATA = DATA; 40 | PTR->next = nullptr; 41 | START = PTR; 42 | MAXSTK++; 43 | AVAIL--; 44 | } 45 | if(AVAIL <= 0) 46 | { 47 | cout << "OVERFLOW\n"; 48 | } 49 | else 50 | { 51 | STK *PTR = new STK; 52 | PTR->DATA = DATA; 53 | PTR->next = START; 54 | START = PTR; 55 | MAXSTK++; 56 | AVAIL--; 57 | } 58 | } 59 | //POPPING DATA FROM STACK 60 | int STACK::POP() 61 | { 62 | if(START== nullptr) 63 | { 64 | cout << "UNDER FLOW\n"; 65 | return 0; 66 | } 67 | int temp = START->DATA; 68 | START = START->next; 69 | AVAIL++; 70 | MAXSTK--; 71 | return temp; 72 | } 73 | void STACK::DISPLAY() 74 | { 75 | int ITEM = POP(); 76 | while(ITEM) 77 | { 78 | cout << ITEM<< " "; 79 | ITEM = POP(); 80 | } 81 | 82 | 83 | } 84 | STACK::~STACK() 85 | { 86 | } 87 | int main() 88 | { 89 | STACK S(10); 90 | for(int i = 1 ; i < 7; i++) 91 | { 92 | S.PUSH(i); 93 | }; 94 | S.DISPLAY(); 95 | } 96 | -------------------------------------------------------------------------------- /Chapter-03/3-1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Let W be the string ABCD. 3 | (a) Find the length of W. 4 | (b) List all substrings of W. 5 | (c) List all the initial substrings of W. 6 | */ 7 | #include 8 | #include 9 | using namespace std; 10 | int main() 11 | { 12 | string W, R; 13 | cout << "Enter your String \n"; 14 | cin >> W; 15 | //(a) Find the length of W. 16 | cout << "Length of String = " << W.length() << endl; 17 | 18 | int k = W.length(); 19 | //(b) List all substrings of W. 20 | cout << "(b) List all substrings of W.\n"; 21 | for(int i = 0; i <= W.length(); i++,k--) 22 | { 23 | for(int j = 0; j <= i; j++) 24 | { 25 | R = W.substr(j,k); 26 | cout << R << " " ; 27 | } 28 | cout << "\n"; 29 | } 30 | k = W.length(); 31 | //(c) List all the initial substrings of W. 32 | cout << "(c)List all the initial substrings of W\n"; 33 | for(int i = 0; i <= W.length(); i++,k--) 34 | { 35 | for(int j = 0; j < 1; j++) 36 | { 37 | R = W.substr(j,k); 38 | cout << R << " " ; 39 | } 40 | cout << "\n"; 41 | } 42 | // 43 | return 0; 44 | } 45 | /* 46 | Enter your String 47 | Bangladesh 48 | Length of String = 10 49 | (b) List all substrings of W. 50 | Bangladesh 51 | Banglades angladesh 52 | Banglade anglades ngladesh 53 | Banglad anglade nglades gladesh 54 | Bangla anglad nglade glades ladesh 55 | Bangl angla nglad glade lades adesh 56 | Bang angl ngla glad lade ades desh 57 | Ban ang ngl gla lad ade des esh 58 | Ba an ng gl la ad de es sh 59 | B a n g l a d e s h 60 | 61 | (c)List all the initial substrings of W 62 | Bangladesh 63 | Banglades 64 | Banglade 65 | Banglad 66 | Bangla 67 | Bangl 68 | Bang 69 | Ban 70 | Ba 71 | B 72 | */ -------------------------------------------------------------------------------- /Chapter-04/Solved Problems/4-9.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 102 3 | Program 4 | 4.9 Using the bubble sort algorithm, Algorithm 4.4, 5 | find the number C of comparisons and the number D of 6 | interchanges which alphabetize the n = 6 letters in PEOPLE 7 | */ 8 | #include 9 | #include 10 | using namespace std; 11 | /*********************************/ 12 | template 13 | void Bubble_Sort(T DATA[], int N) 14 | { 15 | int K, PTR; 16 | T CHANGE; 17 | N = N - 1; 18 | for(K = 0; K < N; K++) 19 | { 20 | PTR = 0; 21 | while(PTR < (N - K)) 22 | { 23 | if(DATA[PTR] > DATA[PTR+1]) 24 | { 25 | CHANGE = DATA[PTR]; 26 | DATA[PTR] = DATA[PTR+1]; 27 | DATA[PTR+1] = CHANGE; 28 | cout << CHANGE << " "; 29 | } 30 | 31 | PTR++; 32 | } 33 | } 34 | return; 35 | } 36 | 37 | void Bubble(char DATA[], int N) 38 | { 39 | char CHANGE; 40 | N = N - 1; 41 | for(int K = 0; K < N; K++) 42 | { 43 | for(int PTR = 0; PTR < N - K; PTR++) 44 | { 45 | if(DATA[PTR]> DATA[PTR+1]) 46 | { 47 | CHANGE = DATA[PTR]; 48 | DATA[PTR] = DATA[PTR+1]; 49 | DATA[PTR+1] = CHANGE; 50 | } 51 | } 52 | } 53 | return; 54 | } 55 | //Main from Here 56 | int main() 57 | { 58 | char D[7] = {"PEOPLE"}; 59 | for(int i = 0; i < 7; i++) 60 | { 61 | cout << D[i]; 62 | } 63 | cout << "\n"; 64 | Bubble_Sort(D,7); 65 | for(int i = 0; i < 7; i++) 66 | { 67 | cout << D[i] << endl; 68 | } 69 | } 70 | 71 | /* 72 | Output 73 | PEOPLE 74 | P P P P P P P P O O O L L E E 75 | E 76 | E 77 | L 78 | O 79 | P 80 | P 81 | 82 | */ -------------------------------------------------------------------------------- /Chapter-03/3-11.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3.11 Recall that we use S1//S2 to denote the concatenation of strings S1 and S2. 3 | 4 | (a) How is this function denoted in (i) PL/1, (ii) FORTRAN, (iii) BASIC, (iv) SNOBOL and (v) UCSD Pascal? 5 | 6 | (b) Find (i) 'THE' // 'END' and (ii) 'THE' // ''//'END'.s 7 | 8 | (c) Find (i) SUBSTRING(S, 11, 5)//','//SUBSTRING(S, 1, 9) and (ii) SUBSTRING (T, 28, 3)//'GIVEN'. 9 | */ 10 | #include 11 | #include 12 | using namespace std; 13 | 14 | int LENGTH(char const *S) 15 | { 16 | int i = 0; 17 | while(S[i++] != '\0'); 18 | return i; 19 | } 20 | char *SUBSTR(char const *S, int from, int to) 21 | { int i; 22 | char *str = new char[to + 1]; 23 | for( i = 0; i < to && S[i+from] != '\0'; i++) 24 | { 25 | str[i] = S[i+from]; 26 | } 27 | str[i] = '\0'; 28 | return str; 29 | 30 | } 31 | char *STRCAT(char const *S, char const *C, char const *T) 32 | { 33 | int i = LENGTH(S) + LENGTH(T); 34 | char * str = new char[i+1]; 35 | char * str0 = str; 36 | 37 | str = strcat(str,S); 38 | str = strcat(str, C); 39 | str = strcat(str,T); 40 | 41 | return str0; 42 | } 43 | char *STRCAT(char const *S, char const *T) 44 | { 45 | int i = LENGTH(S) + LENGTH(T); 46 | char * str = new char[i+1]; 47 | 48 | str = strcat(str,S); 49 | //str = strcat(str, " "); 50 | str = strcat(str,T); 51 | 52 | return str; 53 | } 54 | int main() 55 | { 56 | char const *S = "JOHN PAUL JONES", *T = "A THING OF BEAUTY IS A JOY FOREVER"; 57 | cout << LENGTH(S) << " " << LENGTH(T) << " " << endl; 58 | 59 | cout << STRCAT("THE"," ", "END") << endl; 60 | cout << STRCAT(SUBSTR(S,10, 5)," ",SUBSTR(S, 0, 9)) << endl; 61 | cout << STRCAT(SUBSTR(T,27,3), "GIVEN") << endl; 62 | } 63 | /* 64 | output 65 | 16 35 66 | THE END 67 | JONES JOHN PAUL 68 | FORGIVE 69 | */ -------------------------------------------------------------------------------- /Chapter-05/Supplementary Problems/5-26.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | PAGE 160 3 | 5.14 Find the polynomials stored in the three header lists in Fig. 5.50. 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | typedef struct _link 12 | { 13 | int COEF; 14 | int XEXP; 15 | _link *next; 16 | _link *prev; 17 | }LINKED; 18 | void INSERT_END(LINKED **START, LINKED PP) 19 | { 20 | if((*START) == NULL) 21 | { 22 | LINKED *NEW = new LINKED; 23 | NEW->COEF = PP.COEF; 24 | NEW->XEXP = PP.XEXP; 25 | 26 | NEW->next = NEW->prev = NEW; 27 | *START = NEW; 28 | } 29 | else 30 | { 31 | LINKED *LAST = (*START)->prev; 32 | LINKED *NEW = new LINKED; 33 | NEW->COEF = PP.COEF; 34 | NEW->XEXP = PP.XEXP; 35 | 36 | NEW->next = (*START); 37 | NEW->prev = LAST; 38 | (*START)->prev = NEW; 39 | LAST->next = NEW; 40 | } 41 | } 42 | void DISPLAY(LINKED *HEAD) 43 | { 44 | LINKED *PTR = HEAD; 45 | while(PTR->next != HEAD) 46 | { 47 | cout << PTR->COEF << " "; 48 | cout << PTR->XEXP << " \n"; 49 | PTR = PTR->next; 50 | } 51 | cout << PTR->COEF << " "; 52 | cout << PTR->XEXP << " \n"; 53 | } 54 | 55 | int main() 56 | { 57 | srand(time(0)); 58 | LINKED *START[3] ={ NULL,NULL,NULL}; 59 | LINKED PP; 60 | cout << "ENTER COEF AND EXP\nENTER -99 to STOP\n"; 61 | cin >> PP.COEF >> PP.XEXP; 62 | for(int i = 0; i < 3; i++) 63 | while (PP.COEF != -99) 64 | { 65 | INSERT_END(&START[i],PP); 66 | cout << "ENTER COEF AND EXP\nENTER -99 to STOP\n"; 67 | cin >> PP.COEF >> PP.XEXP; 68 | } 69 | 70 | for(int i = 0 ; i < 3; i++) 71 | { 72 | DISPLAY(START[i]); 73 | } 74 | } 75 | /* 76 | OUTPUT 77 | 78 | */ -------------------------------------------------------------------------------- /Chapter-03/3-17.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 62 3 | Program 4 | 3.17 Outline an algorithm which will interchange the kth and lth paragraphs in the short story S. 5 | The algorithm reduces to two procedures: 6 | 7 | Procedure A. 8 | Find the values of arrays BEG and END where 9 | LINE[BEG[K]]   and   LINE[END[K]] 10 | contain, respectively, the first and last lines of paragraph K of the story S. 11 | 12 | Procedure B. 13 | Using the values of BEG[K] and END[K] and the values of BEG[L] 14 | and END[L], interchange the block of lines of paragraph K with the block of lines of paragraph L 15 | */ 16 | #include 17 | #include 18 | using namespace std; 19 | void INTERCHANGE(string LINK[], int N, int FirstLine, int secontLine) 20 | { 21 | string S; 22 | S = LINK[FirstLine]; 23 | LINK[FirstLine] = LINK[secontLine]; 24 | LINK[secontLine] = S; 25 | 26 | 27 | } 28 | int main() 29 | { 30 | string S[100]; 31 | int i, FirstLine, SecondLine; 32 | cout << "Enter your string\n"; 33 | for(i ; i < 100; i++) 34 | { 35 | getline(cin,S[i]); 36 | if(S[i].substr(0,5) == "$$$$$") 37 | break; 38 | } 39 | cout << "Enter line number to Interchange\n"; 40 | cout << "First Line " ; cin >> FirstLine ; 41 | cout << "Second Line "; cin >> SecondLine; 42 | INTERCHANGE(S,i,FirstLine+1,SecondLine+1); 43 | for(int k = 0; k < i ; k++) 44 | { 45 | cout << S[k] << endl; 46 | } 47 | return 0; 48 | } 49 | /* 50 | Output 51 | Enter line number to Interchange 52 | First Line 1 53 | Second Line 3 54 | 1 the We do not count the in mother, and we assume no sentence ends with the word the 55 | 2 the We do not count the in mother, and we assume no sentence ends with the word the 56 | 1 57 | 4 the We do not count the in mother, and we assume no sentence ends with the word the 58 | 3 the We do not count the in mother, and we assume no sentence ends with the word the 59 | 3 60 | 61 | */ -------------------------------------------------------------------------------- /Chapter-07/ALGORITHM/Algorithm-7-9.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | page 246 3 | Procedure 7.9: INSHEAP(TREE, N, ITEM) 4 | A heap H with N elements is stored in the array TREE, 5 | and an ITEM of information is given. This procedure inserts 6 | ITEM as a new element of H. PTR gives the location of ITEM 7 | as it rises in the tree, and PAR denotes the location of 8 | the parent of ITEM. 9 | 10 | 1. [Add new node to H and initialize PTR.] 11 | Set N := N + 1 and PTR := N. 12 | 13 | 2. [Find location to insert ITEM.] 14 | Repeat Steps 3 to 6 while PTR < 1. 15 | 16 | 3.   Set PAR := ⌊PTR/2⌋. [Location of parent node.] 17 | 18 | 4.   If ITEM ≤ TREE[PAR], then: 19 |     Set TREE[PTR] := ITEM, and Return. 20 |   [End of If structure.] 21 | 22 | 5.   Set TREE[PTR] := TREE[PAR]. [Moves node down.] 23 | 24 | 6.   Set PTR := PAR. [Updates PTR.] 25 | [End of Step 2 loop.] 26 | 27 | 7. [Assign ITEM as the root of H.] 28 | Set TREE[I] := ITEM. 29 | 30 | 8. Return. 31 | */ 32 | #include 33 | #include 34 | using namespace std; 35 | 36 | void INSHEAP(int TREE[], int &N, int ITEM) 37 | { 38 | N = N + 1; 39 | int PTR = N, PAR, temp; 40 | while(PTR > 0) 41 | { 42 | PAR = floor(PTR/2); 43 | if(ITEM <= TREE[PAR]) 44 | { 45 | TREE[PTR] = ITEM; 46 | return; 47 | } 48 | temp = TREE[PTR]; 49 | TREE[PTR] = TREE[PAR]; 50 | TREE[PAR] = temp; 51 | PTR = PAR; 52 | } 53 | TREE[0] = ITEM; 54 | return; 55 | } 56 | int main() 57 | { 58 | int TREE[50]; 59 | int N = -1, temp; 60 | for(int i = 0; i < 11; i++) 61 | { 62 | cin >> temp; 63 | INSHEAP(TREE, N, temp); 64 | } 65 | for(int i = 0; i <= N; i++) 66 | { 67 | cout << TREE[i] << " " ; 68 | } 69 | } 70 | /* 71 | 95 72 | 85 73 | 70 74 | 55 75 | 33 76 | 30 77 | 65 78 | 15 79 | 20 80 | 15 81 | 22 82 | 95 85 70 65 33 30 55 15 20 15 22 83 | */ -------------------------------------------------------------------------------- /Chapter-08/SOLVED PROBLEMS/8-9.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define INF 99999 6 | int MIN(int A, int B) 7 | { 8 | return (A>B) ? B : A; 9 | } 10 | void SORTEST(int **W, int N) 11 | { 12 | int Q[N][N]; 13 | for(int i = 0; i < N; i++) 14 | { 15 | for(int j = 0; j < N; j++) 16 | { 17 | cout << W[i][j] << " "; 18 | if(W[i][j] == 0) 19 | Q[i][j] = INF; 20 | else 21 | Q[i][j] = W[i][j]; 22 | } 23 | cout << "\n"; 24 | } 25 | for(int k = 0; k < N; k++) 26 | { 27 | for(int i = 0; i < N; i++) 28 | { 29 | for(int j = 0; j < N; j++) 30 | { 31 | Q[i][j] = MIN(Q[i][j], Q[i][k] + Q[k][j]); 32 | } 33 | } 34 | } 35 | cout << "\n"; 36 | for(int i = 0; i < N; i++) 37 | { 38 | for(int j = 0; j < N; j++) 39 | { 40 | cout<< setw(3) << Q[i][j] << " "; 41 | } 42 | cout << "\n"; 43 | } 44 | } 45 | int main() 46 | { 47 | int M, N; 48 | cout << "Matrix col number\n"; 49 | cin >> M; 50 | N = M; 51 | int **A = new int *[M]; 52 | for (int i = 0; i < M; i++) 53 | { 54 | A[i] = new int[N]; 55 | } 56 | for (int i = 0; i < M; i++) 57 | { 58 | for (int j = 0; j < N; j++) 59 | { 60 | cin >> A[i][j]; 61 | } 62 | } 63 | cout << "\n*********************\n"; 64 | SORTEST(A, M); 65 | return 0; 66 | } 67 | /* 68 | Matrix col number 69 | 5 70 | 0 6 0 4 1 71 | 6 0 5 0 8 72 | 0 5 0 0 2 73 | 4 0 0 0 3 74 | 1 8 2 3 0 75 | 76 | ********************* 77 | 0 6 0 4 1 78 | 6 0 5 0 8 79 | 0 5 0 0 2 80 | 4 0 0 0 3 81 | 1 8 2 3 0 82 | 83 | 2 6 3 4 1 84 | 6 10 5 10 7 85 | 3 5 4 5 2 86 | 4 10 5 6 3 87 | 1 7 2 3 2 88 | */ -------------------------------------------------------------------------------- /Chapter-03/3-28.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 65 3 | Program 3.28 4 | Find (a) INDEX(S, 'P'), 5 | (b) INDEX(S, 'E'), 6 | (c) INDEX(S, 'THE'), 7 | (d) INDEX(T, 'THE'), 8 | (e) INDEX(T, 'THEN') and 9 | (f) INDEX(T, 'TE'). 10 | */ 11 | #include 12 | #include 13 | using namespace std; 14 | string Q[100]; 15 | string F(string S, string C) 16 | { 17 | string ck = S + C; 18 | int i ; 19 | for(i = 0; i < ck.length(); i++) 20 | { 21 | if(ck.substr(i,ck.length()) == Q[(ck.length() - i)]) 22 | { 23 | // cout << " CK " << (ck.length() - i) << endl; 24 | break; 25 | 26 | } 27 | 28 | } 29 | // cout << " CK " << (ck.length() - i) << endl; 30 | return Q[(ck.length() - i)]; 31 | } 32 | void INITIAL(string p) 33 | { 34 | for(int i = 0; i <= p.length(); i++) 35 | { 36 | Q[i] = p.substr(0,i); 37 | // cout << "Q[" << i << "] = " << Q[i] << endl; 38 | } 39 | } 40 | 41 | int IND(string T, string p) 42 | { 43 | string S[100]; 44 | int i, INDEX = 0; 45 | INITIAL(p); 46 | S[0] = Q[0]; 47 | for(i = 0; i < T.length(); i++) 48 | { 49 | 50 | S[i+1] = F(S[i],T.substr(i,1)); 51 | if(S[i+1] == p) 52 | { 53 | INDEX = i - p.length(); 54 | break; 55 | } 56 | } 57 | cout << S[i+1] << " INDEX = " << INDEX + 1<< endl; 58 | 59 | return INDEX + 1; 60 | } 61 | int main() 62 | { 63 | string S("WE THE PEOPLE"); 64 | string T("OF THE UNITED STATES"); 65 | cout << IND(S,"P") << endl; 66 | cout << IND(S,"E") << endl; 67 | cout << IND(S,"THE") << endl; 68 | cout << IND(T,"THE") << endl; 69 | cout << IND(T,"THEN") << endl; 70 | cout << IND(S,"TH") << endl; 71 | return 0; 72 | } 73 | /* 74 | Output 75 | P INDEX = 7 76 | 7 77 | E INDEX = 1 78 | 1 79 | THE INDEX = 3 80 | 3 81 | THE INDEX = 3 82 | 3 83 | INDEX = 1 84 | 1 85 | TH INDEX = 3 86 | 3 87 | */ -------------------------------------------------------------------------------- /Chapter-08/SUPPLEMENTARY PROBLEMS/8-26.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 8.6 Consider the graph G in Fig. 8.37. Suppose the nodes are stored in 3 | memory in an array DATA as follows: 4 | DATA: X, Y, Z, S, T 5 | (a) Find the adjacency matrix A of G. 6 | (b) Find the path matrix P or G. 7 | (c) Is G strongly connected? 8 | 9 | Fig. 8.37 10 | */ 11 | #include 12 | #include 13 | using namespace std; 14 | void PRINT(list A, list X[]) 15 | { 16 | list::iterator ptr = A.begin(); 17 | cout << "\n==========GRAPH=============\n"; 18 | while (ptr != A.end()) 19 | { 20 | list::iterator ptr1 = X[*ptr].begin(); 21 | cout << *ptr << " -> "; 22 | while (ptr1 != X[*ptr].end()) 23 | { 24 | cout << *ptr1++ << " "; 25 | } 26 | cout << "\n"; 27 | ptr++; 28 | } 29 | 30 | cout << "=======================\n"; 31 | } 32 | int main() 33 | { 34 | list A; 35 | list X[2000]; 36 | int EDGE, NODE, SRC, DEST; 37 | cout << "Enter Nodes and EDGE\n"; 38 | cin >> NODE >> EDGE; 39 | 40 | int M, N; 41 | M = N = NODE; 42 | int **MATRIX = new int *[M]; 43 | for (int i = 0; i < M; i++) 44 | { 45 | MATRIX[i] = new int[N]; 46 | } 47 | for (int i = 0; i < M; i++) 48 | { 49 | for (int j = 0; j < N; j++) 50 | { 51 | MATRIX[i][j] = 0; 52 | } 53 | } 54 | 55 | for (int i = 0; i < EDGE; i++) 56 | { 57 | cin >> SRC >> DEST; 58 | X[SRC].push_back(DEST); 59 | MATRIX[SRC][DEST]; 60 | } 61 | for (int i = 0; i < NODE; i++) 62 | { 63 | A.push_back(i); 64 | } 65 | for (int i = 0; i < M; i++) 66 | { 67 | for (int j = 0; j < N; j++) 68 | { 69 | cout << MATRIX[i][j] << " "; 70 | } 71 | cout << "\n"; 72 | } 73 | 74 | cout << "\n*********************\n"; 75 | return 0; 76 | } -------------------------------------------------------------------------------- /Chapter-02/Programming Problems/2-25.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 40 3 | Program 4 | (a) Write a procedure FIND(DATA, N, LOC1, LOC2) which finds the location LOC1 5 | of the largest element and the location LOC2 of the second largest element in 6 | an array DATA with n > 1 elements. 7 | 8 | (b) Why not let FIND also find the values of the largest and second largest elements? 9 | 10 | 11 | Translate Procedure P2.8 into a computer program; i.e., write a program which finds 12 | the location LOC1 of the largest element and the location LOC2 of the second largest 13 | element in an array DATA with N > 1 elements. Test the program using 70, 30, 25, 80, 14 | 60, 50, 30, 75, 25, and 60. 15 | */ 16 | #include 17 | using namespace std; 18 | 19 | 20 | template 21 | void FIND(T DATA[], int N, int &LOC1, int &LOC2) 22 | { 23 | T FIRST = DATA[1]; 24 | T SECOND = DATA[2]; 25 | if (FIRST < SECOND) 26 | { 27 | swap(FIRST, SECOND); 28 | LOC1 = 2; 29 | LOC2 = 1; 30 | } 31 | for (int i = 0; i < N; i++) 32 | { 33 | if (FIRST < DATA[i]) 34 | { 35 | SECOND = FIRST; 36 | FIRST = DATA[i]; 37 | LOC2 = LOC1; 38 | LOC1 = i; 39 | } 40 | else 41 | if(SECOND < DATA[i]) 42 | { 43 | SECOND = DATA[i]; 44 | LOC2 = i; 45 | } 46 | 47 | } 48 | } 49 | //main begin 50 | int main() 51 | { 52 | int LOC1, LOC2; 53 | int DATA[] = {70, 30, 25, 80, 60, 50, 30, 75, 25,60}; 54 | FIND (DATA,(sizeof(DATA)/sizeof(int)),LOC1,LOC2); 55 | cout << "LOC1 = " << LOC1 << " LOC2 = " << LOC2 << endl; 56 | cout << "Leargest number is " << DATA[LOC1] << "\nSecond leargest is " << DATA[LOC2] << endl; 57 | 58 | getchar(); 59 | return 0; 60 | } 61 | //main end 62 | ///Program end 63 | 64 | /* 65 | output 66 | LOC1 = 3 LOC2 = 7 67 | Leargest number is 80 68 | Second leargest is 75 69 | */ -------------------------------------------------------------------------------- /Chapter-06/ALGORITHM/Algorithm-6-15.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | page 196 3 | Algorithm 6.19: This algorithm deletes and processes the first element 4 | in a priority queue maintained by a two-dimensional array QUEUE. 5 | 6 | 1. [Find the first nonempty queue.] 7 | Find the smallest K such that FRONT[K] ≠ NULL. 8 | 9 | 2. Delete and process the front element in row K of QUEUE. 10 | 11 | 3. Exit. 12 | */ 13 | #include 14 | #include 15 | using namespace std; 16 | void INSPQA(int A[][6],int FRONT[], int REAR[], int ITEM, int M) 17 | { 18 | M = M -1 ; 19 | if(FRONT[M] == 0 && REAR[M] == 5) 20 | { 21 | cout << "OVERFLOW\n"; 22 | return; 23 | } 24 | if(FRONT[M] == -1) 25 | { 26 | FRONT[M] = 0; 27 | REAR[M] = 0; 28 | } 29 | else 30 | if(REAR[M] == 5) 31 | { 32 | REAR = 0; 33 | } 34 | else 35 | REAR[M] = REAR[M] + 1; 36 | A[M][REAR[M]] = ITEM; 37 | return; 38 | } 39 | int DELPQA(int A[][6],int FRONT[], int REAR[], int &ITEM, int M) 40 | { 41 | int i = 0; 42 | while((FRONT[i]==-1) && (i < M)) 43 | { 44 | if((i == M-1) && FRONT[i] == -1) 45 | { 46 | cout << "UNDERFLOW\n"; 47 | return 0; 48 | } 49 | 50 | i = i + 1; 51 | } 52 | ITEM = A[i][FRONT[i]]; 53 | if(FRONT[i] == REAR[i]) 54 | { 55 | FRONT[i] = -1; 56 | REAR[i] = -1; 57 | } 58 | else 59 | if(FRONT[i] == 5) 60 | { 61 | FRONT[i] = 0; 62 | } 63 | else 64 | FRONT[i] = FRONT[i]+1; 65 | return 1; 66 | } 67 | int main() 68 | { 69 | srand(time(0)); 70 | int x = 5, y = 6, FRONT[x], REAR[x],M, ITEM; 71 | int PQUE[x][6]; 72 | for(int i = 0 ; i < x; i++) 73 | { 74 | FRONT[i] = -1; 75 | REAR[i] = -1; 76 | } 77 | 78 | ITEM = 100, M = 1; 79 | INSPQA(PQUE,FRONT, REAR, ITEM, M); 80 | if(DELPQA(PQUE,FRONT, REAR, ITEM, M)) 81 | cout << ITEM << endl; 82 | } -------------------------------------------------------------------------------- /Chapter-06/SUPPLEMENTARY PROBLEMS/6-31.CPP: -------------------------------------------------------------------------------- 1 | /* 2 | Algorithm 6.6: POLISH(Q, P) 3 | 4 | 6.6 Translate, by inspection and hand, each infix expression into its equivalent postfix expression: 5 | 6 | (a) (A – B)/((D + E) * F) 7 | 8 | (b) ((A + B)/D) ↑ ((E – F) * G) 9 | */ 10 | #include 11 | #include 12 | #include 13 | using namespace std; 14 | 15 | // Function to return perecendece of operators 16 | int PRECENDCE(char c) 17 | { 18 | if (c == '^') 19 | return 3; 20 | else if (c == '/' || c == '*') 21 | return 2; 22 | else if (c == '+' || c == '-') 23 | return 1; 24 | else 25 | return -1; 26 | } 27 | string infixToPostFix(string s) 28 | { 29 | stack STACK; 30 | STACK.push('('); 31 | s.push_back(')'); 32 | string P; 33 | for(int i = 0 ; i < s.length(); i++) 34 | { 35 | char c = s[i]; 36 | if(c == ' ')continue; 37 | if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) 38 | P.push_back(c); 39 | 40 | else 41 | if(c == '(') 42 | STACK.push('('); 43 | 44 | else 45 | if(c == ')') 46 | { 47 | while(STACK.top() != '(') 48 | { 49 | P.push_back(STACK.top()); 50 | STACK.pop(); 51 | } 52 | STACK.pop(); 53 | } 54 | else 55 | { 56 | while (!STACK.empty() && PRECENDCE(s[i]) <= PRECENDCE(STACK.top())) 57 | { 58 | P.push_back(STACK.top()); 59 | STACK.pop(); 60 | } 61 | STACK.push(c); 62 | } 63 | } 64 | return P; 65 | } 66 | int main() 67 | { 68 | string exp = "(A - B)/((D + E) * F)"; 69 | cout << infixToPostFix(exp) << endl; 70 | exp = "((A + B)/D) ^ ((E - F) * G)"; 71 | cout << infixToPostFix(exp) << endl; 72 | } 73 | /* 74 | AB-DE+F* / 75 | AB+D/EF-G*^ 76 | */ -------------------------------------------------------------------------------- /Chapter-07/Programming Problems/7-43.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | page 275 3 | 7.43 Write a program which prints the nodes of T in (a) preorder, (b) inorder and (c) postorder. 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | typedef struct _tree 11 | { 12 | int data; 13 | _tree *left; 14 | _tree *right; 15 | } TREE; 16 | 17 | TREE *insert(TREE *root, int data) 18 | { 19 | if (root == NULL) 20 | { 21 | TREE *ptr = new TREE; 22 | ptr->data = data; 23 | ptr->left = NULL; 24 | ptr->right = NULL; 25 | root = ptr; 26 | } 27 | else if (root->data > data) 28 | { 29 | root->right = insert(root->right, data); 30 | } 31 | else 32 | root->left = insert(root->left, data); 33 | return root; 34 | } 35 | void PreOrder(TREE *root) 36 | { 37 | if(root != NULL) 38 | { 39 | cout << root->data << " "; 40 | PreOrder(root->left); 41 | PreOrder(root->right); 42 | } 43 | } 44 | void inorder(TREE *root) 45 | { 46 | 47 | if(root != NULL) 48 | { 49 | inorder(root->left); 50 | cout << root->data << " "; 51 | inorder(root->right); 52 | } 53 | 54 | } 55 | void postorder(TREE *root) 56 | { 57 | 58 | if(root != NULL) 59 | { 60 | postorder(root->left); 61 | postorder(root->right); 62 | cout << root->data << " "; 63 | } 64 | 65 | } 66 | int main() 67 | { 68 | int AVAIL = 9; 69 | int temp = 0; 70 | TREE *root = NULL; 71 | for(int i = 0; i < AVAIL; i++) 72 | { 73 | cin >> temp; 74 | root = insert(root, temp); 75 | } 76 | inorder(root); 77 | cout << "\n"; 78 | PreOrder(root); 79 | cout << "\n"; 80 | postorder(root); 81 | } 82 | /* 83 | 60 84 | 30 85 | 70 86 | 20 87 | 55 88 | 35 89 | 45 90 | 40 91 | 50 92 | 70 60 55 50 45 40 35 30 20 93 | 60 70 30 55 35 45 50 40 20 94 | 70 50 40 45 35 55 20 30 60 95 | */ -------------------------------------------------------------------------------- /Chapter-04/MISCELLANEOUS/4-45.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 112 3 | Program 4 | 4.14 Suppose A and B are n-element vector arrays in memory and X and Y are scalars. Write a program to find 5 | (a) XA + YB and 6 | (b) A · B 7 | Test the program using A = (16, −6, 7), B = (4, 2, −3), X = 2 and Y= −5. 8 | */ 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | //Main from Here 14 | int main() 15 | { 16 | int AR = 2 , AC = 2 , BR = 2 , BC = 3, X, Y; 17 | cout << "Enter A's row number\n"; 18 | cin >> AR; 19 | cout << "Enter A's column number\n"; 20 | cin >> AC; 21 | cout << "Enter B's row number\n"; 22 | cin >> BR; 23 | cout << "Enter B's column number\n"; 24 | cin >> BC; 25 | 26 | 27 | int A[AR][AC]; 28 | int B[BR][BC]; 29 | int C[AR][BC]; 30 | if(AC != BR) 31 | { 32 | cout << "MATRIX MULTIPLICATION NOT POSSIBLE"; 33 | } 34 | else 35 | { 36 | cout << "Enter Value of A MATRIX " << AR << " x " << AC << endl; 37 | for(int i = 0; i < AR; i++) 38 | { 39 | for(int j = 0; j < AC; j++) 40 | cin >> A[i][j]; 41 | } 42 | 43 | cout << "Enter Value of A MATRIX " << BR << " x " << BC << endl; 44 | for(int i = 0; i < BR; i++) 45 | { 46 | for(int j = 0; j < BC; j++) 47 | cin >> B[i][j]; 48 | } 49 | 50 | for(int i = 0; i < AR; i++) 51 | { 52 | for(int j = 0; j < BC; j++) 53 | { 54 | C[i][j] = 0; 55 | for(int k = 0; k < BR; k++) 56 | { 57 | C[i][j] += A[i][k] * B[k][j]; 58 | } 59 | } 60 | } 61 | cout << "\n\n\n"; 62 | for(int i = 0; i < AR; i++) 63 | { 64 | for(int j = 0; j < BC; j++) 65 | cout << setw(5) << C[i][j] << " "; 66 | cout << endl; 67 | } 68 | 69 | } 70 | 71 | } 72 | 73 | /* 74 | Output 75 | 76 | */ -------------------------------------------------------------------------------- /Chapter-06/SUPPLEMENTARY PROBLEMS/6-37.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | page 211 3 | 6.12 Use Algorithm 6.6 to translate each infix expression into its equivalent postfix expression: 4 | 5 | (a) (A – B)/((D + E) * F)  6 | 7 | (b) ((A + B)/D) ↑ ((E – F) * G) 8 | 9 | (Compare with Supplementary Problem 6.31) 10 | */ 11 | #include 12 | #include 13 | #include 14 | using namespace std; 15 | 16 | // Function to return perecendece of operators 17 | int PRECENDCE(char c) 18 | { 19 | if (c == '^') 20 | return 3; 21 | else if (c == '/' || c == '*') 22 | return 2; 23 | else if (c == '+' || c == '-') 24 | return 1; 25 | else 26 | return -1; 27 | } 28 | string infixToPostFix(string s) 29 | { 30 | stack STACK; 31 | STACK.push('('); 32 | s.push_back(')'); 33 | string P; 34 | for(int i = 0 ; i < s.length(); i++) 35 | { 36 | char c = s[i]; 37 | if(c == ' ')continue; 38 | if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) 39 | P.push_back(c); 40 | 41 | else 42 | if(c == '(') 43 | STACK.push('('); 44 | 45 | else 46 | if(c == ')') 47 | { 48 | while(STACK.top() != '(') 49 | { 50 | P.push_back(STACK.top()); 51 | STACK.pop(); 52 | } 53 | STACK.pop(); 54 | } 55 | else 56 | { 57 | while (!STACK.empty() && PRECENDCE(s[i]) <= PRECENDCE(STACK.top())) 58 | { 59 | P.push_back(STACK.top()); 60 | STACK.pop(); 61 | } 62 | STACK.push(c); 63 | } 64 | } 65 | return P; 66 | } 67 | int main() 68 | { 69 | string exp = "(A - B)/((D + E) * F)"; 70 | cout << infixToPostFix(exp) << endl; 71 | exp = "((A + B)/D) ^ ((E - F) * G)"; 72 | cout << infixToPostFix(exp) << endl; 73 | } 74 | /* 75 | AB-DE+F* / 76 | AB+D/EF-G*^ 77 | */ -------------------------------------------------------------------------------- /Chapter-06/EXAMPLE/6-2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | */ 4 | #include 5 | #include 6 | using namespace std; 7 | class STACK 8 | { 9 | private: 10 | typedef struct _node 11 | { 12 | string DATA; 13 | _node *next; 14 | }STK; 15 | int MAXSTK; 16 | int AVAIL; 17 | STK *START; 18 | 19 | public: 20 | STACK(int N); 21 | ~STACK(); 22 | void PUSH(string DATA); 23 | string POP(); 24 | void DISPLAY(); 25 | }; 26 | STACK::STACK(int N) 27 | { 28 | MAXSTK = 0; 29 | AVAIL = N; 30 | START = nullptr; 31 | } 32 | //INSERT DATA INTO THE STACK 33 | void STACK::PUSH(string DATA) 34 | { 35 | if(AVAIL <= 0) 36 | { 37 | cout << "OVERFLOW\n"; 38 | } 39 | if(START == nullptr) 40 | { 41 | STK *PTR = new STK; 42 | PTR->DATA = DATA; 43 | PTR->next = nullptr; 44 | START = PTR; 45 | MAXSTK++; 46 | AVAIL--; 47 | } 48 | else 49 | { 50 | STK *PTR = new STK; 51 | PTR->DATA = DATA; 52 | PTR->next = START; 53 | START = PTR; 54 | MAXSTK++; 55 | AVAIL--; 56 | } 57 | } 58 | //POPPING DATA FROM STACK 59 | string STACK::POP() 60 | { 61 | if(START== nullptr) 62 | { 63 | cout << "UNDER FLOW\n"; 64 | return "NULL"; 65 | } 66 | string temp = START->DATA; 67 | START = START->next; 68 | AVAIL++; 69 | MAXSTK--; 70 | return temp; 71 | } 72 | void STACK::DISPLAY() 73 | { 74 | string ITEM = POP(); 75 | int CK = 1; 76 | while(ITEM != "NULL") 77 | { 78 | cout <> temp; 94 | S.PUSH(temp); 95 | 96 | }; 97 | S.DISPLAY(); 98 | } 99 | -------------------------------------------------------------------------------- /Chapter-07/ALGORITHM/Algorithm-7-3.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | page 229 3 | Algorithm 7.3: POSTORD(INFO, LEFT, RIGHT, ROOT) 4 | 5 | A binary tree T is in memory. This algorithm does a postorder traversal of T, applying an operation PROCESS to each of its nodes. An array STACK is used to temporarily hold the addresses of nodes. 6 | 7 | 1. [Push NULL onto STACK and initialize PTR.] 8 | Set TOP := 1, STACK[1] := NULL and PTR := ROOT. 9 | 10 | 2. [Push left-most path onto STACK.] 11 | Repeat Steps 3 to 5 while PTR ≠ NULL: 12 | 13 | 3.    Set TOP := TOP + 1 and STACK[TOP] := PTR. 14 |    [Pushes PTR on STACK.] 15 | 16 | 4.    If RIGHT[PTR] ≠ NULL, then: [Push on STACK.] 17 |    Set TOP := TOP + 1 and STACK[TOP] := –RIGHT[PTR]. 18 |    [End of If structure.] 19 | 20 | 5.    Set PTR := LEFT[PTR]. [Updates pointer PTR.] 21 |    [End of Step 2 loop.] 22 | 23 | 6.    Set PTR := STACK[TOP] and TOP := TOP – 1. 24 |    [Pops node from STACK,] 25 | 26 | 7. Repeat while PTR > 0: 27 | 28 |   (a)   Apply PROCESS to INFO[PTR]. 29 | 30 |   (b)   Set PTR := STACK[TOP] and TOP := TOP – 1. 31 |    [Pops node from STACK.] 32 |    [End of loop.] 33 | 34 | 8. If PTR < 0, then: 35 | 36 |   (a)   Set PTR := –PTR. 37 | 38 |   (b)   Go to Step 2. 39 |    [End of If structure.] 40 | 41 | 9. Exit. 42 | */ 43 | template 44 | void POSTORD(T INFO[], int LEFT[], int RIGHT[], int ROOT, int N) 45 | { 46 | int STACK[N] , PTR, TOP ; 47 | STACK[0] = -1; 48 | TOP = 0; 49 | PTR = ROOT; 50 | STEP2: 51 | while(PTR != -1) 52 | { 53 | TOP = TOP + 1; 54 | STACK[TOP] = PTR; 55 | if(RIGHT[PTR] != -1) 56 | { 57 | TOP = TOP + 1; 58 | STACK[TOP] = - RIGHT[PTR]; 59 | } 60 | PTR = LEFT[PTR]; 61 | } 62 | PTR = STACK[TOP]; 63 | TOP = TOP - 1; 64 | while(PTR > 0) 65 | { 66 | cout << INFO[PTR] << " "; 67 | PTR = STACK[TOP]; 68 | TOP = TOP - 1; 69 | } 70 | if(PTR < 0) 71 | { 72 | PTR = -PTR; 73 | goto STEP2; 74 | } 75 | } -------------------------------------------------------------------------------- /Chapter-08/ALGORITHM/Algorithm-8-1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Algorithm 8.1 : (Warshall’s Algorithm) A directed graph G with M 3 | nodes is maintained in memory by its adjacency matrix A. This 4 | algorithm finds the (Boolean) path matrix P of the graph G. 5 | 6 | 7 | 1. Repeat for I, J = 1, 2,…, M: [Initializes P.] 8 | 9 | If A[I, J] = 0, then: Set P[I, J] := 0; 10 | 11 | Else: Set P[I, J] := 1. 12 | 13 | [End of loop.] 14 | 15 | 2. Repeat Steps 3 and 4 for K = 1, 2, …, M: [Updates P.] 16 | 17 | 3.     Repeat Step 4 for I = 1, 2, …, M: 18 | 19 | 4.          Repeat for J = 1, 2, …, M: 20 | 21 | Set P[I, J] : = P[I, J] (P[I, K] P[K, J]). 22 | 23 | [End of loop.] 24 | 25 | [End of Step 3 loop.] 26 | 27 | [End of Step 2 loop.] 28 | 29 | 5. Exit. 30 | */ 31 | 32 | #include 33 | #include 34 | #include 35 | using namespace std; 36 | #define INF 99999 37 | int MIN(int A, int B) 38 | { 39 | return (A>B) ? B : A; 40 | } 41 | void SORTEST(int A[][4], int N) 42 | { 43 | int P[N][N]; 44 | for(int i = 0; i < N; i++) 45 | { 46 | for(int j = 0; j < N; j++) 47 | { 48 | if(A[i][j] == 0) 49 | P[i][j] = 0; 50 | else 51 | P[i][j] = 1; 52 | } 53 | } 54 | for(int k = 0; k < N; k++) 55 | { 56 | for(int i = 0; i < N; i++) 57 | { 58 | for(int j = 0; j < N; j++) 59 | { 60 | P[i][j] = P[i][j] || (P[i][k] && P[k][j]); 61 | } 62 | } 63 | } 64 | cout << "\n"; 65 | for(int i = 0; i < N; i++) 66 | { 67 | for(int j = 0; j < N; j++) 68 | { 69 | cout << P[i][j] << " "; 70 | } 71 | cout << "\n"; 72 | } 73 | } 74 | int main() 75 | { 76 | int A[4][4] = { {7,5,0,0}, 77 | {7,0,0,2}, 78 | {0,3,0,0}, 79 | {4,0,1,0}}; 80 | SORTEST(A, 4); 81 | 82 | } 83 | 84 | /* 85 | output 86 | 87 | 1 1 1 1 88 | 1 1 1 1 89 | 1 1 1 1 90 | 1 1 1 1 91 | */ -------------------------------------------------------------------------------- /Chapter 01/Example/Example01-03.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Program Example 1.3 3 | A chain of 28 stores,each store having 4 departments, may list its 4 | weekly sales(to the nearest doller) as fig 1.2. Such data can be stored 5 | in the computer using two-dimensional array in which the first subscript 6 | denotes the store and the second subscript the department. If SALED is the 7 | name given to array then 8 | SALES[1, 1] = 2872, SALES[1, 2] = 805, SALES[1, 3] = 3211, …, 9 | SALES[28, 4] = 982 10 | 11 | */ 12 | #include 13 | using namespace std; 14 | //main begin 15 | int main() 16 | { 17 | int count = 5; 18 | int col = 4; 19 | int sales[count][col]; 20 | cout << "Enter stores sales" << endl; 21 | 22 | for (size_t i = 0; i < count; i++) 23 | { 24 | for (size_t j = 0; j < col; j++) 25 | { 26 | cin >> sales[count][col]; 27 | } 28 | cout << "Enter stores sales" << endl; 29 | } 30 | 31 | cout << "St/De\t"; 32 | for (size_t i = 1; i < count; i++) 33 | { 34 | cout << i << "\t"; 35 | } 36 | cout << "\n"; 37 | 38 | for (size_t i = 0; i < count; i++) 39 | { 40 | cout << i << "\t"; 41 | for (size_t j = 0; j < col; j++) 42 | { 43 | cout<< sales[count][col] << "\t"; 44 | } 45 | cout << "\n"; 46 | } 47 | getchar(); 48 | return 0; 49 | } 50 | //main end 51 | ///Program end 52 | /* 53 | output 54 | Enter stores sales 55 | 2872 56 | 805 57 | 3211 58 | 1560 59 | Enter stores sales 60 | 2196 61 | 1223 62 | 2525 63 | 1744 64 | Enter stores sales 65 | 3257 66 | 1017 67 | 3686 68 | 1951 69 | Enter stores sales 70 | 2618 71 | 931 72 | 2333 73 | 982 74 | Enter stores sales 75 | 154 76 | 5698 77 | 4563 78 | 2456 79 | Enter stores sales 80 | St/De 1 2 3 4 81 | 0 2456 2456 2456 2456 82 | 1 2456 2456 2456 2456 83 | 2 2456 2456 2456 2456 84 | 3 2456 2456 2456 2456 85 | 4 2456 2456 2456 2456 86 | */ 87 | -------------------------------------------------------------------------------- /Chapter-07/Supplemenary Problems/7-19.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 269 3 | 7.1  Consider the tree T in Fig. 7.98(a). 4 | 5 | 6 | 7 | Fig. 7.19 8 | 9 | (a) Fill in the values for ROOT, LEFT and RIGHT in Fig. 7.98(b) so that T will be stored in memory. 10 | 11 | (b) Find (i) the depth D of T, (ii) the number of null subtrees and (iii) the descendants of node B. 12 | */ 13 | #include 14 | #include 15 | #include 16 | using namespace std; 17 | typedef struct _tree 18 | { 19 | int data; 20 | _tree *left; 21 | _tree *right; 22 | } TREE; 23 | 24 | TREE *insert(TREE *root, int data) 25 | { 26 | if (root == NULL) 27 | { 28 | TREE *ptr = new TREE; 29 | ptr->data = data; 30 | ptr->left = NULL; 31 | ptr->right = NULL; 32 | root = ptr; 33 | } 34 | else if (root->data > data) 35 | { 36 | root->right = insert(root->right, data); 37 | } 38 | else 39 | root->left = insert(root->left, data); 40 | return root; 41 | } 42 | void inorder(TREE *root) 43 | { 44 | 45 | if(root != NULL) 46 | { 47 | inorder(root->left); 48 | cout << root->data << " "; 49 | inorder(root->right); 50 | } 51 | 52 | } 53 | int MAX(int i, int j) 54 | { 55 | return (i>j)? i:j; 56 | } 57 | int Find_Depth(TREE *root) 58 | { 59 | if(root == NULL) 60 | { 61 | return -1; 62 | } 63 | int l = Find_Depth(root->left); 64 | int r = Find_Depth(root->right); 65 | return MAX(l, r) + 1; 66 | } 67 | void COUNT(TREE *root, int &NUM) 68 | { 69 | 70 | if(root == NULL) 71 | { 72 | return; 73 | } 74 | else 75 | { 76 | NUM = NUM + 1; 77 | COUNT(root->left,NUM); 78 | COUNT(root->right,NUM); 79 | } 80 | 81 | } 82 | int main() 83 | { 84 | int AVAIL = 11, temp; 85 | TREE *root = NULL; 86 | for(int i = 0; i < AVAIL; i++) 87 | { 88 | cin >> temp; 89 | root = insert(root, temp); 90 | } 91 | inorder(root); 92 | 93 | } 94 | /* 95 | */ -------------------------------------------------------------------------------- /Chapter-02/Chapter Algorithm/Algorithm2-2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Program 3 | The solutions of the quadratic equation 4 | ax^2 + bx + c = 0 5 | where a ≠ 0, are given by the quadratic formula 6 | The quantity D = b2 – 4ac is called the discriminant 7 | of the equation. If D is negative, then there are no 8 | real solutions. If D = 0, then there is only one 9 | (double) real solution, x = – b/2a. If D is positive, 10 | the formula gives the two distinct real solutions. The 11 | following algorithm finds the solutions of a quadratic 12 | equation. 13 | */ 14 | //Algorithm 15 | /* 16 | Algorithm 2.2: (Quadratic Equation) This algorithm inputs 17 | the coefficients A, B, C of a quadratic equation and outputs 18 | the real solutions, if any. 19 | 20 | Step 1. Read: A, B, C. 21 | Step 2. Set D: = B2 – 4AC. 22 | Step 3. If D > 0, then: 23 | (a) Set X1 := (–B + )/2A and 24 |  X2 := (–B – )/2A. 25 | (b) Write: X1, X2. 26 | Else if D = 0, then: 27 | (a) Set X := –B/2A. 28 | (b) Write: ‘UNIQUE SOLUTION’, X. 29 | Else: 30 | Write: ‘NO REAL SOLUTIONS’ 31 | [End of If structure.] 32 | Step 4. Exit. 33 | */ 34 | #include 35 | #include 36 | using namespace std; 37 | 38 | //main begin 39 | int main() 40 | { 41 | int A, B, C, X1, X2, X; 42 | cout << "TO CALCULATE QUADRATIC EQUATION " << endl; 43 | cout << "Enter Value of A, B and C " << endl; 44 | cout << "A = "; cin >> A; 45 | cout << "B = "; cin >> B; 46 | cout << "C = "; cin >> C; 47 | int D = (B*B) - (4 * A * C); 48 | if (D > 0) 49 | { 50 | X1 = ((-B) + sqrt(D)) / (2*A); 51 | X2 = ((-B) -(sqrt(D))) / (2*A); 52 | cout << "X1 = " << X1 << endl; 53 | cout << "X2 = " << X2 << endl; 54 | } 55 | else if(D = 0) 56 | { 57 | X = (-B) / (2 * A); 58 | cout << "UNIQUE SOLUTION, X"; 59 | } 60 | else 61 | cout << "NO REAL SOLUTIONS" << endl; 62 | getchar(); 63 | return 0; 64 | } 65 | //main end 66 | ///Program end 67 | 68 | /* 69 | output 70 | Enter Value of A, B and C 71 | A = 10 72 | B = 10 73 | C = 10 74 | NO REAL SOLUTIONS 75 | */ -------------------------------------------------------------------------------- /Chapter-09/ALGORITHM/Algorithm-9-6.cpp: -------------------------------------------------------------------------------- 1 | // Merge sort in C++ 2 | 3 | #include 4 | using namespace std; 5 | 6 | // Merge two subarrays L and M into arr 7 | void merge(int arr[], int p, int q, int r) { 8 | 9 | // Create L ← A[p..q] and M ← A[q+1..r] 10 | int n1 = q - p + 1; 11 | int n2 = r - q; 12 | 13 | int L[n1], M[n2]; 14 | 15 | for (int i = 0; i < n1; i++) 16 | L[i] = arr[p + i]; 17 | for (int j = 0; j < n2; j++) 18 | M[j] = arr[q + 1 + j]; 19 | 20 | // Maintain current index of sub-arrays and main array 21 | int i, j, k; 22 | i = 0; 23 | j = 0; 24 | k = p; 25 | 26 | // Until we reach either end of either L or M, pick larger among 27 | // elements L and M and place them in the correct position at A[p..r] 28 | while (i < n1 && j < n2) { 29 | if (L[i] <= M[j]) { 30 | arr[k] = L[i]; 31 | i++; 32 | } else { 33 | arr[k] = M[j]; 34 | j++; 35 | } 36 | k++; 37 | } 38 | 39 | // When we run out of elements in either L or M, 40 | // pick up the remaining elements and put in A[p..r] 41 | while (i < n1) { 42 | arr[k] = L[i]; 43 | i++; 44 | k++; 45 | } 46 | 47 | while (j < n2) { 48 | arr[k] = M[j]; 49 | j++; 50 | k++; 51 | } 52 | } 53 | 54 | // Divide the array into two subarrays, sort them and merge them 55 | void mergeSort(int arr[], int l, int r) { 56 | if (l < r) { 57 | // m is the point where the array is divided into two subarrays 58 | int m = l + (r - l) / 2; 59 | 60 | mergeSort(arr, l, m); 61 | mergeSort(arr, m + 1, r); 62 | 63 | // Merge the sorted subarrays 64 | merge(arr, l, m, r); 65 | } 66 | } 67 | 68 | // Print the array 69 | void printArray(int arr[], int size) { 70 | for (int i = 0; i < size; i++) 71 | cout << arr[i] << " "; 72 | cout << endl; 73 | } 74 | 75 | // Driver program 76 | int main() { 77 | int arr[] = {6, 5, 12, 10, 9, 1}; 78 | int size = sizeof(arr) / sizeof(arr[0]); 79 | 80 | mergeSort(arr, 0, size - 1); 81 | 82 | cout << "Sorted array: \n"; 83 | printArray(arr, size); 84 | return 0; 85 | } -------------------------------------------------------------------------------- /Chapter-09/ALGORITHM/Algorithm-9-5.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | page 327 3 | Procedure 9.5: MERGE(A, R, LBA, S, LBB, C, LBC) 4 | 5 | This procedure merges the sorted arrays A and B into the array C. 6 | 7 | 1.    Set NA := LBA, NB := LBB, PTR := LBC, UBA := LBA + R – 1, UBB := LBB + S – 1. 8 | 9 | 2.    Same as Algorithm 9.4 except R is replaced by UBA and S by UBB. 10 | 11 | 3.    Same as Algorithm 9.4 except R is replaced by UBA and S by UBB. 12 | 13 | 4.    Return. 14 | */ 15 | #include 16 | using namespace std; 17 | void MIN(int A[], int K, int N, int &LOC) 18 | { 19 | int min = A[K]; 20 | LOC = K; 21 | for (int i = K + 1; i < N; i++) 22 | { 23 | if (min > A[i]) 24 | { 25 | min = A[i]; 26 | LOC = i; 27 | } 28 | } 29 | } 30 | void selection(int A[], int N) 31 | { 32 | int LOC = 0; 33 | int temp; 34 | for (int i = 0; i < N - 1; i++) 35 | { 36 | MIN(A, i, N, LOC); 37 | temp = A[i]; 38 | A[i] = A[LOC]; 39 | A[LOC] = temp; 40 | } 41 | } 42 | void MARCHING(int A[], int R, int LBA, int B[], int S, int LBB, int C[], int LBC) 43 | { 44 | 45 | int NA = LBA, NB = LBB, PTR = LBC; 46 | int UBA = LBA + R - 1, UBB = LBB + S - 1; 47 | while (NA <= UBA && NB <= UBB) 48 | { 49 | if (A[NA] < B[NB]) 50 | C[PTR++] = A[NA++]; 51 | else 52 | C[PTR++] = B[NB++]; 53 | } 54 | while (NA <= UBA) 55 | C[PTR++] = A[NA++]; 56 | while (NB <= UBB) 57 | C[PTR++] = B[NB++]; 58 | } 59 | int main() 60 | { 61 | int R = 6, S = 7, C[R + S]; 62 | int A[R] = {45, 12, 34, 66, 99, 88}; 63 | int B[S] = {65, 25, 35, 15, 75, 55, 45}; 64 | selection(B, S); 65 | selection(A, R); 66 | MARCHING(A, R, 0, B, S, 0, C, 0); 67 | cout << "\n"; 68 | for (int x : A) 69 | { 70 | cout << x << " "; 71 | } 72 | cout << "\n"; 73 | for (int x : B) 74 | { 75 | cout << x << " "; 76 | } 77 | cout << "\n"; 78 | for (int x : C) 79 | { 80 | cout << x << " "; 81 | } 82 | } -------------------------------------------------------------------------------- /Chapter-06/SUPPLEMENTARY PROBLEMS/6-44.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | page 212 3 | 6.19 Show that the recursive solution to the Towers of Hanoi problem in 4 | Sec. 6.8 requires f (n) = 2n – 1 moves for n disks. Show that no other 5 | solution uses fewer than f (n) moves 6 | */ 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | void TOWER(int N, string BEG, string AUX, string END) 12 | { 13 | if(N > 0) 14 | { 15 | TOWER(N-1, BEG, END, AUX); 16 | cout << "Move top disk form pag " << BEG << " to pag " << END << endl; 17 | TOWER(N-1, AUX, BEG, END); 18 | } 19 | } 20 | int main() 21 | { 22 | int N; 23 | cout << "Enter disk number\n"; 24 | cin >> N; 25 | TOWER(N, "A", "B", "C"); 26 | cout << "number of move need\n" << pow(2, N) - 1; 27 | return 0; 28 | } 29 | /* 30 | Enter disk number 31 | 5 32 | Move top disk form pag A to pag C 33 | Move top disk form pag A to pag B 34 | Move top disk form pag C to pag B 35 | Move top disk form pag A to pag C 36 | Move top disk form pag B to pag A 37 | Move top disk form pag B to pag C 38 | Move top disk form pag A to pag C 39 | Move top disk form pag A to pag B 40 | Move top disk form pag C to pag B 41 | Move top disk form pag C to pag A 42 | Move top disk form pag B to pag A 43 | Move top disk form pag C to pag B 44 | Move top disk form pag A to pag C 45 | Move top disk form pag A to pag B 46 | Move top disk form pag C to pag B 47 | Move top disk form pag A to pag C 48 | Move top disk form pag B to pag A 49 | Move top disk form pag B to pag C 50 | Move top disk form pag A to pag C 51 | Move top disk form pag B to pag A 52 | Move top disk form pag C to pag B 53 | Move top disk form pag C to pag A 54 | Move top disk form pag B to pag A 55 | Move top disk form pag B to pag C 56 | Move top disk form pag A to pag C 57 | Move top disk form pag A to pag B 58 | Move top disk form pag C to pag B 59 | Move top disk form pag A to pag C 60 | Move top disk form pag B to pag A 61 | Move top disk form pag B to pag C 62 | Move top disk form pag A to pag C 63 | number of move need 64 | 31 65 | */ -------------------------------------------------------------------------------- /Chapter-05/LINKED LIST/HELP.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | void PRINT_LINE(char ch); 6 | void PRINT_LINE(string ch); 7 | void PRINT_LINE(int wirdth = 50, char ch = '_'); 8 | 9 | 10 | void PRINT_LINE(int wirdth, char ch) 11 | { 12 | while (wirdth-- != 0) 13 | { 14 | cout << ch; 15 | } 16 | cout << "\n"; 17 | } 18 | void PRINT_LINE(char ch) 19 | { 20 | int wirdth = 50; 21 | while (wirdth-- != 0) 22 | { 23 | cout << ch; 24 | } 25 | cout << "\n"; 26 | } 27 | void PRINT_LINE(string ch) 28 | { 29 | int wirdth = 50; 30 | while (wirdth-- != 0) 31 | { 32 | cout << ch; 33 | } 34 | cout << "\n"; 35 | } 36 | 37 | void OPTION() 38 | { 39 | int i = 1; 40 | int check = 0; 41 | cout << "**ENTER YOUR CHOISE**\n"; 42 | cout << setw(3) << (check+=i) << ". INPUT RECORD \n"; 43 | cout << setw(3) << (check+=i) << ". SHOW RECORD\n"; 44 | cout << setw(3) << (check+=i) << ". DELETE ALL RECORD\n"; 45 | cout << setw(3) << (check+=i) << ". INSERT AN ITEM\n"; 46 | cout << setw(3) << (check+=i) << ". DELETE AN ITEM\n"; 47 | cout << setw(3) << (check+=i) << ". SHOW NUMBER OF ELEMENT\n"; 48 | cout << setw(3) << (check+=i) << ". REPLACE A RECORD\n"; 49 | cout << setw(3) << (check+=i) << ". RSEARCH ITEM\n"; 50 | cout << setw(3) << (check+=i) << ". EXIT\n"; 51 | return; 52 | } 53 | void INSERT_OPTION() 54 | { 55 | int i = 1, check = 0; 56 | cout << setw(3) << (check+=i) << ". INSERT BEGINING OF THE LIST\n"; 57 | cout << setw(3) << (check+=i) << ". INSERT END OF THE LIST\n"; 58 | cout << setw(3) << (check+=i) << ". INSERT AFTER A RECORD OF THE LIST\n"; 59 | cout << setw(3) << (check+=i) << ". INSERT BEFOR A RECORD OF THE LIST\n"; 60 | } 61 | void DELETE_OPTION() 62 | { 63 | int i = 1, check = 0; 64 | cout << setw(3) << (check+=i) << ". DELETE FIRST RECORD\n"; 65 | cout << setw(3) << (check+=i) << ". DELETE LAST RECORD\n"; 66 | cout << setw(3) << (check+=i) << ". DELETE SPACIFIC SINGLE RECORD\n"; 67 | cout << setw(3) << (check+=i) << ". DELETE SPACIFIC ALL RECORD\n"; 68 | } 69 | -------------------------------------------------------------------------------- /Chapter-05/LINKED LIST/START.h: -------------------------------------------------------------------------------- 1 | void START() 2 | { 3 | LINKED *START = NULL; 4 | char c; 5 | int NUM, NUM1; 6 | while (1) 7 | { 8 | PRINT_LINE("#"); 9 | OPTION(); 10 | PRINT_LINE("#"); 11 | cout << "-> "; 12 | cin >> c; 13 | PRINT_LINE("#"); 14 | switch (c) 15 | { 16 | case '1': 17 | cout << "Enter integer number \n to end Enter -1\n"; 18 | cin >> NUM; 19 | while (NUM != -1) 20 | { 21 | INSERT_DATA_SORTED(&START, NUM); 22 | cin >> NUM; 23 | } 24 | break; 25 | case '2': 26 | cout << "\nFORWORD ORDER\n"; 27 | DISPLAY_FORWORD(&START); 28 | cout << "\nREVERCE ORDER\n"; 29 | DISPLAY_REVERSE(&START); 30 | cout << "\n"; 31 | break; 32 | case '3': 33 | DELETE_ALL(&START); 34 | break; 35 | case '4': 36 | cout << "ENTER NUMBER\n"; 37 | cin >> NUM; 38 | INSERT_DATA_SORTED(&START, NUM); 39 | break; 40 | case '5': 41 | cout << "ENTER NUMBRE\n"; 42 | cin >> NUM; 43 | DELETE_ITEM(&START, NUM); 44 | break; 45 | case '6': 46 | ELEMENT(&START); 47 | break; 48 | case '7': 49 | cout << "WHICH NUMBER SHOULD BR REPLACE\n"; 50 | cin >> NUM; 51 | cout << "ENTER REPLACED NUMBER\n"; 52 | cin >> NUM1; 53 | REPLACE(&START, NUM, NUM1); 54 | break; 55 | case '8': 56 | cout << "ENTER YOUR NUMBER\n"; 57 | cin >> NUM; 58 | NUM1 = SEARCH_SORTED_LIST(&START, NUM); 59 | if(NUM1 == 0) 60 | { 61 | cout << "ITEM NOT FOUND\n"; 62 | } 63 | else 64 | cout << "ITEM FOUND AT " << NUM1 << endl; 65 | case '9': 66 | exit(0); 67 | break; 68 | default: 69 | cout << "INVALID INPUT\n"; 70 | cout << "TRY AGAIN\n"; 71 | break; 72 | } 73 | } 74 | } -------------------------------------------------------------------------------- /Chapter 01/Example/Example01-04.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Program 3 | 4 | */ 5 | #include 6 | using namespace std; 7 | 8 | struct Name 9 | { 10 | string First; 11 | string Middle; 12 | string last; 13 | }; 14 | 15 | 16 | struct Area 17 | { 18 | string city; 19 | string state; 20 | int zip; 21 | }; 22 | struct Address 23 | { 24 | string street; 25 | Area *area = new Area; 26 | }; 27 | struct Employee 28 | { 29 | string SSN; 30 | Address *address = new Address; 31 | Name *name = new Name; 32 | int Age; 33 | float Salary; 34 | string Dependents; 35 | Employee *next; 36 | }; 37 | //main begin 38 | int main() 39 | { 40 | Employee *head; 41 | Employee Firoz; 42 | head = &Firoz; 43 | cout << "Enter details of Firoz\n"; 44 | 45 | cout << "Enter SSN \n"; 46 | cin >> head->SSN; 47 | cout << "Enter First name\n"; 48 | cin >> head->name->First; 49 | cout << "Enter Middle Name\n"; 50 | cin >> head->name->Middle; 51 | cout << "Enter last name\n"; 52 | cin >> head->name->last; 53 | 54 | cout << "Enter Address \n"; 55 | cout << "Street \n"; 56 | cin >> head->address->street; 57 | cout << "City \n"; 58 | cin >> head->address->area->city ; 59 | cout << "state \n"; 60 | cin >> head->address->area->state; 61 | cout << "ZIP \n"; 62 | cin >> head->address->area->zip; 63 | 64 | cout << "Enter age \n"; 65 | cin >> head->Age; 66 | 67 | cout << "Enter salary\n"; 68 | cin >> head->Salary; 69 | 70 | cout << "Dependents\n"; 71 | cin >> head->Dependents; 72 | 73 | getchar(); 74 | return 0; 75 | } 76 | //main end 77 | ///Program end 78 | 79 | /* 80 | output 81 | Enter details of Firoz 82 | Enter SSN 83 | Np-5452125 84 | Enter First name 85 | Mohammad 86 | Enter Middle Name 87 | Firoz 88 | Enter last name 89 | Mahmud 90 | Enter Address 91 | Street 92 | Dhaka-Narayanganj 93 | City 94 | Narayanganj 95 | state 96 | Dhaka 97 | ZIP 98 | 1421 99 | Enter age 100 | 22 101 | Enter salary 102 | 8000 103 | Dependents 104 | Nothing 105 | */ 106 | -------------------------------------------------------------------------------- /Chapter-03/Programming Problems/3-44.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 66 3 | Program 3.44 4 | Write a program which reads words WORD1 and WORD2 5 | and then replaces each occurrence of WORD1 in TEXT 6 | by WORD2. Test the program using WORD1 = 'HENCE' and WORD2 = 'THUS' 7 | 8 | */ 9 | #include 10 | #include 11 | using namespace std; 12 | int COUNT(string WORD[], int N,int NUM, string key) 13 | { 14 | 15 | string BEG(key + " "), END(" " + key), MID(" "+key+" "); 16 | 17 | for(int i = 0; i < N ; i++) 18 | { 19 | // cout << "Hello 1\n"; 20 | if( WORD[i].substr(0, 4) == BEG) 21 | { 22 | WORD[i].replace(0, BEG.length(), "THIS "); 23 | NUM++; 24 | } 25 | if(WORD[i].substr(WORD[i].length()- 4, WORD[i].length()) == END) 26 | { 27 | WORD[i].replace(WORD[i].length()- 4, END.length(), " THIS "); 28 | NUM++; 29 | } 30 | for(int K = 2; K < WORD[i].length()-3; K++) 31 | { 32 | // cout << "Hello 2\n"; 33 | if(WORD[i].substr(K,5) == MID) 34 | { 35 | WORD[i].replace(K, MID.length(), " THIS "); 36 | NUM++; 37 | } 38 | } 39 | } 40 | 41 | return NUM; 42 | } 43 | 44 | int main() 45 | { 46 | 47 | string S[100]; 48 | int i; 49 | cout << "Enter your string\n"; 50 | for(i = 0 ; i < 100; i++) 51 | { 52 | getline(cin,S[i]); 53 | if(S[i].substr(0,5) == "$$$$$") 54 | break; 55 | } 56 | string key; 57 | cout << "Enter your keyword\n"; 58 | cin >> key; 59 | int NUM = COUNT(S,i,0,key); 60 | cout << "Number of " < 16 | #include 17 | #include 18 | using namespace std; 19 | typedef struct _link 20 | { 21 | int DATA; 22 | _link *next; 23 | }LINKED; 24 | /****************************/ 25 | void ADD_TILE(LINKED **HEAD, int DATA) 26 | { 27 | LINKED *PTR = *HEAD , *NEW; 28 | if(*HEAD == NULL) 29 | { 30 | *HEAD = new LINKED; 31 | (*HEAD)->DATA = DATA; 32 | (*HEAD)->next = *HEAD; 33 | } 34 | else 35 | { 36 | while(PTR->next != *HEAD) 37 | { 38 | PTR = PTR->next; 39 | } 40 | NEW = new LINKED; 41 | NEW->DATA = DATA; 42 | NEW->next = *HEAD; 43 | PTR->next = NEW; 44 | } 45 | return; 46 | } 47 | void DISPLAY(LINKED *HEAD) 48 | { 49 | LINKED *PTR = HEAD; 50 | while(PTR->next != HEAD) 51 | { 52 | cout << PTR->DATA << " \n"; 53 | PTR = PTR->next; 54 | } 55 | cout << PTR->DATA << " \n"; 56 | } 57 | void SRCHHL(LINKED *HEAD, int ITEM) 58 | { 59 | int LOC = 1; 60 | LINKED *PTR = HEAD; 61 | while(PTR->next != HEAD && PTR->DATA != ITEM) 62 | { 63 | LOC++; 64 | PTR = PTR->next; 65 | } 66 | if(PTR->DATA == ITEM) 67 | { 68 | cout << "ITEM FOUND AT " << LOC << " \n"; 69 | } 70 | else 71 | cout << "ITEM NOT FOUND\n"; 72 | 73 | } 74 | int main() 75 | { 76 | int DATA; 77 | LINKED *HEAD; 78 | HEAD = NULL; 79 | for(int i = 1; i < 11; i++) 80 | { 81 | ADD_TILE(&HEAD, i*10); 82 | //cout << "I = " << i*10 << endl; 83 | } 84 | DISPLAY(HEAD); 85 | SRCHHL(HEAD, 10); 86 | 87 | } -------------------------------------------------------------------------------- /Chapter-02/Chapter Algorithm/Algorithm2-4.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 28 3 | Algorithm 2.4 4 | Suppose a linear array DATA contains n 5 | elements, and suppose a specific ITEM of 6 | information is given. We want either to 7 | find the location LOC of ITEM in the array 8 | DATA, or to send some message, such as LOC = 0, 9 | to indicate that ITEM does not appear in DATA. 10 | The linear search algorithm solves this problem 11 | by comparing ITEM, one by one, with each element 12 | in DATA. That is, we compare ITEM with DATA[1], 13 | then DATA[2], and so on, until we find LOC such 14 | that ITEM = DATA[LOC]. A formal presentation of 15 | this algorithm follows. 16 | 17 | */ 18 | /* 19 | Algorithm 20 | Algorithm 2.4: (Linear Search) A linear array DATA 21 | with N elements and a specific ITEM of information 22 | are given. This algorithm finds the location LOC of 23 | ITEM in the array DATA or sets LOC = O. 24 | 25 | 1. [Initialize] Set K := 1 and LOC := 0. 26 | 2. Repeat Steps 3 and 4 while LOC = 0 and K ≤ N. 27 | 3. If ITEM = DATA[K], then: Set LOC: = K. 28 | 4. Set K := K + 1. [Increments counter.] 29 | [End of Step 2 loop.] 30 | 5. [Successful?] 31 | If LOC = 0, then: 32 | Write: ITEM is not in the array DATA. 33 | Else: 34 | Write: LOC is the location of ITEM. 35 | [End of If structure.] 36 | 6. Exit. 37 | */ 38 | 39 | #include 40 | using namespace std; 41 | 42 | //main begin 43 | int main() 44 | { 45 | int K, LOC, N, ITEM; 46 | K = 0; 47 | LOC = -1; 48 | N = 5; 49 | int DATA[N] = {22,33,44,55,66}; 50 | cout << "Enter Search Key\n"; 51 | cin >> ITEM; 52 | while(LOC == -1 && K <= N) 53 | { 54 | if(ITEM == DATA[K]) 55 | LOC = K ; 56 | K++; 57 | } 58 | if (LOC == -1) 59 | { 60 | cout << "ITEM in not in the array DATA" << endl; 61 | } 62 | else 63 | { 64 | cout << LOC + 1 << " is the location of " << ITEM << endl; 65 | } 66 | 67 | getchar(); 68 | return 0; 69 | } 70 | //main end 71 | ///Program end 72 | 73 | /* 74 | output 75 | -----First Run------- 76 | Enter Search Key 77 | 77 78 | ITEM in not in the array DATA 79 | -----Second RUN -------- 80 | Enter Search Key 81 | 55 82 | 4 is the location of 55 83 | 84 | */ -------------------------------------------------------------------------------- /Chapter-03/Programming Problems/3-40.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 66 3 | Program 4 | Write a program which interchanges the Jth and Kth paragraphs. 5 | Test the program using J = 2 and K = 4. 6 | */ 7 | #include 8 | #include 9 | using namespace std; 10 | void INTERCHANGE(string LINK[], int N, int FirstLine, int secontLine) 11 | { 12 | string S; 13 | S = LINK[FirstLine]; 14 | LINK[FirstLine] = LINK[secontLine]; 15 | LINK[secontLine] = S; 16 | 17 | 18 | } 19 | int main() 20 | { 21 | string S[100]; 22 | int i, FirstLine, SecondLine; 23 | cout << "Enter your string\n"; 24 | for(i ; i < 100; i++) 25 | { 26 | getline(cin,S[i]); 27 | if(S[i].substr(0,5) == "$$$$$") 28 | break; 29 | } 30 | cout << "Enter line number to Interchange\n"; 31 | cout << "First Line " ; cin >> FirstLine ; 32 | cout << "Second Line "; cin >> SecondLine; 33 | INTERCHANGE(S,i,FirstLine+1,SecondLine+1); 34 | for(int k = 0; k < i ; k++) 35 | { 36 | cout << S[k] << endl; 37 | } 38 | return 0; 39 | } 40 | /* 41 | Output 42 | Enter your string 43 | 1 the We do not count the in mother, and we assume no sentence ends with the word the 44 | 2 the We do not count the in mother, and we assume no sentence ends with the word the 45 | 3 the We do not count the in mother, and we assume no sentence ends with the word the 46 | 4 the We do not count the in mother, and we assume no sentence ends with the word the 47 | 5 the We do not count the in mother, and we assume no sentence ends with the word the 48 | 6 the We do not count the in mother, and we assume no sentence ends with the word the 49 | $$$$$ 50 | Enter line number to Interchange 51 | First Line 2 52 | Second Line 4 53 | 1 the We do not count the in mother, and we assume no sentence ends with the word the 54 | 2 the We do not count the in mother, and we assume no sentence ends with the word the 55 | 3 the We do not count the in mother, and we assume no sentence ends with the word the 56 | 6 the We do not count the in mother, and we assume no sentence ends with the word the 57 | 5 the We do not count the in mother, and we assume no sentence ends with the word the 58 | 4 the We do not count the in mother, and we assume no sentence ends with the word the 59 | */ -------------------------------------------------------------------------------- /Chapter-03/Programming Problems/3-43.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Page 66 3 | Program 4 | Write a program which interchanges the Jth and Kth paragraphs. 5 | Test the program using J = 2 and K = 4. 6 | */ 7 | #include 8 | #include 9 | using namespace std; 10 | void INTERCHANGE(string LINK[], int N, int FirstLine, int secontLine) 11 | { 12 | string S; 13 | S = LINK[FirstLine]; 14 | LINK[FirstLine] = LINK[secontLine]; 15 | LINK[secontLine] = S; 16 | 17 | 18 | } 19 | int main() 20 | { 21 | string S[100]; 22 | int i, FirstLine, SecondLine; 23 | cout << "Enter your string\n"; 24 | for(i ; i < 100; i++) 25 | { 26 | getline(cin,S[i]); 27 | if(S[i].substr(0,5) == "$$$$$") 28 | break; 29 | } 30 | cout << "Enter line number to Interchange\n"; 31 | cout << "First Line " ; cin >> FirstLine ; 32 | cout << "Second Line "; cin >> SecondLine; 33 | INTERCHANGE(S,i,FirstLine+1,SecondLine+1); 34 | for(int k = 0; k < i ; k++) 35 | { 36 | cout << S[k] << endl; 37 | } 38 | return 0; 39 | } 40 | /* 41 | Output 42 | Enter your string 43 | 1 the We do not count the in mother, and we assume no sentence ends with the word the 44 | 2 the We do not count the in mother, and we assume no sentence ends with the word the 45 | 3 the We do not count the in mother, and we assume no sentence ends with the word the 46 | 4 the We do not count the in mother, and we assume no sentence ends with the word the 47 | 5 the We do not count the in mother, and we assume no sentence ends with the word the 48 | 6 the We do not count the in mother, and we assume no sentence ends with the word the 49 | $$$$$ 50 | Enter line number to Interchange 51 | First Line 2 52 | Second Line 4 53 | 1 the We do not count the in mother, and we assume no sentence ends with the word the 54 | 2 the We do not count the in mother, and we assume no sentence ends with the word the 55 | 3 the We do not count the in mother, and we assume no sentence ends with the word the 56 | 6 the We do not count the in mother, and we assume no sentence ends with the word the 57 | 5 the We do not count the in mother, and we assume no sentence ends with the word the 58 | 4 the We do not count the in mother, and we assume no sentence ends with the word the 59 | */ -------------------------------------------------------------------------------- /Chapter-07/Supplemenary Problems/7-33.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | page 272/ 3 | 7.33  Consider the binary tree T in Fig. 7.60(a). 4 | 5 | (a) Draw the one-way inorder threading of T. 6 | 7 | (b) Draw the one-way preorder threading of T. 8 | 9 | (c) Draw the two-way inorder threading of T. 10 | */ 11 | 12 | #include 13 | #include 14 | #include 15 | using namespace std; 16 | typedef struct _tree 17 | { 18 | char data; 19 | _tree *left; 20 | _tree *right; 21 | } TREE; 22 | 23 | TREE *insert(TREE *root, char data) 24 | { 25 | if (root == NULL) 26 | { 27 | TREE *ptr = new TREE; 28 | ptr->data = data; 29 | ptr->left = NULL; 30 | ptr->right = NULL; 31 | root = ptr; 32 | } 33 | else if(data <= root->data) 34 | root->left = insert(root->left,data); 35 | else 36 | root->right = insert(root->right,data); 37 | return root; 38 | } 39 | void PreOrder(TREE *root) 40 | { 41 | if(root != NULL) 42 | { 43 | cout << root->data << " "; 44 | PreOrder(root->left); 45 | PreOrder(root->right); 46 | } 47 | } 48 | void inorder(TREE *root) 49 | { 50 | 51 | if(root != NULL) 52 | { 53 | inorder(root->left); 54 | cout << root->data << " "; 55 | inorder(root->right); 56 | } 57 | 58 | } 59 | void PostOrder(TREE *root) 60 | { 61 | if(root != NULL) 62 | { 63 | PostOrder(root->left); 64 | PostOrder(root->right); 65 | cout << root->data << " "; 66 | } 67 | } 68 | int main() 69 | { 70 | int AVAIL = 8; 71 | char temp; 72 | TREE *root = NULL; 73 | for(int i = 0; i < AVAIL; i++) 74 | { 75 | cin >> temp; 76 | root = insert(root, temp); 77 | } 78 | cout << "\nInorder travelsel\n"; 79 | inorder(root); 80 | cout << "\nPreorder travelsel\n"; 81 | PreOrder(root); 82 | cout << "\nPostorder travelsel\n"; 83 | PostOrder(root); 84 | return 0; 85 | } 86 | /* 87 | A 88 | B 89 | D 90 | E 91 | H 92 | G 93 | C 94 | F 95 | 96 | Inorder travelsel 97 | A B C D E F G H 98 | Preorder travelsel 99 | A B D C E H G F 100 | Postorder travelsel 101 | C F G H E D B A 102 | */ -------------------------------------------------------------------------------- /Chapter-06/PROGRAMMING PROBLEMS/6-53.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 6.3  Translate Algorithm 6.6 into a subprogram POLISH(Q, P) 3 | which transforms an infix expression Q into its equivalent 4 | postfix expression P. Assume each operand is a single alphabetic 5 | character, and use the usual symbols for addition (+), subtraction (–), 6 | multiplication (*) and division (/), but use the symbol ↑ or $ for exponentiation. 7 | (Some programming languages do not accept ↑.) Test the program using 8 | 9 | (a) ( ( A + B ) * D ) $ (E – F) 10 | 11 | (b) A + ( B * C – ( D / E $ F ) * G ) * H 12 | */ 13 | #include 14 | #include 15 | #include 16 | using namespace std; 17 | 18 | // Function to return perecendece of operators 19 | int PRECENDCE(char c) 20 | { 21 | if (c == '$') 22 | return 3; 23 | else if (c == '/' || c == '*') 24 | return 2; 25 | else if (c == '+' || c == '-') 26 | return 1; 27 | else 28 | return -1; 29 | } 30 | string infixToPostFix(string s) 31 | { 32 | stack STK; 33 | STK.push('('); 34 | s.push_back(')'); 35 | string P; 36 | for(int i = 0 ; i < s.length(); i++) 37 | { 38 | char c = s[i]; 39 | if(c == ' ')continue; 40 | if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) 41 | P.push_back(c); 42 | 43 | else 44 | if(c == '(') 45 | STK.push('('); 46 | 47 | else 48 | if(c == ')') 49 | { 50 | while(STK.top() != '(') 51 | { 52 | P.push_back(STK.top()); 53 | STK.pop(); 54 | } 55 | STK.pop(); 56 | } 57 | else 58 | { 59 | while (!STK.empty() && PRECENDCE(s[i]) <= PRECENDCE(STK.top())) 60 | { 61 | P.push_back(STK.top()); 62 | STK.pop(); 63 | } 64 | STK.push(c); 65 | } 66 | } 67 | return P; 68 | } 69 | int main() 70 | { 71 | string exp = "( ( A + B ) * D ) $ (E - F)"; 72 | cout << infixToPostFix(exp) << endl; 73 | exp = "A + ( B * C - ( D / E $ F ) * G ) * H"; 74 | cout << infixToPostFix(exp) << endl; 75 | } 76 | /* 77 | AB+D*EF-$ 78 | ABC*DEF$/G*-H*+ 79 | */ --------------------------------------------------------------------------------