├── .gitignore ├── Year 1 Sem 2 ├── Graph │ ├── BFS │ │ ├── bfs.out │ │ ├── bfs.in │ │ └── main.cpp │ ├── DFS │ │ ├── dfs.out │ │ ├── dfs.in │ │ └── main.cpp │ ├── dijkstra │ │ ├── dijkstra.out │ │ ├── dijkstra.in │ │ └── main.cpp │ ├── DisiojntSetDataStructure │ │ ├── disjoint.out │ │ ├── disjoint.in │ │ └── main.cpp │ ├── .DS_Store │ ├── TopologicalSort │ │ └── ToplogicalSort.cpp │ ├── RoyFloyd │ │ └── royfloyd.cpp │ └── MST │ │ ├── Prim.cpp │ │ └── Kruskal.cpp ├── pooExamQuestions │ ├── probleme │ │ ├── 78.cpp │ │ ├── 95.cpp │ │ ├── .htaccess │ │ ├── 24.cpp │ │ ├── 49.cpp │ │ ├── 63.cpp │ │ ├── 91.cpp │ │ ├── 76.cpp │ │ ├── 83.cpp │ │ ├── 112.cpp │ │ ├── 45.cpp │ │ ├── 102.cpp │ │ ├── 90.cpp │ │ ├── 10.cpp │ │ ├── 100.cpp │ │ ├── 34.cpp │ │ ├── 48.cpp │ │ ├── 74.cpp │ │ ├── 64.cpp │ │ ├── 60.cpp │ │ ├── 77.cpp │ │ ├── 105.cpp │ │ ├── 86.cpp │ │ ├── 99.cpp │ │ ├── 53.cpp │ │ ├── 108.cpp │ │ ├── 40.cpp │ │ ├── 18.cpp │ │ ├── 109.cpp │ │ ├── 70.cpp │ │ ├── 110.cpp │ │ ├── 72.cpp │ │ ├── 106.cpp │ │ ├── 85.cpp │ │ ├── 103.cpp │ │ ├── 25.cpp │ │ ├── 59.cpp │ │ ├── 67.cpp │ │ ├── 16.cpp │ │ ├── 26.cpp │ │ ├── 46.cpp │ │ ├── 38.cpp │ │ ├── 94.cpp │ │ ├── 80.cpp │ │ ├── 2.cpp │ │ ├── 37.cpp │ │ ├── 3.cpp │ │ ├── 42.cpp │ │ ├── 51.cpp │ │ ├── 81.cpp │ │ ├── 111.cpp │ │ ├── 11.cpp │ │ ├── 35.cpp │ │ ├── 65.cpp │ │ ├── 73.cpp │ │ ├── 4.cpp │ │ ├── 44.cpp │ │ ├── 96.cpp │ │ ├── 47.cpp │ │ ├── 56.cpp │ │ ├── 7.cpp │ │ ├── 97.cpp │ │ ├── 17.cpp │ │ ├── 28.cpp │ │ ├── 57.cpp │ │ ├── 58.cpp │ │ ├── 30.cpp │ │ ├── 31.cpp │ │ ├── 6.cpp │ │ ├── 41.cpp │ │ ├── 89.cpp │ │ ├── 12.cpp │ │ ├── 15.cpp │ │ ├── 82.cpp │ │ ├── 36.cpp │ │ ├── 71.cpp │ │ ├── 27.cpp │ │ ├── 88.cpp │ │ ├── 8.cpp │ │ ├── 32.cpp │ │ ├── 93.cpp │ │ ├── 61.cpp │ │ ├── 62.cpp │ │ ├── 19.cpp │ │ ├── 68.cpp │ │ ├── 75.cpp │ │ ├── 107.cpp │ │ ├── 22.cpp │ │ ├── 52.cpp │ │ ├── 43.cpp │ │ ├── 14.cpp │ │ ├── 84.cpp │ │ ├── 33.cpp │ │ ├── 9.cpp │ │ ├── 104.cpp │ │ ├── 101.cpp │ │ ├── 92.cpp │ │ ├── 39.cpp │ │ ├── 21.cpp │ │ ├── 1.cpp │ │ ├── 87.cpp │ │ ├── 50.cpp │ │ ├── 23.cpp │ │ ├── 79.cpp │ │ ├── 29.cpp │ │ ├── 98.cpp │ │ ├── 13.cpp │ │ ├── 5.cpp │ │ └── 20.cpp │ └── index.php └── .DS_Store ├── Year 1 Sem 1 ├── Stack │ ├── ap │ │ ├── text.txt │ │ └── ap.cpp │ ├── check_if_correct_brackets │ │ ├── brackets.txt │ │ └── brackets.cpp │ ├── pillars │ │ ├── pillars.png │ │ ├── stack.h │ │ ├── pillars.cpp │ │ └── stack.cpp │ ├── README.md │ └── Stack-Implementation │ │ └── Stack.cpp ├── DFS │ ├── adjlist.txt │ ├── queue.h │ ├── queue.cpp │ └── DFS.cpp ├── Permutari │ ├── permutare.txt │ ├── Binaries_exe │ │ ├── permutare.txt │ │ └── Permutari.exe │ └── permutari.cpp ├── OddEvenIndexList │ ├── Readme.md │ └── OddEvenIndexList.cpp ├── Queue │ ├── README.md │ ├── Lee using queue │ │ ├── matrix.txt │ │ ├── queue.h │ │ ├── queue.cpp │ │ └── Source.cpp │ └── Queue-Implementation │ │ └── Queue.cpp ├── FlipList │ ├── README.md │ └── FlipList.cpp ├── BInaryFileC │ ├── binar │ └── program.c ├── PolynomialCPP │ ├── README.md │ └── Polynomial.cpp ├── Adjacency_matrix │ ├── graph.png │ ├── adjmatrix.txt │ └── Adjacency_matrix.cpp ├── CExamenPrep │ ├── lab │ │ ├── movies.txt │ │ ├── matricedinamica.c │ │ ├── qsorttest.c │ │ └── movieDB.c │ └── exam │ │ ├── string.c │ │ ├── vaarg.c │ │ ├── qsort.c │ │ ├── proggenerica.c │ │ ├── prg.c │ │ └── proggenericainlocuire.c ├── BinarySearchTree │ ├── implementation │ │ ├── tree.png │ │ └── BinarySearchTree.cpp │ ├── Numbers_in_interval │ │ ├── interval.cpp │ │ ├── BST.h │ │ └── BST.cpp │ ├── SortWithBTS │ │ ├── BST.h │ │ ├── BST.cpp │ │ └── BST - Copy.h │ └── to_include │ │ ├── BST.h │ │ └── BST.cpp ├── SparseArray │ ├── README.md │ ├── array.txt │ └── SparseArray.cpp ├── randomQuickSort │ └── Rquicksort.cpp ├── MergeSort │ └── mergesort.cpp ├── BinaryTree │ └── Binarytree.cpp ├── HeapSort │ └── heapsort.cpp ├── Circular List │ └── CircularList.cpp ├── deque │ └── deque.cpp ├── PriorityQueue │ └── PriorityQueue.cpp ├── PriorityQueueHeap │ └── pqueueHeap.cpp └── Doubly Linked List │ └── DLL.cpp ├── .DS_Store ├── Year 2 Sem 1 ├── so │ ├── lab4 │ │ ├── a.out │ │ ├── prob1.c │ │ ├── prob2.c │ │ └── prob3.c │ ├── lab5 │ │ ├── a.out │ │ └── sharedmemory.c │ ├── lab6 │ │ ├── a.out │ │ └── matrix.c │ ├── lab7 │ │ ├── a.out │ │ ├── barrier.c │ │ └── mutex.c │ └── lab2 │ │ ├── prob1.c │ │ └── prob2.c ├── PS │ ├── StatisticsEx4.ipynb │ └── StatisticsEx1.ipynb ├── BinaryTree.cpp └── RBtree.cpp ├── README.md └── Year 2 Sem 2 └── Prolog ├── lab2.swinb └── lab1.swinb /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | .DS_Store 3 | -------------------------------------------------------------------------------- /Year 1 Sem 2/Graph/BFS/bfs.out: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Year 1 Sem 2/Graph/DFS/dfs.out: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Year 1 Sem 1/Stack/ap/text.txt: -------------------------------------------------------------------------------- 1 | aabbbbaa -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/78.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/95.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Year 1 Sem 2/Graph/dijkstra/dijkstra.out: -------------------------------------------------------------------------------- 1 | 1 3 2 5 10 5 -------------------------------------------------------------------------------- /Year 1 Sem 1/DFS/adjlist.txt: -------------------------------------------------------------------------------- 1 | 6 3 2 | 1 2 3 | 1 4 4 | 3 5 -------------------------------------------------------------------------------- /Year 1 Sem 1/Permutari/permutare.txt: -------------------------------------------------------------------------------- 1 | 5 1 3 8 4 11 10 7 6 2 9 -------------------------------------------------------------------------------- /Year 1 Sem 2/Graph/DFS/dfs.in: -------------------------------------------------------------------------------- 1 | 6 3 2 | 1 2 3 | 1 4 4 | 3 5 5 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/.htaccess: -------------------------------------------------------------------------------- 1 | deny from all -------------------------------------------------------------------------------- /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mihainsto/FMI/HEAD/.DS_Store -------------------------------------------------------------------------------- /Year 1 Sem 1/OddEvenIndexList/Readme.md: -------------------------------------------------------------------------------- 1 | using Singly Linked List 2 | -------------------------------------------------------------------------------- /Year 1 Sem 1/Stack/check_if_correct_brackets/brackets.txt: -------------------------------------------------------------------------------- 1 | (())(())( -------------------------------------------------------------------------------- /Year 1 Sem 1/Permutari/Binaries_exe/permutare.txt: -------------------------------------------------------------------------------- 1 | 4 5 6 7 8 9 10 11 12 13 2 3 1 -------------------------------------------------------------------------------- /Year 1 Sem 2/Graph/DisiojntSetDataStructure/disjoint.out: -------------------------------------------------------------------------------- 1 | NU 2 | DA 3 | DA 4 | -------------------------------------------------------------------------------- /Year 1 Sem 1/Queue/README.md: -------------------------------------------------------------------------------- 1 | Queue implementation 2 | using Singly Linked List 3 | 4 | -------------------------------------------------------------------------------- /Year 1 Sem 2/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mihainsto/FMI/HEAD/Year 1 Sem 2/.DS_Store -------------------------------------------------------------------------------- /Year 1 Sem 2/Graph/BFS/bfs.in: -------------------------------------------------------------------------------- 1 | 5 7 2 2 | 1 2 3 | 2 1 4 | 2 2 5 | 3 2 6 | 2 5 7 | 5 3 8 | 4 5 9 | -------------------------------------------------------------------------------- /Year 1 Sem 1/FlipList/README.md: -------------------------------------------------------------------------------- 1 | #FlipList 2 | Flip a list - 2 methods 3 | using Singly Linked List 4 | -------------------------------------------------------------------------------- /Year 2 Sem 1/so/lab4/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mihainsto/FMI/HEAD/Year 2 Sem 1/so/lab4/a.out -------------------------------------------------------------------------------- /Year 2 Sem 1/so/lab5/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mihainsto/FMI/HEAD/Year 2 Sem 1/so/lab5/a.out -------------------------------------------------------------------------------- /Year 2 Sem 1/so/lab6/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mihainsto/FMI/HEAD/Year 2 Sem 1/so/lab6/a.out -------------------------------------------------------------------------------- /Year 2 Sem 1/so/lab7/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mihainsto/FMI/HEAD/Year 2 Sem 1/so/lab7/a.out -------------------------------------------------------------------------------- /Year 1 Sem 2/Graph/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mihainsto/FMI/HEAD/Year 1 Sem 2/Graph/.DS_Store -------------------------------------------------------------------------------- /Year 1 Sem 1/BInaryFileC/binar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mihainsto/FMI/HEAD/Year 1 Sem 1/BInaryFileC/binar -------------------------------------------------------------------------------- /Year 1 Sem 1/PolynomialCPP/README.md: -------------------------------------------------------------------------------- 1 | #Polynomial 2 | Operations with polynoms 3 | using Singly Linked List 4 | -------------------------------------------------------------------------------- /Year 1 Sem 2/Graph/DisiojntSetDataStructure/disjoint.in: -------------------------------------------------------------------------------- 1 | 4 6 2 | 1 1 2 3 | 1 3 4 4 | 2 1 3 5 | 2 1 2 6 | 1 1 3 7 | 2 1 4 8 | -------------------------------------------------------------------------------- /Year 1 Sem 1/Adjacency_matrix/graph.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mihainsto/FMI/HEAD/Year 1 Sem 1/Adjacency_matrix/graph.png -------------------------------------------------------------------------------- /Year 1 Sem 1/Stack/pillars/pillars.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mihainsto/FMI/HEAD/Year 1 Sem 1/Stack/pillars/pillars.png -------------------------------------------------------------------------------- /Year 1 Sem 1/CExamenPrep/lab/movies.txt: -------------------------------------------------------------------------------- 1 | Aquaman 2018 8.2 2 32 2 | Dunkirk 2017 7.9 1 46 3 | Logan 2017 8.4 2 17 4 | Venom 2018 6.9 1 52 -------------------------------------------------------------------------------- /Year 1 Sem 1/Adjacency_matrix/adjmatrix.txt: -------------------------------------------------------------------------------- 1 | 6 2 | 0 1 0 0 1 0 3 | 1 0 1 0 1 0 4 | 0 1 0 1 0 0 5 | 0 0 1 0 1 1 6 | 1 1 0 1 0 0 7 | 0 0 0 1 0 0 -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/24.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mihainsto/FMI/HEAD/Year 1 Sem 2/pooExamQuestions/probleme/24.cpp -------------------------------------------------------------------------------- /Year 1 Sem 1/Permutari/Binaries_exe/Permutari.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mihainsto/FMI/HEAD/Year 1 Sem 1/Permutari/Binaries_exe/Permutari.exe -------------------------------------------------------------------------------- /Year 1 Sem 2/Graph/dijkstra/dijkstra.in: -------------------------------------------------------------------------------- 1 | 7 9 2 | 1 4 2 3 | 1 2 1 4 | 2 3 3 5 | 4 3 1 6 | 3 5 2 7 | 5 6 5 8 | 6 3 1 9 | 3 7 2 10 | 1 5 9 11 | -------------------------------------------------------------------------------- /Year 1 Sem 1/BinarySearchTree/implementation/tree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mihainsto/FMI/HEAD/Year 1 Sem 1/BinarySearchTree/implementation/tree.png -------------------------------------------------------------------------------- /Year 1 Sem 1/SparseArray/README.md: -------------------------------------------------------------------------------- 1 | add and multiply two sparse arrays 2 | using Singly Linked List 3 | 4 | https://en.wikipedia.org/wiki/Sparse_matrix 5 | -------------------------------------------------------------------------------- /Year 1 Sem 1/Queue/Lee using queue/matrix.txt: -------------------------------------------------------------------------------- 1 | 7 2 | 0 0 1 0 0 0 0 3 | 0 0 1 1 0 0 0 4 | 0 0 0 0 1 0 0 5 | 0 0 0 1 1 0 0 6 | 0 1 0 0 1 0 1 7 | 1 1 1 0 0 0 1 8 | 1 1 1 0 0 1 1 -------------------------------------------------------------------------------- /Year 2 Sem 1/so/lab2/prob1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | const char *message = "Hello World! \n"; 6 | write(1 , message, strlen(message)); 7 | return 0; 8 | } -------------------------------------------------------------------------------- /Year 1 Sem 1/SparseArray/array.txt: -------------------------------------------------------------------------------- 1 | 40 2 | 4 8 0 0 0 0 0 0 0 0 0 0 9 0 0 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 5 6 0 0 0 0 0 0 0 4 3 | 42 4 | 9 4 0 0 0 0 0 0 0 0 0 0 7 0 0 0 0 2 0 0 0 0 0 0 5 0 0 0 0 0 9 1 0 0 0 0 0 0 0 2 9 3 -------------------------------------------------------------------------------- /Year 1 Sem 1/Stack/README.md: -------------------------------------------------------------------------------- 1 | Stack implementation 2 | using Singly Linked List 3 | 4 | ap.cpp - count if the character 'a' and 'b' number of apparitions are the same. 5 | check_if_correct_brackets - check if brackets are positioned correctly using stack -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/49.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | class cls 3 | { int x; 4 | public: cls(int y) {x=y; } 5 | friend int operator*(cls a,cls b){return (a.x*b.x); } }; 6 | int main() 7 | { cls m(100),n(15); 8 | cout << m*n; 9 | return 0; } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # FMI 2 | All my coding as a bachelor student of Computer science at Univeristy of Bucharest 3 | 4 | Python repo: https://github.com/mihainsto/FMI.py 5 | OOP repo: https://github.com/mihainsto/FMIoop 6 | CopyService: https://github.com/mihainsto/CopyService 7 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/63.cpp: -------------------------------------------------------------------------------- 1 | include 2 | 3 | class cls { 4 | public: 5 | int x; 6 | cls() { x = 3; } 7 | void f(cls& c) { cout << c.x; } 8 | }; 9 | 10 | void main() 11 | { 12 | const cls d; 13 | f(d); 14 | } 15 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/91.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class A 5 | { 6 | int x; 7 | public: 8 | A(int i = 25) { x = i; } 9 | int& f() const { return x; } 10 | }; 11 | int main() 12 | { 13 | A ob(5); 14 | cout << ob.f(); 15 | return 0; 16 | } -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/76.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | class cls { 3 | int x; 4 | 5 | public: 6 | cls(int i = 25) { x = i; } 7 | int f(); 8 | }; 9 | int cls::f() { return x; } 10 | int main() 11 | { 12 | const cls d(15); 13 | cout << d.f(); 14 | return 0; 15 | } -------------------------------------------------------------------------------- /Year 1 Sem 1/DFS/queue.h: -------------------------------------------------------------------------------- 1 | struct queuenode 2 | { 3 | int val; 4 | queuenode *next; 5 | }; 6 | struct queue 7 | { 8 | queuenode *first_q; 9 | queuenode *last_q; 10 | }; 11 | 12 | void queue_init(queue q); 13 | int queue_pop(queue &q); 14 | void queue_push(int x, queue &q); 15 | bool queue_empty(queue q); -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/83.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | class cls { 3 | int x; 4 | 5 | public: 6 | cls(int i = -20) { x = i; } 7 | const int& f() { return x; } 8 | }; 9 | int main() 10 | { 11 | cls a(14); 12 | int b = a.f()++; 13 | cout << b; 14 | return 0; 15 | } -------------------------------------------------------------------------------- /Year 1 Sem 1/Stack/pillars/stack.h: -------------------------------------------------------------------------------- 1 | #include 2 | struct stack 3 | { 4 | int val; 5 | stack *next; 6 | }; 7 | void stack_push(int a, stack *&st); 8 | int stack_pop(stack *&st); 9 | int stack_peek(stack *st); 10 | bool stack_empty(stack *st); 11 | void print(stack *st); 12 | int search(int a, stack *st); -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/112.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | class cls { 3 | int x; 4 | 5 | public: 6 | cls(int y) { x = y; } 7 | int operator*(cls a, cls b) { return (a.x * b.x); } 8 | }; 9 | int main() 10 | { 11 | cls m(100), n(15); 12 | cout << m * n; 13 | return 0; 14 | } -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/45.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class cls 4 | { 5 | int x; 6 | public: cls(int i=3) {x=i;} 7 | int &f() const{ return x;} 8 | }; 9 | int main() 10 | { 11 | const cls a(-3); 12 | int b=a.f(); 13 | cout< 2 | using namespace std; 3 | template 4 | int functie(X x, X y) 5 | { return x+y; 6 | } 7 | int functie(int & x, int *y) 8 | { return x-*y; 9 | } 10 | int main() 11 | { int a=7, *b=new int(4); 12 | cout< 2 | template 3 | int f(T x, T y) 4 | { 5 | return x + y; 6 | } 7 | int f(int x, int y) 8 | { 9 | return x - y; 10 | } 11 | int main() 12 | { 13 | int a = 5; 14 | float b = 8.6; 15 | cout << f(a, b); 16 | return 0; 17 | } -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/10.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | template 3 | T f(T x, U y) 4 | { 5 | return x + y; 6 | } 7 | int f(int x, int y) 8 | { 9 | return x - y; 10 | } 11 | int main() 12 | { 13 | int *a = new int(3), b(23); 14 | cout << *f(a, b); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/100.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class A 4 | { int x; 5 | static int y; 6 | public: A(int i,int j):x(i),y(j){} 7 | int f() const;}; 8 | int A::y; 9 | int A::f() const {return y;} 10 | int main() 11 | { const A a(21,2); 12 | cout< 2 | template 3 | T f(T x, U y) 4 | { 5 | return x + y; 6 | } 7 | int f(int x, int y) 8 | { 9 | return x - y; 10 | } 11 | int main() 12 | { 13 | int *a = new int(3), b(23); 14 | cout << *f(a, b); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/48.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class cls1 4 | { protected: int x; 5 | public: cls1(int i=10) { x=i; } 6 | int get_x() { return x;} }; 7 | class cls2: cls1 8 | { public: cls2(int i):cls1(i) {} }; int main() 9 | { cls2 d(37); 10 | cout< 2 | class cls { 3 | int vi; 4 | 5 | public: 6 | cls(int v = 37) { vi = v; } 7 | friend int& f(cls); 8 | }; 9 | int& f(cls c) { return c.vi; } 10 | int main() 11 | { 12 | const cls d(15); 13 | f(d) = 8; 14 | cout << f(d); 15 | return 0; 16 | } -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/64.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class cls { 4 | public: 5 | int x, y; 6 | cls(int i = 0, int j = 0) 7 | { 8 | x = i; 9 | y = j; 10 | } 11 | }; 12 | 13 | void main() 14 | { 15 | cls a, b, c[3] = { cls(1, 1), cls(2, 2), a }; 16 | cout << c[2].x; 17 | } 18 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/60.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class cls { 4 | public: 5 | float sa; 6 | cls(float s = 0) { sa = s; } 7 | operator float() { return sa; } 8 | float f(float c) { return (sa * (1 + c / 100)); } 9 | }; 10 | 11 | void main() 12 | { 13 | cls p(100); 14 | cout << p.f(p); 15 | } 16 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/77.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | template 3 | tip dif(tip x, tip y) 4 | { 5 | return x - y; 6 | } 7 | unsigned dif(unsigned x, unsigned y) 8 | { 9 | return x >= y ? x - y : y - x; 10 | } 11 | int main() 12 | { 13 | unsigned i = 7, j = 8; 14 | cout << dif(i, j); 15 | return 0; 16 | } -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/105.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class A 4 | { static int *x; 5 | public: A() {} 6 | int get_x() { return (++(*x))++; } }; 7 | int *A::x(new int(19)); 8 | int main() 9 | { A *p=new A,b; 10 | cout<get_x(); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/86.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | class cls { 3 | static int x; 4 | 5 | public: 6 | cls(int i = 1) { x = i; } 7 | cls f(cls a) { return x + a.x; } 8 | static int g() { return f() / 2; } 9 | }; 10 | int cls::x = 7; 11 | int main() 12 | { 13 | cls ob; 14 | cout << cls::g(); 15 | return 0; 16 | } -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/99.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class A 4 | { protected: static int x; 5 | private: int y; 6 | public: A(int i) { x=i; y=-i+4; } 7 | int put_x(A a) { return a.x+a.y; } }; 8 | int A::x=7; 9 | int main() 10 | { A a(10); 11 | cout< 2 | class cls 3 | { int x; 4 | public: cls(int i) { x=i; } 5 | int set_x(int i) { int y=x; x=i; return y; } 6 | int get_x(){ return x; } }; 7 | int main() 8 | { cls *p=new cls[10]; 9 | int i=0; 10 | for(;i<10;i++) p[i].set_x(i); 11 | for(i=0;i<10;i++) cout< 2 | using namespace std; 3 | class cls 4 | { int x; 5 | public: cls(int i=2) { x=i; } 6 | int set_x(int i) { int y=x; x=i; return y; } 7 | int get_x(){ return x; } }; 8 | int main() 9 | { cls *p=new cls[15]; 10 | for(int i=2;i<8;i++) p[i].set_x(i); 11 | for(int i=1;i<6;i++) cout< 2 | using namespace std; 3 | class A 4 | { 5 | int valoare; 6 | public: A(int param1=3):valoare(param1){} 7 | int getValoare(){return this->valoare;} 8 | }; 9 | int main() 10 | { 11 | A vector[]={*(new A(3)),*(new A(4)),*(new A(5)),*(new A(6)) }; 12 | cout< 2 | using namespace std; 3 | template 4 | T fun(T x, U y) 5 | { 6 | return x + y; 7 | } 8 | int fun(int x, int y) 9 | { 10 | return x - y; 11 | } 12 | int fun(int x) 13 | { 14 | return x + 1; 15 | } 16 | int main() 17 | { 18 | int *a = new int(10), b(5); 19 | cout << fun(a, b); 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/109.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct X { int i; 4 | public: X(int ii ) { i = ii; }; 5 | int f1() { return i; } 6 | X f2() const { int i=this->f1(); return X(34-i); }}; 7 | const X f3() { return X(16); } 8 | int f4(const X& x) { return x.f1(); } 9 | int main() {X ob(11); 10 | cout< 2 | class cls1 { 3 | protected: 4 | int x; 5 | 6 | public: 7 | cls1() { x = 13; } 8 | }; 9 | class cls2 : public cls1 { 10 | int y; 11 | 12 | public: 13 | cls2() { y = 15; } 14 | int f(cls2 ob) { return (ob.x + ob.y); } 15 | }; 16 | int main() 17 | { 18 | cls2 ob; 19 | cout << ob.f(ob); 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/110.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class A 4 | { protected: static int x; 5 | public: A(int i=1) {x=i; } 6 | int get_x() { return x; } 7 | int& set_x(int i) { int y=x; x=i; return y;} 8 | A operator=(A a1) { set_x(a1.get_x()); return a1;} 9 | } a(33); 10 | int main() 11 | { A a(18), b(7); 12 | cout<<(b=a).set_x(27); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/72.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | class cls1 { 3 | protected: 4 | int x; 5 | 6 | public: 7 | cls1(int i = 10) { x = i; } 8 | int get_x() { return x; } 9 | }; 10 | class cls2 : cls1 { 11 | public: 12 | cls2(int i) 13 | : cls1(i) 14 | { 15 | } 16 | }; 17 | int main() 18 | { 19 | cls d(37); 20 | cout << d.get_x(); 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/106.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class X { int i; 4 | public: X(int ii = 5) { i = ii; cout<< i<< " ";}; 5 | const int tipareste(int j) const { cout<tipareste(7); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/85.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | class B { 3 | int x; 4 | 5 | public: 6 | B(int i = 17) { x = i; } 7 | int get_x() { return x; } 8 | operator int() { return x; } 9 | }; 10 | class D : public B { 11 | public: 12 | D(int i = -16) 13 | : B(i) 14 | { 15 | } 16 | }; 17 | int main() 18 | { 19 | D a; 20 | cout << 27 + a; 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/103.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class B 4 | { int i; 5 | public: B() { i=80; } 6 | virtual int get_i() { return i; } 7 | }; 8 | class D: public B 9 | { int j; 10 | public: D() { j=27; } 11 | int get_j() {return j; } 12 | }; 13 | int main() 14 | { D *p=new B; 15 | cout<get_i(); 16 | cout<<((D*)p)->get_j(); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/25.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | class A { 3 | static int x; 4 | 5 | public: 6 | A(int i = 0) { x = i; } 7 | int get_x() { return x; } 8 | int& set_x(int i) { x = i; } 9 | A operator=(A a1) 10 | { 11 | set_x(a1.get_x()); 12 | return a1; 13 | } 14 | }; 15 | int main() 16 | { 17 | A a(212), b; 18 | cout << (b = a).get_x(); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/59.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class B { 4 | int x; 5 | 6 | public: 7 | B(int v) { v = x; } 8 | int get_x() { return x; } 9 | }; 10 | 11 | class D : private B { 12 | int y; 13 | 14 | public: 15 | D(int v) 16 | : B(v) 17 | { 18 | } 19 | int get_x() { return x; } 20 | }; 21 | 22 | void main() 23 | { 24 | D d(10); 25 | cout << d.get_x(); 26 | } 27 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/67.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Y; 4 | 5 | class Z; 6 | 7 | class X { 8 | int x; 9 | 10 | public: 11 | X(int n = 0) { x = n; } 12 | friend Y; 13 | }; 14 | 15 | class Y { 16 | int y; 17 | friend Z; 18 | }; 19 | 20 | class Z { 21 | public: 22 | void f(X u) { cout << u.x; } 23 | }; 24 | 25 | void main() 26 | { 27 | X a; 28 | Z b; 29 | b.f(a); 30 | } 31 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/16.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class A { 4 | protected: 5 | int x; 6 | 7 | public: 8 | A(int i = 14) { x = i; } 9 | }; 10 | class B : A { 11 | public: 12 | B() 13 | : A(2) 14 | { 15 | } 16 | B(B& b) { x = b.x - 14; } 17 | void afisare() { cout << x; } 18 | }; 19 | int main() 20 | { 21 | B b1, b2(b1); 22 | b2.afisare(); 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/26.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class A { 4 | protected: 5 | int x; 6 | 7 | public: 8 | A(int i = 14) { x = i; } 9 | }; 10 | class B : A { 11 | public: 12 | B() 13 | : A(2) 14 | { 15 | } 16 | B(B& b) { x = b.x - 14; } 17 | void afisare() { cout << x; } 18 | }; 19 | int main() 20 | { 21 | B b1, b2(b1); 22 | b2.afisare(); 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/46.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class B 4 | { 5 | protected: int x; 6 | public: B(int i=0) {x=i;} 7 | virtual B minus() {return (1-x);} 8 | }; 9 | class D: public B 10 | { 11 | public: D(int i=0):B(i) {} 12 | void afisare() {cout<minus(); 18 | p1->afisare(); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/38.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | template 4 | X f(X x, Y y) 5 | { 6 | //aici o sa ajunga niste pointer si nu-i poti aduna!!! 7 | return x+y; 8 | } 9 | int *f(int *x,int y) 10 | { 11 | return x-y; 12 | } 13 | int main() 14 | { 15 | int *a=new int(200), *b=a; 16 | //cout< 2 | using namespace std; 3 | 4 | class A 5 | { 6 | int x; 7 | public: A(int i = 17) { x = i; } 8 | int get_x() { return x; } 9 | }; 10 | class B 11 | { 12 | int x; 13 | public: B(int i = -16) { x = i; } 14 | operator A() { return x; } 15 | int get_x() { return x; } 16 | }; 17 | int main() 18 | { 19 | B a; 20 | A b = a; 21 | cout << b.get_x(); 22 | return 0; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/80.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | class B { 3 | int x; 4 | 5 | public: 6 | B(int i = 10) { x = i; } 7 | int get_x() { return x; } 8 | }; 9 | class D : public B { 10 | public: 11 | D(int i) 12 | : B(i) 13 | { 14 | } 15 | D operator+(const D& a) { return x + a.x; } 16 | }; 17 | int main() 18 | { 19 | D ob1(7), ob2(-12); 20 | cout << (ob1 + ob2).get_x(); 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Year 1 Sem 1/Queue/Lee using queue/queue.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | struct queuenode 5 | { 6 | int val; 7 | queuenode *next; 8 | }; 9 | struct queue 10 | { 11 | queuenode *first_q; 12 | queuenode *last_q; 13 | }; 14 | 15 | void queue_push(int x, queue &q); 16 | int queue_pop(queue &q); 17 | int queue_peek(queue q); 18 | bool queue_empty(queue q); 19 | int queue_search(int x, queue q); 20 | void queue_print(queue q); 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class A { 4 | static int x; 5 | 6 | public: 7 | A(int i = 0) { x = i; } 8 | int get_x() { return x; } 9 | int& set_x(int i) { x = i; } 10 | A operator=(A a1) 11 | { 12 | set_x(a1.get_x()); 13 | return a1; 14 | } 15 | }; 16 | int main() 17 | { 18 | A a(212), b; 19 | cout << (b = a).get_x(); 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/37.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class A 4 | { 5 | int i; 6 | protected: static int x; 7 | public: A(int j=7) {i=j;x=j;} 8 | int get_x() {return x;} 9 | int set_x(int j) {int y=x; x=j; return y;} 10 | A operator=(A a1) {set_x(a1.get_x()); return a1;} 11 | }; 12 | int A::x=15; 13 | int main() 14 | { 15 | A a(212),b; 16 | cout<<(b=a).get_x(); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class A 4 | { int i; 5 | public: A(int x=2):i(x+1) {} 6 | virtual int get_i() { return i; }}; 7 | class B: public A 8 | { int j; 9 | public: B(int x=20):j(x-2) {} 10 | virtual int get_j() {return A::get_i()+j; }}; 11 | int main() 12 | { A o1(5); 13 | B o2; 14 | cout< 2 | using namespace std; 3 | class A 4 | { 5 | int i; 6 | public: A() {i=1;} 7 | virtual int get_i() {return i;} 8 | }; 9 | class B: public A 10 | { 11 | int j; 12 | public: B() {j=2;} 13 | int get_i() { return A::get_i()+j;} 14 | }; 15 | int main() 16 | { 17 | const int i=cin.get(); 18 | if(i%3) {A o;} 19 | else {B o;} 20 | cout< 2 | #include 3 | using namespace std; 4 | 5 | class B 6 | { int i; 7 | public: B() { i=1; } 8 | int get_i() { return i; } 9 | }; 10 | class D: public B 11 | { int j; 12 | public: D() { j=2; } 13 | int get_j() {return j; } 14 | }; 15 | int main() 16 | { B *p=new D; 17 | cout<get_i(); 18 | if (typeid((B*)p).name()=="B") cout<<((D*)p)->get_j(); 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/81.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | class B { 3 | public: 4 | int x; 5 | B(int i = 16) { x = i; } 6 | B f(B ob) { return x + ob.x; } 7 | }; 8 | class D : public B { 9 | public: 10 | D(int i = 25) { x = i; } 11 | B f(B ob) { return x + ob.x + 1; } 12 | void afisare() { cout << x; } 13 | }; 14 | int main() 15 | { 16 | B *p1 = new D, *p2 = new B, *p3 = new B(p1->f(*p2)); 17 | cout << p3->x; 18 | return 0; 19 | } -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/111.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class B { 5 | public: 6 | int x; 7 | B(int i = 16) { x = i; } 8 | B f(B ob) { return x + ob.x; } 9 | }; 10 | class D : public B { 11 | public: 12 | D(int i = 25) { x = i; } 13 | D f(D ob) { return x + ob.x + 1; } 14 | void afisare() { cout << x; } 15 | }; 16 | int main() 17 | { 18 | D *p1 = new D, *p2 = new B, *p3 = new D(p1->f(*p2)); 19 | cout << p3->x; 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/11.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | class A { 3 | int x; 4 | 5 | public: 6 | A(int i = 0) { x = i; } 7 | A operator+(const A& a) { return x + a.x; } 8 | template 9 | ostream& operator<<(ostream&); 10 | }; 11 | template 12 | ostream& A::operator<<(ostream& o) 13 | { 14 | o << x; 15 | return o; 16 | } 17 | int main() 18 | { 19 | A a1(33), a2(-21); 20 | cout << a1 + a2; 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/35.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | class A { 3 | int x; 4 | 5 | public: 6 | A(int i = 0) { x = i; } 7 | A operator+(const A& a) { return x + a.x; } 8 | template 9 | ostream& operator<<(ostream&); 10 | }; 11 | template 12 | ostream& A::operator<<(ostream& o) 13 | { 14 | o << x; 15 | return o; 16 | } 17 | int main() 18 | { 19 | A a1(33), a2(-21); 20 | cout << a1 + a2; 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/65.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class vector { 4 | int *p, nr; 5 | 6 | public: 7 | operator int() { return nr; } 8 | vector(int); 9 | }; 10 | 11 | vector::vector(int n) 12 | { 13 | p = new int[n]; 14 | nr = n; 15 | while (n--) 16 | p[n] = n; 17 | } 18 | 19 | void f(int i) 20 | { 21 | while (i--) 22 | cout << i << endl; 23 | } 24 | 25 | void main() 26 | { 27 | vector x(10); 28 | f(x); 29 | } 30 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/73.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class B { 5 | public: 6 | int x; 7 | B(int i = 16) { x = i; } 8 | B(){} 9 | B f(B ob) { return x + ob.x; } 10 | }; 11 | class D : public B { 12 | public: 13 | D(int i = 25) { x = i; } 14 | B f(B ob) { return x + ob.x + 1; } 15 | void afisare() { cout << x; } 16 | }; 17 | int main() 18 | { 19 | B *p1 = new D, *p2 = new B, *p3 = new B(p1->f(*p2)); 20 | cout << p3->x; 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | class problema { 3 | int i; 4 | 5 | public: 6 | problema(int j = 5) { i = j; } 7 | void schimba() { i++; } 8 | void afiseaza() { cout << "starea curenta " << i << "\n"; } 9 | }; 10 | problema mister1() { return problema(6); } 11 | void mister2(problema& o) 12 | { 13 | o.afiseaza(); 14 | o.schimba(); 15 | o.afiseaza(); 16 | } 17 | int main() 18 | { 19 | mister2(mister1()); 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/44.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | class B 5 | { 6 | int i; 7 | public: 8 | B() { i=1;} 9 | int get_i() {return i;} 10 | }; 11 | class D: public B 12 | { 13 | int j; 14 | public: 15 | D() {j=2;} 16 | int get_j(){return j;} 17 | }; 18 | int main() 19 | { 20 | B *p=new D; 21 | cout<get_i(); 22 | if(typeid((B*)p).name()=="D*") cout<<((D*)p)->get_j(); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/96.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class A 5 | { 6 | protected: int x; 7 | public: A(int i = -16) { x = i; } 8 | virtual A f(A a) { return x + a.x; } 9 | void afisare() { cout << x; } 10 | }; 11 | class B: public A 12 | { 13 | public: B(int i=3):A(i){} 14 | A f(A a) { return x + a.x + 1; } 15 | }; 16 | int main() 17 | { 18 | A *p1 = new B, *p2 = new A, *p3 = new A(p1->f(*p2)); 19 | p3->afisare(); 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/47.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class cls1 { 4 | int x; 5 | public: 6 | cls1 () { 7 | x = 13; 8 | } 9 | int g() { 10 | static int i; i++; 11 | return (i+x); 12 | } 13 | }; 14 | 15 | class cls2 { 16 | int x; 17 | public: 18 | cls2() { 19 | x = 27; 20 | } 21 | cls1& f() { 22 | cls1 ob; return ob; 23 | } 24 | }; 25 | 26 | int main() { 27 | cls2 ob; 28 | cout << ob.f().g(); 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/56.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class cls1 { 4 | int x; 5 | public: 6 | cls1 () { 7 | x = 13; 8 | } 9 | int g() { 10 | static int i; i++; 11 | return (i+x); 12 | } 13 | }; 14 | 15 | class cls2 { 16 | int x; 17 | public: 18 | cls2() { 19 | x = 27; 20 | } 21 | cls1& f() { 22 | cls1 ob; return ob; 23 | } 24 | }; 25 | 26 | int main() { 27 | cls2 ob; 28 | cout << ob.f().g(); 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | class B { 4 | int i; 5 | 6 | public: 7 | B() { i = 1; } 8 | int get_i() { return i; } 9 | }; 10 | class D : B { 11 | int j; 12 | 13 | public: 14 | D() { j = 2; } 15 | int get_j() { return j; } 16 | }; 17 | int main() 18 | { 19 | B* p = new D; 20 | cout << p->get_i(); 21 | if (typeid((B*)p).name() == "D*") 22 | cout << ((D*)p)->get_j(); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/97.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class A 5 | { 6 | protected: int x; 7 | public: A(int i = -16) { x = i; } 8 | virtual A f(A a) { return x + a.x; } 9 | void afisare() { cout << x; } 10 | }; 11 | class B: public A 12 | { 13 | public: B(int i=3):A(i){} 14 | A f(A a) { return x + 1; } 15 | B operator+ (B a) { return x + a.x; } 16 | }; 17 | int main() 18 | { 19 | B a; int b = -21; 20 | a += b; 21 | cout << b; 22 | return 0; 23 | 24 | } -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/17.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class A { 4 | protected: 5 | static int x; 6 | 7 | public: 8 | A(int i = 0) { x = i; } 9 | virtual A schimb() { return (7 - x); } 10 | }; 11 | class B : public A { 12 | public: 13 | B(int i = 0) { x = i; } 14 | void afisare() { cout << x; } 15 | }; 16 | int A::x = 5; 17 | int main() 18 | { 19 | A* p1 = new B(18); 20 | *p1 = p1->schimb(); 21 | ((B*)p1)->afisare(); 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/28.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | class problema { 3 | int i; 4 | 5 | public: 6 | problema(int j = 5) { i = j; } 7 | void schimba() { i++; } 8 | void afiseaza() { cout << "starea curenta " << i << "\n"; } 9 | }; 10 | problema mister1() { return problema(6); } 11 | void mister2(problema& o) 12 | { 13 | o.afiseaza(); 14 | o.schimba(); 15 | o.afiseaza(); 16 | } 17 | int main() 18 | { 19 | mister2(mister1()); //(1) 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/57.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class D; 4 | 5 | class B { 6 | int x; 7 | friend void f(B, D); 8 | 9 | public: 10 | B(int i = 0) { x = i; } 11 | }; 12 | 13 | class D : public B { 14 | public: 15 | int y; 16 | D(int i = 0, int j = 0) 17 | : B(i) 18 | { 19 | y = j; 20 | } 21 | }; 22 | 23 | void f(B b, D d) { cout << b.x << " " << d.y; } 24 | 25 | void main() 26 | { 27 | B b; 28 | D d; 29 | f(b, d); 30 | } 31 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/58.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class D; 4 | 5 | class B { 6 | int x; 7 | friend void f(B, D); 8 | 9 | public: 10 | B(int i = 0) { x = i; } 11 | }; 12 | 13 | class D : public B { 14 | public: 15 | int y; 16 | D(int i = 0, int j = 0) 17 | : B(i) 18 | { 19 | y = j; 20 | } 21 | }; 22 | 23 | void f(B b, D d) { cout << b.x << " " << d.y; } 24 | 25 | void main() 26 | { 27 | B b; 28 | D d; 29 | f(b, d); 30 | } 31 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/30.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | class B { 3 | int x; 4 | 5 | public: 6 | B(int i = 7) { x = i; } 7 | int get_x() { return x; } 8 | operator int() { return x; } 9 | }; 10 | class D : public B { 11 | public: 12 | D(int i = -12) 13 | : B(i) 14 | { 15 | } 16 | D operator+(D a) { return get_x() + a.get_x() + 1; } 17 | }; 18 | int main() 19 | { 20 | D a; 21 | int b = 18; 22 | b += a; 23 | cout << b; 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/31.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | class B { 4 | int i; 5 | 6 | public: 7 | B() { i = 1; } 8 | int get_i() { return i; } 9 | }; 10 | class D : B { 11 | int j; 12 | 13 | public: 14 | D() { j = 2; } 15 | int get_j() { return j; } 16 | }; 17 | int main() 18 | { 19 | B* p = new D; //(1) 20 | cout << p->get_i(); //(2) 21 | if (typeid((B*)p).name() == "D*") 22 | cout << ((D*)p)->get_j(); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | class B { 3 | int x; 4 | 5 | public: 6 | B(int i = 7) { x = i; } 7 | int get_x() { return x; } 8 | operator int() { return x; } 9 | }; 10 | class D : public B { 11 | public: 12 | D(int i = -12) 13 | : B(i) 14 | { 15 | } 16 | D operator+(D a) { return get_x() + a.get_x() + 1; } 17 | }; 18 | int main() 19 | { 20 | D a; 21 | int b = 18; 22 | b += a; 23 | cout << b; 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/41.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int f(int y) 4 | { 5 | if(y<0) throw y; 6 | return y/2; 7 | } 8 | int main() 9 | { 10 | int x; 11 | try 12 | { 13 | cout<<"Da-mi un numar par: "; 14 | cin>>x; 15 | if(x%3)x=f(x); 16 | else throw x; 17 | cout<<"Numarul "< 2 | class cls { 3 | int x; 4 | 5 | public: 6 | cls(int i) { x = i; } 7 | int set_x(int i) 8 | { 9 | int y = x; 10 | x = i; 11 | return y; 12 | } 13 | int get_x() { return x; } 14 | }; 15 | int main() 16 | { 17 | cls* p = new cls[10]; 18 | int i = 0; 19 | for (; i < 10; i++) 20 | p[i].set_x(i); 21 | for (i = 0; i < 10; i++) 22 | cout << p[i].get_x(i); 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Year 1 Sem 1/BinarySearchTree/Numbers_in_interval/interval.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "BST.h" 3 | int main() 4 | { 5 | B_search_treenode *tree; 6 | BStree_initialize(tree); 7 | 8 | int n,x; 9 | std::cin >> n; 10 | for(int i=1;i<=n;i++) 11 | { 12 | std::cin >> x; 13 | BStree_insert(tree,x); 14 | } 15 | int a, b; 16 | std::cin >> a; 17 | std::cin >> b; 18 | for(int i=a;i<=b;i++) 19 | { 20 | if (BStree_search(tree, i)) std::cout << i << " "; 21 | } 22 | 23 | system("pause"); 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/12.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | class cls { 3 | int x; 4 | 5 | public: 6 | cls(int i) { x = i; } 7 | int set_x(int i) 8 | { 9 | int y = x; 10 | x = i; 11 | return y; 12 | } 13 | int get_x() { return x; } 14 | }; 15 | int main() 16 | { 17 | cls* p = new cls[10]; 18 | int i = 0; 19 | for (; i < 10; i++) 20 | p[i].set_x(i); 21 | for (i = 0; i < 10; i++) 22 | cout << p[i].get_x(); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/15.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct cls { 4 | int x; 5 | 6 | public: 7 | int set_x(int i) 8 | { 9 | int y = x; 10 | x = i; 11 | return x; 12 | } 13 | int get_x() { return x; } 14 | }; 15 | int main() 16 | { 17 | cls* p = new cls[100]; 18 | int i = 0; 19 | for (; i < 50; i++) 20 | p[i].set_x(i); 21 | for (i = 5; i < 20; i++) 22 | cout << p[i].get_x() << " "; 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/82.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | class cls { 3 | int *v, nr; 4 | 5 | public: 6 | cls(int i) 7 | { 8 | nr = i; 9 | v = new int[i]; 10 | for (int j = 1; j < nr; j++) 11 | v[j] = 0; 12 | } 13 | int size() { return nr; } 14 | int& operator[](int i) { return *(v + i); } 15 | }; 16 | int main() 17 | { 18 | cls x(10); 19 | x[4] = -15; 20 | for (int i = 0; i < x.size(); i++) 21 | cout << x[i]; 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/36.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | class cls { 3 | int x; 4 | 5 | public: 6 | cls(int i) { x = i; } 7 | int set_x(int i) 8 | { 9 | int y = x; 10 | x = i; 11 | return y; 12 | } 13 | int get_x() { return x; } 14 | }; 15 | int main() 16 | { 17 | cls* p = new cls[10]; //(1) 18 | int i = 0; 19 | for (; i < 10; i++) 20 | p[i].set_x(i); 21 | for (i = 0; i < 10; i++) 22 | cout << p[i].get_x(); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/71.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | class cls1 { 3 | int x; 4 | 5 | public: 6 | cls1() { x = 13; } 7 | int g() 8 | { 9 | static int i; 10 | i++; 11 | return (i + x); 12 | } 13 | }; 14 | class cls2 { 15 | int x; 16 | 17 | public: 18 | cls2() { x = 27; } 19 | cls1& f() 20 | { 21 | static cls1 ob; 22 | return ob; 23 | } 24 | }; 25 | int main() 26 | { 27 | cls2 ob; 28 | cout << ob.f().g(); 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/27.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | class B { 3 | int i; 4 | 5 | public: 6 | B() { i = 1; } 7 | virtual int get_i() { return i; } 8 | }; 9 | class D : public B { 10 | int j; 11 | 12 | public: 13 | D() { j = 2; } 14 | int get_i() { return B::get_i() + j; } 15 | }; 16 | int main() 17 | { 18 | const int i = cin.get(); 19 | if (i % 3) { 20 | D o; 21 | } 22 | else { 23 | B o; 24 | } 25 | cout << o.get_i(); //(1) 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/88.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | class B { 3 | int i; 4 | 5 | public: 6 | B() { i = 1; } 7 | int get_i() { return i; } 8 | }; 9 | class D : public B { 10 | int j; 11 | 12 | public: 13 | D() { j = 2; } 14 | int get_j() { return j; } 15 | }; 16 | int main() 17 | { 18 | B* p; 19 | int x = 0; 20 | if (x) 21 | p = new B; 22 | else 23 | p = new D; 24 | if (typeid(p).name() == "D*") 25 | cout << ((D*)p)->get_j(); 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Year 2 Sem 1/so/lab4/prob1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | int main() { 7 | 8 | pid_t pid = fork(); 9 | if (pid < 0) 10 | return errno; 11 | else 12 | if (pid == 0) { 13 | char *argv[] = {"ls", NULL}; 14 | execve("/bin/ls", argv, NULL); 15 | perror(NULL); 16 | } 17 | else { 18 | printf("My PID = %d Child PID = %d\n", getppid(), getpid()); 19 | wait(); 20 | printf("Child %d finished\n", getpid()); 21 | } 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/8.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | class B { 3 | protected: 4 | int x; 5 | 6 | public: 7 | B(int i = 28) { x = i; } 8 | virtual B f(B ob) { return x + ob.x + 1; } 9 | void afisare() { cout << x; } 10 | }; 11 | class D : public B { 12 | public: 13 | D(int i = -32) 14 | : B(i) 15 | { 16 | } 17 | B f(B ob) { return x + ob.x - 1; } 18 | }; 19 | int main() 20 | { 21 | B *p1 = new D, *p2 = new B, *p3 = new B(p1->f(*p2)); 22 | p3->afisare(); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/32.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | class B { 3 | protected: 4 | int x; 5 | 6 | public: 7 | B(int i = 28) { x = i; } 8 | virtual B f(B ob) { return x + ob.x + 1; } 9 | void afisare() { cout << x; } 10 | }; 11 | class D : public B { 12 | public: 13 | D(int i = -32) 14 | : B(i) 15 | { 16 | } 17 | B f(B ob) { return x + ob.x - 1; } 18 | }; 19 | int main() 20 | { 21 | B *p1 = new D, *p2 = new B, *p3 = new B(p1->f(*p2)); 22 | p3->afisare(); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/93.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class A 6 | { 7 | int i; 8 | public: A() { i = 1; } 9 | int get_i() { return i; } 10 | }; 11 | class B: public A 12 | { 13 | int j; 14 | public: B() { j = 2; } 15 | int get_j() { return j; } 16 | }; 17 | int main() 18 | { 19 | A *p; 20 | int x = 0; 21 | if (x) p = new A; 22 | else p = new B; 23 | if (typeid(p).name() == typeid(B*).name()) cout << ((B*)p)->get_j(); 24 | else cout << "tipuri diferite"; 25 | return 0; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/61.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class cls { 4 | int vi; 5 | 6 | public: 7 | cls(int v = 18) { vi = v; } 8 | operator int() { return vi; } 9 | cls operator++() 10 | { 11 | vi++; 12 | return *this; 13 | } 14 | cls operator++(int); 15 | }; 16 | 17 | cls cls::operator++(int) 18 | { 19 | cls aux = *this; 20 | vi++; 21 | return aux; 22 | } 23 | 24 | void main() 25 | { 26 | cls p(20); 27 | int x = p++, y = ++p; 28 | cout << "x=" << x << ", y=" << y << endl; 29 | } 30 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/62.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class cls { 4 | int vi; 5 | 6 | public: 7 | cls(int v = 18) { vi = v; } 8 | operator int() { return vi; } 9 | cls operator++() 10 | { 11 | vi++; 12 | return *this; 13 | } 14 | cls operator++(int); 15 | }; 16 | 17 | cls cls::operator++(int) 18 | { 19 | cls aux = *this; 20 | vi++; 21 | return aux; 22 | } 23 | 24 | void main() 25 | { 26 | cls p(20); 27 | int x = p++, y = ++p; 28 | cout << "x=" << x << ", y=" << y << endl; 29 | } 30 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/19.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class B { 4 | protected: 5 | int x; 6 | 7 | public: 8 | B(int i = 12) { x = i; } 9 | virtual B f(B ob) { return x + ob.x + 1; } 10 | void afisare() { cout << x; } 11 | }; 12 | class D : public B { 13 | public: 14 | D(int i = -15) 15 | : B(i - 1) 16 | { 17 | x++; 18 | } 19 | B f(B ob) { return x - 2; } 20 | }; 21 | int main() 22 | { 23 | B *p1 = new D, *p2 = new B, *p3 = new B(p1->f(*p2)); 24 | p3->afisare(); 25 | return 0; 26 | } -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/68.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class cls2; 4 | 5 | class cls1 { 6 | public: 7 | int vi; 8 | cls1(int v = 30) { vi = v; } 9 | cls1(cls2 p) { vi = p.vi; } 10 | }; 11 | 12 | class cls2 { 13 | public: 14 | int vi; 15 | cls2(int v = 20) { vi = v; } 16 | }; 17 | 18 | cls1 f(cls1 p) 19 | { 20 | p.vi++; 21 | return p; 22 | } 23 | 24 | void main() 25 | { 26 | cls1 p; 27 | f(p); 28 | cout << endl 29 | << p.vi; 30 | cls2 r; 31 | f(r); 32 | cout << endl 33 | << r.vi; 34 | } 35 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/75.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | class cls1 { 3 | public: 4 | int x; 5 | cls1(int i = 20) { x = i; } 6 | }; 7 | class cls2 { 8 | public: 9 | int y; 10 | cls2(int i = 30) { y = i; } 11 | operator cls1() 12 | { 13 | cls1 ob; 14 | ob.x = y; 15 | return ob; 16 | } 17 | }; 18 | cls1 f(cls1 ob) 19 | { 20 | ob.x++; 21 | return ob; 22 | } 23 | int main() 24 | { 25 | cls1 ob1; 26 | f(ob1); 27 | cout << ob1.x; 28 | cls2 ob2; 29 | f(ob2); 30 | cout << ob2.y; 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/107.cpp: -------------------------------------------------------------------------------- 1 | include 2 | using namespace std; 3 | class A 4 | { protected: int x; 5 | public: A(int i):x(i){} 6 | int get_x(){ return x; } }; 7 | class B: A 8 | { protected: int y; 9 | public: B(int i,int j):y(i),A(j){} 10 | int get_y(){ return get_x()+y; } }; 11 | class C: protected B 12 | { protected: int z; 13 | public: C(int i,int j,int k):z(i),B(j,k){} 14 | int get_z(){ return get_x()+get_y()+z; } }; 15 | int main() 16 | { C c(5,6,7); 17 | cout< 2 | #include 3 | using namespace std; 4 | class B { 5 | int i; 6 | 7 | public: 8 | B(int x) { i = x + 1; } 9 | int get_i() { return i; } 10 | }; 11 | class D : public B { 12 | int j; 13 | 14 | public: 15 | D() 16 | : B(1) 17 | { 18 | j = i + 2; 19 | } 20 | int get_j() { return j; } 21 | }; 22 | int main() 23 | { 24 | B* p = new D[10]; 25 | cout << p->get_i(); 26 | if (typeid((B*)p).name() == "D*") 27 | cout << ((D*)p)->get_j(); 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/52.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | class B 3 | { protected: static int x; 4 | int i; 5 | public: B() { x++; i=1; } 6 | ~B() { x--; cout << "b";} 7 | int get_x() { return x; } 8 | int get_i() { return i; } }; 9 | int B::x; 10 | class D: public B 11 | { public: D() { x++; } 12 | ~D() { x--; cout << "d";} }; 13 | int f(B *q) 14 | { return (q->get_x())+1; } 15 | int main() 16 | { B *p=new B[10]; 17 | cout< 2 | using namespace std; 3 | class cls 4 | { 5 | int x; 6 | public: 7 | cls(int i=0) {cout<<" c1 "; x=i;} 8 | ~cls() {cout<<" d 1 ";} 9 | }; 10 | class cls1 11 | { 12 | int x; cls xx; 13 | public: 14 | cls1(int i=0){cout<<" c2 ";x=i;} 15 | ~cls1(){cout<<" d2 ";} 16 | }c; 17 | class cls2 18 | { 19 | int x;cls1 xx;cls xxx; 20 | public: 21 | cls2(int i=0) {cout<<" c3 ";x=i;} 22 | ~cls2(){ cout<<" d3 ";} 23 | }; 24 | int main() 25 | { 26 | cls2 s; 27 | return 0; 28 | } 29 | //c1 c2 c1 c2 c1 c3 d3 d1 d2 d1 d2 d1 30 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/14.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class A { 4 | int x; 5 | 6 | public: 7 | A(int i) { x = i; } 8 | int get_x() { return x; } 9 | int& set_x(int i) { x = i; } 10 | A operator=(A a1) 11 | { 12 | set_x(a1.get_x()); 13 | return a1; 14 | } 15 | }; 16 | class B : public A { 17 | int y; 18 | 19 | public: 20 | B(int i) 21 | : A(i) 22 | { 23 | y = i; 24 | } 25 | void afisare() { cout << y; } 26 | }; 27 | int main() 28 | { 29 | B a(112), b, *c; 30 | cout << (b = a).get_x(); 31 | (c = &a)->afisare(); 32 | return 0; 33 | } -------------------------------------------------------------------------------- /Year 1 Sem 1/Stack/pillars/pillars.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "stack.h" 3 | 4 | void array_read(int &n, int v[]) 5 | { 6 | std::cin >> n; 7 | for (int i = 1; i <= n; i++) 8 | std::cin >> v[i]; 9 | } 10 | int check(int n, int v[]) 11 | { 12 | stack *st; 13 | st = NULL; 14 | stack_push(v[1], st); 15 | for(int i=2; i<=n; i++) 16 | { 17 | if (stack_peek(st) == v[i]) stack_pop(st); 18 | else stack_push(v[i], st); 19 | 20 | } 21 | if (stack_empty(st)) return 1; 22 | return 0; 23 | } 24 | int main() 25 | { 26 | int v[100], n; 27 | 28 | array_read(n, v); 29 | std::cout< 2 | class B { 3 | static int x; 4 | int i; 5 | 6 | public: 7 | B() 8 | { 9 | x++; 10 | i = 1; 11 | } 12 | ~B() { x--; } 13 | static int get_x() { return x; } 14 | int get_i() { return i; } 15 | }; 16 | int B::x; 17 | class D : public B { 18 | public: 19 | D() { x++; } 20 | ~D() { x--; } 21 | }; 22 | int f(B* q) 23 | { 24 | return (q->get_i()) + 1; 25 | } 26 | int main() 27 | { 28 | B* p = new B; 29 | cout << f(p); 30 | delete p; 31 | p = new D; 32 | cout << f(p); 33 | delete p; 34 | cout << D::get_x(); 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Year 1 Sem 1/CExamenPrep/lab/matricedinamica.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int **citire() 4 | { 5 | int n; 6 | n = 3; 7 | int **v; 8 | v = malloc(n*sizeof(int*)); 9 | for(int i=0;i 2 | class B { 3 | protected: 4 | static int x; 5 | int i; 6 | 7 | public: 8 | B() 9 | { 10 | x++; 11 | i = 1; 12 | } 13 | ~B() { x--; } 14 | static int get_x() { return x; } 15 | int get_i() { return i; } 16 | }; 17 | int B::x; 18 | class D : public B { 19 | public: 20 | D() { x++; } 21 | ~D() { x--; } 22 | }; 23 | int f(B* q) 24 | { 25 | return (q->get_x()) + 1; 26 | } 27 | int main() 28 | { 29 | B* p = new B[10]; 30 | cout << f(p); 31 | delete[] p; 32 | p = new D; 33 | cout << f(p); 34 | delete p; 35 | cout << D::get_x(); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/9.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | class B { 3 | protected: 4 | static int x; 5 | int i; 6 | 7 | public: 8 | B() 9 | { 10 | x++; 11 | i = 1; 12 | } 13 | ~B() { x--; } 14 | static int get_x() { return x; } 15 | int get_i() { return i; } 16 | }; 17 | int B::x; 18 | class D : public B { 19 | public: 20 | D() { x++; } 21 | ~D() { x--; } 22 | }; 23 | int f(B* q) 24 | { 25 | return (q->get_x()) + 1; 26 | } 27 | int main() 28 | { 29 | B* p = new B[10]; 30 | cout << f(p); 31 | delete[] p; 32 | p = new D; 33 | cout << f(p); 34 | delete p; 35 | cout << D::get_x(); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/104.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class B 4 | { protected: int i; 5 | public: B(int j=5) {cout << " cb "; i=j-2; } 6 | ~B(){ cout << " db ";} 7 | int get_i() { return i; } }; 8 | 9 | class D1: public B 10 | { int j; 11 | public: D1(int k=10) {cout << " cd1 "; j=i-k+3; } 12 | ~D1(){ cout << " dd1 ";} }; 13 | 14 | class D2: public D1 15 | { int k; 16 | public: D2(int l=15) {cout << " cd2 "; k=i-l+3; } 17 | ~D2(){ cout << " dd2 ";} }; 18 | D1 f(D1 d1, D2 d2) {return d1.get_i()+d2.get_i(); } 19 | int main() 20 | { D2 ob2; D1 ob1(3); 21 | cout< 2 | using namespace std; 3 | 4 | class A { 5 | int x; 6 | 7 | public: 8 | A(int i = 2) 9 | : x(i) 10 | { 11 | } 12 | int get_x() const { return x; } 13 | }; 14 | class B : public A { 15 | 16 | int* y; 17 | 18 | public: 19 | B(int i = 2) 20 | : A(i) 21 | { 22 | y = new int[i]; 23 | for (int j = 0; j < i; j++) 24 | y[j] = 1; 25 | } 26 | int& operator[](int i) const { return y[i]; } 27 | }; 28 | ostream& operator<<(ostream& o, const B &b) 29 | { 30 | for (int i = 0; i < b.get_x(); i++) 31 | o << b[i]; 32 | return o; 33 | } 34 | int main() 35 | { 36 | const B b(5); 37 | b[2] = 3; b[4] = 5; 38 | cout << b; 39 | return 0; 40 | } -------------------------------------------------------------------------------- /Year 1 Sem 1/CExamenPrep/exam/string.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | int main() 7 | { 8 | char a, b; 9 | char s[100]; 10 | FILE *f = fopen("string.txt","r"); 11 | //fscanf(f,"%s%s", &a, &b); 12 | a = fgetc(f); 13 | fgetc(f); 14 | b = fgetc(f); 15 | fgetc(f); 16 | 17 | fgets(s, 100, f); 18 | int n = strlen(s), i; 19 | 20 | for(i=0;i 5 | using namespace std; 6 | 7 | class A 8 | { 9 | int x; 10 | public: 11 | A(int i):x(i){} 12 | int get_x() const { return x; } 13 | }; 14 | class B : public A 15 | { 16 | int *y; 17 | public: 18 | B(int i) :A(i) 19 | { 20 | y = new int[i]; 21 | for (int j = 0; j < i; j++) y[j] = 1; 22 | } 23 | B(B&); 24 | int &operator[](int i) { return y[i]; } 25 | }; 26 | B::B(B& a) 27 | { 28 | y = new int[a.get_x()]; 29 | for (int i = 0; i < a.get_x(); i++) y[i] = a[i]; 30 | } 31 | ostream &operator<<(ostream& o, B a) 32 | { 33 | for (int i = 0; i < a.get_x(); i++)o << a[i]; 34 | return o; 35 | } 36 | int main() 37 | { 38 | B b(5); 39 | cout << b; 40 | return 0; 41 | } -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/39.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | templatevoid test(X &a, X &b) 4 | { 5 | X temp; 6 | temp=a; 7 | a=b; 8 | b=temp; 9 | cout<<"ttest\n"; 10 | } 11 | void test(int &c,int &d) 12 | { 13 | int temp; 14 | temp=c; 15 | c=d; 16 | d=temp; 17 | cout<<"test 1\n"; 18 | } 19 | void test(int e,int f) 20 | { 21 | int temp; 22 | temp=e; 23 | e=f; 24 | f=temp; 25 | cout<<"test 2\n"; 26 | } 27 | void test(int g,int *h) 28 | { 29 | int temp; 30 | temp=g; 31 | g=*h; 32 | *h=temp; 33 | cout<<"test 3\n"; 34 | } 35 | int main() 36 | { 37 | int a=5,b=15,c=25,*d=&a; 38 | test(a,b); 39 | test(c,d); 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Year 1 Sem 1/Queue/Lee using queue/queue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "queue.h" 3 | void queue_push(std::pair x, queue &q) 4 | { 5 | queuenode *aux = new queuenode; 6 | aux->val = x; aux->next = NULL; 7 | 8 | if (q.first_q == NULL) q.first_q = q.last_q = aux; 9 | else 10 | { 11 | q.last_q->next = aux; 12 | q.last_q = aux; 13 | } 14 | 15 | 16 | } 17 | std::pair queue_pop(queue &q) 18 | { 19 | std::pair val = q.first_q->val; 20 | q.first_q = q.first_q->next; 21 | return val; 22 | } 23 | std::pair queue_peek(queue q) 24 | { 25 | std::pair val = q.first_q->val; 26 | return val; 27 | } 28 | bool queue_empty(queue q) 29 | { 30 | if (q.first_q == NULL) return true; 31 | else return false; 32 | } -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/21.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class A { 4 | public: 5 | int x; 6 | A(int i = -13) { x = i; } 7 | }; 8 | class B : virtual public A { 9 | public: 10 | B(int i = -15) { x = i; } 11 | }; 12 | class C : virtual public A { 13 | public: 14 | C(int i = -17) { x = i; } 15 | }; 16 | class D : virtual public A { 17 | public: 18 | D(int i = -29) { x = i; } 19 | }; 20 | class E : public B, public D, public C { 21 | public: 22 | int y; 23 | E(int i, int j) 24 | : D(i) 25 | , B(j) 26 | { 27 | y = x + i + j; 28 | } 29 | E(E& ob) { y = ob.x - ob.y; } 30 | }; 31 | int main() 32 | { 33 | E e1(5, 10), e2 = e1; 34 | cout << e2.y; 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Year 1 Sem 1/BinarySearchTree/SortWithBTS/BST.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | struct B_search_treenode 3 | { 4 | int key; 5 | B_search_treenode *right; 6 | B_search_treenode *left; 7 | }; 8 | void BStree_initialize(B_search_treenode *&tree); 9 | void BStree_insert(B_search_treenode *&tree, int x); 10 | int BStree_search(B_search_treenode *temp, int x); 11 | int BStree_find_max(B_search_treenode *temp); 12 | int BStree_find_min(B_search_treenode *temp); 13 | void BStree_delete(B_search_treenode *temp, int x); 14 | void BStree_delete(B_search_treenode *prev, B_search_treenode *temp); 15 | bool BStree_exist(B_search_treenode *temp); 16 | void inorder(B_search_treenode *temp); 17 | void preorder(B_search_treenode *temp); 18 | void postorder(B_search_treenode *temp); 19 | void DFS(B_search_treenode *temp); -------------------------------------------------------------------------------- /Year 1 Sem 1/BinarySearchTree/to_include/BST.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | struct B_search_treenode 3 | { 4 | int key; 5 | B_search_treenode *right; 6 | B_search_treenode *left; 7 | }; 8 | void BStree_initialize(B_search_treenode *&tree); 9 | void BStree_insert(B_search_treenode *&tree, int x); 10 | int BStree_search(B_search_treenode *temp, int x); 11 | int BStree_find_max(B_search_treenode *temp); 12 | int BStree_find_min(B_search_treenode *temp); 13 | void BStree_delete(B_search_treenode *temp, int x); 14 | void BStree_delete(B_search_treenode *prev, B_search_treenode *temp); 15 | bool BStree_exist(B_search_treenode *temp); 16 | void inorder(B_search_treenode *temp); 17 | void preorder(B_search_treenode *temp); 18 | void postorder(B_search_treenode *temp); 19 | void DFS(B_search_treenode *temp); -------------------------------------------------------------------------------- /Year 1 Sem 1/BinarySearchTree/Numbers_in_interval/BST.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | struct B_search_treenode 3 | { 4 | int key; 5 | B_search_treenode *right; 6 | B_search_treenode *left; 7 | }; 8 | void BStree_initialize(B_search_treenode *&tree); 9 | void BStree_insert(B_search_treenode *&tree, int x); 10 | int BStree_search(B_search_treenode *temp, int x); 11 | int BStree_find_max(B_search_treenode *temp); 12 | int BStree_find_min(B_search_treenode *temp); 13 | void BStree_delete(B_search_treenode *temp, int x); 14 | void BStree_delete(B_search_treenode *prev, B_search_treenode *temp); 15 | bool BStree_exist(B_search_treenode *temp); 16 | void inorder(B_search_treenode *temp); 17 | void preorder(B_search_treenode *temp); 18 | void postorder(B_search_treenode *temp); 19 | void DFS(B_search_treenode *temp); -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int f(int y) 4 | { 5 | if (y < 0) 6 | throw y; 7 | return y / 2; 8 | } 9 | int f(int y, int z) 10 | { 11 | if (y < z) 12 | throw z - y; 13 | return y / 2; 14 | } 15 | float f(int& y) 16 | { 17 | cout <<” y este referinta”; 18 | return (float)y / 2; 19 | } 20 | int main() 21 | { 22 | int x; 23 | try { 24 | cout <<”Da - mi un numar par : ”; 25 | cin >> x; 26 | if (x % 2) 27 | x = f(x, 0); 28 | else 29 | x = f(x); 30 | cout <<”Numarul ”<< x <<” e bun !”<< endl; 31 | } 32 | catch (int i) { 33 | cout <<”Numarul ”<< i <<” nu e bun !”<< endl; 34 | } 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/87.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | class cls { 3 | int *v, nr; 4 | 5 | public: 6 | cls(int i = 0) 7 | { 8 | nr = i; 9 | v = new int[i]; 10 | for (int j = 0; j < size(); j++) 11 | v[j] = 3 * j; 12 | } 13 | ~cls() { delete[] v; } 14 | int size() { return nr; } 15 | int& operator[](int i) { return v[i]; } 16 | cls operator+(cls); 17 | }; 18 | cls cls::operator+(cls y) 19 | { 20 | cls x(size()); 21 | for (int i = 0; i < size(); i++) 22 | x[i] = v[i] + y[i]; 23 | return x; 24 | } 25 | int main() 26 | { 27 | cls x(10), y = x, z; 28 | x[3] = y[6] = -15; 29 | z = x + y; 30 | for (int i = 0; i < x.size(); i++) 31 | cout << z[i]; 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Year 2 Sem 1/so/lab4/prob2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | int main(int argc, char* argv[]) { 7 | 8 | pid_t pid = fork(); 9 | if(pid < 0) 10 | return errno; 11 | else 12 | if (pid == 0) { 13 | if(argc <= 1) { 14 | printf("Nu exista argumente suficiente.\n"); 15 | perror(NULL); 16 | } 17 | 18 | int numarul = atoi(argv[1]); 19 | printf("%d: ", numarul); 20 | while(1) { 21 | printf("%d ", numarul); 22 | if(numarul == 1) 23 | break; 24 | if(numarul % 2 == 0) 25 | numarul = numarul / 2; 26 | else 27 | numarul = 3*numarul + 1; 28 | } 29 | printf("\n"); 30 | } 31 | else { 32 | wait(); 33 | printf("Child %d finished\n", getpid()); 34 | } 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/50.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class B 4 | { int i; 5 | public: B() { i=1; cout<<"B ";} 6 | virtual int get_i() { return i; } }; 7 | class D: virtual public B 8 | { int j; 9 | public: D() { j=2; cout<<"D ";} 10 | int get_i() {return B::get_i()+j; } }; 11 | class D2: virtual public B 12 | { int j2; 13 | public: D2() { j2=3; cout<<"D2 "; } 14 | int get_i() {return B::get_i()+j2; } }; 15 | class MM: public D, public D2 16 | { int x; 17 | public: MM() { x=D::get_i()+D2::get_i();cout<<"MM \n"; } 18 | int get_i() {return x; } }; 19 | int main() 20 | { B *o= new MM(); 21 | cout<get_i()<<"\n"; 22 | MM *p= dynamic_cast(o); 23 | if (p) cout<get_i()<<"\n"; 24 | D *p2= dynamic_cast(o); 25 | if (p2) cout<get_i()<<"\n"; 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/23.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class B { 4 | protected: 5 | static int x; 6 | int i; 7 | 8 | public: 9 | B() 10 | { 11 | x++; 12 | i = 1; 13 | } 14 | ~B() { x--; } 15 | static int get_x() { return x; } 16 | int get_i() { return i; } 17 | }; 18 | int B::x; 19 | class D : public B { 20 | public: 21 | D() 22 | { 23 | x++; 24 | i++; 25 | } 26 | ~D() 27 | { 28 | x--; 29 | i--; 30 | } 31 | int f1(B o) { return 5 + get_i(); } 32 | }; 33 | int f(B* q) 34 | { 35 | return (q->get_x()) + 1; 36 | } 37 | int main() 38 | { 39 | B* p = new B[10]; 40 | cout << f(p); 41 | delete[] p; 42 | p = new D; 43 | cout << p->f1(p); 44 | delete p; 45 | cout << D::get_x(); 46 | return 0; 47 | } -------------------------------------------------------------------------------- /Year 1 Sem 1/CExamenPrep/exam/vaarg.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int sum(int n,...) 5 | { 6 | va_list valist; 7 | int s = 0; 8 | va_start(valist, n); 9 | 10 | for(int i=1;i<=n;i++) 11 | { 12 | int x = va_arg(valist, int); 13 | s += x; 14 | } 15 | return s; 16 | } 17 | 18 | int arraysum(int n,...) 19 | { 20 | va_list valist; 21 | int s = 0; 22 | va_start(valist, n); 23 | 24 | for(int i=1;i<=n;i++) 25 | { 26 | int* v = va_arg(valist, int*); 27 | 28 | int m = v[0]; 29 | for(int j=1; j<=m;j++) 30 | { 31 | s += v[j]; 32 | 33 | } 34 | } 35 | return s; 36 | } 37 | int main() 38 | { 39 | printf("%d\n", sum(4, 1, 2, 3, 4)); 40 | 41 | int v[100] = { 2,1,2 }; 42 | int b[100] = { 1,3 }; 43 | int c[100] = { 1,4 }; 44 | 45 | printf("%d\n", arraysum(3, v, b, c)); 46 | system("pause"); 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/79.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | class B { 3 | int x; 4 | 5 | public: 6 | B(int i = 2) 7 | : x(i) 8 | { 9 | } 10 | int get_x() const { return x; } 11 | }; 12 | class D : public B { 13 | int* y; 14 | 15 | public: 16 | D(int i = 2) 17 | : B(i) 18 | { 19 | y = new int[i]; 20 | for (int j = 0; j < i; j++) 21 | y[j] = 1; 22 | } 23 | D(D& a) 24 | { 25 | y = new int[a.get_x()]; 26 | for (int i = 0; i < a.get_x(); i++) 27 | y[i] = a[i]; 28 | } 29 | int& operator[](int i) { return y[i]; } 30 | }; 31 | ostream& operator<<(ostream& o, const D& a) 32 | { 33 | for (int i = 0; i < a.get_x(); i++) 34 | o << a[i]; 35 | return o; 36 | } 37 | int main() 38 | { 39 | D ob(5); 40 | cout << ob; 41 | return 0; 42 | } -------------------------------------------------------------------------------- /Year 1 Sem 1/DFS/queue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "queue.h" 3 | 4 | void queue_push(int x, queue &q) 5 | { 6 | queuenode *aux = new queuenode; 7 | aux->val = x; aux->next = NULL; 8 | 9 | if (q.first_q == NULL) q.first_q = q.last_q = aux; 10 | else 11 | { 12 | q.last_q->next = aux; 13 | q.last_q = aux; 14 | } 15 | 16 | 17 | } 18 | int queue_pop(queue &q) 19 | { 20 | int val = q.first_q->val; 21 | q.first_q = q.first_q->next; 22 | return val; 23 | } 24 | int queue_peek(queue q) 25 | { 26 | int val = q.first_q->val; 27 | return val; 28 | } 29 | bool queue_empty(queue q) 30 | { 31 | if (q.first_q == NULL) return true; 32 | else return false; 33 | } 34 | void queue_print(queue q) 35 | { 36 | while (!queue_empty(q)) 37 | { 38 | std::cout << queue_pop(q) << " "; 39 | } 40 | std::cout << "\n"; 41 | } 42 | void queue_init(queue q) 43 | { 44 | q.first_q = q.last_q = NULL; 45 | } -------------------------------------------------------------------------------- /Year 1 Sem 1/Stack/pillars/stack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "stack.h" 3 | void stack_push(int a, stack *&st) 4 | { 5 | stack *aux = new stack; 6 | aux->val = a; 7 | if (st == NULL) aux->next = NULL; 8 | else aux->next = st; 9 | st = aux; 10 | } 11 | int stack_pop(stack *&st) 12 | { 13 | int x = st->val; 14 | st = st->next; 15 | return x; 16 | } 17 | int stack_peek(stack *st) 18 | { 19 | if (st == NULL) return -1; 20 | int x = st->val; 21 | return x; 22 | } 23 | bool stack_empty(stack *st) 24 | { 25 | if (st == NULL) return true; 26 | return false; 27 | } 28 | void print(stack *st) 29 | { 30 | while (!stack_empty(st)) 31 | std::cout << stack_pop(st) << " "; 32 | std::cout << "\n"; 33 | } 34 | int search(int a, stack *st) 35 | { 36 | int distance = 0; 37 | while (!stack_empty(st)) 38 | { 39 | int x = stack_pop(st); 40 | if (x == a) return distance; 41 | distance++; 42 | } 43 | return -1; 44 | } -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/index.php: -------------------------------------------------------------------------------- 1 | '; 5 | echo '
'; 6 | 7 | if(isset($_POST['show_problem'])) { 8 | $directory_files = glob('probleme/*.cpp'); 9 | 10 | $random_File = $directory_files[array_rand($directory_files)]; 11 | 12 | if(isset($_SESSION['last_problem'])) { 13 | while($random_File == $_SESSION['last_problem']) { 14 | $random_File = $directory_files[array_rand($directory_files)]; 15 | } 16 | } 17 | 18 | $_SESSION['last_problem'] = $random_File; 19 | 20 | 21 | echo '

'.'Problema '.str_replace(".cpp", "", str_replace("probleme/", "", $random_File)).':


'.htmlspecialchars(file_get_contents($random_File)).'
'; 22 | } 23 | ?> -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/29.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | class B { 3 | int i; 4 | 5 | public: 6 | B() { i = 1; } 7 | virtual int get_i() { return i; } 8 | }; 9 | class D : virtual public B { 10 | int j; 11 | 12 | public: 13 | D() { j = 2; } 14 | int get_i() { return B::get_i() + j; } 15 | }; 16 | class D2 : virtual public B { 17 | int j2; 18 | 19 | public: 20 | D2() { j2 = 3; } 21 | int get_i() { return B::get_i() + j2; } 22 | }; 23 | class MM : public D, public D2 { 24 | int x; 25 | 26 | public: 27 | MM() { x = D::get_i() + D2::get_i(); } 28 | int get_i() { return x; } 29 | }; 30 | int main() 31 | { 32 | B* o = new MM(); 33 | cout << o->get_i() << "\n"; 34 | MM* p = dynamic_cast(o); 35 | if (p) 36 | cout << p->get_i() << "\n"; 37 | D* p2 = dynamic_cast(o); 38 | if (p2) 39 | cout << p2->get_i() << "\n"; 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/98.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class A { 5 | int x; 6 | 7 | public: 8 | A(int i = 2) 9 | : x(i) 10 | { 11 | } 12 | int get_x() const { return x; } 13 | }; 14 | class B : public A { 15 | 16 | int* y; 17 | 18 | public: 19 | B(int i = 2) 20 | : A(i) 21 | { 22 | y = new int[i]; 23 | for (int j = 0; j < i; j++) 24 | y[j] = 1; 25 | } 26 | B(B& b) 27 | { 28 | y = new int[b.get_x()]; 29 | for (int i = 0; i < b.get_x(); i++) 30 | y[i] = b[i]; 31 | } 32 | int& operator[](int i) const { return y[i]; } 33 | }; 34 | ostream& operator<<(ostream& o, const B b) 35 | { 36 | for (int i = 0; i < b.get_x(); i++) 37 | o << b[i]; 38 | return o; 39 | } 40 | 41 | 42 | 43 | int main() 44 | { 45 | const B b(5); 46 | cout << b; 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/13.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int f(int y) 4 | { 5 | try { 6 | if (y > 0) 7 | throw y; 8 | } 9 | catch (int i) { 10 | throw; 11 | } 12 | return y - 2; 13 | } 14 | int f(int y, int z) 15 | { 16 | try { 17 | if (y < z) 18 | throw z - y; 19 | } 20 | catch (int i) { 21 | throw; 22 | } 23 | return y + 2; 24 | } 25 | float f(float& y) 26 | { 27 | cout << " y este referinta"; 28 | return (float)y / 2; 29 | } 30 | int main() 31 | { 32 | int x; 33 | try { 34 | cout << "Da-mi un numar par: "; 35 | cin >> x; 36 | if (x % 2) 37 | x = f(x, 0); 38 | else 39 | x = f(x); 40 | cout << "Numarul " << x << " e bun!" << endl; 41 | } 42 | catch (int i) { 43 | cout << "Numarul " << i << " nu e bun!" << endl; 44 | } 45 | return 0; 46 | } -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/5.cpp: -------------------------------------------------------------------------------- 1 | //ordinea constructorilor 2 | #include 3 | class B { 4 | int i; 5 | 6 | public: 7 | B() { i = 1; } 8 | virtual int get_i() { return i; } 9 | }; 10 | class D : virtual public B { 11 | int j; 12 | 13 | public: 14 | D() { j = 2; } 15 | int get_i() { return B::get_i() + j; } 16 | }; 17 | class D2 : virtual public B { 18 | int j2; 19 | 20 | public: 21 | D2() { j2 = 3; } 22 | int get_i() { return B::get_i() + j2; } 23 | }; 24 | class MM : public D, public D2 { 25 | int x; 26 | 27 | public: 28 | MM() { x = D::get_i() + D2::get_i(); } 29 | int get_i() { return x; } 30 | }; 31 | int main() 32 | { 33 | B* o = new MM(); 34 | cout << o->get_i() << "\n"; 35 | MM* p = dynamic_cast(o); 36 | if (p) 37 | cout << p->get_i() << "\n"; 38 | D* p2 = dynamic_cast(o); 39 | if (p2) 40 | cout << p2->get_i() << "\n"; 41 | return 0; 42 | } -------------------------------------------------------------------------------- /Year 1 Sem 2/pooExamQuestions/probleme/20.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct B { 4 | int i; 5 | 6 | public: 7 | B() { i = 1; } 8 | virtual int get_i() { return i; } 9 | } a; 10 | class D : virtual public B { 11 | int j; 12 | 13 | public: 14 | D() { j = 2; } 15 | int get_i() { return B::get_i() + j; } 16 | }; 17 | class D2 : virtual public B { 18 | int j2; 19 | 20 | public: 21 | D2() { j2 = 3; } 22 | int get_i() { return B::get_i() + j2; } 23 | }; 24 | class MM : public D2, public D { 25 | int x; 26 | 27 | public: 28 | MM() { x = D::get_i() + D2::get_i(); } 29 | int get_i() { return x; } 30 | }; 31 | { 32 | MM b; 33 | } 34 | int main() 35 | { 36 | B* o = new MM(); 37 | cout << o->get_i() << "\n"; 38 | MM* p = dynamic_cast(o); 39 | if (p) 40 | cout << p->get_i() << "\n"; 41 | D* p2 = dynamic_cast(o); 42 | if (p2) 43 | cout << p2->get_i() << "\n"; 44 | return 0; 45 | } -------------------------------------------------------------------------------- /Year 1 Sem 2/Graph/TopologicalSort/ToplogicalSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int visited[50001]; 10 | vector edges[50001]; 11 | stack sorted; 12 | void visit(int thisNode) 13 | { 14 | int ok = 0; 15 | visited[thisNode] = 1; 16 | for(auto nextNode : edges[thisNode]) 17 | if(!visited[nextNode]) 18 | visit(nextNode); 19 | 20 | sorted.push(thisNode); 21 | } 22 | int main() 23 | { 24 | ifstream fin; 25 | ofstream fout; 26 | fin.open("sortaret.in"); 27 | fout.open("sortaret.out"); 28 | int nodeCount, edgeCount; 29 | 30 | fin >> nodeCount >> edgeCount; 31 | for(int i=1;i<=edgeCount;i++) 32 | { 33 | int node1, node2; 34 | fin >> node1 >> node2; 35 | edges[node1].push_back(node2); 36 | } 37 | 38 | for(int i=1;i<=nodeCount;i++) 39 | { 40 | if (!visited[i]) visit(i); 41 | } 42 | while(!sorted.empty()) 43 | { 44 | fout << sorted.top()<<" "; 45 | sorted.pop(); 46 | } 47 | return 0; 48 | } -------------------------------------------------------------------------------- /Year 2 Sem 1/so/lab4/prob3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | void collaz(int n) 9 | { 10 | printf("%d: ", n); 11 | while (n != 1) 12 | { 13 | printf("%d ", n); 14 | if (n % 2 == 0) 15 | n = n / 2; 16 | else 17 | n = 3 * n + 1; 18 | } 19 | printf("%d \n", n); 20 | } 21 | 22 | 23 | int main(int argc, char const* argv[]) 24 | { 25 | if (argc == 0) 26 | { 27 | printf("Numar insuficent de argumente.\n"); 28 | return -1; 29 | } 30 | printf("Starting Parent %d\n", getpid()); 31 | for (int i = 1; i <= argc; ++i) 32 | { 33 | pid_t pid = fork(); 34 | if (pid < 0) 35 | { 36 | return errno; 37 | } 38 | else 39 | if (pid == 0) 40 | { 41 | int n = atoi(argv[i]); 42 | collaz(n); 43 | printf("Done Parent %d Me %d\n", getppid(), getpid()); 44 | exit(1); 45 | } 46 | } 47 | for (int i = 1; i <= argc; ++i) 48 | wait(); 49 | printf("Done Parent %d Me %d\n", getppid(), getpid()); 50 | return 0; 51 | } -------------------------------------------------------------------------------- /Year 2 Sem 1/so/lab7/barrier.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | pthread_mutex_t mtx; 9 | sem_t sem; 10 | int nr; 11 | int n = 5; 12 | void bariera() 13 | { 14 | pthread_mutex_lock(&mtx); 15 | nr++; 16 | 17 | 18 | if(nr < n) 19 | { 20 | pthread_mutex_unlock(&mtx); 21 | sem_wait(&sem); 22 | } 23 | else 24 | { 25 | pthread_mutex_unlock(&mtx); 26 | 27 | for(int i=0; i 2 | #include 3 | #include 4 | typedef struct 5 | { 6 | char name[25]; 7 | int id; 8 | }data; 9 | 10 | data* declare() 11 | { 12 | data *x; 13 | x = malloc(10 * sizeof(data)); 14 | strcpy(x[0].name, "Mihai"); 15 | strcpy(x[1].name, "Cristi"); 16 | strcpy(x[2].name, "Andrei"); 17 | strcpy(x[3].name, "Cosmin"); 18 | 19 | x[0].id = 10; 20 | x[1].id = 5; 21 | x[2].id = 10; 22 | x[3].id = 1; 23 | return x; 24 | } 25 | 26 | void print(data *x) 27 | { 28 | for(int i=0;i<4;i++) 29 | { 30 | printf("%d %s\n",x[i].id,x[i].name); 31 | } 32 | } 33 | int comparator(const void *p, const void *q) 34 | { 35 | data x, y; 36 | x = *(data*)p; 37 | y = *(data*)q; 38 | if(x.idy.id) return 1; 40 | if(x.id==y.id) 41 | { 42 | if(x.name[0]y.name[0]) return 1; 44 | } 45 | 46 | return 0; 47 | } 48 | int main() 49 | { 50 | data *x; 51 | int size = 4; 52 | x = declare(); 53 | qsort((void*)x,size,sizeof(data),comparator); 54 | print(x); 55 | 56 | return 0; 57 | } -------------------------------------------------------------------------------- /Year 1 Sem 1/CExamenPrep/exam/qsort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int** citire_matrice(char* file_name) 5 | { 6 | int** v; 7 | int i,n,j; 8 | FILE *f = fopen(file_name, "r"); 9 | 10 | fscanf(f, "%d", &n); 11 | 12 | v = malloc((n+1) * sizeof(int*)); 13 | for (i = 1; i <= n; i++) 14 | v[i] = malloc((n+1) * sizeof(int)); 15 | 16 | for(i=1;i<=n;i++) 17 | for(j=1;j<=n;j++) 18 | fscanf(f, "%d", &v[i][j]); 19 | v[0][0] = n; 20 | 21 | return v; 22 | } 23 | void afisare_matrice(int** v) 24 | { 25 | int n, i, j; 26 | n = v[0][0]; 27 | 28 | for (i = 1; i <= n; i++) 29 | { 30 | for (j = 1; j <= n; j++) 31 | printf("%d ", v[i][j]); 32 | printf("\n"); 33 | } 34 | } 35 | 36 | int cmp(const void** p, const void** q) 37 | { 38 | int* a = (int*) *p; 39 | int* b = (int*) *q; 40 | 41 | int e1 = a[1]; 42 | int e2 = b[1]; 43 | 44 | return e1-e2; 45 | } 46 | int main() 47 | { 48 | int** v; 49 | int n; 50 | v = citire_matrice("matrice.txt"); 51 | n = v[0][0]; 52 | 53 | qsort((void*)(v+1), n, sizeof(int), cmp); 54 | 55 | afisare_matrice(v); 56 | system("pause"); 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /Year 1 Sem 1/CExamenPrep/exam/proggenerica.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int cmp_int(void* a, void* b) 5 | { 6 | int x = *(int*)a; 7 | int y = *(int*)b; 8 | printf("%d %d\n", x, y); 9 | if (x == y) return 1; 10 | return 0; 11 | } 12 | int cmp_char(void* a, void* b) 13 | { 14 | int x = *(char*)a; 15 | int y = *(char*)b; 16 | printf("%c %c\n", x, y); 17 | if (x == y) return 1; 18 | return 0; 19 | } 20 | 21 | 22 | int egale(void* a, void* b, int a_count, int b_count, int size, int(*cmp)(void*,void*)) 23 | { 24 | if (a_count != b_count) return 0; 25 | int i,n = a_count; 26 | 27 | for(i=0;i 2 | #include 3 | int degree(int x, int m[100][100], int n) 4 | { 5 | int i, deg=0; 6 | for(i=1;i<=n;i++) 7 | { 8 | if (m[x][i]) deg++; 9 | } 10 | return deg; 11 | } 12 | int edges(int m[100][100], int n) 13 | { 14 | int i, j, edg = 0; 15 | for (i = 1; i <= n; i++) 16 | for (j = i; j <= n; j++) 17 | { 18 | if (m[i][j]) edg++; 19 | } 20 | return edg; 21 | } 22 | void max_degree(int m[100][100], int n) 23 | { 24 | int max_deg = degree(1, m, n), i; 25 | 26 | for(i=2;i<=n;i++) 27 | { 28 | if (max_deg < degree(i, m, n)) 29 | max_deg = degree(i, m, n); 30 | } 31 | 32 | for (i = 1; i <= n; i++) 33 | { 34 | if (max_deg == degree(i, m, n)) std::cout << i << " "; 35 | 36 | } 37 | } 38 | int main() 39 | { 40 | std::ifstream fin("adjmatrix.txt"); 41 | int i,j,n,m[100][100] = { {0} }; 42 | fin >> n; 43 | for (i = 1; i <= n; i++) 44 | for (j = 1; j <= n; j++) 45 | fin >> m[i][j]; 46 | 47 | std::cout << degree(1, m, n) << "\n"; 48 | std::cout << edges(m, n) << "\n"; 49 | max_degree(m, n); 50 | system("pause"); 51 | return 0; 52 | } -------------------------------------------------------------------------------- /Year 1 Sem 1/randomQuickSort/Rquicksort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | void print(int n, int arr[]) 3 | { 4 | for(int i=0;i 2 | #include 3 | #include 4 | #include 5 | int visited[100001]; 6 | using namespace std; 7 | int main() { 8 | freopen("dfs.in","r",stdin); 9 | freopen("dfs.out","w",stdout); 10 | int nodeCount, edgeCount; 11 | vectoredges[100001]; 12 | cin>>nodeCount>>edgeCount; 13 | for(int i = 1; i<=edgeCount;i++){ 14 | int node1, node2; 15 | cin>>node1>>node2; 16 | edges[node1].push_back(node2); 17 | edges[node2].push_back(node1); 18 | } 19 | queue q; 20 | int conComponents=0; 21 | for(int node=1;node<=nodeCount;node++){ 22 | if(visited[node]==0){ 23 | conComponents++; 24 | q.push(node); 25 | while (!q.empty()) { 26 | int thisNode = q.front(); 27 | q.pop(); 28 | if(visited[thisNode] == 1) continue; 29 | visited[thisNode] = 1; 30 | for(auto nextNode:edges[thisNode]){ 31 | q.push(nextNode); 32 | } 33 | } 34 | 35 | } 36 | } 37 | cout< 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | void printMatrix(int v[101][101], int n, ostream &fout) 11 | { 12 | for (int i = 1; i <= n; i++) 13 | { 14 | for (int j = 1; j <= n; j++) 15 | fout << v[i][j] << " "; 16 | fout << "\n"; 17 | } 18 | } 19 | int graphMatrix[101][101], dist[101][101]; 20 | int main() 21 | { 22 | ifstream fin; 23 | ofstream fout; 24 | fin.open("royfloyd.in"); 25 | fout.open("royfloyd.out"); 26 | int n; 27 | fin >> n; 28 | 29 | for (int i = 1; i <= n; i++) 30 | { 31 | for (int j = 1; j <= n; j++) 32 | { 33 | fin >> graphMatrix[i][j]; 34 | dist[i][j] = graphMatrix[i][j]; 35 | } 36 | dist[i][i] = 0; 37 | } 38 | 39 | 40 | for (int k = 1; k <= n; k++) 41 | for (int i = 1; i <= n; i++) 42 | for (int j = 1; j <= n; j++) 43 | { 44 | if (i == j || j == k || i == k) continue; 45 | if ((dist[i][j] > dist[i][k] + dist[k][j] || dist[i][j] == 0) && dist[i][k] && dist[k][j]) 46 | { 47 | dist[i][j] = dist[i][k] + dist[k][j]; 48 | } 49 | } 50 | printMatrix(dist, n, fout); 51 | return 0; 52 | } -------------------------------------------------------------------------------- /Year 1 Sem 1/BInaryFileC/program.c: -------------------------------------------------------------------------------- 1 | /** Fishiere binare*/ 2 | #include 3 | #include 4 | int read_pos(int pos, FILE *f) 5 | { 6 | int x; 7 | fseek(f, sizeof(int)*(pos-1), SEEK_SET); 8 | fread(&x, sizeof(int), 1, f); 9 | return x; 10 | } 11 | void write_to_pos(int pos, int x, FILE *f) 12 | { 13 | 14 | fseek(f, sizeof(int)*(pos-1), SEEK_SET); 15 | fwrite(&x, sizeof(int), 1, f); 16 | } 17 | void create_rando_binaryfile() 18 | { 19 | int n, x; 20 | scanf("%d",&n); 21 | FILE *f = fopen("binar","w+b"); 22 | for(int i=1; i<=10; i++) 23 | { 24 | x = rand() % (2*n+1); 25 | x -= n; 26 | fwrite(&x, sizeof(int), 1, f); 27 | } 28 | //write_to_pos(3, 69, f); 29 | printf("%d\n", read_pos(1, f)); 30 | 31 | for(int i=1; i<=10; i++) 32 | for(int j=1; j<=10; j++) 33 | { 34 | int x, y; 35 | x = read_pos(i,f); y = read_pos(j,f); 36 | if(x 2 | #include 3 | int* creare1d() 4 | { 5 | int n; 6 | scanf("%d", &n); 7 | int* v; 8 | 9 | v = malloc((n + 1) * sizeof(int)); 10 | 11 | for (int i = 1; i <= n; i++) 12 | { 13 | scanf("%d", &v[i]); 14 | } 15 | v[0] = n; 16 | return v; 17 | } 18 | 19 | void afisare1d(int* v) 20 | { 21 | int n = v[0]; 22 | printf("\n"); 23 | 24 | for (int i = 1; i <= n; i++) 25 | printf("%d ", v[i]); 26 | 27 | printf("\n"); 28 | } 29 | int** creare2d() 30 | { 31 | int n, i, j; 32 | scanf("%d", &n); 33 | int** v; 34 | 35 | v = malloc((n + 1) * sizeof(int*)); 36 | 37 | for (i = 1; i <= n; i++) 38 | v[i] = malloc((n + 1) * sizeof(int)); 39 | 40 | for (i = 1; i <= n; i++) 41 | { 42 | for (j = 1; j <= n; j++) 43 | { 44 | scanf("%d", &v[i][j]); 45 | } 46 | } 47 | 48 | v[0][0] = n; 49 | return v; 50 | } 51 | 52 | void printare2d(int** v) 53 | { 54 | int n, i, j; 55 | 56 | n = v[0][0]; 57 | 58 | for (i = 1; i <= n; i++) 59 | { 60 | for (j = 1; j <= n; j++) 61 | { 62 | printf("%d ", v[i][j]); 63 | } 64 | printf("\n"); 65 | } 66 | 67 | 68 | } 69 | int main() 70 | { 71 | int** v; 72 | v = creare2d(); 73 | 74 | printare2d(v); 75 | system("pause"); 76 | return 0; 77 | } -------------------------------------------------------------------------------- /Year 2 Sem 1/so/lab2/prob2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int main(int argc, char const *argv[]) 8 | { 9 | int fd1, fd2; 10 | 11 | fd1 = open(argv[1], O_RDONLY); 12 | if(fd1 == -1) 13 | { 14 | printf("Eroare la desciderea primului fisier, aflat la: %s \n", argv[1]); 15 | return -1; 16 | } 17 | 18 | fd2 = open(argv[2], O_WRONLY); 19 | if(fd2 == -1) 20 | { 21 | printf("Eroare la desciderea celui de al doilea fisier, aflat la: %s \n", argv[2]); 22 | return -1; 23 | } 24 | 25 | char* buff; 26 | buff = malloc(1024 * sizeof(char)); 27 | 28 | while (read(fd1, buff, 1024 * sizeof(char)) == 1024) 29 | { 30 | size_t w = write(fd2, buff, 1024 * sizeof(char)); 31 | if (w == -1) 32 | { 33 | printf("Eroare la scrierea in fisier."); 34 | return -1; 35 | } 36 | } 37 | 38 | 39 | if (read(fd1, buff, 1024 * sizeof(char)) == -1) 40 | { 41 | printf("Eroare la citirea din primul fisier"); 42 | return - 1; 43 | } 44 | 45 | size_t w = write(fd2, buff, 1024 * sizeof(char)); 46 | if(w == -1) 47 | { 48 | printf("Eroare la scrierea in fisier.(Ultima parte)"); 49 | return -1; 50 | } 51 | 52 | close(fd1); 53 | close(fd2); 54 | 55 | return 0; 56 | } -------------------------------------------------------------------------------- /Year 1 Sem 1/Stack/Stack-Implementation/Stack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | struct stack 3 | { 4 | int val; 5 | stack *next; 6 | }; 7 | void push(int a, stack *&st) 8 | { 9 | stack *aux = new stack; 10 | aux->val = a; 11 | if (st == NULL) aux->next = NULL; 12 | else aux->next = st; 13 | st = aux; 14 | } 15 | int pop(stack *&st) 16 | { 17 | int x = st->val; 18 | st = st->next; 19 | return x; 20 | } 21 | int peek(stack *st) 22 | { 23 | int x = st->val; 24 | return x; 25 | } 26 | bool empty(stack *st) 27 | { 28 | if (st == NULL) return true; 29 | return false; 30 | } 31 | void print(stack *st) 32 | { 33 | while (!empty(st)) 34 | std::cout << pop(st) <<" "; 35 | std::cout << "\n"; 36 | } 37 | int search(int a, stack *st) 38 | { 39 | int distance = 0; 40 | while(!empty(st)) 41 | { 42 | int x = pop(st); 43 | if (x == a) return distance; 44 | distance++; 45 | } 46 | return -1; 47 | } 48 | int main() 49 | { 50 | stack *st; 51 | st = NULL; 52 | push(6, st); push(14, st); push(4, st); push(3, st); 53 | print(st); 54 | std::cout << "searching 14 " << search(14, st) << "\n"; 55 | std::cout << "searching 13 " << search(13, st) << "\n"; 56 | pop(st); pop(st); 57 | 58 | std::cout << pop(st) <<"\n"; 59 | std::cout << peek(st)<<"\n"; 60 | system("pause"); 61 | 62 | } -------------------------------------------------------------------------------- /Year 1 Sem 1/MergeSort/mergesort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void merge(int v[], int l, int m, int r) 4 | { 5 | int i, j, k; 6 | int n1 = m - l + 1; 7 | int n2 = r - m; 8 | int L[100], R[100]; 9 | 10 | for (i = 0; i < n1; i++) 11 | L[i] = v[l + i]; 12 | for (j = 0; j < n2; j++) 13 | R[j] = v[m + 1 + j]; 14 | 15 | int a_it = 1, b_it = 1; 16 | 17 | i = 0; 18 | j = 0; 19 | k = l; 20 | while (i < n1 && j < n2) 21 | { 22 | if (L[i] <= R[j]) 23 | { 24 | v[k] = L[i]; 25 | i++; 26 | } 27 | else 28 | { 29 | v[k] = R[j]; 30 | j++; 31 | } 32 | k++; 33 | } 34 | while (i < n1) 35 | { 36 | v[k] = L[i]; 37 | i++; 38 | k++; 39 | } 40 | while (j < n2) 41 | { 42 | v[k] = R[j]; 43 | j++; 44 | k++; 45 | } 46 | 47 | } 48 | void merge_sort (int v[], int l, int r) 49 | { 50 | if(l> n; 66 | for (i = 0; i < n; i++) 67 | std::cin >> a[i]; 68 | 69 | merge_sort(a, 0, n - 1); 70 | 71 | for (i = 0; i < n ; i++) 72 | { 73 | std::cout << a[i] << " "; 74 | } 75 | 76 | system("pause"); 77 | return 0; 78 | } -------------------------------------------------------------------------------- /Year 1 Sem 2/Graph/DisiojntSetDataStructure/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | int fatherOf[100002]; 6 | int grade[100002]; 7 | int findFather(int node){ 8 | if(fatherOf[node] == node){ 9 | return node; 10 | } 11 | fatherOf[node] = findFather(fatherOf[node]); 12 | return fatherOf[node]; 13 | } 14 | int Disjoint(int o, int x, int y) 15 | { 16 | int A,B; 17 | A = findFather(x); B = findFather(y); 18 | if(o==2 && A==B && A != 0) return true; 19 | else if(o==2) return false; 20 | if(A == B) return false; 21 | if(grade[A] < grade[B]){ 22 | fatherOf[A] = B; 23 | grade[B] += grade[A]; 24 | } else{ 25 | fatherOf[B] = A; 26 | grade[A] += grade[B]; 27 | } 28 | return false; 29 | } 30 | int main() { 31 | int n,m,x,y,A,B,o; 32 | ifstream f("disjoint.in"); 33 | ofstream g("disjoint.out"); 34 | f>>n>>m; 35 | for(int i=1;i<=n;i++){ 36 | fatherOf[i] = i; 37 | grade[i] = 1; 38 | } 39 | for(int i=1;i<=m;i++){ 40 | f>>o; 41 | f>>x>>y; 42 | int r = Disjoint(o, x, y); 43 | if(o == 2){ 44 | if(r == false) g<<"NU\n"; 45 | else g<<"DA\n"; 46 | } 47 | 48 | } 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /Year 1 Sem 1/DFS/DFS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "queue.h" 4 | struct adj_list 5 | { 6 | int node; 7 | adj_list *next; 8 | }; 9 | void adj_list_add(adj_list *&l, int n) 10 | { 11 | adj_list *aux = new adj_list; 12 | aux->node = n; 13 | if (l == NULL) 14 | { 15 | aux->next = NULL; 16 | } 17 | else 18 | aux->next = l; 19 | 20 | l = aux; 21 | } 22 | int main() 23 | { 24 | std::ifstream fin("adjlist.txt"); 25 | int n, m, i, j, visited_ar[100] = {0}; 26 | fin >> n; 27 | fin >> m; 28 | 29 | adj_list *lv[100]; 30 | for (i = 1; i <= n; i++) 31 | lv[i] = NULL; 32 | 33 | for(i=1;i<=m;i++) 34 | { 35 | int a, b; 36 | fin >> a >> b; 37 | adj_list_add(lv[a], b); 38 | adj_list_add(lv[b], a); 39 | } 40 | queue q; 41 | q.first_q = q.last_q = NULL; 42 | 43 | for(i=1;i<=n;i++) 44 | { 45 | if (visited_ar[i] == 0) 46 | { 47 | queue_push(i, q); 48 | std::cout << "\n"; 49 | 50 | } 51 | while(!queue_empty(q)) 52 | { 53 | int x; 54 | x = queue_pop(q); 55 | std::cout << x << " "; 56 | 57 | visited_ar[x] = 1; 58 | adj_list *aux; 59 | aux = lv[x]; 60 | while(aux != NULL) 61 | { 62 | if(visited_ar[aux->node] == 0) queue_push(aux->node, q); 63 | aux = aux->next; 64 | } 65 | 66 | } 67 | } 68 | std::cout << "\n"; 69 | system("pause"); 70 | return 0; 71 | } -------------------------------------------------------------------------------- /Year 1 Sem 2/Graph/BFS/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | int visited[100001]; 8 | int minDistance[100001]; 9 | int main() { 10 | ifstream fin; 11 | ofstream fout; 12 | fin.open("bfs.in"); 13 | fout.open("bfs.out"); 14 | int nodeCount, edgeCount, startingNode; 15 | fin>>nodeCount>>edgeCount>>startingNode; 16 | vector edges[100001]; 17 | 18 | for(int i=1;i<=edgeCount;i++){ 19 | int node1,node2; 20 | fin>>node1>>node2; 21 | edges[node1].push_back(node2); 22 | } 23 | for(int i=1;i<=nodeCount;i++) 24 | minDistance[i] = INT_MAX; 25 | 26 | queue st; 27 | st.push(startingNode); 28 | minDistance[startingNode] = 0; 29 | while(!st.empty()){ 30 | int thisNode = st.front(); 31 | st.pop(); 32 | if(visited[thisNode]==1) continue; 33 | visited[thisNode]=1; 34 | for(auto nextNode:edges[thisNode]){ 35 | st.push(nextNode); 36 | if(minDistance[nextNode] > minDistance[thisNode] + 1) 37 | minDistance[nextNode] = minDistance[thisNode] + 1; 38 | } 39 | } 40 | for(int i=1;i<=nodeCount;i++){ 41 | if(minDistance[i] == INT_MAX) minDistance[i] = -1; 42 | cout< 2 | 3 |
4 | % distance between points 5 | distance((X,Y),(A,B),R) :- R is sqrt((A-X)**2 + (B-Y)**2). 6 |
7 | 8 |
9 | % Fibonacci sequence 10 | fib(0,1). 11 | fib(1,1). 12 | fib(N, X) :- N>1, N1 is N-1, N2 is N-2, fib(N1, X1), fib(N2,X2), X is X1 + X2. 13 |
14 | 15 |
16 | % Fibonacci sequence more efficient 17 | % the number we search, N, N-1 18 | fibe(0,1,0). 19 | fibe(1,1,1). 20 | fibe(N,X,Y) :- N>1, N1 is N-1, fibe(N1, Y, Z), X is Y+Z. % z is the N-2 number 21 | % the question asked to use only two parameters so we improvise 22 | fibe2(N,X) :- fibe(N,X,_). 23 |
24 | 25 |
26 | % write N characters on a line 27 | line(0, _) :- nl. % write new line if N reaches 0 28 | % general case 29 | line(N, Char) :- N > 0, write(Char), Pred is (N - 1), line(Pred, Char). 30 | 31 | square(_, 0, _). 32 | square(NOriginal, N, Char) :- N > 0, line(NOriginal, Char), Pred is (N-1), square(NOriginal,Pred, Char). 33 | 34 | square2(N, Char) :- square(N, N, Char). 35 |
36 | 37 |
38 | % verify if all the elements al 'a' 39 | all_a([]). 40 | % T is talil (all the elements other than the first one) 41 | all_a([a|T]) :- all_a(T). 42 |
43 | 44 | 45 | -------------------------------------------------------------------------------- /Year 2 Sem 1/so/lab7/mutex.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int available_resources = 5; 8 | pthread_mutex_t mtx; 9 | 10 | int decrease_count(int count) 11 | { 12 | pthread_mutex_lock(&mtx); 13 | if (available_resources < count) 14 | { 15 | pthread_mutex_unlock(&mtx); 16 | return -1; 17 | } 18 | else 19 | { 20 | available_resources -= count; 21 | printf("Got: %d , remaining: %d \n",count, available_resources); 22 | } 23 | pthread_mutex_unlock(&mtx); 24 | return 1; 25 | } 26 | int increase_count(int count) 27 | { pthread_mutex_lock(&mtx); 28 | available_resources += count; 29 | printf("Released: %d , remaining: %d \n",count, available_resources); 30 | pthread_mutex_unlock(&mtx); 31 | return 0; 32 | } 33 | void thread_init(void *arg) 34 | { 35 | int cnt = (int) arg; 36 | if(decrease_count(cnt) == 1); 37 | increase_count(cnt); 38 | return NULL; 39 | 40 | } 41 | int main() 42 | { 43 | 44 | int count; 45 | pthread_t thr[6]; 46 | for (int i=0; i<6; i++) 47 | { 48 | count = rand() % 5; 49 | pthread_create(&thr[i], NULL, thread_init, count); 50 | } 51 | 52 | for (int i=0; i <6; i++) 53 | { 54 | pthread_join(thr[i], NULL); 55 | } 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /Year 1 Sem 2/Graph/MST/Prim.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int visited[200001]; 10 | vector>edges[200001]; 11 | priority_queue> > q; 12 | int main() 13 | { 14 | ifstream fin; 15 | ofstream fout; 16 | fin.open("apm.in"); 17 | fout.open("apm.out"); 18 | int nodeCount, edgeCount; 19 | 20 | fin >> nodeCount >> edgeCount; 21 | for(int i=1;i<=edgeCount;i++) 22 | { 23 | int node1, node2, cost; 24 | fin >> node1 >> node2 >> cost;; 25 | edges[node1].push_back(make_pair(cost*(-1), node2)); 26 | edges[node2].push_back(make_pair(cost*(-1), node1)); 27 | } 28 | 29 | q.push(make_pair(0, make_pair(0,1))); 30 | queue> > mst; 31 | int totalCost = 0; 32 | while(!q.empty()) 33 | { 34 | auto thisNode = q.top(); 35 | q.pop(); 36 | if(visited[thisNode.second.second])continue; 37 | visited[thisNode.second.second] = 1; 38 | mst.push(thisNode); 39 | totalCost += thisNode.first*(-1); 40 | for (auto nextNode : edges[thisNode.second.second]) 41 | q.push(make_pair(nextNode.first, make_pair(thisNode.second.second, nextNode.second))); 42 | } 43 | fout << totalCost << "\n"; 44 | fout << mst.size()-1 << "\n"; 45 | mst.pop(); 46 | while(!mst.empty()) 47 | { 48 | auto edge = mst.front(); 49 | mst.pop(); 50 | fout << edge.second.first << " " << edge.second.second << "\n"; 51 | } 52 | 53 | return 0; 54 | } -------------------------------------------------------------------------------- /Year 1 Sem 2/Graph/dijkstra/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | int visited[500001]; 8 | int roadVec[500001]; 9 | int main() { 10 | ifstream fin; 11 | ofstream fout; 12 | fin.open("dijkstra.in"); 13 | fout.open("dijkstra.out"); 14 | int nodeCount, edgeCount; 15 | fin>>nodeCount>>edgeCount; 16 | vector>edges[50001]; 17 | 18 | for(int i=1;i<=edgeCount;i++){ 19 | int node1,node2,cost; 20 | fin>>node1>>node2>>cost; 21 | edges[node1].push_back(make_pair(cost*(-1), node2)); 22 | } 23 | for(int i=1;i<=nodeCount;i++) 24 | roadVec[i] = INT_MAX; 25 | priority_queue>q; 26 | q.push(make_pair(0, 1)); 27 | roadVec[1] = 0; 28 | while (!q.empty()) { 29 | auto thisNode = q.top(); 30 | q.pop(); 31 | if(visited[thisNode.second] == 1) continue; 32 | visited[thisNode.second] = 1; 33 | 34 | for(auto nextNode : edges[thisNode.second]){ 35 | int cost = roadVec[thisNode.second] + nextNode.first*(-1); 36 | if(cost < roadVec[nextNode.second]){ 37 | roadVec[nextNode.second] = cost; 38 | q.push(make_pair(cost*(-1), nextNode.second)); 39 | } 40 | } 41 | } 42 | for(int i=2;i<=nodeCount;i++){ 43 | if(roadVec[i]==INT_MAX) roadVec[i] = 0; 44 | fout< 2 | using namespace std; 3 | struct list 4 | { 5 | int val; 6 | list *next; 7 | }; 8 | void insert_in_list(list *&first, int x) 9 | { 10 | list *aux = new list; 11 | aux->val = x; 12 | if (first == nullptr) aux->next = nullptr; 13 | else aux->next = first; 14 | first = aux; 15 | } 16 | 17 | void print_list(list *first) 18 | { 19 | list *aux = first; 20 | 21 | while (aux != nullptr) 22 | { 23 | cout << aux->val << " "; 24 | aux = aux->next; 25 | } 26 | cout << "\n"; 27 | } 28 | 29 | void fliplist1(list *firstlist) 30 | { 31 | list *firstlist2=nullptr; 32 | list *aux = firstlist; 33 | while (aux) 34 | { 35 | insert_in_list(firstlist2, aux->val); 36 | aux = aux->next; 37 | } 38 | print_list(firstlist2); 39 | } 40 | 41 | void fliplist2(list *firstlist) 42 | { 43 | list *aux = firstlist; 44 | int n=0; 45 | while (aux) aux = aux->next, n++; 46 | for (int i = 1; i < n; i++) 47 | { 48 | aux = firstlist; 49 | for (int j = 1; j <= n - i; j++) 50 | { 51 | swap(aux->val, aux->next->val); 52 | aux = aux->next; 53 | } 54 | 55 | } 56 | print_list(firstlist); 57 | } 58 | int main() 59 | { 60 | list *firstlist; 61 | 62 | firstlist = nullptr; 63 | insert_in_list(firstlist,5); 64 | insert_in_list(firstlist,6); 65 | insert_in_list(firstlist,1); 66 | insert_in_list(firstlist, 10); 67 | insert_in_list(firstlist, 3); 68 | 69 | print_list(firstlist); 70 | 71 | fliplist1(firstlist); 72 | fliplist2(firstlist); 73 | system("pause"); 74 | return 0; 75 | } -------------------------------------------------------------------------------- /Year 1 Sem 1/CExamenPrep/exam/proggenericainlocuire.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int cmp_int(void* a, void* b) 5 | { 6 | int x = *(int*)a; 7 | int y = *(int*)b; 8 | if (x == y) return 1; 9 | return 0; 10 | } 11 | int cmp_char(void* a, void* b) 12 | { 13 | int x = *(char*)a; 14 | int y = *(char*)b; 15 | if (x == y) return 1; 16 | return 0; 17 | } 18 | 19 | int swap_int(void* a, void* b) 20 | { 21 | int x = *(int*)a; 22 | int y = *(int*)b; 23 | *(int*)a = y; 24 | //*(int*)b = x; 25 | return 0; 26 | } 27 | 28 | int swap_char(void* a, void* b) 29 | { 30 | int x = *(char*)a; 31 | int y = *(char*)b; 32 | *(char*)a = y; 33 | //*(char*)b = x; 34 | return 0; 35 | } 36 | void inlocuire(void* v, void* a, void *b, int size,int count, void(*swap)(void*,void*), int(*cmp)(void*,void*)) 37 | { 38 | int i; 39 | for(i=0;i 2 | #include 3 | #include "queue.h" 4 | std::ifstream fin("matrix.txt"); 5 | 6 | void matrix_read(int v[100][100], int n) 7 | { 8 | for (int i = 1; i <= n; i++) 9 | for (int j = 1; j <= n; j++) 10 | fin >> v[i][j]; 11 | } 12 | void matrix_print(int v[100][100], int n) 13 | { 14 | for (int i = 1; i <= n; i++) 15 | { 16 | for (int j = 1; j <= n; j++) 17 | std::cout << v[i][j] << " "; 18 | std::cout << "\n"; 19 | } 20 | 21 | } 22 | void backtrace(int v[100][100], int n, int k, int i, int j) 23 | { 24 | int i_adiacent[] = { -1,1,0,0 }; 25 | int j_adiacent[] = { 0,0,-1,1 }; 26 | queue q; q.first_q = NULL; q.last_q = NULL; 27 | queue_push(std::make_pair(i, j), q); 28 | std::pair ce; 29 | while(!queue_empty(q)) 30 | { 31 | ce = queue_pop(q); 32 | i = ce.first; j = ce.second; 33 | v[i][j] = k; 34 | for(int l=0;l<=3;l++) 35 | { 36 | int newi = i + i_adiacent[l], newj = j + j_adiacent[l]; 37 | 38 | if (v[newi][newj] == 1) queue_push(std::make_pair(newi, newj), q); 39 | 40 | } 41 | } 42 | 43 | } 44 | void lee(int v[100][100], int n) 45 | { 46 | int k = 1; 47 | 48 | for (int i = 1; i <= n; i++) 49 | for (int j = 1; j <= n; j++) 50 | { 51 | if (v[i][j] == 1) 52 | { 53 | k++; backtrace(v, n, k, i, j); 54 | } 55 | 56 | 57 | 58 | } 59 | } 60 | 61 | int main() 62 | { 63 | int v[100][100], n; 64 | fin >> n; 65 | matrix_read(v, n); 66 | lee(v, n); 67 | matrix_print(v, n); 68 | system("pause"); 69 | return 0; 70 | } -------------------------------------------------------------------------------- /Year 1 Sem 1/Queue/Queue-Implementation/Queue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct queuenode 4 | { 5 | int val; 6 | queuenode *next; 7 | }; 8 | struct queue 9 | { 10 | queuenode *first_q; 11 | queuenode *last_q; 12 | }; 13 | 14 | void queue_push(int x, queue &q) 15 | { 16 | queuenode *aux = new queuenode; 17 | aux->val = x; aux->next = NULL; 18 | 19 | if (q.first_q == NULL) q.first_q = q.last_q = aux; 20 | else 21 | { 22 | q.last_q->next = aux; 23 | q.last_q = aux; 24 | } 25 | 26 | 27 | } 28 | int queue_pop(queue &q) 29 | { 30 | int val = q.first_q->val; 31 | q.first_q = q.first_q->next; 32 | return val; 33 | } 34 | int queue_peek(queue q) 35 | { 36 | int val = q.first_q->val; 37 | return val; 38 | } 39 | bool queue_empty(queue q) 40 | { 41 | if (q.first_q == NULL) return true; 42 | else return false; 43 | } 44 | int queue_search(int x, queue q) 45 | { 46 | int distance=0; 47 | 48 | while(!queue_empty(q)) 49 | { 50 | int a = queue_pop(q); 51 | 52 | if (a == x) return distance; 53 | distance++; 54 | } 55 | return 0; 56 | } 57 | void queue_print(queue q) 58 | { 59 | while(!queue_empty(q)) 60 | { 61 | std::cout << queue_pop(q) << " "; 62 | } 63 | std::cout << "\n"; 64 | } 65 | int main() 66 | { 67 | 68 | queue q; 69 | q.first_q = q.last_q = NULL; 70 | 71 | queue_push(12, q); 72 | queue_push(4, q); 73 | queue_push(2, q); 74 | queue_push(9, q); 75 | queue_push(5, q); 76 | 77 | queue_print(q); 78 | std::cout << queue_search(2,q) << "\n"; 79 | std::cout << queue_pop(q) << " "; 80 | std::cout << queue_pop(q) << " "; 81 | 82 | system("pause"); 83 | return 0; 84 | } -------------------------------------------------------------------------------- /Year 2 Sem 1/so/lab6/matrix.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | int m1[50][50], m2[50][50], m3[50][50],n, m, p; 9 | 10 | void *myThreadFun(void *vargp) 11 | { 12 | int *a = (int*)vargp; 13 | int i = a[1]; 14 | int j = a[2]; 15 | m3[i][j] = 0; 16 | for (int k = 1; k <= n; k++) 17 | m3[i][j] += m1[i][k] * m2[k][j]; 18 | 19 | return NULL; 20 | } 21 | 22 | void random_matrix(){ 23 | 24 | srand(time(NULL)); 25 | for(int i=1;i<=m;i++) 26 | for(int j=1;j<=n;j++) 27 | { 28 | m1[i][j] = rand() % 100; 29 | } 30 | 31 | for(int i=1; i<=n;i++) 32 | for(int j=1; j<=p;j++) 33 | { 34 | m2[i][j] = rand() % 100; 35 | } 36 | 37 | } 38 | int main() 39 | { 40 | n = 2; 41 | m = 3; 42 | p = 2; 43 | random_matrix(); 44 | pthread_t thread_ids[100]; 45 | int k=0; 46 | for (int i=1;i<=m;i++) 47 | for(int j=1;j<=p;j++) 48 | { 49 | pthread_t thread_id; 50 | thread_ids[k++] = thread_id; 51 | 52 | int *a = malloc(5 * sizeof(int) ); 53 | 54 | a[1] = i; 55 | a[2] = j; 56 | pthread_create(&thread_id, NULL, myThreadFun, a); 57 | } 58 | 59 | for(int i=1; i 2 | #include 3 | 4 | struct Btreenode 5 | { 6 | int key; 7 | Btreenode *right; 8 | Btreenode *left; 9 | }; 10 | void Btree_insert(Btreenode *&tree, int x) 11 | { 12 | Btreenode *aux = new Btreenode; 13 | aux->key = x; 14 | aux->left = NULL; 15 | aux->right = NULL; 16 | if(tree == NULL) 17 | { 18 | tree = aux; 19 | }else 20 | { 21 | std::queue q; 22 | q.push(tree); 23 | while(!q.empty()) 24 | { 25 | Btreenode *temp = q.front(); 26 | q.pop(); 27 | if (temp->left == NULL) 28 | { 29 | temp->left = aux; return; 30 | } 31 | else 32 | if (temp->right == NULL) 33 | { 34 | temp->right = aux; return; 35 | } 36 | else 37 | { 38 | q.push(temp->left); 39 | q.push(temp->right); 40 | } 41 | } 42 | } 43 | } 44 | void inorder(Btreenode *temp) 45 | { 46 | if (!temp) 47 | return; 48 | inorder(temp->left); 49 | std::cout << temp->key << " "; 50 | inorder(temp->right); 51 | } 52 | void preorder(Btreenode *temp) 53 | { 54 | if (!temp) 55 | return; 56 | std::cout << temp->key<< " "; 57 | preorder(temp->left); 58 | preorder(temp->right); 59 | 60 | 61 | } 62 | void postorder(Btreenode *temp) 63 | { 64 | if (!temp) 65 | return; 66 | postorder(temp->left); 67 | postorder(temp->right); 68 | std::cout << temp->key << " "; 69 | } 70 | int main() 71 | { 72 | Btreenode *tree; 73 | tree = NULL; 74 | Btree_insert(tree, 1); 75 | Btree_insert(tree, 2); 76 | Btree_insert(tree, 3); 77 | Btree_insert(tree, 4); 78 | Btree_insert(tree, 5); 79 | Btree_insert(tree, 6); 80 | 81 | postorder(tree); 82 | system("pause"); 83 | return 0; 84 | } -------------------------------------------------------------------------------- /Year 1 Sem 1/HeapSort/heapsort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void heapify(int heap[], int n) 7 | { 8 | 9 | while(heap[n/2] < heap[n] && n!=1) 10 | { 11 | std::swap(heap[n / 2], heap[n]); 12 | n = n / 2; 13 | } 14 | } 15 | void heap_add(int heap[], int &n, int x) 16 | { 17 | heap[++n] = x; 18 | heapify(heap, n); 19 | } 20 | void heap_print(int heap[], int n) 21 | { 22 | for (int i = 1; i <= n; i++) std::cout << heap[i]<<" "; 23 | std::cout << "\n"; 24 | } 25 | int heap_delete_first_node(int heap[], int &n) 26 | { 27 | int max = heap[1]; 28 | std::swap(heap[1], heap[n]); 29 | n--; 30 | int i = 1; 31 | while(1) 32 | { 33 | 34 | int son1 = INT_MIN, son2 = INT_MIN, maxson, maxsoni; 35 | if (2 * i <= n) son1 = heap[2 * i]; 36 | if (2 * i + 1 <= n) son2 = heap[2 * i + 1]; 37 | 38 | maxson = std::max(son1, son2); 39 | if (maxson == son1) maxsoni = 2 * i; 40 | else maxsoni = 2 * i + 1; 41 | if (heap[i] < maxson) std::swap(heap[i], heap[maxsoni]); 42 | else break; 43 | i = maxsoni; 44 | } 45 | return max; 46 | } 47 | 48 | void heap_sort(int heap[], int n) 49 | { 50 | while (n) 51 | { 52 | int x = heap_delete_first_node(heap, n); 53 | std::cout << x << " "; 54 | } 55 | } 56 | int main() 57 | { 58 | int heap[100], n; 59 | n = 0; 60 | heap_add(heap, n, 104); 61 | heap_add(heap, n, 32); 62 | heap_add(heap, n, 64); 63 | heap_add(heap, n, 30); 64 | heap_add(heap, n, 19); 65 | heap_add(heap, n, 48); 66 | heap_add(heap, n, 15); 67 | heap_add(heap, n, 7); 68 | heap_add(heap, n, 10); 69 | heap_add(heap, n, 8); 70 | 71 | 72 | heap_sort(heap, n); 73 | system("pause"); 74 | return 0; 75 | } -------------------------------------------------------------------------------- /Year 1 Sem 1/CExamenPrep/lab/movieDB.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | typedef struct 4 | { 5 | int hours; 6 | int minutes; 7 | }time; 8 | typedef struct 9 | { 10 | char title[20]; 11 | int relese_year; 12 | float imdb_rate; 13 | time duration; 14 | }movie; 15 | 16 | int database_add(char *moviestxt_filename, char *database_filename) 17 | { 18 | FILE *txt = fopen(moviestxt_filename,"r"); 19 | FILE *bin = fopen(database_filename,"wb"); 20 | if(bin == NULL) { printf("Binary file open error\n"); return 0;} 21 | if(txt == NULL) {printf("txt file open error\n"); return 0;} 22 | movie m; int i=1; 23 | while(1) 24 | { 25 | fscanf(txt,"%s",&m.title); 26 | fscanf(txt,"%d",&m.relese_year); 27 | fscanf(txt,"%f",&m.imdb_rate); 28 | fscanf(txt,"%d",&m.duration.hours); 29 | fscanf(txt,"%d",&m.duration.minutes); 30 | fseek(bin, 0 , SEEK_END); 31 | fwrite(&m, sizeof(m), 1, bin); 32 | if(fwrite == NULL) printf("NUBUN\n"); 33 | if(feof(txt)) break; 34 | 35 | } 36 | fclose(bin); 37 | return 1; 38 | } 39 | int* database_read(char *database_filename) 40 | { 41 | FILE *bin = fopen(database_filename,"rb"); 42 | if(bin == NULL){printf("Binary file open error\n"); return 0;} 43 | fseek(bin, 0, SEEK_END); 44 | int size = ftell(bin); //Here I get the wrong size 45 | printf("%d\n",size); 46 | 47 | rewind(bin); 48 | movie *movie_list; 49 | int movie_count = (size / sizeof(movie))+1; 50 | movie_list = malloc(movie_count * sizeof(movie)); 51 | fread(movie_list,sizeof(movie),movie_count,bin); 52 | printf("%s\n",movie_list[3].title); 53 | 54 | } 55 | int main() 56 | { 57 | movie *v; 58 | database_add("movies.txt","db"); 59 | database_read("db"); 60 | return 0; 61 | } -------------------------------------------------------------------------------- /Year 2 Sem 1/so/lab5/sharedmemory.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | 12 | int collatz(int* buffer, int n) 13 | { 14 | buffer[0] = n; 15 | int k = 1; 16 | while (n != 1) 17 | { 18 | if (n % 2 == 0) 19 | n = n / 2; 20 | else 21 | n = 3 * n + 1; 22 | buffer[k] = n; 23 | k++; 24 | } 25 | return 0; 26 | } 27 | 28 | 29 | 30 | 31 | int main(int argc, char* argv[]) 32 | { 33 | char shm_name[] = "/collatz"; 34 | int shm_fd; 35 | 36 | shm_fd = shm_open(shm_name, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR); 37 | size_t shm_size = 2048 * argc; 38 | ftruncate(shm_fd, shm_size); 39 | int* addr = mmap(0, shm_size, PROT_WRITE, MAP_SHARED, shm_fd, 0); 40 | 41 | printf("Starting parent %d\n", getpid()); 42 | 43 | for (int i = 1; i < argc; i++) { 44 | pid_t pid = fork(); 45 | int n = atoi(argv[i]); 46 | 47 | if (pid < 0) { 48 | printf("Fork failed"); 49 | return errno; 50 | } 51 | else if (pid == 0) { 52 | printf("Done parent %d Me %d\n", getppid(), getpid()); 53 | int* buffer = addr + (i - 1) * 2048 / sizeof(int); 54 | collatz(buffer, n); 55 | return 0; 56 | } 57 | } 58 | 59 | for (int i = 0; i < argc; i++) { 60 | wait(NULL); 61 | } 62 | 63 | for (int i = 1; i < argc; i++) { 64 | int* buffer = addr + (i - 1) * 2048 / sizeof(int); 65 | int j = 0; 66 | printf("%d: ", buffer[j]); 67 | 68 | while (buffer[j] != 1) { 69 | printf("%d ", buffer[j]); 70 | j++; 71 | } 72 | printf("\n"); 73 | 74 | } 75 | shm_unlink("/collatz"); 76 | return 0; 77 | } 78 | -------------------------------------------------------------------------------- /Year 1 Sem 1/Stack/ap/ap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct stack 4 | { 5 | int val; 6 | stack *next; 7 | }; 8 | void push(int a, stack *&st) 9 | { 10 | stack *aux = new stack; 11 | aux->val = a; 12 | if (st == NULL) aux->next = NULL; 13 | else aux->next = st; 14 | st = aux; 15 | } 16 | int pop(stack *&st) 17 | { 18 | int x = st->val; 19 | st = st->next; 20 | return x; 21 | } 22 | int peek(stack *st) 23 | { 24 | int x = st->val; 25 | return x; 26 | } 27 | bool empty(stack *st) 28 | { 29 | if (st == NULL) return true; 30 | return false; 31 | } 32 | void print(stack *st) 33 | { 34 | while (!empty(st)) 35 | std::cout << pop(st) << " "; 36 | std::cout << "\n"; 37 | } 38 | int search(int a, stack *st) 39 | { 40 | int distance = 0; 41 | while (!empty(st)) 42 | { 43 | int x = pop(st); 44 | if (x == a) return distance; 45 | distance++; 46 | } 47 | return -1; 48 | } 49 | int main() 50 | { 51 | stack *st; 52 | st = NULL; 53 | std::ifstream fin("text.txt"); 54 | char c,cd; 55 | int o; 56 | fin >> c; 57 | if (c == 'a')o = 1; else o = 0; 58 | push(o, st); 59 | while(fin>>c) 60 | { 61 | if (o == 1) 62 | { 63 | if (c == 'a')cd = 1; else cd = 0; 64 | if (cd == 1) push(1, st); 65 | else 66 | { 67 | if(!empty(st)) pop(st); 68 | else 69 | { 70 | o = 0; 71 | push(0, st); 72 | } 73 | } 74 | } 75 | else 76 | if(o == 0) 77 | { 78 | if (c == 'a') cd = 1; else cd = 0; 79 | if (cd == 0) push(0, st); 80 | else 81 | { 82 | if(!empty(st)) pop(st); 83 | else 84 | { 85 | o = 1; 86 | push(1, st); 87 | } 88 | } 89 | 90 | } 91 | 92 | 93 | } 94 | std::cout << empty(st); 95 | 96 | 97 | system("pause"); 98 | 99 | } -------------------------------------------------------------------------------- /Year 1 Sem 1/Stack/check_if_correct_brackets/brackets.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct stack 5 | { 6 | int val; 7 | stack *next; 8 | }; 9 | void push(int a, stack *&st) 10 | { 11 | stack *aux = new stack; 12 | aux->val = a; 13 | if (st == NULL) aux->next = NULL; 14 | else aux->next = st; 15 | st = aux; 16 | } 17 | int pop(stack *&st) 18 | { 19 | int x = st->val; 20 | st = st->next; 21 | return x; 22 | } 23 | int peek(stack *st) 24 | { 25 | int x = st->val; 26 | return x; 27 | } 28 | bool empty(stack *st) 29 | { 30 | if (st == NULL) return true; 31 | return false; 32 | } 33 | void print(stack *st) 34 | { 35 | while (!empty(st)) 36 | std::cout << pop(st) << " "; 37 | std::cout << "\n"; 38 | } 39 | int search(int a, stack *st) 40 | { 41 | int distance = 0; 42 | while (!empty(st)) 43 | { 44 | int x = pop(st); 45 | if (x == a) return distance; 46 | distance++; 47 | } 48 | return -1; 49 | } 50 | 51 | int char_to_int(char c) 52 | { 53 | if (c == '(') return 1; 54 | else return 0; 55 | } 56 | int main() 57 | { 58 | std::ifstream fin("brackets.txt"); 59 | stack *st; 60 | char c; 61 | int o, poz = 0; 62 | st = NULL; 63 | 64 | fin >> c; 65 | o = char_to_int(c); 66 | if (o == 0) 67 | { 68 | std::cout << "Incorrect brackets - position 0 "; 69 | system("pause"); 70 | return 0; 71 | } 72 | else 73 | push(1, st); 74 | 75 | while(fin>>c) 76 | { 77 | o = char_to_int(c); poz++; 78 | if(o == 0 && empty(st)) 79 | { 80 | std::cout << "Incorrect brackets - position "< 2 | 3 |
4 | father(eddard,sansa). 5 | father(eddard,jonsnow). 6 | mother(catelyn,sansa). 7 | mother(wylla,jonsnow). 8 | stark(eddard). 9 | stark(catelyn). 10 | stark(X) :- father(Y,X), stark(Y). 11 |
12 | 13 |
14 | right(cersei, tywin). 15 | right(janos, cersei). 16 | right(tyrion, janos). 17 | right(pycele, tyrion). 18 | right(varys, pycele). 19 | right(baelish, varys). 20 | right(tywin, baelish). 21 | 22 | sitsRightof(X,Y) :- right(X,Y). 23 | sitsLeftOf(X,Y) :- right(Y,X). 24 | are_neighbors_of(X,Y,Z) :- sitsLeftOf(X,Z), sitsRightof(Y,Z). 25 | next_to_each_other(X,Y) :- sitsLeftOf(X,Y); sitsLeftOf(Y,X); sitsRightof(X,Y); sitsRightof(Y,X). 26 | /* Doua scaune la dreapta lui cersei sitsRightof(X, cersei), sitsRightof(Y, X) */ 27 | 28 | 29 |
30 | 31 |
32 | %--------------------------------- 33 | % Jon Snow and Daenerys Targaryen 34 | %--------------------------------- 35 | 36 | male(rickardStark). 37 | male(eddardStark). 38 | male(brandonStark). 39 | male(benjenStark). 40 | male(robbStark). 41 | male(aerysTargaryen). 42 | male(rhaegarTargaryen). 43 | 44 | 45 | %--------------------------- 46 | 47 | female(lyarraStark). 48 | female(catelynStark). 49 | female(lyannaStark). 50 | female(sansaStark). 51 | female(aryaStark). 52 | female(rhaellaTargaryen). 53 | female(eliaTargaryen). 54 | 55 | %------------------------ 56 | 57 | related(lyarraStark, lyannaStark). 58 | related(lyarraStark, eddardStark). 59 | related(lyarraStark, brandonStark). 60 | related(lyarraStark, benjenStark). 61 | related(lyarraStark, eddardStark). 62 | 63 | related(rickardStark, lyannaStark). 64 | related(rickardStark, eddardStark). 65 | related(rickardStark, brandonStark). 66 | related(rickardStark, benjenStark). 67 | related(rickardStark, eddardStark). 68 | 69 | motherOf(X,Y) :- related(X,Y), female(X). 70 | fatherOf(X,Y) :- related(X,Y), male(X). 71 | 72 | 73 |
74 | 75 | 76 | -------------------------------------------------------------------------------- /Year 1 Sem 2/Graph/MST/Kruskal.cpp: -------------------------------------------------------------------------------- 1 | //KRUSKAL 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | int visited[200001]; 11 | int fatherOf[200001]; 12 | vector>edges[200001]; 13 | priority_queue> > q; 14 | void merge(int node1, int node2) 15 | { 16 | if (fatherOf[node1] == 0 && fatherOf[node2] == 0) 17 | fatherOf[node2] = node1, fatherOf[node1] = node1; 18 | else 19 | if (fatherOf[node1] == 0) 20 | fatherOf[node1] = fatherOf[node2]; 21 | else 22 | if (fatherOf[node2] == 0) 23 | fatherOf[node2] = fatherOf[node1]; 24 | else 25 | fatherOf[fatherOf[node1]] = node2; 26 | } 27 | int findFather(int node) 28 | { 29 | if (node == fatherOf[node]) return node; 30 | fatherOf[node] = findFather(fatherOf[node]); 31 | return fatherOf[node]; 32 | } 33 | int main() 34 | { 35 | ifstream fin; 36 | ofstream fout; 37 | fin.open("apm.in"); 38 | fout.open("apm.out"); 39 | int nodeCount, edgeCount; 40 | 41 | fin >> nodeCount >> edgeCount; 42 | for(int i=1;i<=edgeCount;i++) 43 | { 44 | int node1, node2, cost; 45 | fin >> node1 >> node2 >> cost;; 46 | edges[node1].push_back(make_pair(cost*(-1), node2)); 47 | edges[node2].push_back(make_pair(cost*(-1), node1)); 48 | q.push(make_pair(cost*(-1), make_pair(node1, node2))); 49 | } 50 | queue> > mst; 51 | int minCost = 0; 52 | while(!q.empty()) 53 | { 54 | auto thisEdge = q.top(); 55 | q.pop(); 56 | if (findFather(thisEdge.second.first) == findFather(thisEdge.second.second) && fatherOf[thisEdge.second.first]!=0) continue; 57 | mst.push(thisEdge); 58 | minCost += thisEdge.first * (-1); 59 | merge(thisEdge.second.first, thisEdge.second.second); 60 | } 61 | fout << minCost<<"\n"; 62 | fout << mst.size()<< "\n"; 63 | //mst.pop(); 64 | while (!mst.empty()) 65 | { 66 | auto edge = mst.front(); 67 | mst.pop(); 68 | fout << edge.second.first << " " << edge.second.second << "\n"; 69 | } 70 | return 0; 71 | } -------------------------------------------------------------------------------- /Year 1 Sem 1/Circular List/CircularList.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | struct listnode 3 | { 4 | int val; 5 | listnode *next; 6 | listnode *prev; 7 | }; 8 | void Clist_insert(listnode *&start, int x) 9 | { 10 | listnode *aux = new listnode; 11 | aux->val = x; 12 | aux->next = NULL; 13 | aux->prev = NULL; 14 | if(start->next == NULL) 15 | { 16 | start->next = aux; 17 | start->prev = aux; 18 | aux->prev = start; 19 | } 20 | else 21 | { 22 | start->prev->next = aux; 23 | aux->next = start; 24 | aux->prev = start->prev; 25 | start->prev = aux; 26 | } 27 | } 28 | void Clist_print(listnode *start) 29 | { 30 | listnode *aux = start; 31 | aux = aux->next; 32 | while(aux != start) 33 | { 34 | std::cout << aux->val << " "; 35 | aux = aux->next; 36 | } 37 | std::cout << "\n"; 38 | } 39 | void Clist_node_delete(listnode *&start, int x) 40 | { 41 | listnode *aux = start; 42 | aux = aux->next; 43 | while (aux != start) 44 | { 45 | if(aux->val == x) 46 | { 47 | aux->prev->next = aux->next; 48 | aux->next->prev = aux->prev; 49 | return; 50 | } 51 | aux = aux->next; 52 | } 53 | } 54 | void Clist_init(listnode *&start) 55 | { 56 | start = new listnode; 57 | start->next = NULL; start->prev = NULL; 58 | } 59 | void Clist_node_left_delete(listnode *&start) 60 | { 61 | listnode *aux; 62 | aux = start->prev->prev; 63 | aux->next = start; 64 | start->prev = aux; 65 | } 66 | void Clist_node_right_insert(listnode *&start, int x) 67 | { 68 | listnode *aux = new listnode; 69 | aux->val = x; 70 | aux->next = NULL; 71 | aux->prev = NULL; 72 | if(start->next == NULL) 73 | { 74 | start->next = aux; 75 | aux->prev = start; 76 | aux->next = start; 77 | } 78 | else 79 | { 80 | start->next->prev = aux; 81 | aux->next = start->next; 82 | start->next = aux; 83 | aux->prev = start; 84 | } 85 | 86 | } 87 | int main() 88 | { 89 | listnode *start; 90 | Clist_init(start); 91 | 92 | Clist_insert(start, 5); 93 | Clist_insert(start, 8); 94 | Clist_insert(start, 9); 95 | Clist_print(start); 96 | Clist_node_left_delete(start); 97 | Clist_node_right_insert(start, 1); 98 | Clist_print(start); 99 | system("pause"); 100 | return 0; 101 | } -------------------------------------------------------------------------------- /Year 1 Sem 1/OddEvenIndexList/OddEvenIndexList.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct listnode 5 | { 6 | int val; 7 | listnode *next; 8 | }; 9 | 10 | void list_add(listnode *&first, int val) 11 | { 12 | listnode *aux = new listnode; 13 | 14 | aux->val = val; 15 | if (first == NULL) aux->next = NULL; 16 | else 17 | aux->next = first; 18 | first = aux; 19 | 20 | } 21 | 22 | void list_print(listnode *first) 23 | { 24 | while(first != NULL) 25 | { 26 | cout << first->val << " "; 27 | first = first->next; 28 | } 29 | 30 | cout << "\n"; 31 | } 32 | 33 | void list_flip(listnode *&firstlist) 34 | { 35 | listnode *aux = firstlist; 36 | int n = 0; 37 | while (aux) aux = aux->next, n++; 38 | for (int i = 1; i < n; i++) 39 | { 40 | aux = firstlist; 41 | for (int j = 1; j <= n - i; j++) 42 | { 43 | swap(aux->val, aux->next->val); 44 | aux = aux->next; 45 | } 46 | 47 | } 48 | 49 | } 50 | 51 | void list_create_even_index_list(listnode *first_list1 , listnode *&first_list2) 52 | { 53 | bool iseven = 1; 54 | first_list1 = first_list1->next; 55 | 56 | while(first_list1) 57 | { 58 | if (iseven) list_add(first_list2, first_list1->val); 59 | iseven = !iseven; 60 | first_list1 = first_list1->next; 61 | } 62 | 63 | list_flip(first_list2); 64 | } 65 | 66 | void list_create_odd_index_list(listnode *first_list1, listnode *&first_list2) 67 | { 68 | bool isodd = 1; 69 | 70 | while (first_list1) 71 | { 72 | if (isodd) list_add(first_list2, first_list1->val); 73 | isodd = !isodd; 74 | first_list1 = first_list1->next; 75 | } 76 | 77 | list_flip(first_list2); 78 | } 79 | 80 | int main() 81 | { 82 | listnode *first_list1, *first_list2, *first_list3; 83 | first_list1 = first_list2 = first_list3 = NULL; 84 | 85 | list_add(first_list1, 10); 86 | list_add(first_list1, 9); 87 | list_add(first_list1, 1); 88 | list_add(first_list1, 6); 89 | list_add(first_list1, 2); 90 | list_add(first_list1, 13); 91 | list_add(first_list1, 7); 92 | list_add(first_list1, 1); 93 | list_add(first_list1, 5); 94 | 95 | list_print(first_list1); 96 | 97 | list_create_even_index_list(first_list1, first_list2); 98 | list_print(first_list2); 99 | 100 | list_create_odd_index_list(first_list1, first_list3); 101 | list_print(first_list3); 102 | 103 | system("pause"); 104 | } -------------------------------------------------------------------------------- /Year 1 Sem 1/deque/deque.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define yellowout SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14); 4 | #define whiteout SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); 5 | struct dequenode 6 | { 7 | int val; 8 | dequenode *next; 9 | dequenode *prev; 10 | }; 11 | void deque_front_add(dequenode *&first, dequenode *&last, int x) 12 | { 13 | dequenode *aux; 14 | aux = new dequenode; 15 | aux->next = NULL; aux->prev = NULL; 16 | aux->val = x; 17 | 18 | if (first == NULL) first = last = aux; 19 | else { 20 | aux->next = first; 21 | first->prev = aux; 22 | } 23 | first = aux; 24 | } 25 | void deque_print(dequenode *first) 26 | { 27 | while (first != NULL) 28 | { 29 | std::cout << first->val << " "; 30 | first = first->next; 31 | } 32 | std::cout << "\n"; 33 | } 34 | void deque_back_add(dequenode *&first, dequenode *&last, int x) 35 | { 36 | dequenode *aux; 37 | aux = new dequenode; 38 | aux->next = NULL; aux->prev = NULL; 39 | aux->val = x; 40 | if (first == NULL) first = last = aux; 41 | else 42 | { 43 | last->next = aux; 44 | aux->prev = last; 45 | } 46 | last = aux; 47 | } 48 | int deque_pop_front(dequenode *first) 49 | { 50 | int x; 51 | x = first->val; 52 | first = first->next; 53 | first->prev = NULL; 54 | return x; 55 | } 56 | int deque_pop_back(dequenode *last) 57 | { 58 | int x; 59 | x = last->val; 60 | last = last->prev; 61 | last->next = NULL; 62 | return x; 63 | 64 | } 65 | int main() 66 | { 67 | dequenode *first, *last; 68 | first = last = NULL; 69 | int step, x; 70 | std::cout << "1 - front add\n2 - back add\n3 - front pop\n4 - back pop\n5 - print\n0 - exit\n"; 71 | std::cin >> step; 72 | while (step) 73 | { 74 | switch (step) 75 | { 76 | case 1: 77 | std::cin >> x; 78 | deque_front_add(first,last, x); 79 | break; 80 | case 2: 81 | std::cin >> x; 82 | deque_back_add(first,last, x); 83 | break; 84 | case 3: 85 | yellowout 86 | std::cout << deque_pop_front(first) << "\n"; 87 | whiteout 88 | break; 89 | case 4: 90 | yellowout 91 | std::cout << deque_pop_back(last) << "\n"; 92 | whiteout 93 | break; 94 | case 5: 95 | deque_print(first); 96 | break; 97 | } 98 | std::cin >> step; 99 | } 100 | 101 | system("pause"); 102 | return 0; 103 | } -------------------------------------------------------------------------------- /Year 1 Sem 1/PriorityQueue/PriorityQueue.cpp: -------------------------------------------------------------------------------- 1 | //Priority_queue 2 | #include 3 | struct queue_node 4 | { 5 | int val; 6 | int priority; 7 | queue_node *next; 8 | }; 9 | struct priority_queue 10 | { 11 | queue_node *first_node; 12 | }; 13 | void pqueue_init(priority_queue &q) 14 | { 15 | q.first_node = NULL; 16 | } 17 | void pqueue_add(priority_queue &q, int x, int priority) 18 | { 19 | queue_node *aux = new queue_node; 20 | aux->next = NULL; 21 | aux->priority = priority; 22 | aux->val = x; 23 | if(q.first_node == NULL) 24 | { 25 | q.first_node = aux; 26 | } 27 | else 28 | { 29 | queue_node *auxp = q.first_node; 30 | if (aux->priority == q.first_node->priority) 31 | { 32 | aux->next = q.first_node; 33 | q.first_node = aux; 34 | } 35 | else 36 | while(1) 37 | { 38 | if(auxp->priority == aux->priority) 39 | { 40 | aux->next = auxp->next; 41 | auxp->next = aux; 42 | x = aux->val; 43 | aux->val = auxp->val; 44 | auxp->val = x; 45 | break; 46 | } 47 | if(auxp->next==NULL) 48 | { 49 | auxp->next = aux; 50 | break; 51 | } 52 | auxp = auxp->next; 53 | } 54 | } 55 | } 56 | int pqueue_pop(priority_queue q, int priority) 57 | { 58 | queue_node *prev = q.first_node; 59 | q.first_node = q.first_node->next; 60 | while(q.first_node != NULL) 61 | { 62 | //std::cout << q.first_node->val << " " << q.first_node->priority; 63 | if(q.first_node->priority == priority+1) 64 | { 65 | int x = prev->val; 66 | prev->val = q.first_node->val; 67 | prev->priority = q.first_node->priority; 68 | prev->next = q.first_node->next; 69 | return x; 70 | } 71 | prev = q.first_node; 72 | q.first_node = q.first_node->next; 73 | //std::cout << "\n"; 74 | } 75 | } 76 | void pqueue_print(priority_queue q) 77 | { 78 | while(q.first_node!=NULL) 79 | { 80 | std::cout << q.first_node->val << " " << q.first_node->priority; 81 | q.first_node = q.first_node->next; 82 | std::cout << "\n"; 83 | } 84 | std::cout << "\n"; 85 | } 86 | int main() 87 | { 88 | priority_queue q; 89 | pqueue_init(q); 90 | pqueue_add(q, 5, 1); 91 | pqueue_add(q, 2, 1); 92 | pqueue_add(q, 7, 2); 93 | pqueue_add(q, 8, 2); 94 | pqueue_add(q, 3, 3); 95 | pqueue_add(q, 1, 1); 96 | pqueue_add(q, 4, 2); 97 | pqueue_add(q, 5, 3); 98 | 99 | pqueue_print(q); 100 | std::cout << "\n"; 101 | std::cout << "\n"; 102 | std::cout << pqueue_pop(q, 2); 103 | std::cout << "\n"; 104 | pqueue_print(q); 105 | 106 | system("pause"); 107 | 108 | } -------------------------------------------------------------------------------- /Year 1 Sem 1/PriorityQueueHeap/pqueueHeap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | struct pqueue 7 | { 8 | int pr; 9 | int key; 10 | int subpr; 11 | }; 12 | void qify(pqueue q[], int n) 13 | { 14 | 15 | while (q[n / 2].pr < q[n].pr && n != 1) 16 | { 17 | std::swap(q[n / 2], q[n]); 18 | n = n / 2; 19 | } 20 | } 21 | int nextsubpr(pqueue q[], int n, int pr) 22 | { 23 | int k = 0; 24 | for(int i=1;i<=n;i++) 25 | { 26 | if (q[i].pr == pr) k++; 27 | } 28 | return k; 29 | } 30 | void q_add(pqueue q[], int &n, pqueue x) 31 | { 32 | 33 | q[++n] = x; 34 | int subpr = nextsubpr(q, n, x.pr); 35 | q[n].subpr = subpr; 36 | qify(q, n); 37 | } 38 | void q_print(pqueue q[], int n) 39 | { 40 | for (int i = 1; i <= n; i++) std::cout << " ( " << q[i].key << " " << q[i].pr <<" "<= maxsonpr) maxsonpr = son2.pr, maxson = son2; 71 | if (son1.pr >= maxsonpr) maxsonpr = son1.pr, maxson = son1; 72 | if (maxson.pr == son1.pr) maxsoni = 2 * i; 73 | else maxsoni = 2 * i + 1; 74 | if (q[i].pr < maxson.pr) std::swap(q[i], q[maxsoni]); 75 | else break; 76 | 77 | } 78 | return max; 79 | } 80 | 81 | 82 | int main() 83 | { 84 | pqueue q[100]; 85 | int n; 86 | n = 0; 87 | pqueue val; 88 | val.key = 10; 89 | val.pr = 3; 90 | q_add(q, n, val); 91 | val.key = 5; 92 | val.pr = 1; 93 | q_add(q, n, val); 94 | val.key = 2; 95 | val.pr = 2; 96 | q_add(q, n, val); 97 | val.key = 7; 98 | val.pr = 2; 99 | q_add(q, n, val); 100 | val.key = 15; 101 | val.pr = 3; 102 | q_add(q, n, val); 103 | val.key = 15; 104 | val.pr = 3; 105 | q_add(q, n, val); 106 | q_print(q, n); 107 | pqueue x = q_pop(q, n, 2); 108 | //x = q_pop(q, n, 3); 109 | //x = q_pop(q, n, 3); 110 | //std::cout << "( " << x.pr << " " << x.key << " ) \n"; 111 | q_print(q, n); 112 | system("pause"); 113 | return 0; 114 | } 115 | -------------------------------------------------------------------------------- /Year 1 Sem 1/Doubly Linked List/DLL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | struct listnode 3 | { 4 | int val; 5 | listnode *next; 6 | listnode *prev; 7 | }; 8 | void list_front_add(listnode *&first, listnode *&last, int x) 9 | { 10 | listnode *aux; 11 | aux = new listnode; 12 | aux->next = NULL; aux->prev = NULL; 13 | aux->val = x; 14 | 15 | if (first == NULL) first = last = aux; 16 | else { 17 | aux->next = first; 18 | first->prev = aux; 19 | } 20 | first = aux; 21 | } 22 | void list_print(listnode *first) 23 | { 24 | while(first != NULL) 25 | { 26 | std::cout << first->val << " "; 27 | first = first->next; 28 | } 29 | std::cout << "\n"; 30 | } 31 | void list_back_add(listnode *&first, listnode *&last, int x) 32 | { 33 | listnode *aux; 34 | aux = new listnode; 35 | aux->next = NULL; aux->prev = NULL; 36 | aux->val = x; 37 | if (first == NULL) first = last = aux; 38 | else 39 | { 40 | last->next = aux; 41 | aux->prev = last; 42 | } 43 | last = aux; 44 | } 45 | void list_print_reverse(listnode *last) 46 | { 47 | while(last != NULL) 48 | { 49 | std::cout << last->val << " "; 50 | last = last->prev; 51 | } 52 | std::cout << "\n"; 53 | } 54 | void list_index_insert(listnode *first, int index, int x) 55 | { 56 | int i = 1; 57 | listnode *aux; 58 | aux = first; 59 | while(1) 60 | { 61 | if(index == i+1) 62 | { 63 | listnode *aux2; 64 | aux2 = new listnode; 65 | aux2->val = x; 66 | aux2->prev = aux; 67 | aux2->next = aux->next; 68 | aux->next = aux2; 69 | return; 70 | } 71 | i++; 72 | aux = aux->next; 73 | } 74 | } 75 | void list_index_delete(listnode *first, int index) 76 | { 77 | int i = 1; 78 | listnode *aux; 79 | aux = first; 80 | while(aux!=NULL) 81 | { 82 | if(i+1==index) 83 | { 84 | aux->next = aux->next->next; 85 | aux->next->prev = aux; 86 | return; 87 | } 88 | i++; 89 | aux = aux->next; 90 | } 91 | } 92 | void list_value_delete(listnode *first, int x) 93 | { 94 | listnode *aux; 95 | aux = first; 96 | while (aux != NULL) 97 | { 98 | if (aux->next->val == x) 99 | { 100 | aux->next = aux->next->next; 101 | aux->next->prev = aux; 102 | return; 103 | } 104 | aux = aux->next; 105 | } 106 | } 107 | int main() 108 | { 109 | listnode *first, *last; 110 | first = last = NULL; 111 | list_front_add(first, last, 5); 112 | list_front_add(first, last, 6); 113 | list_front_add(first, last, 1); 114 | list_front_add(first, last, 2); 115 | std::cout << "front add 5-6-1-2:\n"; 116 | list_print(first); 117 | std::cout << "back add 10-3:\n"; 118 | list_back_add(first, last, 10); 119 | list_back_add(first, last, 3); 120 | 121 | list_print(first); 122 | std::cout << "print reverse:\n"; 123 | list_print_reverse(last); 124 | std::cout << "insert on index 3 val 4: \n"; 125 | list_index_insert(first, 3, 4); 126 | list_print(first); 127 | std::cout << "delete index 3:\n"; 128 | list_index_delete(first, 3); 129 | list_print(first); 130 | std::cout << "delete value 10:\n"; 131 | list_value_delete(first, 10); 132 | list_print(first); 133 | system("pause"); 134 | return 0; 135 | } -------------------------------------------------------------------------------- /Year 1 Sem 1/Permutari/permutari.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct cic 4 | { 5 | int n; 6 | int key[100]; 7 | }; 8 | void citeste_perm(int v[]) 9 | { 10 | int x, k = 0; 11 | std::ifstream fin; 12 | fin.open("permutare.txt"); 13 | while(fin>>x) 14 | { 15 | v[++k] = x; 16 | } 17 | v[0] = k; 18 | 19 | fin.close(); 20 | 21 | } 22 | void cicli_perm(int v[], cic cicli[]) 23 | { 24 | int n = v[0]; 25 | int viz[100] = { 0 }; 26 | int k = 0; 27 | 28 | for(int i=1;i<=n;i++) 29 | { 30 | 31 | if(viz[i] == 0) 32 | { 33 | 34 | viz[i] = 1; 35 | int l = 0; 36 | cicli[++k].key[++l] = i; 37 | int j = v[i]; 38 | while(viz[j] == 0) 39 | { 40 | 41 | cicli[k].key[++l] = j; 42 | viz[j] = 1; 43 | j = v[j]; 44 | } 45 | cicli[k].n = l; 46 | } 47 | } 48 | cicli[0].n = k; 49 | } 50 | void afisare_cicli(cic cicli[]) 51 | { 52 | int n = cicli[0].n,i,j; 53 | 54 | for(i=1;i<=n;i++) 55 | { 56 | std::cout << "( "; 57 | for(j=1;j ordin) p = p % ordin; 122 | if (p == -1) ok = 1; 123 | for (i = 1; i <= n; i++) 124 | { 125 | if (ok == 1) p = cicli[i].n - 1; 126 | 127 | if (cicli[i].n == p || cicli[i].n == 1) continue; 128 | 129 | j = 1; 130 | 131 | int viz[100] = { 0 }; 132 | for (k = 1; k <= cicli[i].n; k++) 133 | { 134 | if (viz[cicli[i].key[k]] == 1) continue; 135 | std::cout << " ( "; 136 | viz[cicli[i].key[k]] = 1; 137 | 138 | j = k; 139 | int primu = cicli[i].key[j]; 140 | std::cout << cicli[i].key[j]; 141 | j = j + p; 142 | while (j > cicli[i].n) 143 | { 144 | j = j - cicli[i].n; 145 | } 146 | 147 | 148 | while (cicli[i].key[j] != primu) 149 | { 150 | viz[cicli[i].key[j]] = 1; 151 | std::cout <<", "<< cicli[i].key[j]; 152 | 153 | j = j + p; 154 | while (j > cicli[i].n) 155 | { 156 | j = j - cicli[i].n; 157 | } 158 | } 159 | std::cout << " ) "; 160 | } 161 | } 162 | 163 | } 164 | int main() 165 | { 166 | int v[100], option, p; 167 | cic cicli[100]; 168 | citeste_perm(v); 169 | cicli_perm(v,cicli); 170 | std::cout << " 1 - afisare cicli\n 2 - afisare transpozitii\n 3 - afisare epsilon\n 4 - ridica permutarea la o putere \n 5 - exit\n"; 171 | 172 | while (1) 173 | { 174 | std::cout << "\n Optiune:"; 175 | std::cin >> option; 176 | switch (option) 177 | { 178 | case 1: afisare_cicli(cicli); 179 | break; 180 | case 2: afisare_transpozitii(cicli); 181 | break; 182 | case 3: afisare_epsilon(cicli); 183 | break; 184 | case 4: 185 | std::cout << "Introduce puterea: \n"; 186 | std::cin >> p; 187 | ridicare_la_putere(cicli, p); 188 | break; 189 | case 5: 190 | return 0; 191 | break; 192 | } 193 | } 194 | 195 | system("pause"); 196 | return 0; 197 | } 198 | -------------------------------------------------------------------------------- /Year 1 Sem 1/SparseArray/SparseArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct arraynode 5 | { 6 | int val , index; 7 | arraynode *next; 8 | 9 | }; 10 | 11 | void array_add(arraynode *&first, arraynode *&last, int x) 12 | { 13 | if(first == NULL) 14 | { 15 | arraynode *aux = new arraynode; 16 | aux->index = 1; aux->val = x; aux->next = NULL; 17 | first = aux; 18 | aux = new arraynode; 19 | aux->val = 0; aux->index = 2; 20 | first->next = aux; 21 | last = aux; 22 | } 23 | else 24 | { 25 | if (x == 0) last->index++; 26 | else 27 | { 28 | last->val = x; 29 | arraynode *aux = new arraynode; 30 | aux->val = 0; aux->index = last->index + 1; aux->next = NULL; 31 | last->next = aux; 32 | last = aux; 33 | 34 | } 35 | 36 | } 37 | 38 | } 39 | 40 | void print_zero(int z) 41 | { 42 | for (int i = 1; i <= z; i++) 43 | std::cout << "0 "; 44 | } 45 | 46 | void array_print(arraynode *first) 47 | { 48 | int previous_index = 0; 49 | while (first != NULL && first->val != 0) 50 | { 51 | print_zero(first->index - previous_index - 1); 52 | std::cout << first->val << " "; 53 | previous_index = first->index; 54 | first = first->next; 55 | } 56 | std::cout << "\n"; 57 | } 58 | 59 | void array_sum(arraynode *array1_first, arraynode *array2_first, arraynode *&resultarray_first, arraynode *&resultarray_last, int max_size) 60 | { 61 | for(int i=1;i<=max_size;i++) 62 | { 63 | int val1_index, val2_index, val1 = 0, val2 = 0, result = 0; 64 | 65 | if (array1_first != NULL) val1_index = array1_first->index, val1 = array1_first->val; 66 | if (array2_first != NULL) val2_index = array2_first->index, val2 = array2_first->val; 67 | 68 | if(array1_first != NULL) if (i > val1_index) array1_first = array1_first->next; 69 | if(array2_first != NULL) if (i > val2_index) array2_first = array2_first->next; 70 | 71 | if (array1_first != NULL) val1_index = array1_first->index, val1 = array1_first->val; 72 | if (array2_first != NULL) val2_index = array2_first->index, val2 = array2_first->val; 73 | 74 | if (val1_index == i) result += val1; 75 | if (val2_index == i) result += val2; 76 | 77 | array_add(resultarray_first, resultarray_last, result); 78 | 79 | } 80 | } 81 | 82 | void array_mult(arraynode *array1_first, arraynode *array2_first, arraynode *&resultarray_first, arraynode *&resultarray_last, int size_array1, int size_array2) 83 | { 84 | for(int i=1;i<=size_array1;i++) 85 | { 86 | if (array1_first != NULL) if (i > array1_first->index) array1_first = array1_first->next; 87 | int val_array1 = 0; 88 | if (array1_first->index == i)val_array1 += array1_first->val; 89 | 90 | arraynode *aux_array2 = array2_first; 91 | for(int j=1;j<=size_array2;j++) 92 | { 93 | if (aux_array2 != NULL) if (j > aux_array2->index) aux_array2 = aux_array2->next; 94 | int val_array2 = 0; 95 | if (aux_array2->index == j) val_array2 += aux_array2->val; 96 | array_add(resultarray_first, resultarray_last, val_array2*val_array1); 97 | } 98 | 99 | } 100 | } 101 | int main() 102 | { 103 | std::ifstream fin("array.txt"); 104 | int size_ar1, size_ar2, size_ar3, x; 105 | arraynode *array1_first, *array2_first, *sumarray_first, *multarray_first, *array1_last, *array2_last,*sumarray_last, *multarray_last; 106 | array1_first = array2_first = sumarray_first = multarray_first = array1_last = array2_last = sumarray_last = multarray_last = NULL; 107 | 108 | fin >> size_ar1; 109 | for(int i=1;i<=size_ar1;i++) 110 | { 111 | fin >> x; 112 | array_add(array1_first, array1_last, x); 113 | } 114 | 115 | fin >> size_ar2; 116 | for (int i = 1; i <= size_ar2; i++) 117 | { 118 | fin >> x; 119 | array_add(array2_first, array2_last, x); 120 | } 121 | 122 | int max_size; 123 | if (size_ar1 > size_ar2) max_size = size_ar1; else max_size = size_ar2; 124 | 125 | array_sum(array1_first, array2_first, sumarray_first, sumarray_last, max_size); 126 | array_print(sumarray_first); 127 | 128 | std::cout << "-----------------\n"; 129 | 130 | array_mult(array1_first, array2_first, multarray_first, multarray_last, size_ar1, size_ar2); 131 | array_print(multarray_first); 132 | system("pause"); 133 | } -------------------------------------------------------------------------------- /Year 2 Sem 1/PS/StatisticsEx4.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 36, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "cov <- function(XU,XD,YU,YD) {\n", 10 | " mult <- randomVarMultiply(XU,XD,YU,YD)\n", 11 | " \n", 12 | " result <- randomVarMean(mult$XU, mult$XD) - \n", 13 | " randomVarMean(XU,XD) * randomVarMean(YU,YD)\n", 14 | " return (result)\n", 15 | "}\n", 16 | "\n", 17 | "randomVarMultiply <- function(XU,XD,YU,YD){\n", 18 | " ZU <- rep(1, length(XU) * length(YU) )\n", 19 | " ZD <- rep(1, length(XU) * length(YU) )\n", 20 | " cnt <- 1\n", 21 | " for(i in XU) {\n", 22 | " for (j in YU)\n", 23 | " {\n", 24 | " ZU[cnt] <- i*j\n", 25 | " cnt <- cnt+1\n", 26 | " \n", 27 | " }\n", 28 | " \n", 29 | " }\n", 30 | " cnt <- 1\n", 31 | " for(i in XD) {\n", 32 | " for (j in YD)\n", 33 | " {\n", 34 | " ZD[cnt] <- i*j\n", 35 | " cnt <- cnt+1\n", 36 | " \n", 37 | " }\n", 38 | " \n", 39 | " }\n", 40 | " results <- list()\n", 41 | " results$XU <- ZU\n", 42 | " results$XD <- ZD\n", 43 | " return (results)\n", 44 | "}\n", 45 | "\n", 46 | "randomVarMean <- function(XU, XD){\n", 47 | " result <- 0\n", 48 | " cnt<-1\n", 49 | " for(i in XU) {\n", 50 | " \n", 51 | " result <- as.double(result) + as.double(i*XD[cnt])\n", 52 | " cnt <- cnt+1\n", 53 | " \n", 54 | " }\n", 55 | " \n", 56 | " \n", 57 | " return (result)\n", 58 | "}" 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": null, 64 | "metadata": {}, 65 | "outputs": [], 66 | "source": [] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 37, 71 | "metadata": {}, 72 | "outputs": [], 73 | "source": [ 74 | "# XU <- c(-2, 1)\n", 75 | "# XD <- c(0.4, 0.6)\n", 76 | "# YU <- c(-1, 3)\n", 77 | "# YD <- c(0.7, 0.3)\n", 78 | "\n", 79 | "XU <- c(-1,3)\n", 80 | "XD <- c(0.6, 0.4)\n", 81 | "YU <- c(-2,-1,1)\n", 82 | "YD <- c(0.3,0.4,0.3)" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": 38, 88 | "metadata": {}, 89 | "outputs": [ 90 | { 91 | "name": "stdout", 92 | "output_type": "stream", 93 | "text": [ 94 | "[1] -1\n", 95 | "[1] 3\n" 96 | ] 97 | } 98 | ], 99 | "source": [ 100 | "for(i in XU) {\n", 101 | " print(i)\n", 102 | "}" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": null, 108 | "metadata": {}, 109 | "outputs": [], 110 | "source": [] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": 39, 115 | "metadata": {}, 116 | "outputs": [], 117 | "source": [ 118 | "D <- randomVarMultiply(XU,XD,YU, YD)" 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": 40, 124 | "metadata": {}, 125 | "outputs": [ 126 | { 127 | "data": { 128 | "text/html": [ 129 | "5.55111512312578e-17" 130 | ], 131 | "text/latex": [ 132 | "5.55111512312578e-17" 133 | ], 134 | "text/markdown": [ 135 | "5.55111512312578e-17" 136 | ], 137 | "text/plain": [ 138 | "[1] 5.551115e-17" 139 | ] 140 | }, 141 | "metadata": {}, 142 | "output_type": "display_data" 143 | } 144 | ], 145 | "source": [ 146 | "cov(XU,XD,YU,YD)" 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": null, 152 | "metadata": {}, 153 | "outputs": [], 154 | "source": [] 155 | }, 156 | { 157 | "cell_type": "code", 158 | "execution_count": null, 159 | "metadata": {}, 160 | "outputs": [], 161 | "source": [] 162 | } 163 | ], 164 | "metadata": { 165 | "kernelspec": { 166 | "display_name": "R", 167 | "language": "R", 168 | "name": "ir" 169 | }, 170 | "language_info": { 171 | "codemirror_mode": "r", 172 | "file_extension": ".r", 173 | "mimetype": "text/x-r-source", 174 | "name": "R", 175 | "pygments_lexer": "r", 176 | "version": "3.6.1" 177 | } 178 | }, 179 | "nbformat": 4, 180 | "nbformat_minor": 4 181 | } 182 | -------------------------------------------------------------------------------- /Year 1 Sem 1/BinarySearchTree/SortWithBTS/BST.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "BST.h" 4 | 5 | void BStree_initialize(B_search_treenode *&tree) 6 | { 7 | tree = NULL; 8 | } 9 | void BStree_insert(B_search_treenode *&tree, int x) 10 | { 11 | B_search_treenode *aux = new B_search_treenode; 12 | aux->key = x; 13 | aux->right = NULL; 14 | aux->left = NULL; 15 | if(tree == NULL) 16 | { 17 | tree = aux; 18 | } 19 | else 20 | { 21 | B_search_treenode *temp = tree; 22 | while (1) 23 | { 24 | 25 | 26 | if (temp->key > aux->key && temp->left == NULL) 27 | { 28 | temp->left = aux; break; 29 | } 30 | else 31 | if (temp->key < aux->key && temp->right == NULL) 32 | { 33 | temp->right = aux; break; 34 | } 35 | else 36 | if (temp->key > aux->key) temp = temp->left; 37 | else 38 | temp = temp->right; 39 | 40 | } 41 | } 42 | } 43 | int BStree_search(B_search_treenode *temp, int x) 44 | { 45 | while(temp != NULL) 46 | { 47 | if (temp->key == x) return 1; 48 | if (temp->key > x) temp = temp->left; 49 | else 50 | temp = temp->right; 51 | } 52 | return 0; 53 | } 54 | int BStree_find_max(B_search_treenode *temp) 55 | { 56 | int x; 57 | while(temp!=NULL) 58 | { 59 | x = temp->key; 60 | temp = temp->right; 61 | } 62 | 63 | return x; 64 | } 65 | int BStree_find_min(B_search_treenode *temp) 66 | { 67 | int x; 68 | while (temp != NULL) 69 | { 70 | x = temp->key; 71 | temp = temp->left; 72 | } 73 | 74 | return x; 75 | } 76 | void BStree_delete(B_search_treenode *temp, int x); 77 | void BStree_delete(B_search_treenode *prev, B_search_treenode *temp) 78 | { 79 | int question; 80 | if (temp->left != NULL && temp->right != NULL) question = 3; // has 2 children 81 | else 82 | if (temp->left == NULL && temp->right == NULL) question = 1; // is leaf 83 | else 84 | if(temp == prev) question = 4; // is first node and has only a child 85 | else 86 | if (temp->left == NULL || temp->right == NULL) question = 2; // has only a child 87 | 88 | switch (question) 89 | { 90 | case 1: 91 | if (prev->right == temp) prev->right = NULL; 92 | else 93 | prev->left = NULL; 94 | free(temp); 95 | break; 96 | case 2: 97 | if (prev->right == temp) 98 | { 99 | if (temp->right != NULL) prev->right = temp->right; 100 | else prev->right = temp->left; 101 | 102 | } 103 | else 104 | { 105 | if (temp->right != NULL) prev->left = temp->right; 106 | else prev->left = temp->left; 107 | 108 | 109 | } 110 | break; 111 | case 3: 112 | //minimum right subtree 113 | B_search_treenode *aux; 114 | aux = temp; 115 | int x; 116 | aux = aux->right; 117 | while(aux != NULL) 118 | { 119 | x = aux->key; 120 | if (aux->left != NULL) aux = aux->left; 121 | else 122 | aux = aux->right; 123 | } 124 | BStree_delete(temp, x); 125 | temp->key = x; 126 | break; 127 | case 4: 128 | if (temp->right != NULL) 129 | { 130 | temp->key = temp->right->key; 131 | temp->left = temp->right->left; 132 | temp->right = temp->right->right; 133 | 134 | 135 | } 136 | else 137 | { 138 | temp->key = temp->left->key; 139 | temp->right = temp->left->right; 140 | temp->left = temp->left->left; 141 | 142 | } 143 | break; 144 | } 145 | } 146 | void BStree_delete(B_search_treenode *temp, int x) 147 | { 148 | B_search_treenode *prev; 149 | prev = temp; 150 | if (temp->key == x) 151 | { 152 | BStree_delete(temp, temp); 153 | return; 154 | } 155 | if (temp->key > x) temp = temp->left; 156 | else 157 | temp = temp->right; 158 | 159 | while (temp != NULL) 160 | { 161 | if (temp->key == x) 162 | { 163 | BStree_delete(prev, temp); 164 | return; 165 | } 166 | 167 | prev = temp; 168 | if (temp->key > x) temp = temp->left; 169 | else 170 | temp = temp->right; 171 | } 172 | 173 | }//search a value and then calls the deletion function for that pointer 174 | bool BStree_exist(B_search_treenode *temp) 175 | { 176 | if (temp->right != NULL || temp->left != NULL) return 1; 177 | return 0; 178 | }//0 - if we have only one node in the tree 179 | void inorder(B_search_treenode *temp) 180 | { 181 | if (!temp) 182 | return; 183 | inorder(temp->left); 184 | std::cout << temp->key << " "; 185 | inorder(temp->right); 186 | } 187 | void preorder(B_search_treenode *temp) 188 | { 189 | if (!temp) 190 | return; 191 | std::cout << temp->key<< " "; 192 | preorder(temp->left); 193 | preorder(temp->right); 194 | 195 | 196 | } 197 | void postorder(B_search_treenode *temp) 198 | { 199 | if (!temp) 200 | return; 201 | postorder(temp->left); 202 | postorder(temp->right); 203 | std::cout << temp->key << " "; 204 | } 205 | void DFS(B_search_treenode *temp) 206 | { 207 | std::queue q; 208 | q.push(temp); 209 | while(!q.empty()) 210 | { 211 | temp = q.front(); 212 | std::cout << temp->key << " "; 213 | q.pop(); 214 | if (temp->left != NULL) q.push(temp->left); 215 | if (temp->right != NULL) q.push(temp->right); 216 | } 217 | 218 | } 219 | -------------------------------------------------------------------------------- /Year 1 Sem 1/BinarySearchTree/to_include/BST.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "BST.h" 4 | 5 | void BStree_initialize(B_search_treenode *&tree) 6 | { 7 | tree = NULL; 8 | } 9 | void BStree_insert(B_search_treenode *&tree, int x) 10 | { 11 | B_search_treenode *aux = new B_search_treenode; 12 | aux->key = x; 13 | aux->right = NULL; 14 | aux->left = NULL; 15 | if(tree == NULL) 16 | { 17 | tree = aux; 18 | } 19 | else 20 | { 21 | B_search_treenode *temp = tree; 22 | while (1) 23 | { 24 | 25 | 26 | if (temp->key > aux->key && temp->left == NULL) 27 | { 28 | temp->left = aux; break; 29 | } 30 | else 31 | if (temp->key < aux->key && temp->right == NULL) 32 | { 33 | temp->right = aux; break; 34 | } 35 | else 36 | if (temp->key > aux->key) temp = temp->left; 37 | else 38 | temp = temp->right; 39 | 40 | } 41 | } 42 | } 43 | int BStree_search(B_search_treenode *temp, int x) 44 | { 45 | while(temp != NULL) 46 | { 47 | if (temp->key == x) return 1; 48 | if (temp->key > x) temp = temp->left; 49 | else 50 | temp = temp->right; 51 | } 52 | return 0; 53 | } 54 | int BStree_find_max(B_search_treenode *temp) 55 | { 56 | int x; 57 | while(temp!=NULL) 58 | { 59 | x = temp->key; 60 | temp = temp->right; 61 | } 62 | 63 | return x; 64 | } 65 | int BStree_find_min(B_search_treenode *temp) 66 | { 67 | int x; 68 | while (temp != NULL) 69 | { 70 | x = temp->key; 71 | temp = temp->left; 72 | } 73 | 74 | return x; 75 | } 76 | void BStree_delete(B_search_treenode *temp, int x); 77 | void BStree_delete(B_search_treenode *prev, B_search_treenode *temp) 78 | { 79 | int question; 80 | if (temp->left != NULL && temp->right != NULL) question = 3; // has 2 children 81 | else 82 | if (temp->left == NULL && temp->right == NULL) question = 1; // is leaf 83 | else 84 | if(temp == prev) question = 4; // is first node and has only a child 85 | else 86 | if (temp->left == NULL || temp->right == NULL) question = 2; // has only a child 87 | 88 | switch (question) 89 | { 90 | case 1: 91 | if (prev->right == temp) prev->right = NULL; 92 | else 93 | prev->left = NULL; 94 | free(temp); 95 | break; 96 | case 2: 97 | if (prev->right == temp) 98 | { 99 | if (temp->right != NULL) prev->right = temp->right; 100 | else prev->right = temp->left; 101 | 102 | } 103 | else 104 | { 105 | if (temp->right != NULL) prev->left = temp->right; 106 | else prev->left = temp->left; 107 | 108 | 109 | } 110 | break; 111 | case 3: 112 | //minimum right subtree 113 | B_search_treenode *aux; 114 | aux = temp; 115 | int x; 116 | aux = aux->right; 117 | while(aux != NULL) 118 | { 119 | x = aux->key; 120 | if (aux->left != NULL) aux = aux->left; 121 | else 122 | aux = aux->right; 123 | } 124 | BStree_delete(temp, x); 125 | temp->key = x; 126 | break; 127 | case 4: 128 | if (temp->right != NULL) 129 | { 130 | temp->key = temp->right->key; 131 | temp->left = temp->right->left; 132 | temp->right = temp->right->right; 133 | 134 | 135 | } 136 | else 137 | { 138 | temp->key = temp->left->key; 139 | temp->right = temp->left->right; 140 | temp->left = temp->left->left; 141 | 142 | } 143 | break; 144 | } 145 | } 146 | void BStree_delete(B_search_treenode *temp, int x) 147 | { 148 | B_search_treenode *prev; 149 | prev = temp; 150 | if (temp->key == x) 151 | { 152 | BStree_delete(temp, temp); 153 | return; 154 | } 155 | if (temp->key > x) temp = temp->left; 156 | else 157 | temp = temp->right; 158 | 159 | while (temp != NULL) 160 | { 161 | if (temp->key == x) 162 | { 163 | BStree_delete(prev, temp); 164 | return; 165 | } 166 | 167 | prev = temp; 168 | if (temp->key > x) temp = temp->left; 169 | else 170 | temp = temp->right; 171 | } 172 | 173 | }//search a value and then calls the deletion function for that pointer 174 | bool BStree_exist(B_search_treenode *temp) 175 | { 176 | if (temp->right != NULL || temp->left != NULL) return 1; 177 | return 0; 178 | }//0 - if we have only one node in the tree 179 | void inorder(B_search_treenode *temp) 180 | { 181 | if (!temp) 182 | return; 183 | inorder(temp->left); 184 | std::cout << temp->key << " "; 185 | inorder(temp->right); 186 | } 187 | void preorder(B_search_treenode *temp) 188 | { 189 | if (!temp) 190 | return; 191 | std::cout << temp->key<< " "; 192 | preorder(temp->left); 193 | preorder(temp->right); 194 | 195 | 196 | } 197 | void postorder(B_search_treenode *temp) 198 | { 199 | if (!temp) 200 | return; 201 | postorder(temp->left); 202 | postorder(temp->right); 203 | std::cout << temp->key << " "; 204 | } 205 | void DFS(B_search_treenode *temp) 206 | { 207 | std::queue q; 208 | q.push(temp); 209 | while(!q.empty()) 210 | { 211 | temp = q.front(); 212 | std::cout << temp->key << " "; 213 | q.pop(); 214 | if (temp->left != NULL) q.push(temp->left); 215 | if (temp->right != NULL) q.push(temp->right); 216 | } 217 | 218 | } 219 | -------------------------------------------------------------------------------- /Year 1 Sem 1/BinarySearchTree/SortWithBTS/BST - Copy.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "BTS.h" 4 | 5 | void BStree_initialize(B_search_treenode *&tree) 6 | { 7 | tree = NULL; 8 | } 9 | void BStree_insert(B_search_treenode *&tree, int x) 10 | { 11 | B_search_treenode *aux = new B_search_treenode; 12 | aux->key = x; 13 | aux->right = NULL; 14 | aux->left = NULL; 15 | if(tree == NULL) 16 | { 17 | tree = aux; 18 | } 19 | else 20 | { 21 | B_search_treenode *temp = tree; 22 | while (1) 23 | { 24 | 25 | 26 | if (temp->key > aux->key && temp->left == NULL) 27 | { 28 | temp->left = aux; break; 29 | } 30 | else 31 | if (temp->key < aux->key && temp->right == NULL) 32 | { 33 | temp->right = aux; break; 34 | } 35 | else 36 | if (temp->key > aux->key) temp = temp->left; 37 | else 38 | temp = temp->right; 39 | 40 | } 41 | } 42 | } 43 | int BStree_search(B_search_treenode *temp, int x) 44 | { 45 | while(temp != NULL) 46 | { 47 | if (temp->key == x) return 1; 48 | if (temp->key > x) temp = temp->left; 49 | else 50 | temp = temp->right; 51 | } 52 | return 0; 53 | } 54 | int BStree_find_max(B_search_treenode *temp) 55 | { 56 | int x; 57 | while(temp!=NULL) 58 | { 59 | x = temp->key; 60 | temp = temp->right; 61 | } 62 | 63 | return x; 64 | } 65 | int BStree_find_min(B_search_treenode *temp) 66 | { 67 | int x; 68 | while (temp != NULL) 69 | { 70 | x = temp->key; 71 | temp = temp->left; 72 | } 73 | 74 | return x; 75 | } 76 | void BStree_delete(B_search_treenode *temp, int x); 77 | void BStree_delete(B_search_treenode *prev, B_search_treenode *temp) 78 | { 79 | int question; 80 | if (temp->left != NULL && temp->right != NULL) question = 3; // has 2 children 81 | else 82 | if (temp->left == NULL && temp->right == NULL) question = 1; // is leaf 83 | else 84 | if(temp == prev) question = 4; // is first node and has only a child 85 | else 86 | if (temp->left == NULL || temp->right == NULL) question = 2; // has only a child 87 | 88 | switch (question) 89 | { 90 | case 1: 91 | if (prev->right == temp) prev->right = NULL; 92 | else 93 | prev->left = NULL; 94 | free(temp); 95 | break; 96 | case 2: 97 | if (prev->right == temp) 98 | { 99 | if (temp->right != NULL) prev->right = temp->right; 100 | else prev->right = temp->left; 101 | 102 | } 103 | else 104 | { 105 | if (temp->right != NULL) prev->left = temp->right; 106 | else prev->left = temp->left; 107 | 108 | 109 | } 110 | break; 111 | case 3: 112 | //minimum right subtree 113 | B_search_treenode *aux; 114 | aux = temp; 115 | int x; 116 | aux = aux->right; 117 | while(aux != NULL) 118 | { 119 | x = aux->key; 120 | if (aux->left != NULL) aux = aux->left; 121 | else 122 | aux = aux->right; 123 | } 124 | BStree_delete(temp, x); 125 | temp->key = x; 126 | break; 127 | case 4: 128 | if (temp->right != NULL) 129 | { 130 | temp->key = temp->right->key; 131 | temp->left = temp->right->left; 132 | temp->right = temp->right->right; 133 | 134 | 135 | } 136 | else 137 | { 138 | temp->key = temp->left->key; 139 | temp->right = temp->left->right; 140 | temp->left = temp->left->left; 141 | 142 | } 143 | break; 144 | } 145 | } 146 | void BStree_delete(B_search_treenode *temp, int x) 147 | { 148 | B_search_treenode *prev; 149 | prev = temp; 150 | if (temp->key == x) 151 | { 152 | BStree_delete(temp, temp); 153 | return; 154 | } 155 | if (temp->key > x) temp = temp->left; 156 | else 157 | temp = temp->right; 158 | 159 | while (temp != NULL) 160 | { 161 | if (temp->key == x) 162 | { 163 | BStree_delete(prev, temp); 164 | return; 165 | } 166 | 167 | prev = temp; 168 | if (temp->key > x) temp = temp->left; 169 | else 170 | temp = temp->right; 171 | } 172 | 173 | }//search a value and then calls the deletion function for that pointer 174 | bool BStree_exist(B_search_treenode *temp) 175 | { 176 | if (temp->right != NULL || temp->left != NULL) return 1; 177 | return 0; 178 | }//0 - if we have only one node in the tree 179 | void inorder(B_search_treenode *temp) 180 | { 181 | if (!temp) 182 | return; 183 | inorder(temp->left); 184 | std::cout << temp->key << " "; 185 | inorder(temp->right); 186 | } 187 | void preorder(B_search_treenode *temp) 188 | { 189 | if (!temp) 190 | return; 191 | std::cout << temp->key<< " "; 192 | preorder(temp->left); 193 | preorder(temp->right); 194 | 195 | 196 | } 197 | void postorder(B_search_treenode *temp) 198 | { 199 | if (!temp) 200 | return; 201 | postorder(temp->left); 202 | postorder(temp->right); 203 | std::cout << temp->key << " "; 204 | } 205 | void DFS(B_search_treenode *temp) 206 | { 207 | std::queue q; 208 | q.push(temp); 209 | while(!q.empty()) 210 | { 211 | temp = q.front(); 212 | std::cout << temp->key << " "; 213 | q.pop(); 214 | if (temp->left != NULL) q.push(temp->left); 215 | if (temp->right != NULL) q.push(temp->right); 216 | } 217 | 218 | } 219 | -------------------------------------------------------------------------------- /Year 1 Sem 1/BinarySearchTree/Numbers_in_interval/BST.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "BST.h" 4 | 5 | void BStree_initialize(B_search_treenode *&tree) 6 | { 7 | tree = NULL; 8 | } 9 | void BStree_insert(B_search_treenode *&tree, int x) 10 | { 11 | B_search_treenode *aux = new B_search_treenode; 12 | aux->key = x; 13 | aux->right = NULL; 14 | aux->left = NULL; 15 | if(tree == NULL) 16 | { 17 | tree = aux; 18 | } 19 | else 20 | { 21 | B_search_treenode *temp = tree; 22 | while (1) 23 | { 24 | 25 | 26 | if (temp->key > aux->key && temp->left == NULL) 27 | { 28 | temp->left = aux; break; 29 | } 30 | else 31 | if (temp->key < aux->key && temp->right == NULL) 32 | { 33 | temp->right = aux; break; 34 | } 35 | else 36 | if (temp->key > aux->key) temp = temp->left; 37 | else 38 | temp = temp->right; 39 | 40 | } 41 | } 42 | } 43 | int BStree_search(B_search_treenode *temp, int x) 44 | { 45 | while(temp != NULL) 46 | { 47 | if (temp->key == x) return 1; 48 | if (temp->key > x) temp = temp->left; 49 | else 50 | temp = temp->right; 51 | } 52 | return 0; 53 | } 54 | int BStree_find_max(B_search_treenode *temp) 55 | { 56 | int x; 57 | while(temp!=NULL) 58 | { 59 | x = temp->key; 60 | temp = temp->right; 61 | } 62 | 63 | return x; 64 | } 65 | int BStree_find_min(B_search_treenode *temp) 66 | { 67 | int x; 68 | while (temp != NULL) 69 | { 70 | x = temp->key; 71 | temp = temp->left; 72 | } 73 | 74 | return x; 75 | } 76 | void BStree_delete(B_search_treenode *temp, int x); 77 | void BStree_delete(B_search_treenode *prev, B_search_treenode *temp) 78 | { 79 | int question; 80 | if (temp->left != NULL && temp->right != NULL) question = 3; // has 2 children 81 | else 82 | if (temp->left == NULL && temp->right == NULL) question = 1; // is leaf 83 | else 84 | if(temp == prev) question = 4; // is first node and has only a child 85 | else 86 | if (temp->left == NULL || temp->right == NULL) question = 2; // has only a child 87 | 88 | switch (question) 89 | { 90 | case 1: 91 | if (prev->right == temp) prev->right = NULL; 92 | else 93 | prev->left = NULL; 94 | free(temp); 95 | break; 96 | case 2: 97 | if (prev->right == temp) 98 | { 99 | if (temp->right != NULL) prev->right = temp->right; 100 | else prev->right = temp->left; 101 | 102 | } 103 | else 104 | { 105 | if (temp->right != NULL) prev->left = temp->right; 106 | else prev->left = temp->left; 107 | 108 | 109 | } 110 | break; 111 | case 3: 112 | //minimum right subtree 113 | B_search_treenode *aux; 114 | aux = temp; 115 | int x; 116 | aux = aux->right; 117 | while(aux != NULL) 118 | { 119 | x = aux->key; 120 | if (aux->left != NULL) aux = aux->left; 121 | else 122 | aux = aux->right; 123 | } 124 | BStree_delete(temp, x); 125 | temp->key = x; 126 | break; 127 | case 4: 128 | if (temp->right != NULL) 129 | { 130 | temp->key = temp->right->key; 131 | temp->left = temp->right->left; 132 | temp->right = temp->right->right; 133 | 134 | 135 | } 136 | else 137 | { 138 | temp->key = temp->left->key; 139 | temp->right = temp->left->right; 140 | temp->left = temp->left->left; 141 | 142 | } 143 | break; 144 | } 145 | } 146 | void BStree_delete(B_search_treenode *temp, int x) 147 | { 148 | B_search_treenode *prev; 149 | prev = temp; 150 | if (temp->key == x) 151 | { 152 | BStree_delete(temp, temp); 153 | return; 154 | } 155 | if (temp->key > x) temp = temp->left; 156 | else 157 | temp = temp->right; 158 | 159 | while (temp != NULL) 160 | { 161 | if (temp->key == x) 162 | { 163 | BStree_delete(prev, temp); 164 | return; 165 | } 166 | 167 | prev = temp; 168 | if (temp->key > x) temp = temp->left; 169 | else 170 | temp = temp->right; 171 | } 172 | 173 | }//search a value and then calls the deletion function for that pointer 174 | bool BStree_exist(B_search_treenode *temp) 175 | { 176 | if (temp->right != NULL || temp->left != NULL) return 1; 177 | return 0; 178 | }//0 - if we have only one node in the tree 179 | void inorder(B_search_treenode *temp) 180 | { 181 | if (!temp) 182 | return; 183 | inorder(temp->left); 184 | std::cout << temp->key << " "; 185 | inorder(temp->right); 186 | } 187 | void preorder(B_search_treenode *temp) 188 | { 189 | if (!temp) 190 | return; 191 | std::cout << temp->key<< " "; 192 | preorder(temp->left); 193 | preorder(temp->right); 194 | 195 | 196 | } 197 | void postorder(B_search_treenode *temp) 198 | { 199 | if (!temp) 200 | return; 201 | postorder(temp->left); 202 | postorder(temp->right); 203 | std::cout << temp->key << " "; 204 | } 205 | void DFS(B_search_treenode *temp) 206 | { 207 | std::queue q; 208 | q.push(temp); 209 | while(!q.empty()) 210 | { 211 | temp = q.front(); 212 | std::cout << temp->key << " "; 213 | q.pop(); 214 | if (temp->left != NULL) q.push(temp->left); 215 | if (temp->right != NULL) q.push(temp->right); 216 | } 217 | 218 | } 219 | -------------------------------------------------------------------------------- /Year 1 Sem 1/BinarySearchTree/implementation/BinarySearchTree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct B_search_treenode 5 | { 6 | int key; 7 | B_search_treenode *right; 8 | B_search_treenode *left; 9 | }; 10 | void BStree_insert(B_search_treenode *&tree, int x) 11 | { 12 | B_search_treenode *aux = new B_search_treenode; 13 | aux->key = x; 14 | aux->right = NULL; 15 | aux->left = NULL; 16 | if(tree == NULL) 17 | { 18 | tree = aux; 19 | } 20 | else 21 | { 22 | B_search_treenode *temp = tree; 23 | while (1) 24 | { 25 | 26 | 27 | if (temp->key > aux->key && temp->left == NULL) 28 | { 29 | temp->left = aux; break; 30 | } 31 | else 32 | if (temp->key < aux->key && temp->right == NULL) 33 | { 34 | temp->right = aux; break; 35 | } 36 | else 37 | if (temp->key > aux->key) temp = temp->left; 38 | else 39 | temp = temp->right; 40 | 41 | } 42 | } 43 | } 44 | int BStree_search(B_search_treenode *temp, int x) 45 | { 46 | while(temp != NULL) 47 | { 48 | if (temp->key == x) return 1; 49 | if (temp->key > x) temp = temp->left; 50 | else 51 | temp = temp->right; 52 | } 53 | return 0; 54 | } 55 | int BStree_find_max(B_search_treenode *temp) 56 | { 57 | int x; 58 | while(temp!=NULL) 59 | { 60 | x = temp->key; 61 | temp = temp->right; 62 | } 63 | 64 | return x; 65 | } 66 | void BStree_delete(B_search_treenode *temp, int x); 67 | void BStree_delete(B_search_treenode *prev, B_search_treenode *temp) 68 | { 69 | int question; 70 | if (temp->left != NULL && temp->right != NULL) question = 3; // has 2 children 71 | else 72 | if (temp->left == NULL && temp->right == NULL) question = 1; // is leaf 73 | else 74 | if(temp == prev) question = 4; // is first node and has only a child 75 | else 76 | if (temp->left == NULL || temp->right == NULL) question = 2; // has only a child 77 | 78 | switch (question) 79 | { 80 | case 1: 81 | if (prev->right == temp) prev->right = NULL; 82 | else 83 | prev->left = NULL; 84 | free(temp); 85 | break; 86 | case 2: 87 | if (prev->right == temp) 88 | { 89 | if (temp->right != NULL) prev->right = temp->right; 90 | else prev->right = temp->left; 91 | 92 | } 93 | else 94 | { 95 | if (temp->right != NULL) prev->left = temp->right; 96 | else prev->left = temp->left; 97 | 98 | 99 | } 100 | break; 101 | case 3: 102 | //minimum right subtree 103 | B_search_treenode *aux; 104 | aux = temp; 105 | int x; 106 | aux = aux->right; 107 | while(aux != NULL) 108 | { 109 | x = aux->key; 110 | if (aux->left != NULL) aux = aux->left; 111 | else 112 | aux = aux->right; 113 | } 114 | BStree_delete(temp, x); 115 | temp->key = x; 116 | break; 117 | case 4: 118 | if (temp->right != NULL) temp = temp->right; 119 | else 120 | { 121 | temp->key = temp->left->key; 122 | temp->right = temp->left->right; 123 | temp->left = temp->left->left; 124 | 125 | } 126 | break; 127 | } 128 | } 129 | void BStree_delete(B_search_treenode *temp, int x) 130 | { 131 | B_search_treenode *prev; 132 | prev = temp; 133 | if (temp->key == x) 134 | { 135 | BStree_delete(temp, temp); 136 | return; 137 | } 138 | if (temp->key > x) temp = temp->left; 139 | else 140 | temp = temp->right; 141 | 142 | while (temp != NULL) 143 | { 144 | if (temp->key == x) 145 | { 146 | BStree_delete(prev, temp); 147 | return; 148 | } 149 | 150 | prev = temp; 151 | if (temp->key > x) temp = temp->left; 152 | else 153 | temp = temp->right; 154 | } 155 | 156 | }//search a value and then calls the deletion function for that pointer 157 | bool BStree_exist(B_search_treenode *temp, int x) 158 | { 159 | if (temp->right != NULL || temp->left != NULL) return 1; 160 | return 0; 161 | }//0 - if we have only one node in the tree 162 | void inorder(B_search_treenode *temp) 163 | { 164 | if (!temp) 165 | return; 166 | inorder(temp->left); 167 | std::cout << temp->key << " "; 168 | inorder(temp->right); 169 | } 170 | void preorder(B_search_treenode *temp) 171 | { 172 | if (!temp) 173 | return; 174 | std::cout << temp->key<< " "; 175 | preorder(temp->left); 176 | preorder(temp->right); 177 | 178 | 179 | } 180 | void postorder(B_search_treenode *temp) 181 | { 182 | if (!temp) 183 | return; 184 | postorder(temp->left); 185 | postorder(temp->right); 186 | std::cout << temp->key << " "; 187 | } 188 | void DFS(B_search_treenode *temp) 189 | { 190 | std::queue q; 191 | q.push(temp); 192 | while(!q.empty()) 193 | { 194 | temp = q.front(); 195 | std::cout << temp->key << " "; 196 | q.pop(); 197 | if (temp->left != NULL) q.push(temp->left); 198 | if (temp->right != NULL) q.push(temp->right); 199 | } 200 | 201 | } 202 | int main() 203 | { 204 | B_search_treenode *tree; 205 | tree = NULL; 206 | 207 | BStree_insert(tree, 20); 208 | BStree_insert(tree, 5); 209 | BStree_insert(tree, 25); 210 | BStree_insert(tree, 7); 211 | BStree_insert(tree, 24); 212 | BStree_insert(tree, 30); 213 | BStree_insert(tree, 29); 214 | //inorder(tree); 215 | //std::cout << "\n"; 216 | //preorder(tree); 217 | 218 | 219 | //BStree_delete(tree,20); 220 | 221 | //DFS(tree); 222 | system("pause"); 223 | return 0; 224 | } -------------------------------------------------------------------------------- /Year 1 Sem 1/PolynomialCPP/Polynomial.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct pol 5 | { 6 | int deg, val; 7 | pol *next; 8 | 9 | }; 10 | 11 | void add_to_pol(pol *&first_pol, int x, int deg) 12 | { 13 | pol *aux = new pol; 14 | aux->val = x; 15 | aux->deg = deg; 16 | if (first_pol == NULL) aux->next = NULL; 17 | else aux->next = first_pol; 18 | first_pol = aux; 19 | 20 | } 21 | 22 | void print_pol(pol *&first_pol) 23 | { 24 | pol *aux; 25 | aux = first_pol; 26 | pol *nul = reinterpret_cast(0xCCCCCCCC); 27 | 28 | while(aux != nul && aux != NULL) 29 | { 30 | cout << " + "; 31 | cout << aux->val << " *x^" << aux->deg; 32 | aux = aux->next; 33 | } 34 | cout << "\n"; 35 | } 36 | 37 | void mult_pol__by_number(pol *&first_pol, int x) 38 | { 39 | pol *aux; 40 | aux = first_pol; 41 | pol *nul = reinterpret_cast(0xCCCCCCCC); 42 | 43 | while(aux != nul) 44 | { 45 | aux->val *= x; 46 | aux = aux->next; 47 | 48 | } 49 | 50 | } 51 | int poww(int x,int e) 52 | { 53 | if (e == 0) return 1; 54 | int x1 = x; 55 | for (int i = 1; i < e; i++) 56 | x = x*x1; 57 | return x; 58 | } 59 | int pol_evaluate(pol *&first_pol, int x) 60 | { 61 | pol *aux; 62 | aux = first_pol; 63 | int result = 0; 64 | pol *nul = reinterpret_cast(0xCCCCCCCC); 65 | 66 | while (aux != nul) 67 | { 68 | result += aux->val * (x, aux->deg); 69 | aux = aux->next; 70 | } 71 | return result; 72 | } 73 | 74 | void pol_sum(pol *&first_pol1, pol *&first_pol2, pol *&first_sumpol) 75 | { 76 | pol *nul = reinterpret_cast(0xCCCCCCCC); 77 | pol *aux_pol1 = first_pol1; 78 | pol *aux_pol2 = first_pol2; 79 | int coef_pol3, deg_pol3; 80 | 81 | while(( aux_pol1 != nul && aux_pol1) || ( aux_pol2 != nul && aux_pol2) ) 82 | { 83 | coef_pol3 = 0; deg_pol3 = 0; 84 | if (aux_pol1 != nul && aux_pol1) coef_pol3 += aux_pol1->val, deg_pol3 = aux_pol1->deg, aux_pol1 = aux_pol1->next; 85 | if (aux_pol2 != nul && aux_pol2) coef_pol3 += aux_pol2->val, deg_pol3 = aux_pol2->deg, aux_pol2 = aux_pol2->next; 86 | add_to_pol(first_sumpol, coef_pol3, deg_pol3); 87 | } 88 | 89 | } 90 | 91 | void pol_sum(pol *&first_pol1, pol *&first_pol2) 92 | { 93 | pol *nul = reinterpret_cast(0xCCCCCCCC); 94 | pol *first_sumpol; 95 | pol_sum(first_pol1, first_pol2, first_sumpol); 96 | //delete first_pol1; 97 | first_pol1 = NULL; 98 | while(first_sumpol != nul) 99 | { 100 | add_to_pol(first_pol1, first_sumpol->val, first_sumpol->deg); 101 | first_sumpol = first_sumpol->next; 102 | } 103 | 104 | } 105 | void add_to_end_of_pol(pol *&first_pol, pol *&last_pol, int x, int deg) 106 | { 107 | pol *nul = reinterpret_cast(0xCCCCCCCC); 108 | pol *aux = new pol; 109 | aux->val = x; 110 | aux->deg = deg; 111 | aux->next = NULL; 112 | if (first_pol == NULL || first_pol == nul) { first_pol = aux; last_pol = aux; } 113 | else last_pol->next = aux; 114 | last_pol = aux; 115 | 116 | } 117 | 118 | void add_coef_to_pol(pol *first_pol, int x, int deg) 119 | { 120 | pol *aux; 121 | aux = first_pol; 122 | pol *nul = reinterpret_cast(0xCCCCCCCC); 123 | 124 | while (aux != nul && aux != NULL) 125 | { 126 | if (aux->deg == deg) { aux->val += x; return; } 127 | 128 | aux = aux->next; 129 | } 130 | 131 | 132 | } 133 | 134 | void pol_mult(pol *&first_pol1, pol *&first_pol2, pol *&first_multpol,int maxdeg) 135 | { 136 | for (int i = 0; i <= maxdeg; i++) 137 | add_to_pol(first_multpol, 0, maxdeg - i); 138 | 139 | print_pol(first_multpol); 140 | 141 | pol *nul = reinterpret_cast(0xCCCCCCCC); 142 | pol *aux_pol1 = first_pol1; 143 | pol *aux_pol2; 144 | int coef_pol3, deg_pol3, coef_pol1, deg_pol1, coef_pol2, deg_pol2; 145 | while (aux_pol1 != nul) 146 | { 147 | pol *first_pol_for_add, *last_pol_for_add; 148 | coef_pol1 = aux_pol1->val; deg_pol1 = aux_pol1->deg; 149 | aux_pol2 = first_pol2; 150 | while (aux_pol2 != nul) 151 | { 152 | coef_pol2 = aux_pol2->val; deg_pol2 = aux_pol2->deg; 153 | coef_pol3 = coef_pol1 * coef_pol2; 154 | deg_pol3 = deg_pol1 + deg_pol2; 155 | add_coef_to_pol(first_multpol, coef_pol3,deg_pol3); 156 | aux_pol2 = aux_pol2->next; 157 | } 158 | 159 | aux_pol1 = aux_pol1->next; 160 | print_pol(first_multpol); 161 | } 162 | 163 | } 164 | int main() 165 | { 166 | int n, m, x,deg; 167 | pol *first_pol1, *first_pol2; 168 | cout << "Deg pol1:\n"; 169 | cin >> n; 170 | cout << "Coef pol1:\n"; 171 | deg = n; 172 | for(int i=1;i<=n+1;i++) 173 | { 174 | cin >> x; add_to_pol(first_pol1, x, deg); 175 | deg--; 176 | } 177 | 178 | cout << "Deg pol2:\n"; 179 | cin >> m; 180 | cout << "Coef pol2:\n"; 181 | deg = m; 182 | for (int i = 1; i <= m+1; i++) 183 | { 184 | cin >> x; add_to_pol(first_pol2, x, deg); 185 | deg--; 186 | } 187 | print_pol(first_pol1); 188 | print_pol(first_pol2); 189 | 190 | cout << "pol1 mult by:"; 191 | cin >> x; 192 | mult_pol__by_number(first_pol1, x); 193 | print_pol(first_pol1); 194 | 195 | cout << "pol2 evaluate with x="; cin >> x; 196 | cout << pol_evaluate(first_pol2, x); 197 | cout << "\n"; 198 | 199 | pol *first_sumpol; 200 | pol_sum(first_pol1, first_pol2, first_sumpol); 201 | print_pol(first_sumpol); 202 | 203 | 204 | 205 | pol *first_multpol; 206 | int maxdeg; 207 | maxdeg = n + m; 208 | pol_mult(first_pol1, first_pol2, first_multpol, maxdeg); 209 | print_pol(first_multpol); 210 | 211 | system("pause"); 212 | 213 | } 214 | -------------------------------------------------------------------------------- /Year 2 Sem 1/PS/StatisticsEx1.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "library(rcompanion)" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": null, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "rock" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": null, 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [ 27 | "summary(rock)" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": null, 33 | "metadata": {}, 34 | "outputs": [], 35 | "source": [ 36 | "print(var(rock$area))\n", 37 | "print(var(rock$peri))\n", 38 | "print(var(rock$shape))\n", 39 | "print(var(rock$perm))" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": null, 45 | "metadata": {}, 46 | "outputs": [], 47 | "source": [ 48 | "print(quantile(rock$area))\n", 49 | "print(quantile(rock$peri))\n", 50 | "print(quantile(rock$shape))\n", 51 | "print(quantile(rock$perm))" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": null, 57 | "metadata": {}, 58 | "outputs": [], 59 | "source": [ 60 | "print( cor(rock$shape, rock$perm) ) \n", 61 | "print( cor(rock$area, rock$perm) )\n", 62 | "print( cor(rock$peri, rock$perm) )\n" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": null, 68 | "metadata": {}, 69 | "outputs": [], 70 | "source": [ 71 | "print( cov(rock$shape, rock$perm) ) \n", 72 | "print( cov(rock$area, rock$perm) )\n", 73 | "print( cov(rock$peri, rock$perm) )" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": null, 79 | "metadata": {}, 80 | "outputs": [], 81 | "source": [ 82 | "\n", 83 | "scatter.smooth(x=rock$shape, y=rock$perm, main=\"shape ~ perm\") # scatterplot\n", 84 | "# observam ca pe parcurs ce creste shape creste si permeabilitatea\n", 85 | "scatter.smooth(x=rock$area, y=rock$perm, main=\"area ~ perm\") # scatterplot\n", 86 | "scatter.smooth(x=rock$peri, y=rock$perm, main=\"peri ~ perm\") # scatterplot\n", 87 | "# observam ca pe parcurs ce creste perimeetrul permeabilitatea scade" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": null, 93 | "metadata": {}, 94 | "outputs": [], 95 | "source": [ 96 | "par(mfrow=c(1, 4))\n", 97 | "boxplot(rock$perm, main=\"Permeability\", sub=paste(\"Outlier rows: \", boxplot.stats(rock$perm)$out)) # box plot pentru permeabilitate\n", 98 | "boxplot(rock$shape, main=\"Shape\", sub=paste(\"Outlier rows: \", boxplot.stats(rock$shape)$out)) # box plot for arie\n", 99 | "boxplot(rock$area, main=\"area\", sub=paste(\"Outlier rows: \", boxplot.stats(rock$area)$out)) # box plot for spatiu dinre pori\n", 100 | "boxplot(rock$peri, main=\"perimetru\", sub=paste(\"Outlier rows: \", boxplot.stats(rock$peri)$out)) # box plot for spatiu dinre perimetru\n", 101 | "\n", 102 | "#Observam ca ne apar niste outlieri la shape\n", 103 | "#Si mediana este destul de jos la permeabilitate" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": null, 109 | "metadata": {}, 110 | "outputs": [], 111 | "source": [ 112 | "boxplot.stats(rock$shape)$out # outlierii lui shape" 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": null, 118 | "metadata": {}, 119 | "outputs": [], 120 | "source": [ 121 | "boxplot(rock$perm, rock$shape, rock$area, rock$peri)" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": null, 127 | "metadata": {}, 128 | "outputs": [], 129 | "source": [ 130 | "normalize <- function(x){(x-min(x))/(max(x)-min(x))}\n", 131 | "boxplot(normalize(rock$perm), normalize(rock$shape), normalize(rock$area), normalize(rock$peri))" 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": null, 137 | "metadata": {}, 138 | "outputs": [], 139 | "source": [ 140 | "# verificam distributia lui shape\n", 141 | "plotNormalHistogram(rock$shape)" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": null, 147 | "metadata": {}, 148 | "outputs": [], 149 | "source": [ 150 | "# verificam distributia lui area\n", 151 | "plotNormalHistogram(rock$area)" 152 | ] 153 | }, 154 | { 155 | "cell_type": "code", 156 | "execution_count": null, 157 | "metadata": {}, 158 | "outputs": [], 159 | "source": [ 160 | "# verificam distributia lui peri\n", 161 | "plotNormalHistogram(rock$peri)" 162 | ] 163 | }, 164 | { 165 | "cell_type": "code", 166 | "execution_count": null, 167 | "metadata": {}, 168 | "outputs": [], 169 | "source": [ 170 | "qqnorm(rock$shape, pch = 1, frame = FALSE)\n", 171 | "qqline(rock$shape, col = \"steelblue\", lwd = 2)" 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": null, 177 | "metadata": {}, 178 | "outputs": [], 179 | "source": [ 180 | "qqnorm(rock$area, pch = 1, frame = FALSE)\n", 181 | "qqline(rock$area, col = \"steelblue\", lwd = 2)\n", 182 | "# observam ca area este distribuita normal" 183 | ] 184 | } 185 | ], 186 | "metadata": { 187 | "kernelspec": { 188 | "display_name": "R", 189 | "language": "R", 190 | "name": "ir" 191 | }, 192 | "language_info": { 193 | "codemirror_mode": "r", 194 | "file_extension": ".r", 195 | "mimetype": "text/x-r-source", 196 | "name": "R", 197 | "pygments_lexer": "r", 198 | "version": "3.6.1" 199 | } 200 | }, 201 | "nbformat": 4, 202 | "nbformat_minor": 4 203 | } 204 | -------------------------------------------------------------------------------- /Year 2 Sem 1/BinaryTree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node 5 | { 6 | public: 7 | Node* left; 8 | Node* right; 9 | Node* parent; 10 | 11 | Node(); 12 | ~Node(); 13 | static Node* removeNodes(Node* node, int value); 14 | static void addValue(Node* root, int value); 15 | static void inorder(const Node* node); 16 | private: 17 | static Node* minValueNode(Node* node); 18 | static void removeValue(Node* node, int value); 19 | static void removeNode(Node* node); 20 | static Node* findValue(Node* node, int value); 21 | int value; 22 | }; 23 | 24 | 25 | 26 | Node::Node() 27 | { 28 | this->left = NULL; 29 | this->right = NULL; 30 | this->parent = NULL; 31 | } 32 | 33 | Node::~Node() 34 | { 35 | if(this->right != NULL) 36 | delete(this->right); 37 | if (this->left != NULL) 38 | delete(this->left); 39 | } 40 | 41 | Node* Node::removeNodes(Node* node, int value) 42 | { 43 | auto root = node; 44 | while(root->parent != NULL) 45 | { 46 | root = root->parent; 47 | } 48 | auto delNode = findValue(root, value); 49 | while(delNode != NULL) 50 | { 51 | root->removeNode(delNode); 52 | delNode = findValue(root, value); 53 | } 54 | 55 | return root; 56 | } 57 | 58 | void Node::addValue(Node* root, int value) 59 | { 60 | Node* aux = new Node; 61 | aux->value = value; 62 | 63 | if (root->right == NULL) 64 | { 65 | root->right = aux; 66 | aux->parent = root; 67 | } 68 | else 69 | { 70 | auto currentNode = root->right; 71 | while(1) 72 | { 73 | if(value > currentNode->value) 74 | { 75 | if (currentNode->right != NULL) 76 | { 77 | currentNode = currentNode->right; 78 | continue; 79 | } 80 | else 81 | { 82 | currentNode->right = aux; 83 | aux->parent = currentNode; 84 | return; 85 | } 86 | } 87 | else 88 | { 89 | if (currentNode->left != NULL) 90 | { 91 | currentNode = currentNode->left; 92 | continue; 93 | } 94 | else 95 | { 96 | currentNode->left = aux; 97 | aux->parent = currentNode; 98 | return; 99 | } 100 | } 101 | } 102 | } 103 | } 104 | 105 | Node* Node::findValue(Node* node, int value) 106 | { 107 | auto currentNode = node; 108 | 109 | if (currentNode->parent == NULL) 110 | currentNode = currentNode->right; 111 | while(1) 112 | { 113 | if (currentNode == NULL) 114 | return NULL; 115 | if(value == currentNode->value) 116 | { 117 | return currentNode; 118 | } 119 | else 120 | { 121 | if (currentNode->right == NULL && currentNode->left == NULL) 122 | return NULL; 123 | if (value > currentNode->value) 124 | { 125 | currentNode = currentNode->right; 126 | } 127 | else 128 | { 129 | currentNode = currentNode->left; 130 | } 131 | } 132 | 133 | 134 | } 135 | } 136 | 137 | void Node::removeNode(Node* node) 138 | { 139 | //auto delNode = findValue(node, value); 140 | auto delNode = node; 141 | auto delParent = delNode->parent; 142 | 143 | 144 | 145 | if(delParent->left == delNode) 146 | { 147 | // case is leaf 148 | if(delNode->left == NULL && delNode->right == NULL) 149 | { 150 | delParent->left = NULL; 151 | 152 | } 153 | else // not leaf 154 | { 155 | // case has only one child 156 | 157 | if(delNode->right == NULL && delNode->left != NULL) 158 | { 159 | delParent->left = delNode->left; 160 | delNode->left->parent = delParent; 161 | 162 | delNode->parent = NULL; 163 | delNode->left = NULL; 164 | delNode->right = NULL; 165 | delete(delNode); 166 | } 167 | else 168 | if(delNode->right != NULL && delNode->left == NULL) 169 | { 170 | delParent->left = delNode->right; 171 | delNode->right->parent = delParent; 172 | 173 | delNode->parent = NULL; 174 | delNode->left = NULL; 175 | delNode->right = NULL; 176 | delete(delNode); 177 | } 178 | else // case has two children 179 | { 180 | auto succesor = minValueNode(delNode->right); 181 | delNode->value = succesor->value; 182 | removeNode(succesor); 183 | 184 | 185 | } 186 | 187 | } 188 | } 189 | else 190 | { 191 | // case is leaf 192 | if (delNode->left == NULL && delNode->right == NULL) 193 | { 194 | delParent->right = NULL; 195 | 196 | } 197 | else // not leaf 198 | { 199 | // case has only one child 200 | 201 | if (delNode->right == NULL && delNode->left != NULL) 202 | { 203 | delParent->right = delNode->left; 204 | delNode->left->parent = delParent; 205 | 206 | delNode->parent = NULL; 207 | delNode->left = NULL; 208 | delNode->right = NULL; 209 | } 210 | else 211 | if (delNode->right != NULL && delNode->left == NULL) 212 | { 213 | delParent->right = delNode->right; 214 | delNode->right->parent = delParent; 215 | 216 | delNode->parent = NULL; 217 | delNode->left = NULL; 218 | delNode->right = NULL; 219 | } 220 | else // case has two children 221 | { 222 | auto succesor = minValueNode(delNode->right); 223 | delNode->value = succesor->value; 224 | removeNode(succesor); 225 | 226 | 227 | } 228 | 229 | } 230 | } 231 | 232 | } 233 | 234 | void Node::removeValue(Node* node, int value) 235 | { 236 | auto delNode = findValue(node, value); 237 | removeNode(delNode); 238 | } 239 | 240 | Node* Node::minValueNode(Node* node) 241 | { 242 | Node* current = node; 243 | 244 | while (current->left != NULL) 245 | current = current->left; 246 | 247 | return current; 248 | } 249 | 250 | 251 | void Node::inorder(const Node* node) 252 | { 253 | if (node != NULL) 254 | { 255 | inorder(node->left); 256 | cout << node->value<<" "; 257 | inorder(node->right); 258 | } 259 | } 260 | 261 | int main() 262 | { 263 | 264 | Node* root = new Node; 265 | root->addValue(root,50); 266 | root->addValue(root, 80); 267 | root->addValue(root, 30); 268 | root->addValue(root, 30); 269 | root->addValue(root, 30); 270 | root->addValue(root, 80); 271 | root->addValue(root, 20); 272 | root->addValue(root, 40); 273 | root->addValue(root, 70); 274 | root->addValue(root, 70); 275 | root->addValue(root, 70); 276 | root->addValue(root, 60); 277 | root->addValue(root, 80); 278 | root->addValue(root, 80); 279 | 280 | root->inorder(root->right); 281 | cout << "\n"; 282 | root->removeNodes(root, 30); 283 | root->removeNodes(root, 80); 284 | root->removeNodes(root, 70); 285 | root->inorder(root->right); 286 | cout << "\n"; 287 | 288 | 289 | delete (root); 290 | return 0; 291 | } 292 | -------------------------------------------------------------------------------- /Year 2 Sem 1/RBtree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | /* 7 | Removes all the nodes with a given value 8 | @param node belonging to tree 9 | @param value to search for 10 | @return root node 11 | */ 12 | using namespace std; 13 | class Node { 14 | public: 15 | Node* left; 16 | Node* right; 17 | Node* parent; 18 | 19 | Node(); 20 | static Node* removeNodes(Node* node, int value); 21 | void addValue(int value); 22 | const int getValue(); 23 | static Node* findValue(Node* node, int value); 24 | void displayBFS(); 25 | 26 | public: 27 | int value; 28 | char color; 29 | static Node* findPlace(Node* node, int value); 30 | static void fixTree(Node*& root, Node* node); 31 | static void rotateRight(Node*& root, Node*& node); 32 | static void rotateLeft(Node*& root, Node*& node); 33 | }; 34 | 35 | void inorderHelper(Node* root) 36 | { 37 | if (root == NULL) 38 | return; 39 | 40 | inorderHelper(root->left); 41 | cout << root->value << " "; 42 | inorderHelper(root->right); 43 | } 44 | 45 | int main() { 46 | 47 | Node* r = new Node; 48 | 49 | r->addValue(13); 50 | r->addValue(17); 51 | r->addValue(8); 52 | r->addValue(1); 53 | r->addValue(11); 54 | r->addValue(15); 55 | r->addValue(25); 56 | r->addValue(6); 57 | r->addValue(22); 58 | r->addValue(27); 59 | //Node* aux = r->findValue(r, 3); 60 | 61 | r->displayBFS(); 62 | cout << "\n"; 63 | //inorderHelper(r); 64 | return 0; 65 | } 66 | 67 | 68 | 69 | 70 | Node::Node() 71 | { 72 | this->parent = NULL; 73 | this->left = NULL; 74 | this->right = NULL; 75 | } 76 | 77 | void Node::addValue(int value) 78 | { 79 | auto& root = this->right; 80 | 81 | if (root == NULL) 82 | { 83 | Node* aux = new Node; 84 | aux->value = value; 85 | aux->color = 'B'; 86 | root = aux; 87 | } 88 | else { 89 | Node* goodParent = findPlace(root, value); 90 | Node* aux = new Node; 91 | aux->value = value; 92 | aux->color = 'R'; 93 | aux->parent = goodParent; 94 | if (value < goodParent->value) { 95 | goodParent->left = aux; 96 | } 97 | else { 98 | goodParent->right = aux; 99 | } 100 | 101 | if (aux->parent->color == 'R') 102 | { 103 | fixTree(root, aux); 104 | //cout << "I need fixing!"; 105 | } 106 | } 107 | } 108 | 109 | const int Node::getValue() 110 | { 111 | try { 112 | if (this == NULL) throw 1; 113 | return this->value; 114 | } 115 | catch (int e) { 116 | cout << "This node dose not exist"; 117 | } 118 | } 119 | 120 | Node* Node::findPlace(Node* node, int value) 121 | { 122 | Node* aux = node; 123 | while (1) { 124 | if (value < aux->value) { 125 | if (aux->left == NULL) 126 | return aux; 127 | aux = aux->left; 128 | } 129 | else { 130 | if (aux->right == NULL) 131 | return aux; 132 | aux = aux->right; 133 | } 134 | } 135 | } 136 | 137 | void Node::fixTree(Node*& root, Node* aux) 138 | { 139 | //3 parent is red 140 | while (aux != root && aux->color != 'B' && aux->parent->color == 'R') 141 | { 142 | auto parent = aux->parent; 143 | auto grandParent = aux->parent->parent; 144 | 145 | //Case 1 uncle is right 146 | if (parent == grandParent->left) 147 | { 148 | auto uncle = grandParent->right; 149 | // if uncle is red ->only recoloring 150 | if (uncle != NULL && uncle->color == 'R') 151 | { 152 | uncle->color = 'B'; 153 | parent->color = 'B'; 154 | grandParent->color = 'R'; 155 | aux = grandParent; 156 | } 157 | // uncle black -> need rotations 158 | else 159 | { 160 | if (aux == parent->right) 161 | { 162 | rotateLeft(root, parent); 163 | aux = parent; 164 | parent = aux->parent; 165 | } 166 | 167 | rotateRight(root, grandParent); 168 | swap(parent->color, grandParent->color); 169 | aux = parent; 170 | } 171 | } 172 | //Case 2 uncle is left 173 | else 174 | { 175 | auto uncle = grandParent->left; 176 | // if uncle red -> only recoloring 177 | 178 | if (uncle != NULL && uncle->color == 'R') 179 | { 180 | parent->color = 'B'; 181 | grandParent->color = 'R'; 182 | uncle->color = 'B'; 183 | aux = grandParent; 184 | } 185 | // uncle black -> need rotation 186 | else 187 | { 188 | if (aux == parent->left) 189 | { 190 | rotateRight(root, parent); 191 | aux = parent; 192 | parent = aux->parent; 193 | } 194 | 195 | rotateLeft(root, grandParent); 196 | swap(parent->color, grandParent->color); 197 | aux = parent; 198 | } 199 | } 200 | } 201 | root->color = 'B'; 202 | } 203 | 204 | void Node::rotateRight(Node*& root, Node*& node) 205 | { 206 | Node* childLeft = node->left; 207 | 208 | node->left = childLeft->right; 209 | 210 | if (node->left != NULL) 211 | node->left->parent = node; 212 | 213 | childLeft->parent = node->parent; 214 | 215 | if (node->parent == NULL) 216 | root = childLeft; 217 | 218 | else if (node == node->parent->left) 219 | node->parent->left = childLeft; 220 | 221 | else 222 | node->parent->right = childLeft; 223 | 224 | childLeft->right = node; 225 | node->parent = childLeft; 226 | } 227 | 228 | void Node::rotateLeft(Node*& root, Node*& node) 229 | { 230 | Node* childRight = node->right; 231 | 232 | node->right = childRight->left; 233 | 234 | if (node->right != NULL) 235 | node->right->parent = node; 236 | 237 | childRight->parent = node->parent; 238 | 239 | if (node->parent == NULL) 240 | root = childRight; 241 | 242 | else if (node == node->parent->left) 243 | node->parent->left = childRight; 244 | 245 | else 246 | node->parent->right = childRight; 247 | 248 | childRight->left = node; 249 | node->parent = childRight; 250 | } 251 | 252 | 253 | Node* Node::findValue(Node* node, int value) 254 | { 255 | Node* aux = node; 256 | while (1) { 257 | if (value == aux->value) 258 | return aux; 259 | if (aux->left == NULL && aux->right == NULL) 260 | return NULL; 261 | if (value < aux->value) { 262 | aux = aux->left; 263 | } 264 | else { 265 | aux = aux->right; 266 | } 267 | } 268 | } 269 | 270 | void Node::displayBFS() 271 | { 272 | Node* aux = this; 273 | map vis; 274 | queue q; 275 | q.push(this); 276 | 277 | while (!q.empty()) 278 | { 279 | auto thisNode = q.front(); 280 | q.pop(); 281 | if (vis.find(thisNode) != vis.end()) 282 | continue; 283 | 284 | vis[thisNode] = 1; 285 | cout << "(" << thisNode->value << "," << thisNode->color << ") "; 286 | 287 | if (thisNode->left != NULL) 288 | q.push(thisNode->left); 289 | if (thisNode->right != NULL) 290 | q.push(thisNode->right); 291 | 292 | } 293 | 294 | } 295 | --------------------------------------------------------------------------------