├── .project
├── LICENSE
├── README.md
├── arithmeticExpressions
├── infixEvaluation
│ ├── InfixEvaluation.cpp
│ └── InfixEvaluation_Input.txt
├── infixToPostfix
│ ├── InfixToPostfix.cpp
│ └── InfixToPostfix_Input.txt
├── infixToPrefix
│ ├── InfixToPrefix.cpp
│ └── InfixToPrefix_Input.txt
├── postfixEvaluation
│ ├── PostfixEvaluation.cpp
│ └── PostfixEvaluation_Input.txt
└── prefixEvaluation
│ ├── PrefixEvaluation.cpp
│ └── PrefixEvaluation_Input.txt
├── dataStructures
├── listImplementation
│ ├── ListImplementation_Input.txt
│ ├── OneWayLinkedList.cpp
│ ├── TwoWayLinkedList.cpp
│ └── implementationUsingNode
│ │ ├── ListImplementation_Input.txt
│ │ ├── OneWayLinkedList.cpp
│ │ └── TwoWayLinkedList.cpp
├── stack
│ ├── StackImplementation.asm
│ └── StackImplementation.c
├── string
│ ├── String.c
│ ├── StringLongestCommonSubSecuence.cpp
│ └── StringLongestIncreasingSubSecuence.cpp
└── tree
│ ├── BinaryHeap.cpp
│ ├── BinomialTree.cpp
│ ├── activitySelection
│ ├── ActivitySelection.cpp
│ └── ActivitySelection_Input.txt
│ ├── balanceTree
│ ├── BalanceTree.java
│ └── Touple.java
│ └── kdTree
│ └── KdTreeImpl.java
├── graph
├── breathFirstSearch
│ ├── BreadthFirstSearch.cpp
│ ├── BreadthFirstSearch_Input.PNG
│ ├── BreadthFirstSearch_Input.txt
│ ├── BreadthFirstSearch_Input2.PNG
│ └── BreadthFirstSearch_Input2.txt
├── depthFirstSearch
│ ├── DepthFirstSearch.cpp
│ ├── DepthFirstSearch_Input.PNG
│ └── DepthFirstSearch_Input.txt
├── maxFlow
│ ├── MaxFlow.cpp
│ ├── MaxFlow_Input.png
│ └── MaxFlow_Input.txt
├── shortestPathAlgorithm
│ ├── ShortestPathAlgorithm.cpp
│ ├── ShortestPathAlgorithm_Input.PNG
│ ├── ShortestPathAlgorithm_Input.txt
│ ├── ShortestPathAlgorithm_Input2.PNG
│ └── ShortestPathAlgorithm_Input2.txt
└── topologicalSort
│ ├── TopologicalSort.cpp
│ ├── TopologicalSort_Input.PNG
│ └── TopologicalSort_Input.txt
├── numericalMethods
├── calculus
│ ├── DerivetivesOnDifferentPoints.c
│ ├── IntegrationForwardInterpolation.c
│ ├── IntegrationSimpsonRule.c
│ └── IntersectingArea.c
└── equationSolving
│ ├── LinearEquationSolvingProcess.c
│ └── NonLinearEquationSolvingProcess.c
├── others
├── cigaretteSmokersProblem
│ ├── Agent.java
│ ├── CigaretteSmokersProblemMain.java
│ ├── Graph.java
│ └── Smoker.java
├── geneticAlgorithm
│ ├── Board.java
│ └── Player.java
└── huffmanAlgorithm
│ ├── Extractor.cpp
│ ├── ZipMaker.cpp
│ └── huffmanAlgorithm_Input.txt
├── playWithNumbers
├── factorial
│ ├── BigFactorials.c
│ ├── DigitsOfFactorial.c
│ └── FactorsOfFactorial.c
├── fibonaciiNumber
│ ├── FibonaciiNumber.c
│ ├── FibonaciiSerise.c
│ └── FibonaciiSum.c
├── otherNumbers
│ ├── FriendNumbers.c
│ └── PerfectNumberHaunting.c
├── pascalTriangle
│ ├── BetterPascalTriangle.c
│ ├── EasyPascalTriangle.c
│ ├── PascalTriangle.c
│ ├── Piramid.c
│ └── RealPascalTriangle.c
├── primeNumber
│ ├── AnotherWayOfPrimeNumberHaunting.c
│ ├── DefinitePrimeNumberHaunting.c
│ ├── Factors.c
│ ├── GreatPrimeNumberHaunting.c
│ ├── GreatestPrimeNumberHaunting.c
│ ├── PrimeNumber.c
│ ├── PrimeNumberHaunting.asm
│ └── PrimeNumberHaunting.c
└── time
│ ├── AgeCalculator.c
│ ├── LeapYearCounter.c
│ ├── Stopwatch.c
│ └── TimeAfter.c
├── search
├── aStarSearch
│ └── AStarSearch.java
├── binarySearch
│ ├── BinarySearch.cpp
│ └── BinarySearch_Input.txt
└── knuthMorrisPrattAlgorithm
│ ├── KnuthMorrisPrattAlgorithm.cpp
│ └── KnuthMorrisPrattAlgorithm_Input.txt
├── sort
└── quickSort
│ ├── QuickSort.cpp
│ └── QuickSort_Input.txt
└── thread
├── Input
├── priorityBased
├── nonpremitive
│ ├── CPU.java
│ └── PriorityBasedNonpremitive.java
└── premitive
│ ├── CPU.java
│ └── PriorityBasedPremitive.java
├── shortestJob
├── nonpremitive
│ ├── CPU.java
│ └── ShortestJobFirstNonpremitive.java
└── premitive
│ ├── CPU.java
│ └── ShortestJobFirstPremitive.java
└── utilClasses
├── Graph.java
├── Process.java
└── Result.java
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | AlgorithmImplementations
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2015 Minhas Kamal
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/arithmeticExpressions/infixEvaluation/InfixEvaluation.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | Name: Minhas Kamal (minhaskamal024@gmail.com)
3 | Description: This program reads an infix notation from a file & finds the result.
4 | Date: Oct-2013
5 | **/
6 |
7 | #include
8 | #include
9 | #include
10 | #include
11 | #include
12 |
13 | using namespace std;
14 |
15 |
16 | double stringToDouble(string str); //converts a string to a double
17 | int operatorPrecedence(char operat); //returns bigger integer depending on the precedence
18 | double operation(double a, double b, char operat); //returns a result depending on the operator
19 |
20 |
21 | struct element{
22 | string str; //string property
23 | double value; //value of the string
24 | bool opert; //operator-true, operand-false
25 | };
26 |
27 | int main(){
28 | ifstream input; //the input file
29 |
30 | input.open("InfixEvaluation_Input.txt");
31 | if(!input.is_open()){ //when file is not found exit
32 | cout << "File name is wrong!\n";
33 | return 0;
34 | }
35 |
36 | string s; //stores the input file's element as string
37 | vector inputElement; //holds the behaviour of the input elements
38 | while(input >> s){ //the loop determines the behaviour of the input elements
39 | element elem;
40 | elem.str = s;
41 |
42 | if(s[0]=='+' || s[0]=='-' || s[0]=='*' || s[0]=='/' || s[0]=='^'
43 | || s[0]=='(' || s[0]==')'){ //when an operator is found
44 | elem.value = -0.0;
45 | elem.opert = true;
46 | }
47 | else{
48 | elem.value = stringToDouble(s); //this method converts string to double
49 | elem.opert = false;
50 | }
51 |
52 | inputElement.push_back(elem);
53 | }
54 |
55 | input.close(); //closing the stream
56 |
57 | /*cout << "string \toperat \tvalue \n"; //output of the inputElement
58 | for(int i=0; i operand; //postfix form of the input
71 | stack operat; //temporarily stores operators
72 |
73 | operat.push('('); //pushing '(' at the beginning of stack
74 |
75 | bool everyThingOkFlag=true;
76 |
77 | for(int i=0; !operat.empty(); i++){ //evaluation
78 | if(!inputElement[i].opert){
79 | operand.push(inputElement[i].value);
80 | }
81 | else{
82 | if(inputElement[i].str[0]=='('){
83 | operat.push('(');
84 | }
85 | else if(inputElement[i].str[0]==')'){
86 | while(operat.top()!='('){
87 | double b = operand.top(); //taking first two elements of the stack
88 | operand.pop();
89 | double a = operand.top();
90 | operand.pop();
91 |
92 | char oprt = operat.top(); //taking the top the operator of stack
93 | operat.pop();
94 |
95 | double result = 0; //result after the operation of a & b
96 | result = operation(a, b, oprt);
97 |
98 | operand.push(result); //pushing the result in the operand stack
99 | }
100 | operat.pop(); //pops the '('
101 | }
102 | else{
103 | int opertPrecedence = operatorPrecedence(inputElement[i].str[0]);
104 |
105 | while(operat.top()!='('){
106 | int stackPrecedence = operatorPrecedence(operat.top());
107 |
108 | if(stackPrecedence < opertPrecedence) break;
109 | else{
110 | double b = operand.top(); //taking first two elements of the stack
111 | operand.pop();
112 | double a = operand.top();
113 | operand.pop();
114 |
115 | char oprt = operat.top(); //taking the top the operator of stack
116 | operat.pop();
117 |
118 | double result = 0; //result after the operation of a & b
119 | result = operation(a, b, oprt);
120 |
121 | operand.push(result); //pushing the result in the operand stack
122 | }
123 | }
124 | operat.push(inputElement[i].str[0]);
125 | }
126 | }
127 |
128 | if(i>inputElement.size()) {
129 | cout << "Math error! \n";
130 | everyThingOkFlag=false;
131 | break;
132 | }
133 | }
134 |
135 | if(everyThingOkFlag) cout << "The result is: " << operand.top() << endl;
136 |
137 | return 0;
138 | }
139 |
140 |
141 | double stringToDouble(string str){ //converts a string to a double
142 | double value=0, //holds the value of the string
143 | afterPoint=0; //holds the value after a point
144 |
145 | bool flag = false; //represents if any point is found in the string
146 | int numberOfElementAfterPoint=0;
147 |
148 | for(int i=0; str[i]!=NULL; i++){
149 | if(str[i]=='.') flag = true; //waiting for a '.'
150 | else{
151 | if(flag){
152 | afterPoint = afterPoint*10 + (str[i]-48);
153 | numberOfElementAfterPoint++;
154 | }
155 | else
156 | value = value*10 + (str[i]-48);
157 | }
158 | }
159 |
160 | int divisor=1; //divides the afterPoint element by precise number of 10s
161 | for(int i=0; i
8 | #include
9 | #include
10 | #include
11 | #include
12 | using namespace std;
13 |
14 |
15 | double stringToDouble(string str);
16 | int operatorPrecedence(string str);
17 | struct element{
18 | string str; //string property
19 | double value; //value of the string
20 | bool opert; //operator-true, operand-false
21 | };
22 |
23 | int main(){
24 | ifstream input; //the input file
25 |
26 | input.open("InfixToPostfix_Input.txt");
27 | if(!input.is_open()){ //when file is not found exit
28 | cout << "File name is wrong!\n";
29 | return 0;
30 | }
31 |
32 | string s; //stores the input file's element as string
33 | vector inputElement; //holds the behaviour of the input elements
34 | while(input >> s){ //the loop determines the behaviour of the input elements
35 | element elem;
36 | elem.str = s;
37 |
38 | if(s[0]=='+' || s[0]=='-' || s[0]=='*' || s[0]=='/' || s[0]=='^' || s[0]=='(' || s[0]==')'){ //when an operator is found
39 | elem.value = -0.0;
40 | elem.opert = true;
41 | }
42 | else{
43 | elem.value = stringToDouble(s); //this method converts string to double
44 | elem.opert = false;
45 | }
46 |
47 | inputElement.push_back(elem);
48 | }
49 |
50 | input.close(); //closing the stream
51 |
52 | /*cout << "string \toperat \tvalue \n"; //output of the inputElement
53 | for(int i=0; i postfix; //postfix form of the input
66 | stack tempOpertStore; //temporarily stores operators
67 |
68 | tempOpertStore.push("(");
69 | for(int i=0; !tempOpertStore.empty(); i++){ //conversion
70 | if(!inputElement[i].opert){
71 | postfix.push_back(inputElement[i].str);
72 | }
73 | else{
74 | if(inputElement[i].str[0]=='('){
75 | tempOpertStore.push(inputElement[i].str);
76 | }
77 | else if(inputElement[i].str[0]==')'){
78 | while(tempOpertStore.top()[0]!='('){
79 | postfix.push_back(tempOpertStore.top());
80 | tempOpertStore.pop();
81 | }
82 | tempOpertStore.pop(); //pops the '('
83 | }
84 | else{
85 | int opertPrecedence = operatorPrecedence(inputElement[i].str);
86 |
87 | while(tempOpertStore.top()[0]!='('){
88 | int stackPrecedence = operatorPrecedence(tempOpertStore.top());
89 |
90 | if(stackPrecedence < opertPrecedence) break;
91 | else{
92 | postfix.push_back(tempOpertStore.top());
93 | tempOpertStore.pop();
94 | }
95 | }
96 | tempOpertStore.push(inputElement[i].str);
97 | }
98 | }
99 |
100 | /*cout << "after " << inputElement[i].str << ": "; //analysing every step
101 | for(int j=0; j
8 | #include
9 | #include
10 | #include
11 | #include
12 | using namespace std;
13 |
14 |
15 | double stringToDouble(string str);
16 | int operatorPrecedence(string str);
17 | struct element{
18 | string str; //string property
19 | double value; //value of the string
20 | bool opert; //operator-true, operand-false
21 | };
22 |
23 | int main(){
24 | ifstream input; //the input file
25 |
26 | input.open("InfixToPrefix_Input.txt");
27 | if(!input.is_open()){ //when file is not found exit
28 | cout << "File name is wrong!\n";
29 | return 0;
30 | }
31 |
32 | string s; //stores the input file's element as string
33 | vector inputElement; //holds the behaviour of the input elements
34 |
35 | element e; //pushing '(' at the start of the input representing the start
36 | e.str = "(";
37 | e.opert = true;
38 | e.value = -0.0;
39 | inputElement.push_back(e);
40 |
41 | while(input >> s){ //the loop determines the behaviour of the input elements
42 | element elem;
43 | elem.str = s;
44 |
45 | if(s[0]=='+' || s[0]=='-' || s[0]=='*' || s[0]=='/' || s[0]=='^' || s[0]=='(' || s[0]==')'){ //when an operator is found
46 | elem.value = -0.0;
47 | elem.opert = true;
48 | }
49 | else{
50 | elem.value = stringToDouble(s); //this method converts string to double
51 | elem.opert = false;
52 | }
53 |
54 | inputElement.push_back(elem);
55 | }
56 |
57 | input.close(); //closing the stream
58 |
59 | /*/cout << "string \toperat \tvalue \n"; //output of the inputElement
60 | for(int i=0; i prefix; //postfix form of the input
65 | stack tempOpertStore; //temporarily stores operators
66 |
67 | tempOpertStore.push(")");
68 |
69 | for(int i=inputElement.size()-1; !tempOpertStore.empty(); i--){ //conversion
70 | if(!inputElement[i].opert){
71 | prefix.push_back(inputElement[i].str);
72 | }
73 | else{
74 | if(inputElement[i].str[0]==')'){
75 | tempOpertStore.push(inputElement[i].str);
76 | }
77 | else if(inputElement[i].str[0]=='('){
78 | while(tempOpertStore.top()[0]!=')'){
79 | prefix.push_back(tempOpertStore.top());
80 | tempOpertStore.pop();
81 | }
82 | tempOpertStore.pop(); //pops the ')'
83 | }
84 | else{
85 | int opertPrecedence = operatorPrecedence(inputElement[i].str);
86 |
87 | while(tempOpertStore.top()[0]!=')'){
88 | int stackPrecedence = operatorPrecedence(tempOpertStore.top());
89 |
90 | if(stackPrecedence < opertPrecedence) break;
91 | else{
92 | prefix.push_back(tempOpertStore.top());
93 | tempOpertStore.pop();
94 | }
95 | }
96 | tempOpertStore.push(inputElement[i].str);
97 | }
98 | }
99 |
100 | /*cout << "after " << inputElement[i].str << ": "; //analysing every step
101 | for(int j=0; j=0; i--) //the output
110 | cout << prefix[i] << " " ;
111 | cout << "\n\n";
112 |
113 |
114 |
115 | return 0;
116 | }
117 |
118 |
119 | double stringToDouble(string str){ //converts a string to a double
120 | double value=0, //holds the value of the string
121 | afterPoint=0; //holds the value after a point
122 |
123 | bool flag = false; //represents if any point is found in the string
124 | int numberOfElementAfterPoint=0;
125 |
126 | for(int i=0; str[i]!=NULL; i++){
127 | if(str[i]=='.') flag = true;
128 | else{
129 | if(flag){
130 | afterPoint = afterPoint*10 + (str[i]-48);
131 | numberOfElementAfterPoint++;
132 | }
133 | else
134 | value = value*10 + (str[i]-48);
135 | }
136 | }
137 |
138 | int divisor=1; //divides the afterPoint element by precise number of 10s
139 | for(int i=0; i
8 | #include
9 | #include
10 | #include
11 |
12 | using namespace std;
13 |
14 |
15 | double stringToDouble(string str);
16 | double exponent(double a, double b);
17 |
18 | struct element{
19 | string str; //string property
20 | double value; //value of the string
21 | bool opert; //operator-true, operand-false
22 | };
23 |
24 |
25 | int main(){
26 | ifstream input; //the input file
27 |
28 | input.open("PostfixEvaluation_Input.txt");
29 | if(!input.is_open()){ //when file is not found exit
30 | cout << "File name is wrong!\n";
31 | return 0;
32 | }
33 |
34 |
35 | string s; //stores the input file's element as string
36 | vector inputElement; //holds the behaviour of the input elements
37 | while(input >> s){ //the loop determines the behaviour of the input elements
38 | element elem;
39 | elem.str = s;
40 |
41 | if(s[0]=='+' || s[0]=='-' || s[0]=='*' || s[0]=='/' || s[0]=='^' || s[0]=='(' || s[0]==')'){ //when an operator is found
42 | elem.value = -0.0;
43 | elem.opert = true;
44 | }
45 | else{
46 | elem.value = stringToDouble(s); //this method converts string to double
47 | elem.opert = false;
48 | }
49 |
50 | inputElement.push_back(elem);
51 | }
52 |
53 | input.close(); //closing the stream
54 |
55 |
56 | /*cout << "string \toperat \tvalue \n"; //output of the inputElement
57 | for(int i=0; i tempOperndStore; //here operands are stored temporarily
62 |
63 | for(int i=0; i
8 | #include
9 | #include
10 | #include
11 |
12 | using namespace std;
13 |
14 |
15 | double stringToDouble(string str);
16 | double exponent(double a, double b);
17 |
18 | struct element{
19 | string str; //string property
20 | double value; //value of the string
21 | bool opert; //operator-true, operand-false
22 | };
23 |
24 |
25 | int main(){
26 | ifstream input; //the input file
27 |
28 | input.open("PrefixEvaluation_Input.txt");
29 | if(!input.is_open()){ //when file is not found exit
30 | cout << "File name is wrong!\n";
31 | return 0;
32 | }
33 |
34 |
35 | string s; //stores the input file's element as string
36 | vector inputElement; //holds the behaviour of the input elements
37 |
38 | element el; //pushing a '(' to determine the start
39 | el.opert=true;
40 | el.str="(";
41 | el.value=-0;
42 | inputElement.push_back(el);
43 |
44 |
45 | while(input >> s){ //the loop determines the behaviour of the input elements
46 | element elem;
47 | elem.str = s;
48 |
49 | if(s[0]=='+' || s[0]=='-' || s[0]=='*' || s[0]=='/' || s[0]=='^' || s[0]=='(' || s[0]==')'){ //when an operator is found
50 | elem.value = -0.0;
51 | elem.opert = true;
52 | }
53 | else{
54 | elem.value = stringToDouble(s); //this method converts string to double
55 | elem.opert = false;
56 | }
57 |
58 | inputElement.push_back(elem);
59 | }
60 |
61 | input.close(); //closing the stream
62 |
63 |
64 | /*cout << "string \toperat \tvalue \n"; //output of the inputElement
65 | for(int i=0; i tempStore; //here operands are stored temporarily
70 |
71 | for(int i=0; i
7 | #include
8 |
9 | using namespace std;
10 |
11 | list creatList(); //creates the list
12 | void traversing(list myList); //prints all the integers in the list
13 | int searching(list myList, int searchingElement); //searches for selected element
14 | list deleting(list myList, int deletingElement, int a); //deleting selected element
15 | list inserting(list myList, int destinationElement, int insertingElement, int a); //inserts certain element in definite place
16 | list destroy(list myList); //destroying the list
17 |
18 |
19 | int main()
20 | {
21 | list myList;
22 | myList = creatList();
23 |
24 | while(true)
25 | {
26 | int selection; //user action
27 | cout << "\n##What do you want to do? \npress- 1 for traversing\n"; //user prompt
28 | cout << " 2 for searching\n 3 for deleting\n 4 for inserting\n";
29 | cout << " 5 for exit\nSelection: ";
30 | cin >> selection;
31 |
32 |
33 | if(selection==1)
34 | traversing(myList);
35 |
36 | else if(selection==2)
37 | {
38 | int searchingElement; //the element to be searched
39 | cout << "Enter the element you want to search for: ";
40 | cin >> searchingElement;
41 |
42 | cout << "\n* " << searchingElement << " is found " << searching(myList, searchingElement) << " times.\n";
43 | }
44 |
45 | else if(selection==3)
46 | {
47 | int deletingElement; //the element to be deleted
48 | cout << "Enter the element: ";
49 | cin >> deletingElement;
50 |
51 | int a=0; //determines the correct element
52 | cout << "Press 1 for deleting " << deletingElement;
53 | cout << "\n 2 for deleting the element after " << deletingElement << "\nSelection- ";
54 | cin >> a;
55 |
56 | myList = deleting(myList, deletingElement, a);
57 | }
58 |
59 | else if(selection==4)
60 | {
61 | int insertingElement, destinationElement; //the element to be deleted
62 | cout << "Enter the element: ";
63 | cin >> insertingElement;
64 | cout << "Enter the destination element: ";
65 | cin >> destinationElement;
66 |
67 | int a=0; //determines the correct element
68 | cout << "Press 1 for inserting after " << destinationElement;
69 | cout << "\n 2 for inserting before " << destinationElement << "\nSelection- ";
70 | cin >> a;
71 |
72 | myList = inserting(myList, destinationElement, insertingElement, a);
73 | }
74 |
75 | else if(selection==5) break;
76 |
77 | else cout << "#Invalid input!\n";
78 | }
79 | myList = destroy(myList);
80 | }
81 |
82 |
83 | list creatList() //creates the list
84 | {
85 | list myList; //declaring the list
86 | int intNumber, //the number of integers
87 | input; //the input that the user gives
88 |
89 | cin >> intNumber;
90 | for(int i=0; i> input;
93 | myList.push_back(input);
94 | }
95 | return myList;
96 | }
97 |
98 | void traversing(list myList) //prints the List
99 | {
100 | list:: iterator it; //declaring a iterator
101 |
102 | cout << endl;
103 | for(it=myList.begin(); it!=myList.end(); it++)
104 | cout << *it << " ";
105 | cout << endl << endl ;
106 | }
107 |
108 | int searching(list myList, int searchingElement)
109 | {
110 | int found=0; //number of the searching element
111 | list:: iterator it; //declaring iterator
112 |
113 | for(it=myList.begin(); it!=myList.end(); it++)
114 | if(searchingElement==*it) found++;
115 |
116 | return found;
117 | }
118 |
119 | list deleting(list myList, int deletingElement, int a)
120 | {
121 | int flag=1;
122 | if(a==1) myList.remove(deletingElement);
123 | else {
124 | list:: iterator it; //declaring iterator
125 | flag=0;
126 |
127 | for(it=myList.begin(); it!=myList.end(); it++){
128 | if(deletingElement==*it){
129 | it++;
130 | it=myList.erase(it);
131 | flag=1;
132 | if(it==myList.end()) break;
133 | }
134 | }
135 | }
136 |
137 | if(flag) cout << "\n* Deleted!\n";
138 | return myList;
139 | }
140 |
141 | list inserting(list myList, int destinationElement, int insertingElement, int a)
142 | {
143 | int flag=0;
144 | list:: iterator it; //declaring iterator
145 |
146 | for(it=myList.begin(); it!=myList.end(); it++){
147 | if(destinationElement==*it) {
148 | flag=1;
149 | break;
150 | }
151 | }
152 | if(a==1) it++;
153 | myList.insert(it, insertingElement);
154 |
155 | if(flag) cout << "\n* Inserted!!\n";
156 | else cout << "Destination not found.\n";
157 |
158 | return myList;
159 | }
160 |
161 | list destroy(list myList)
162 | {
163 | myList.clear();
164 | return myList;
165 | }
166 |
167 |
168 |
--------------------------------------------------------------------------------
/dataStructures/listImplementation/TwoWayLinkedList.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Developer: Minhas Kamal (BSSE-0509, IIT, DU)
3 | * Date: 13.Sep.2013
4 | **/
5 |
6 | #include
7 | #include
8 |
9 | using namespace std;
10 |
11 | list creatList(); //creates the list
12 | void traversing(list myList, int choice); //prints all the integers in the list
13 | int searching(list myList, int searchingElement); //searches for selected element
14 | list deleting(list myList, int deletingElement, int selection); //deleting selected element
15 | list insertingAtBeginning(list myList, int insertingElement); //inserting at beginning
16 | list insertingAtEnd(list myList, int insertingElement); //inserting at end
17 | list inserting(list myList, int destinationElement, int insertingElement, int a); //inserts certain element in definite place
18 | list destroy(list myList); //destroying the list
19 |
20 |
21 | int main()
22 | {
23 | list myList;
24 | myList = creatList();
25 |
26 | while(true)
27 | {
28 | int selection; //user action
29 | cout << "\n##What do you want to do? \npress- 1 for traversing\n"; //user prompt
30 | cout << " 2 for searching\n 3 for deleting\n 4 for inserting\n";
31 | cout << " 5 for exit\nSelection: ";
32 | cin >> selection;
33 |
34 |
35 | if(selection==1){
36 | int choice;
37 | cout << "How do you want to traverse?\npress- 1 for traversing first to last";
38 | cout << "\n 2 for traversing last to first\nSelection: ";
39 | cin >> choice;
40 | traversing(myList, choice);
41 | }
42 |
43 | else if(selection==2)
44 | {
45 | int searchingElement; //the element to be searched
46 | cout << "Enter the element you want to search for: ";
47 | cin >> searchingElement;
48 |
49 | cout << "\n";
50 | cout << "* " << searchingElement << " is found " << searching(myList, searchingElement) << " times.\n";
51 | }
52 |
53 | else if(selection==3)
54 | {
55 | int deletingElement; //the element to be deleted
56 | cout << "Enter the element: ";
57 | cin >> deletingElement;
58 |
59 | int sel=0; //determines the correct element
60 | cout << "Press 1 for deleting " << deletingElement;
61 | cout << "\n 2 for deleting the element after " << deletingElement;
62 | cout << "\n 3 for deleting the element before " << deletingElement << "\nSelection- ";
63 | cin >> sel;
64 |
65 | myList = deleting(myList, deletingElement, sel);
66 | }
67 |
68 | else if(selection==4)
69 | {
70 | int insertingElement, destinationElement, sel; //the element to be deleted
71 | cout << "Enter the element: ";
72 | cin >> insertingElement;
73 | cout << "Where do you want to insert? \nPress 1 for beginning";
74 | cout << "\n 2 for end\n 3 for chosing destination\nSelection: ";
75 | cin >> sel;
76 |
77 | if(sel==1) myList = insertingAtBeginning(myList, insertingElement);
78 | else if(sel==2) myList = insertingAtEnd(myList, insertingElement);
79 | else{
80 | cout << "Enter the destination element: ";
81 | cin >> destinationElement;
82 |
83 | int a=0; //determines the correct element
84 | cout << "Press 1 for inserting after " << destinationElement;
85 | cout << "\n 2 for inserting before " << destinationElement << "\nSelection- ";
86 | cin >> a;
87 |
88 | myList = inserting(myList, destinationElement, insertingElement, a);
89 | }
90 | }
91 |
92 | else if(selection==5) break;
93 |
94 | else cout << "#Invalid input!\n";
95 | }
96 | myList = destroy(myList);
97 | }
98 |
99 |
100 | list creatList() //creates the list
101 | {
102 | list myList; //declaring the list
103 | int intNumber, //the number of integers
104 | input; //the input that the user gives
105 |
106 | cin >> intNumber;
107 | for(int i=0; i> input;
110 | myList.push_back(input);
111 | }
112 | return myList;
113 | }
114 |
115 | void traversing(list myList, int choice) //prints the List
116 | {
117 | list:: iterator it; //declaring a iterator
118 |
119 | cout << endl;
120 | if(choice==1)
121 | for(it=myList.begin(); it!=myList.end(); it++)
122 | cout << *it << " ";
123 | else
124 | for(it=myList.end(); it!=myList.begin(); ){
125 | it--;
126 | cout << *it << " ";
127 | }
128 | cout << endl << endl ;
129 | }
130 |
131 | int searching(list myList, int searchingElement)
132 | {
133 | int found=0; //number of the searching element
134 | list:: iterator it, //declaring iterator
135 | old=myList.begin(); //this will store old position
136 |
137 | for(it=myList.begin(); it!=myList.end(); it++){
138 | if(searchingElement==*it){
139 | it++;
140 | if(old!=myList.begin() && it!=myList.end()){
141 | cout << "*between " << *old << " and " << *it << endl;
142 | }
143 | else if(old==myList.begin()) cout << "*at beginning\n";
144 | else cout << "*at end\n";
145 | it--;
146 | found++;
147 | }
148 | old=it;
149 | }
150 | return found;
151 | }
152 |
153 | list deleting(list myList, int deletingElement, int choice)
154 | {
155 | int flag=1;
156 | if(choice==1) myList.remove(deletingElement);
157 | else if(choice==2) {
158 | list:: iterator it; //declaring iterator
159 | flag=0;
160 |
161 | for(it=myList.begin(); it!=myList.end(); it++){
162 | if(deletingElement==*it){
163 | it++;
164 | it=myList.erase(it);
165 | flag=1;
166 | if(it==myList.end()) break;
167 | }
168 | }
169 | }
170 | else {
171 | list:: iterator it; //declaring iterator
172 | flag=0;
173 |
174 | for(it=myList.begin(); it!=myList.end(); it++){
175 | if(deletingElement==*it){
176 | it--;
177 | it=myList.erase(it);
178 | flag=1;
179 | if(it==myList.end()) break;
180 | }
181 | }
182 | }
183 |
184 | if(flag) cout << "\n* Deleted!\n";
185 | return myList;
186 | }
187 |
188 | list insertingAtBeginning(list myList, int insertingElement)
189 | {
190 | myList.insert(myList.begin(), insertingElement);
191 | cout << "\n* Inserted!!\n";
192 | return myList;
193 | }
194 |
195 | list insertingAtEnd(list myList, int insertingElement)
196 | {
197 | myList.insert(myList.end(), insertingElement);
198 | cout << "\n* Inserted!!\n";
199 | return myList;
200 | }
201 |
202 | list inserting(list myList, int destinationElement, int insertingElement, int a)
203 | {
204 | int flag=0;
205 | list:: iterator it; //declaring iterator
206 |
207 | for(it=myList.begin(); it!=myList.end(); it++){
208 | if(destinationElement==*it) {
209 | flag=1;
210 | break;
211 | }
212 | }
213 | if(a==1) it++;
214 | myList.insert(it, insertingElement);
215 |
216 | if(flag) cout << "\n* Inserted!!\n";
217 | else cout << "Destination not found.\n";
218 |
219 | return myList;
220 | }
221 |
222 | list destroy(list myList)
223 | {
224 | myList.clear();
225 | return myList;
226 | }
227 |
228 |
229 |
--------------------------------------------------------------------------------
/dataStructures/listImplementation/implementationUsingNode/ListImplementation_Input.txt:
--------------------------------------------------------------------------------
1 | 10
2 |
3 | 12
4 |
5 | 45
6 |
7 | 98
8 |
9 | 65
10 |
11 | 31
12 |
13 | 54
14 |
15 | 85
16 |
17 | 25
18 |
19 | 31
20 |
21 | 91
22 |
23 | 25
24 |
25 | 35
26 |
--------------------------------------------------------------------------------
/dataStructures/stack/StackImplementation.asm:
--------------------------------------------------------------------------------
1 | ##Writer: Minhas Kamal
2 | ##Date: 02-MAY-2014
3 | ##Function: This program is a simple demonstration of stack.
4 |
5 | #####**data**#####
6 | .data
7 |
8 | prompt: .asciiz "\n# What operation do you want to perform?\nPress-\t1.for push \n\t2.for pop \n\t3.for print all \n\t4.for exit \nSelection: "
9 | prompt2: .asciiz "Enter the value: "
10 | conf_pop: .asciiz "\nOperation pop is successful.\n"
11 | conf_push: .asciiz "Operation push is successful.\n"
12 | exit_msg: .asciiz "\n\n\nProgram exits...."
13 | comma_space: .asciiz ", "
14 |
15 | num: .double 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
16 | .double 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
17 | capacity: .word 40
18 | init: .double 0.0
19 |
20 | #####**text**#####
21 | .text
22 |
23 | main:
24 | lw $t1, capacity #load capacity
25 | l.d $f4, init #load the initial value
26 | li $t2, 0 #initial index is 0
27 |
28 | loop:
29 | la $a0, prompt #prompt
30 | li $v0, 4
31 | syscall
32 |
33 | li $v0, 5 #take choice from user
34 | syscall
35 |
36 | la $t0, num #load array
37 |
38 | beq $v0, 1, push
39 | beq $v0, 2, pop
40 | beq $v0, 3, print_all
41 | j exit
42 |
43 |
44 | #-------------------------------------------------------------------------------------------------------------------
45 | #pushes one number on top
46 | push:
47 | li $t3, 0
48 |
49 | beq $t2, $t1, loop #return when capacity is full
50 |
51 | push_loop: #find the address
52 | add $t0, $t0, 8
53 | add $t3, $t3, 1
54 | ble $t3, $t2, push_loop
55 | sub $t0, $t0, 8 #subtract once
56 |
57 | la $a0, prompt2 #prompt for input
58 | li $v0, 4
59 | syscall
60 |
61 | li $v0, 7 #take input
62 | syscall
63 |
64 | s.d $f0, ($t0) #store the value
65 |
66 | add $t2, $t2, 1 #increase index
67 |
68 | la $a0, conf_push #confirmation
69 | li $v0, 4
70 | syscall
71 |
72 | j loop #return
73 |
74 |
75 | #-------------------------------------------------------------------------------------------------------------------
76 | #pops up top number
77 | pop:
78 | li $t3, 0
79 |
80 | beq $t2, $t3, loop #return when nothing to pop
81 |
82 | pop_loop: #find the number
83 | add $t0, $t0, 8
84 | add $t3, $t3, 1
85 | blt $t3, $t2, pop_loop
86 | sub $t0, $t0, 8 #subtract once
87 |
88 |
89 | l.d $f12, ($t0) #print the number
90 | li $v0, 3
91 | syscall
92 |
93 | s.d $f4, ($t0) #initialize
94 | sub $t2, $t2, 1 #reduce index
95 |
96 | la $a0, conf_pop #confirmation
97 | li $v0, 4
98 | syscall
99 |
100 | j loop #return
101 |
102 | #-------------------------------------------------------------------------------------------------------------------
103 | #prints all the values
104 | print_all:
105 | li $t3, 0
106 | print_loop:
107 | l.d $f12, ($t0) #print the number
108 | li $v0, 3
109 | syscall
110 |
111 | la $a0, comma_space #seperate ", "
112 | li $v0, 4
113 | syscall
114 |
115 | add $t0, $t0, 8
116 | add $t3, $t3, 1 #increase the loop counter
117 | blt $t3, $t1, print_loop
118 | j loop
119 |
120 |
121 | #-------------------------------------------------------------------------------------------------------------------
122 | #exit from the program
123 | exit:
124 | la $a0, exit_msg #exit message
125 | li $v0, 4
126 | syscall
127 |
128 | li $v0, 10
129 | syscall
130 |
--------------------------------------------------------------------------------
/dataStructures/stack/StackImplementation.c:
--------------------------------------------------------------------------------
1 | /**
2 | *Name: Minhas Kamal
3 | *Program Name: StackImplementation
4 | *Date: 04.May.2013
5 | **/
6 |
7 | #include
8 |
9 | void stack_size(void); //working functions
10 | void stack_pop(void);
11 | void stack_push(void);
12 |
13 |
14 | char c[5]="*****"; //array
15 |
16 | int main()
17 | {
18 | for( ; ; )
19 | {
20 | int dis; //decision input
21 | printf("Enter your selection: \npress\n\t1.stack size\n\t2.stack pop\n\t3.stack push\n\t4.quit\n"); //selection
22 | scanf("%d", &dis);
23 | fflush(stdin);
24 |
25 | if(dis==1) stack_size(); //working according to selection & calling function
26 | else if(dis==2) stack_pop();
27 | else if(dis==3) stack_push();
28 | else break; //quit
29 | }
30 | return 0;
31 | }
32 |
33 | void stack_size(void) //shows size
34 | {
35 | int i;
36 | for(i=4; i>=0; i--) if(c[i]!='*') break;
37 | printf("\nThe size is: %d\n\n\n", i+1);
38 | }
39 |
40 | void stack_pop(void) //printing char
41 | {
42 | int i;
43 | for(i=4; i>=0; i--) if(c[i]!='*') break;
44 | if(i>=0)
45 | {
46 | printf("\nIt is: %c\n\n\n", c[i]);
47 | c[i]='*';
48 | }
49 | else printf("\nStack is empty!\n\n\n");
50 | }
51 |
52 | void stack_push(void) //assigns char
53 | {
54 | int i;
55 | if(c[4]!='*')
56 | {
57 | printf("\nStack is Full.\n\n\n");
58 | return ;
59 | }
60 | for(i=4; i>=0; i--) if(c[i]!='*') break;
61 |
62 | fflush(stdin);
63 | char ch;
64 | printf("Enter your input: ");
65 | scanf("%c", &ch);
66 |
67 | c[i+1]=ch;
68 | }
69 |
70 |
--------------------------------------------------------------------------------
/dataStructures/string/String.c:
--------------------------------------------------------------------------------
1 | /**
2 | * Name: Minhas Kamal (BSSE0509, IIT, DU)
3 | * Date: 26-Mar-2013
4 | **/
5 |
6 | #include
7 |
8 | void strlen(int s1);
9 | void strcat(int s1, int s2);
10 | void strcmp(int s1, int s2);
11 |
12 | char s[20][201];
13 |
14 | int main()
15 | {
16 |
17 | int x=20;
18 | //scanf("%d", &x);
19 | //printf("Enter the number of strings: ");
20 |
21 | //char s[x][201];
22 |
23 | int y;
24 | for(y=1; y<=x; y++)
25 | {
26 | printf("Enter string No-%d (not more than 200 chars): ", y);
27 | gets(s[y]);
28 | }
29 |
30 | for( ; ; )
31 | {
32 | int c;
33 | printf("Enter your choice:\npress\t1 to know the length of string\n\t\t");
34 | printf("2 to add two strings\n\t\t3 to compare two strings\n\t\t");
35 | printf("4 to quit.\n\t\t");
36 | scanf("%d", &c);
37 |
38 | if(c==1)
39 | {
40 | int s1;
41 | printf("Enter string number: ");
42 | scanf("%d", &s1);
43 | strlen(s1);
44 | }
45 | else if(c==2)
46 | {
47 | int s1;
48 | printf("Enter first string number: ");
49 | scanf("%d", &s1);
50 | int s2;
51 | printf("Enter second string number: ");
52 | scanf("%d", &s2);
53 | strcat(s1, s2);
54 | }
55 | else if(c==3)
56 | {
57 | int s1;
58 | printf("Enter first string number: ");
59 | scanf("%d", &s1);
60 | int s2;
61 | printf("Enter second string number: ");
62 | scanf("%d", &s2);
63 | strcmp(s1, s2);
64 | }
65 | else if(c==4) break;
66 | else if(c<1 || c>4) {printf("Wrong input!"); continue ;}
67 |
68 |
69 | }
70 |
71 | return 0;
72 | }
73 |
74 |
75 | void strlen(int s1)
76 | {
77 | int c1;
78 | for(c1=0; s[s1][c1]!='0'; c1++);
79 |
80 | printf("Number of chars: %d", c1);
81 |
82 | return ;
83 | }
84 |
85 |
86 | void strcat(int s1, int s2)
87 | {
88 | int c1=0;
89 | char ns[402];
90 |
91 | int y=0;
92 | for( ; s[s1][c1]!='0'; )
93 | {
94 | ns[y] = s[s1][c1];
95 | c1++;
96 | y++;
97 | }
98 |
99 | int z=y, x=0;
100 | for( ; s[s2][x]; )
101 | {
102 | ns[z] = s[s2][x];
103 | x++;
104 | z++;
105 | }
106 | ns[z]=0;
107 |
108 | int v=0;
109 | for( ; ns[v]; v++)printf("%c", ns[v]);
110 |
111 | return ;
112 | }
113 |
114 |
115 | void strcmp(int s1, int s2)
116 | {
117 | int f=1, c1=0, c2=0;
118 | for( ; s[s1][c1]!=0 && s[s2][c2]!=0; )
119 | {
120 | if(s[s1][c1] != s[s2][c2]) f=0;
121 |
122 | c1++;
123 | c2++;
124 | }
125 | if(s[s1][c1]==0 && s[s2][c2]!=0) f=-1;
126 | else if(s[s2][c2]==0 && s[s1][c1]!=0) f=2;
127 |
128 | if(f==1) printf("String matched.");
129 | else if(f==2) printf("The first one is bigger.");
130 | else if(f==-1) printf("The second one is bigger.");
131 | else if(f==0) printf("Size is same but doesn't match.");
132 |
133 | return ;
134 | }
135 |
--------------------------------------------------------------------------------
/dataStructures/string/StringLongestCommonSubSecuence.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Name: Minhas Kamal
3 | * Date: 23-Mar-2014
4 | **/
5 |
6 | #include
7 | #include
8 |
9 | using namespace std;
10 |
11 |
12 | void LCSFinder(char *firstString, char *secondString);
13 | int stringLength(char *string);
14 | void printLcs(char **direction, char *firstString, int i, int j);
15 |
16 |
17 | int main(){
18 | int length = 100;
19 | char firstString[length],
20 | secondString[length];
21 |
22 | cout << "Enter two strings: ";
23 | cin >> firstString >> secondString;
24 |
25 | cout << "Longest common sub-sequence is: ";
26 | LCSFinder(firstString, secondString);
27 | cout << "\n";
28 |
29 |
30 | return 0;
31 | }
32 |
33 |
34 | void LCSFinder(char *firstString, char *secondString){
35 | int firstStringLength = stringLength(firstString),
36 | secondStringLength = stringLength(secondString);
37 | //cout << firstStringLength << " " << secondStringLength;
38 |
39 | int index[firstStringLength+1][secondStringLength+1];
40 |
41 | char **direction;
42 | direction = new char* [firstStringLength];
43 | for(int i=0; i=index[i][j-1]){
60 | index[i][j]=index[i-1][j];
61 | direction[i-1][j-1]='|'; //up
62 | }
63 | else{
64 | index[i][j]=index[i][j-1];
65 | direction[i-1][j-1]='-'; //side
66 | }
67 | }
68 | }
69 |
70 | cout << "\nMatrix: \n";
71 | for(int i=0; i
7 | #include
8 | #include
9 | #include
10 |
11 | using namespace std;
12 |
13 | #define X 30
14 |
15 | int main(){
16 | int array[X+1];
17 | array[0]=0;
18 | for(int i=1; i=array[j]){
36 | if(position[j]>position[i]){
37 | position[i]=position[j];
38 | prevIndex[i]=j;
39 | }
40 | }
41 | }
42 | position[i]++;
43 | }
44 |
45 | ///traverse ///test
46 | /*for(int i=0; ilength){
55 | length=position[i];
56 | index=i;
57 | }
58 | }
59 |
60 | ///sub sequence
61 | stack subSequence;
62 | while(prevIndex[index]!=0){
63 | subSequence.push(array[index]);
64 | index = prevIndex[index];
65 | }
66 | subSequence.push(array[index]);
67 |
68 | while(!subSequence.empty()){
69 | cout << subSequence.top() << endl;
70 | subSequence.pop();
71 | }
72 |
73 | return 0;
74 | }
75 |
--------------------------------------------------------------------------------
/dataStructures/tree/BinomialTree.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Programmer: Minhas Kamal (BSSE0509,IIT,DU)
3 | * Date: 30-Mar-2014
4 | **/
5 |
6 | #include
7 | #include
8 | #include
9 |
10 | using namespace std;
11 |
12 | #define X 3
13 |
14 | struct node{
15 | int value;
16 | int depth;
17 | node *previous;
18 | node *next[X];
19 | } nullNode;
20 |
21 |
22 |
23 | int main(){
24 | ///input array
25 | int arrayLength=1;
26 | for(int i=0; idepth==binomialTree[newNode].depth)
64 | similarityFound=true;
65 | }
66 |
67 | if(similarityFound){
68 | if(binomialTree[j].value < binomialTree[newNode].value){
69 | binomialTree[j].next[binomialTree[j].depth]=&binomialTree[newNode];
70 | binomialTree[newNode].previous=&binomialTree[j];
71 | newNode=j;
72 | binomialTree[j].depth++;
73 | }
74 | else{
75 | binomialTree[newNode].next[binomialTree[newNode].depth]=&binomialTree[j];
76 | binomialTree[j].previous=&binomialTree[newNode];
77 | newNode=newNode;
78 | binomialTree[newNode].depth++;
79 | }
80 |
81 | nullNode.depth--;
82 | nullNode.next[nullNode.depth]=&binomialTree[newNode];
83 |
84 | }else{
85 | break;
86 | }
87 |
88 | }
89 | }
90 |
91 | ///traversing
92 | for(int i=0; i
8 | #include
9 |
10 | using namespace std;
11 |
12 | int main(int argc, char *argv[]){
13 | //**taking the file name
14 | string fileName="";
15 | if(argc>1){
16 | fileName=argv[1];
17 | }else{
18 | cout << "Enter the file name: ";
19 | cin >> fileName;
20 | //fileName="ASInput.txt";//test
21 | }
22 |
23 |
24 | //**opening the file
25 | ifstream input;
26 | input.open(fileName.c_str());
27 | if(!input.is_open()){ //when the file name is wrong
28 | printf("Sorry the file is not found!");
29 | input.close();
30 | return 1;
31 | }
32 |
33 |
34 | //**reading file
35 | int n;
36 | input >> n;
37 | int time[n][3]; ///colour, start position, stop position
38 |
39 | int i=0;
40 | for(; i> time[i][1] >> time[i][2];
43 | }
44 | input.close();
45 |
46 |
47 | /*////test
48 | int t;
49 | cout << endl;
50 | for(t=0; t touples = new LinkedList();
26 |
27 | Scanner scanner = new Scanner(System.in);
28 | System.out.println("Enter your touples(0 for exit): ");
29 |
30 | String[] tempStrings;
31 | tempStrings = scanner.nextLine().split(" ");
32 | while(!tempStrings[0].equals("0")){
33 | touples.add(new Touple(tempStrings));
34 | tempStrings = scanner.nextLine().split(" ");
35 | }
36 |
37 | return touples.toArray(new Touple[0]);
38 | }
39 |
40 | private Touple[] takeInput2(){
41 | LinkedList touples = new LinkedList();
42 |
43 | int[] i = new int[3];
44 |
45 | i[0]=2; i[1]=3; i[2]=9; touples.add(new Touple(i.clone()));
46 | i[0]=4; i[1]=9; i[2]=4; touples.add(new Touple(i.clone()));
47 | i[0]=3; i[1]=5; i[2]=0; touples.add(new Touple(i.clone()));
48 | i[0]=6; i[1]=8; i[2]=2; touples.add(new Touple(i.clone()));
49 | i[0]=8; i[1]=1; i[2]=1; touples.add(new Touple(i.clone()));
50 | i[0]=1; i[1]=4; i[2]=5; touples.add(new Touple(i.clone()));
51 | i[0]=6; i[1]=2; i[2]=8; touples.add(new Touple(i.clone()));
52 | i[0]=7; i[1]=3; i[2]=2; touples.add(new Touple(i.clone()));
53 |
54 | return touples.toArray(new Touple[0]);
55 | }
56 |
57 | private void traverse(Touple[] touples) {
58 | String str = "[" + touples[0].dupm();
59 | for(int i=1; i touples[j].values[elementNumber]){
71 | Touple temp = touples[i];
72 | touples[i] = touples[j];
73 | touples[j] = temp;
74 | }
75 | }
76 | }
77 | }
78 |
79 | private void constructBalanceTree(Touple[] touples, int startRange, int finishRange, int step) {
80 |
81 | partialSort(touples, startRange, finishRange, step%touples[0].values.length);
82 |
83 | int divider = (startRange+finishRange)/2;
84 | System.out.println(touples[divider].values[step%touples[0].values.length]);
85 | traverse(touples);
86 |
87 | if(finishRange-startRange>2){
88 | step++;
89 | constructBalanceTree(touples, startRange, divider, step);
90 | constructBalanceTree(touples, divider, finishRange, step);
91 | }
92 |
93 | return ;
94 | }
95 |
96 | public static void main(String[] args) {
97 | BalanceTree balanceTree = new BalanceTree();
98 | balanceTree.CreateBalanceTree();
99 | }
100 | }
101 |
--------------------------------------------------------------------------------
/dataStructures/tree/balanceTree/Touple.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Developer: Minhas Kamal(BSSE0509, IIT, DU)
3 | * Date: 09-Feb-2015
4 | **/
5 |
6 | package balanceTree;
7 |
8 | public class Touple {
9 | public int[] values;
10 |
11 | public Touple() {
12 | values = new int[3];
13 | for(int i=0; i<3; i++){
14 | values[i] = 0;
15 | }
16 | }
17 |
18 | public Touple(int[] values) {
19 | this.values = values;
20 | }
21 |
22 | public Touple(String[] values) {
23 | this.values = new int[values.length];
24 |
25 | for(int i=0; i> tree = new LinkedList<>();
14 |
15 | public static void main(String[] args) {
16 | //data = convertStringToInt(takeInput());
17 |
18 | data = takeInput2();
19 | traverse(data);
20 | System.out.println("\n##################\n");
21 |
22 | int dimention = 0;
23 | int level = 0;
24 | constructTree(data, 0, data.length, dimention, level);
25 |
26 | System.out.println("\n##################\n");
27 | traverse(data);
28 |
29 | printTree(tree);
30 | }
31 |
32 | private static String[][] takeInput(){
33 | LinkedList inputData = new LinkedList<>();
34 |
35 | @SuppressWarnings("resource")
36 | Scanner scanner = new Scanner(System.in);
37 | System.out.println("Enter your input (0 for exit): ");
38 | String[] inputStr = scanner.nextLine().split(" ");
39 | while(!inputStr[0].equals("0")){
40 | inputData.add(inputStr);
41 | inputStr = scanner.nextLine().split(" ");
42 | }
43 |
44 | String[][] stringData = new String[inputData.size()][];
45 | stringData = inputData.toArray(stringData);
46 |
47 | return stringData;
48 | }
49 |
50 | private static int[][] convertStringToInt(String[][] strdata){
51 | int firstDLen = strdata.length;
52 | int secondDLen = strdata[0].length;
53 |
54 | int[][] data = new int[firstDLen][secondDLen];
55 | for(int i=0; i data[j][dimention]){
93 | int[] temp = data[i];
94 | data[i] = data[j];
95 | data[j] = temp;
96 | }
97 | }
98 | }
99 |
100 | //if there is more than one member
101 | if(to-from > 1){
102 | traverse(data);//output
103 | if(tree.size() ints = new LinkedList();
105 | tree.add(ints);
106 | }
107 | tree.get(level).add(data[(from+to)/2][dimention]);
108 |
109 | level++;
110 | dimention++;
111 | if(dimention>=data[0].length){
112 | dimention=0;
113 | }
114 |
115 | //recursive call
116 | constructTree(data, from, (from+to)/2, dimention, level);
117 | constructTree(data, (from+to)/2, to, dimention, level);
118 | }
119 |
120 | return;
121 | }
122 |
123 | private static void printTree(LinkedList> tree){
124 | for(LinkedList elem: tree){
125 | for(int node: elem){
126 | System.out.println(node + "\t");
127 | }
128 | System.out.println();
129 | }
130 | }
131 | }
132 |
--------------------------------------------------------------------------------
/graph/breathFirstSearch/BreadthFirstSearch.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Developer: Minhas Kamal (BSSE-0509, IIT, DU)
3 | * Date: 12.Nov.2013
4 | **/
5 |
6 | #include
7 | #include
8 | #include
9 |
10 | using namespace std;
11 |
12 | void printPath(int* colour, int* distance, int* previous, int startVertex, int queryVertex);
13 |
14 | int main()//(int argc, char* argv[])
15 | {
16 | ifstream graph; //input file stream
17 | graph.open("BreadthFirstSearch_Input2.txt");
18 | //graph.open(argv[1]);
19 |
20 | if(!graph.is_open()){ //when file is not found
21 | cout << "File name is wrong!\n";
22 | return 0;
23 | }
24 |
25 | int vertexNumber, **matrix;
26 | graph >> vertexNumber;
27 | matrix = new int* [vertexNumber];
28 | for(int i=0; i> matrix[i][j];
34 |
35 |
36 |
37 | /*for(int i=0; i> startVertex;
58 | startVertex = startVertex-1;
59 |
60 | ///Start of BFS
61 | queue bFSQueue;
62 | bFSQueue.push(startVertex);
63 |
64 | colour[startVertex]=2; //colour is set to grey
65 | distance[startVertex]=0; //distance is zero
66 | previous[startVertex]=-2; //there is no previous
67 |
68 | while(!bFSQueue.empty()){ //finding adjacent vertexes and colouring them
69 | int i=bFSQueue.front();
70 | bFSQueue.pop();
71 | colour[i]=3;
72 |
73 | for(int j=0; j> queryVertex;
91 | queryVertex = queryVertex-1;
92 |
93 | cout << "\n\nThe Path is: \n";
94 | printPath(colour, distance, previous, startVertex, queryVertex);
95 |
96 |
97 | for(int i=0; i
7 | #include
8 | #include
9 |
10 | using namespace std;
11 |
12 | int main()//(int argc, char* argv[])
13 | {
14 | ifstream graph; //input file stream
15 | graph.open("DepthFirstSearch_Input.txt");
16 | //graph.open(argv[1]);
17 |
18 | if(!graph.is_open()){ //when file is not found
19 | cout << "File name is wrong!\n";
20 | return 0;
21 | }
22 |
23 | int vertexNumber, **matrix;
24 | graph >> vertexNumber;
25 | matrix = new int* [vertexNumber];
26 | for(int i=0; i> matrix[i][j];
32 |
33 |
34 |
35 | /*/for(int i=0; i> startVertex;
52 | startVertex = startVertex-1;
53 |
54 | ///start of DFS
55 | stack dFSStack;
56 | dFSStack.push(startVertex);
57 |
58 | colour[startVertex]=2; //colour is set to grey
59 |
60 | cout << "Reachable vertexes are: \n";
61 | while(!dFSStack.empty()){
62 | int i=dFSStack.top();
63 | dFSStack.pop();
64 |
65 | for(int j=0; j
8 | #include
9 | #include
10 |
11 | using namespace std;
12 |
13 |
14 | int FordFulkerson(int **matrix, int vertexNumber, int s, int t);
15 | bool BFS(int **residualGraph, int vertexNumber, int s, int t, int parent[]);
16 |
17 |
18 | int main(){
19 | ifstream graph; //input file stream
20 | graph.open("MaxFlow_Input.txt");
21 |
22 | if(!graph.is_open()){ //when file is not found
23 | cout << "File name is wrong!\n";
24 | return 0;
25 | }
26 |
27 | int vertexNumber, **matrix;
28 | graph >> vertexNumber;
29 | matrix = new int* [vertexNumber];
30 | for(int i=0; i> matrix[i][j];
36 |
37 | graph.close(); //close the file graph
38 |
39 | /*for(int i=0; i residualGraph[parent[v]][v]){
72 | pathFlow=residualGraph[parent[v]][v];
73 | }
74 | }
75 |
76 |
77 | for(int v=t; v!=s; v=parent[v]){
78 | residualGraph[parent[v]][v] = residualGraph[parent[v]][v] - pathFlow;
79 | residualGraph[v][parent[v]] = residualGraph[parent[v]][v] + pathFlow;
80 | }
81 |
82 | maxFlow = maxFlow+pathFlow;
83 | }
84 |
85 | return maxFlow;
86 | }
87 |
88 |
89 | //classic BFS
90 | bool BFS(int **residualGraph, int vertexNumber, int s, int t, int parent[]){
91 |
92 | bool visited[vertexNumber];
93 | for(int i=0; i vertexQueue;
98 |
99 | vertexQueue.push(s);
100 | parent[s]=-1;
101 | visited[s]=true;
102 |
103 | while(!vertexQueue.empty()){
104 | int v=vertexQueue.front();
105 | vertexQueue.pop();
106 |
107 | for(int i=0; i0){
109 | vertexQueue.push(i);
110 | parent[i]=v;
111 | visited[i]=true;
112 | }
113 | }
114 | }
115 |
116 | return visited[t];
117 | }
118 |
119 |
--------------------------------------------------------------------------------
/graph/maxFlow/MaxFlow_Input.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MinhasKamal/AlgorithmImplementations/29e45b8b19aaea9a1946fbdfa2da585c5f09b118/graph/maxFlow/MaxFlow_Input.png
--------------------------------------------------------------------------------
/graph/maxFlow/MaxFlow_Input.txt:
--------------------------------------------------------------------------------
1 | 6
2 | 0 12 5 0 8 0
3 | 0 0 2 0 0 7
4 | 0 0 0 14 0 0
5 | 0 0 0 0 0 15
6 | 0 0 0 3 0 0
7 | 0 0 0 0 0 0
--------------------------------------------------------------------------------
/graph/shortestPathAlgorithm/ShortestPathAlgorithm.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Developer: Minhas Kamal (BSSE-0509, IIT, DU)
3 | * Date: 13.Nov.2013
4 | **/
5 |
6 | #include
7 | #include
8 | #include
9 |
10 | using namespace std;
11 |
12 | void printPath(int* distance, int* previous, int startVertex, int queryVertex);
13 |
14 | int main(){
15 | ifstream graph; //input file stream
16 | graph.open("ShortestPathAlgorithm_Input.txt");
17 | //graph.open(argv[1]);
18 |
19 | if(!graph.is_open()){ //when file is not found
20 | cout << "File name is wrong!\n";
21 | return 0;
22 | }
23 |
24 | int vertexNumber, **matrix;
25 | graph >> vertexNumber;
26 | matrix = new int* [vertexNumber];
27 | for(int i=0; i> matrix[i][j];
33 |
34 |
35 |
36 | /*/for(int i=0; i> startVertex;
56 | startVertex = startVertex-1;
57 |
58 | ///Start of SPA
59 | queue SPAQueue;
60 | SPAQueue.push(startVertex);
61 |
62 | distance[startVertex]=0; //distance is zero
63 | previous[startVertex]=-2; //there is no previous
64 |
65 | while(!SPAQueue.empty()){ //finding adjacent vertexes and colouring them
66 | int i=SPAQueue.front();
67 | SPAQueue.pop();
68 |
69 | for(int j=0; jdistance[i]+matrix[i][j])){
71 | distance[j]=distance[i]+matrix[i][j];
72 | previous[j] =i;
73 | SPAQueue.push(j);
74 | }
75 | }
76 |
77 | /*/cout << endl;
78 | for(int i=0; i> queryVertex;
85 | queryVertex = queryVertex-1;
86 |
87 | cout << "\n\nThe distance is: " << distance[queryVertex];
88 |
89 | cout << "\nThe Path is: \n";
90 | printPath(distance, previous, startVertex, queryVertex);
91 |
92 |
93 | for(int i=0; i
7 | #include
8 | #include
9 |
10 | using namespace std;
11 |
12 | int main()//(int argc, char* argv[])
13 | {
14 | ifstream graph; //input file stream
15 | graph.open("TopologicalSort_Input.txt");
16 | //graph.open(argv[1]);
17 |
18 | if(!graph.is_open()){ //when file is not found
19 | cout << "File name is wrong!\n";
20 | return 0;
21 | }
22 |
23 | int vertexNumber, **matrix;
24 | graph >> vertexNumber;
25 | matrix = new int* [vertexNumber];
26 | for(int i=0; i> matrix[i][j];
32 |
33 |
34 |
35 | /*for(int i=0; i sortedList;
51 | for(int x=0; x dFSStack;
55 | dFSStack.push(startVertex);
56 | sortedList.push(startVertex);
57 |
58 | colour[startVertex]=2; //colour is set to grey
59 |
60 | while(!dFSStack.empty()){
61 | int i=dFSStack.top();
62 | dFSStack.pop();
63 |
64 | for(int j=0; j=0; i--)
94 | cout << "\t\t" << result[i]+1 << endl;
95 |
96 |
97 | for(int i=0; i
7 | #include
8 |
9 |
10 | int main(){
11 | int n=201;
12 |
13 | double x[n], fx[n], fx2[n];
14 |
15 | double a=0.0, b=2*M_PI;
16 | double h=(b-a)/n;
17 |
18 |
19 | int i;
20 | x[0]=a;
21 | fx[0]=sin(x[0]); //calculation of function
22 | for(i=1; i
7 | #include
8 |
9 |
10 | #define f(x) sin(x)
11 |
12 |
13 | int main(){
14 | int n=100;
15 | double a=0.0, b=M_PI;
16 | double h=(b-a)/n;
17 |
18 | double I=0;
19 | int i;
20 |
21 | I = I + 0.5*f(a);
22 | for(i=1; i
7 | #include
8 |
9 |
10 | #define f(x) sin(x)
11 |
12 |
13 | int main(){
14 | int n=100;
15 | double a=0.0, b=M_PI;
16 | double h=(b-a)/n;
17 |
18 | double I=0;
19 | int i;
20 |
21 | I = I + f(a);
22 | for(i=1; i
7 | #include
8 |
9 |
10 | #define f1(x) ((x)-5) * ((x)-5)
11 | #define f2(x) 0
12 | #define f(x) (f1(x))-(f2(x))
13 |
14 | int main(){
15 | int n=100;
16 | double a=-5, b=5;
17 | double h=(b-a)/n;
18 |
19 | double I=0;
20 | int i;
21 |
22 | I = I + 0.5*f(a);
23 | for(i=1; i
7 |
8 |
9 | void JacobisMethod(int n, double x[n], double b[n], double a[n][n]);
10 | void GaussSeidalMethod(int n, double x[n], double b[n], double a[n][n]);
11 | void print(int n, double x[n]);
12 |
13 |
14 | int main(){
15 | int n=3; //number of variables
16 |
17 | double x[n]; //variables
18 |
19 | double b[n], //constants
20 | a[n][n]; //arguments
21 |
22 | //assign values
23 | a[0][0]=8; a[0][1]=2; a[0][2]=-2; b[0]=8;
24 | a[1][0]=1; a[1][1]=-8; a[1][2]=3; b[1]=-4;
25 | a[2][0]=2; a[2][1]=1; a[2][2]=9; b[2]=12;
26 |
27 | int i;
28 |
29 | for(i=0; i -0.000001 && (Nx[i]-x[i])/x[i] < 0.000001 )){
64 | rootFound=0;
65 | break;
66 | }
67 | }
68 |
69 | for(i=0; i -0.000001 && (Nx[i]-x[i])/x[i] < 0.000001 )){
100 | rootFound=0;
101 | break;
102 | }
103 | }
104 |
105 | for(i=0; i
7 | #include
8 |
9 |
10 | /// Here define different functions to work with
11 | //1st function set
12 | #define f(x) ( ((x)*(x)*(x)) - (x) - 2 )
13 | #define f2(x) ( (3*(x)*(x)) - 1 )
14 | #define g(x) ( cbrt( (x) + 2 ) )
15 | //2nd function set
16 | //#define f(x) ( ((x)*(x)*(x)) - (4*(x)) - 8.95 )
17 | //#define f2(x) ( (3*(x)*(x)) - 4 )
18 | //#define g(x) ( cbrt( (4*(x)) + 8.95 ) )
19 |
20 |
21 | //functions using different methods in solving the same problem
22 | double BisectionMethod();
23 | double FalsePosition();
24 | double NewtonRaphson();
25 | double FixedPoint();
26 | double Secant();
27 |
28 |
29 | int main(){
30 | double root;
31 |
32 | root = BisectionMethod();
33 | printf("Using Bisection Method the root is: %lf \n\n", root);
34 |
35 | root = FalsePosition();
36 | printf("Using False Position Method the root is: %lf \n\n", root);
37 |
38 | root = NewtonRaphson();
39 | printf("Using Newton-Raphson Method the root is: %lf \n\n", root);
40 |
41 | root = FixedPoint();
42 | printf("Using Fixed Point Method the root is: %lf \n\n", root);
43 |
44 | root = Secant();
45 | printf("Using Secant Method the root is: %lf \n\n", root);
46 |
47 | return 0;
48 | }
49 |
50 | /**
51 | * takes two initial values and shortens the distance by both side
52 | **/
53 | double BisectionMethod(){
54 | double root=0;
55 |
56 | double a=1, b=2;
57 | double c=0;
58 |
59 | int loopCounter=0;
60 | if(f(a)*f(b) < 0){
61 | while(1){
62 | loopCounter++;
63 | c=(a+b)/2;
64 |
65 | if(f(c)<0.00001 && f(c)>-0.00001){
66 | root=c;
67 | break;
68 | }
69 |
70 | if((f(a))*(f(c)) < 0){
71 | b=c;
72 | }else{
73 | a=c;
74 | }
75 |
76 | }
77 | }
78 | printf("It took %d loops.\n", loopCounter);
79 |
80 | return root;
81 | }
82 |
83 | /**
84 | * takes two initial values and shortens the distance by single side
85 | **/
86 | double FalsePosition(){
87 | double root=0;
88 |
89 | double a=1, b=2;
90 | double c=0;
91 |
92 | int loopCounter=0;
93 | if(f(a)*f(b) < 0){
94 | while(1){
95 | loopCounter++;
96 |
97 | c=(a*f(b) - b*f(a)) / (f(b) - f(a));
98 |
99 | /*/printf("%lf\t %lf \n", c, f(c));/**////test
100 | if(f(c)<0.00001 && f(c)>-0.00001){
101 | root=c;
102 | break;
103 | }
104 |
105 | if((f(a))*(f(c)) < 0){
106 | b=c;
107 | }else{
108 | a=c;
109 | }
110 | }
111 | }
112 | printf("It took %d loops.\n", loopCounter);
113 |
114 | return root;
115 | }
116 |
117 | /**
118 | * uses one initial value and gradually takes that value near to the real one
119 | **/
120 | double NewtonRaphson(){
121 | double root=0;
122 |
123 | double x1=1;
124 | double x2=0;
125 |
126 | int loopCounter=0;
127 | while(1){
128 | loopCounter++;
129 |
130 | x2 = x1 - (f(x1)/f2(x1));
131 | /*/printf("%lf \t %lf \n", x2, f(x2));/**////test
132 |
133 | if(f(x2)<0.00001 && f(x2)>-0.00001){
134 | root=x2;
135 | break;
136 | }
137 |
138 | x1=x2;
139 | }
140 | printf("It took %d loops.\n", loopCounter);
141 |
142 | return root;
143 | }
144 |
145 | /**
146 | * uses one initial value and gradually takes that value near to the real one
147 | **/
148 | double FixedPoint(){
149 | double root=0;
150 | double x=1;
151 |
152 | int loopCounter=0;
153 | while(1){
154 | loopCounter++;
155 |
156 | if( (x-g(x)) <0.00001 && (x-g(x)) >-0.00001){
157 | root = x;
158 | break;
159 | }
160 |
161 | /*/printf("%lf \t %lf \n", g(x), x-(g(x)));/**////test
162 |
163 | x=g(x);
164 | }
165 | printf("It took %d loops.\n", loopCounter);
166 |
167 | return root;
168 | }
169 |
170 | /**
171 | * uses two initial values & both value approaches to the root
172 | **/
173 | double Secant(){
174 | double root=0;
175 |
176 | double x0=1;
177 | double x1=2;
178 | double x2=0;
179 |
180 | int loopCounter=0;
181 | while(1){
182 | loopCounter++;
183 |
184 | /*/printf("%lf \t %lf \t %lf \n", x0, x1, f(x1));/**////test
185 |
186 | if(f(x1)<0.00001 && f(x1)>-0.00001){
187 | root=x1;
188 | break;
189 | }
190 |
191 | x2 = ((x0*f(x1))-(x1*f(x0))) / (f(x1)-f(x0));
192 |
193 | x0=x1;
194 | x1=x2;
195 | }
196 | printf("It took %d loops.\n", loopCounter);
197 |
198 | return root;
199 | }
200 |
201 |
--------------------------------------------------------------------------------
/others/cigaretteSmokersProblem/Agent.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Name: Minhas Kamal (BSSE-0509, IIT, DU)
3 | * Date: 04-Dec-2014
4 | **/
5 |
6 | package cigaretteSmokersProblem;
7 |
8 | import java.util.LinkedList;
9 | import java.util.Queue;
10 | import java.util.Random;
11 |
12 | public class Agent {
13 | private Smoker[] smokers;
14 |
15 | public Agent(Smoker[] smokers) {
16 | this.smokers = smokers;
17 | }
18 |
19 | public Queue start(int time){
20 | Queue symbols = new LinkedList();
21 |
22 | int i=0;
23 | while(i