├── Week7 ├── output ├── README.md ├── dfs.cpp ├── bfs.cpp ├── shortest_path_bfs.cpp └── mine_sweeper.cpp ├── Week8 ├── output ├── README.md ├── queue.cpp ├── map.cpp ├── stack.cpp └── vector.cpp ├── Week1 ├── LectureNotes │ ├── output │ ├── lecture2.cpp │ ├── lecture7.cpp │ ├── lecture5.cpp │ ├── lecture8.cpp │ ├── lecture3.cpp │ ├── lecture4.cpp │ ├── lecture1.cpp │ └── lecture6.cpp ├── README.md ├── arrayFib.cpp ├── arrayLinearSearch.cpp ├── arrayOne.cpp ├── swapOne.cpp ├── swapTwo.cpp ├── maximize.cpp ├── PointerOne.cpp ├── PointerTwo.cpp ├── arrayDelete.cpp └── arrayPush.cpp ├── Plan ├── data structes plan.pdf └── data structes plan.docx ├── Week6 ├── README.md ├── fibonacci.cpp ├── factorial.cpp ├── exponentiation.cpp ├── readAndSum.cpp ├── fastExponentiation.cpp ├── collectiveCall.cpp ├── readSquareSum.cpp ├── sumArray.cpp └── multiplyArray.cpp ├── Week2 ├── merging_link_lists │ ├── a.out │ ├── list.h │ ├── reverse.cpp │ ├── merge.cpp │ └── list_functions.cpp ├── README.md ├── 2_struct_func.cpp ├── 1_struct_exp.cpp └── 3_linked_list.cpp ├── Week4 ├── README.md ├── stack_array.cpp ├── queue_array.cpp ├── stack_list.cpp ├── queue_list.cpp ├── expression_checker.cpp └── escape_from_maze.cpp ├── Week3 ├── README.md ├── 4_find_fib_example.cpp ├── 3_check_circular_example.cpp ├── 1_double_linked_list.cpp └── 2_circular_linkedList.cpp ├── .gitignore └── README.md /Week7/output: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sadikekin/Data-Structures-Lecture-2018-Fall-ITU/HEAD/Week7/output -------------------------------------------------------------------------------- /Week8/output: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sadikekin/Data-Structures-Lecture-2018-Fall-ITU/HEAD/Week8/output -------------------------------------------------------------------------------- /Week1/LectureNotes/output: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sadikekin/Data-Structures-Lecture-2018-Fall-ITU/HEAD/Week1/LectureNotes/output -------------------------------------------------------------------------------- /Plan/data structes plan.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sadikekin/Data-Structures-Lecture-2018-Fall-ITU/HEAD/Plan/data structes plan.pdf -------------------------------------------------------------------------------- /Week6/README.md: -------------------------------------------------------------------------------- 1 | ### Topics Of Week6 2 | 3 | | Topics | 4 | | :----------------- | 5 | | Recursion and Examples| 6 | 7 | -------------------------------------------------------------------------------- /Plan/data structes plan.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sadikekin/Data-Structures-Lecture-2018-Fall-ITU/HEAD/Plan/data structes plan.docx -------------------------------------------------------------------------------- /Week2/merging_link_lists/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sadikekin/Data-Structures-Lecture-2018-Fall-ITU/HEAD/Week2/merging_link_lists/a.out -------------------------------------------------------------------------------- /Week7/README.md: -------------------------------------------------------------------------------- 1 | ### Topics Of Week7 2 | 3 | | Topics | 4 | | :----------------- | 5 | | Graph | 6 | | BFS | 7 | | DFS | 8 | | Examples | 9 | 10 | -------------------------------------------------------------------------------- /Week8/README.md: -------------------------------------------------------------------------------- 1 | ### Topics Of Week8 2 | 3 | | Topics | 4 | | :----------------- | 5 | | What is STL ?| 6 | | Vector | 7 | | Stack | 8 | | Queue | 9 | | Map | 10 | 11 | -------------------------------------------------------------------------------- /Week4/README.md: -------------------------------------------------------------------------------- 1 | ### Topics Of Week4 2 | 3 | | Topics | 4 | | :----------------- | 5 | | Stack Implementation | 6 | | Queue Implementation | 7 | | Expression Checker (Example) | 8 | | Escape From Maze (Example) | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Week3/README.md: -------------------------------------------------------------------------------- 1 | ### Topics Of Week3 2 | 3 | | Topics | 4 | | :----------------- | 5 | | Double Linked List | 6 | | Circular Linked List | 7 | | Check Circular (example) | 8 | | Fibonacci in Linked List (example) | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Week6/fibonacci.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int fibo(int a){ 6 | if(a == 1 || a == 2) return 1; 7 | 8 | return fibo(a-1) + fibo(a-2); 9 | } 10 | int main() { 11 | printf("%d\n", fibo(5)); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /Week6/factorial.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int fac(int i){ 6 | // Base case 7 | if(i <= 1) return 1; 8 | 9 | // Recursive calls 10 | return i * fac(i-1); 11 | } 12 | int main(){ 13 | // Call fac 14 | printf("%d\n", fac(5)); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /Week1/LectureNotes/lecture2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int a = 5; 7 | 8 | int &b = a; 9 | 10 | cout << "The value of b " << b << endl; 11 | cout << "The address of b " << &b << endl; 12 | cout << "The address of a" << &a << endl; 13 | } 14 | -------------------------------------------------------------------------------- /Week1/LectureNotes/lecture7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int n = 5; 7 | int *arr = new int[n+1]; 8 | arr[0]=0; 9 | arr[1]=1; 10 | for(int i=2;i 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int n = 4; 7 | int *a = new int[3]; 8 | a[0] = 1; 9 | a[1] = 2; 10 | a[2] = 3; 11 | a[30] = 4; 12 | 13 | for(int i=0;i<30;i++) cout << a[i] << endl; 14 | 15 | delete[] a; 16 | 17 | return 0; 18 | 19 | } 20 | -------------------------------------------------------------------------------- /Week1/README.md: -------------------------------------------------------------------------------- 1 | ### Topics Of Week1 2 | 3 | | Topics | 4 | | :----------------- | 5 | | Pointer Definition | 6 | | Pointer in C | 7 | | Pointer in C++ | 8 | | Swap in C | 9 | | Swap in C++ | 10 | | Maximize given integers (exercise) | 11 | | Arrays | 12 | | Array Push | 13 | | Array Delete | 14 | | Fibonacci Sequence | 15 | | Linear Search | 16 | -------------------------------------------------------------------------------- /Week6/exponentiation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int exp(int base, int pow){ 6 | // Base 7 | if(pow == 0 ){ 8 | return 1; 9 | } 10 | 11 | // Multiply recursively 12 | return base * exp(base, pow-1); 13 | } 14 | 15 | int main(){ 16 | printf("%d\n", exp(2, 5)); 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /Week8/queue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | queue q; 8 | // Push to the queue 9 | for(int i=1;i<=10;i++) 10 | q.push(i); 11 | // Print elements of the queue 12 | while(!q.empty()){ 13 | cout << q.front() << endl; 14 | q.pop(); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /Week1/arrayFib.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | int main() { 7 | int t; 8 | scanf("%d", &t); 9 | int *arr = new int[t+1]; 10 | arr[0] = 0; 11 | arr[1] = 1; 12 | 13 | for(int i=2;i<=t;i++) 14 | arr[i] = arr[i-1] + arr[i-2]; 15 | 16 | printf("Ith fib is %d\n", arr[t]); 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app -------------------------------------------------------------------------------- /Week6/readAndSum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int recSum(int n){ 6 | // Base case 7 | if(n == 0) return 0; 8 | 9 | // Read 10 | int a; 11 | scanf("%d", &a); 12 | 13 | // Recursive calls 14 | return a + recSum(n-1); 15 | 16 | } 17 | 18 | int main() { 19 | int n; 20 | scanf("%d", &n); 21 | 22 | printf("%d\n", recSum(n)); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Week1/arrayLinearSearch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int n; 7 | scanf("%d", &n); 8 | 9 | int *arr = new int[n]; 10 | for(int i=0;i 2 | 3 | using namespace std; 4 | 5 | void changeSecondValue(int *arr) { 6 | arr[1] = 0; 7 | } 8 | 9 | int main() { 10 | // Create an array 11 | int *arr = new int[10]; 12 | for(int i=0;i<10;i++) arr[i] = 2*i-5; 13 | 14 | // Change the second value 15 | changeSecondValue(arr); 16 | 17 | // Print the array 18 | for(int i=0;i<10;i++) cout << arr[i] << endl; 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /Week1/LectureNotes/lecture8.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int n; 7 | scanf("%d", &n); 8 | int arr[n]; 9 | for(int i=0;i 3 | using namespace std; 4 | struct Node{ 5 | Node *next; // Keep address of next node. 6 | int val; //Could be anything. 7 | }; 8 | 9 | struct List{ 10 | Node *head; // Starting point of the list. 11 | int nodeCount; 12 | void init_list(); 13 | void add_end(int val); 14 | void add_start(int val); 15 | void add_index(int val,int index); 16 | void delete_at_index(int index); 17 | void print_list(); 18 | void delete_list(); 19 | }; 20 | -------------------------------------------------------------------------------- /Week1/swapOne.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void swap(int *a, int *b) { 6 | int temp = *a; 7 | *a = *b; 8 | *b = temp; 9 | } 10 | 11 | int main() { 12 | int a = 5; 13 | int b = 50; 14 | 15 | cout << "The value of a is " << a << endl; 16 | cout << "The value of b is " << b << endl; 17 | 18 | swap(&a, &b); 19 | 20 | cout << "The value of a is " << a << endl; 21 | cout << "The value of b is " << b << endl; 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Week1/swapTwo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void swap(int &a, int &b) { 6 | int temp = a; 7 | a = b; 8 | b = temp; 9 | } 10 | 11 | 12 | int main() { 13 | int a = 5; 14 | int b = 50; 15 | 16 | 17 | cout << "The value of a is " << a << endl; 18 | cout << "The value of b is " << b << endl; 19 | 20 | swap(a, b); 21 | 22 | cout << "The value of a is " << a << endl; 23 | cout << "The value of b is " << b << endl; 24 | 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Week1/LectureNotes/lecture3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void swapC(int *a, int *b) { 6 | int temp = *a; 7 | *a = *b; 8 | *b = temp; 9 | } 10 | 11 | void swapCpp(int &a, int &b){ 12 | int temp = a; 13 | a = b; 14 | b = temp; 15 | } 16 | 17 | int main() { 18 | int a=5,b=2; 19 | cout << "a is " << a << " b is " << b << endl; 20 | swapC(&a, &b); 21 | cout << "a is " << a << " b is " << b << endl; 22 | swapCpp(a, b); 23 | cout << "a is " << a << " b is " << b << endl; 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Week1/LectureNotes/lecture4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void maximize(int &a, int &b, int &c){ 6 | int arr[] = {a, b, c}; 7 | 8 | int max = 0; 9 | 10 | for(int i=0;i<3;i++) if(arr[i] > max) max = arr[i]; 11 | 12 | a = max, b = max, c = max; 13 | 14 | // Murat's method 15 | a = a > b ? a: b; 16 | a = b = c = (a > c ? a : c); 17 | 18 | } 19 | 20 | int main(){ 21 | int a,b,c; 22 | scanf("%d %d %d", &a, &b, &c); 23 | maximize(a,b,c); 24 | printf("%d %d %d\n", a, b, c); 25 | } 26 | -------------------------------------------------------------------------------- /Week2/README.md: -------------------------------------------------------------------------------- 1 | ### Topics Of Week2 2 | 3 | | Topics | 4 | | :----------------- | 5 | | Struct Definition | 6 | | Passing Structs to Functions | 7 | | Writing Functions Inside Structs| 8 | | Linked List | 9 | | Reverse a Link List(example)| 10 | | Merging Two Linked Lists(example) | 11 | 12 | 13 | 14 | | Discussions | 15 | | :----------------- | 16 | | Why should we use structs? | 17 | | Valgrind | 18 | | Why should we use linked lists? | 19 | | Linked Lists vs Arrays | 20 | | How can we insert elements to the end of a linked list faster? | 21 | -------------------------------------------------------------------------------- /Week8/map.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | 7 | int main(){ 8 | // Create map 9 | map studentList; 10 | studentList["ali"] = 150140103; 11 | studentList["ahmet"] = 150140102; 12 | studentList["ayse"] = 150140101; 13 | 14 | // Insert an element 15 | studentList.insert(make_pair("serra", 150170104)); 16 | 17 | 18 | // Print values 19 | for(auto el : studentList) { 20 | printf("%s %d\n", el.first.c_str(), el.second); 21 | } 22 | 23 | return 0; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /Week1/maximize.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void maximize(int &a, int &b, int &c){ 6 | int arr[3] = {a, b, c}; 7 | 8 | int maxVal = 0; 9 | 10 | for(int i=0;i<3;i++) if(arr[i] > maxVal) maxVal = arr[i]; 11 | 12 | a = maxVal, b = maxVal, c = maxVal; 13 | } 14 | 15 | int main() { 16 | int a = 5, b = 10, c = 99; 17 | 18 | cout << "a: " << a << " b: " << b << " c: " << c << endl; 19 | 20 | maximize(a, b, c); 21 | 22 | cout << "a: " << a << " b: " << b << " c: " << c << endl; 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Week6/fastExponentiation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int fastExp(int base, int pow){ 6 | // Base-1 7 | if(pow == 0){ 8 | return 1; 9 | } 10 | 11 | // Base-2 12 | if(pow == 1){ 13 | return base; 14 | } 15 | 16 | 17 | // recursive calls 18 | int half = fastExp(base, pow/2); 19 | if(pow%2 == 1){ 20 | return half * half * base; 21 | }else { 22 | return half*half; 23 | } 24 | 25 | } 26 | 27 | int main() { 28 | printf("%d\n",fastExp(2, 10)); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Week1/PointerOne.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int a = 5; 7 | cout << "The address of the a is " << &a << endl; 8 | 9 | int *b = &a; // The address of a is equal to pointer of b. 10 | cout << "The address of the b is " << b << " The value of the b is " << *b << endl; 11 | 12 | int allChanger = 50; 13 | *b = allChanger; 14 | cout << "The address of the a is " << &a << " The value of the a is " << a << endl; 15 | cout << "The address of the b is " << b << " The value of the b is " << *b << endl; 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /Week1/LectureNotes/lecture1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int a = 5; 7 | cout << "The address of a is " << &a << endl; 8 | 9 | int *b = &a; 10 | 11 | *b = 20; 12 | 13 | int c = 50; 14 | b = &c; 15 | 16 | cout << "The value of *b " << *b << endl; 17 | cout << "The value of real b " << b << endl; 18 | cout << "The address of b" << &b << endl; 19 | cout << "The value of c " << &c << endl; 20 | cout << "The new value of a " << a << endl; 21 | cout << "The new address of a " << &a << endl; 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Week6/collectiveCall.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void fTwo(int x); 6 | void fOne(int x); 7 | 8 | void fOne(int x){ 9 | // Base 10 | if(x <= 0){ 11 | return; 12 | } 13 | 14 | // Print 15 | printf("%d ", x); 16 | 17 | // Recursive call 18 | fTwo(x/2); 19 | } 20 | 21 | void fTwo(int x){ 22 | // Base 23 | if(x <= 0){ 24 | return; 25 | } 26 | 27 | // Print 28 | printf("%d ", x); 29 | 30 | // Recursive 31 | fOne(x-1); 32 | } 33 | 34 | int main(){ 35 | 36 | fOne(19); 37 | 38 | printf("\n"); 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Week1/PointerTwo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | // The second pointer access 7 | int a = 5; // Set a 5, again. 8 | cout << "The address of the a is " << &a << endl; 9 | 10 | int &b = a; // adress of b is equal to address of a. 11 | cout << "The address of the b is " << &b << " The value of the b is " << b << endl; 12 | 13 | int allChanger = 100; 14 | b = allChanger; 15 | cout << "The address of the a is " << &a << " The value of the a is " << a << endl; 16 | cout << "The address of the b is " << &b << " The value of the b is " << b << endl; 17 | return 0; 18 | } 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /Week8/stack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | stack> myStack; 9 | vector vec(5); 10 | 11 | // append values 12 | myStack.push(vec); 13 | vec.clear(); 14 | vec.resize(10, 9); 15 | myStack.push(vec); 16 | 17 | while(!myStack.empty()){ 18 | // Element in the stack 19 | auto el = myStack.top(); 20 | myStack.pop(); 21 | 22 | // iterate through vector 23 | for(auto val : el){ 24 | printf("%d ", val); 25 | } 26 | printf("\n"); 27 | 28 | 29 | } 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Week1/arrayDelete.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int *arr; 6 | int size; 7 | 8 | 9 | void deleteArr(int k) { 10 | int *temp = new int[size]; 11 | for(int i=0;ihead; 13 | Node *next; 14 | Node *prev = NULL; // previous node of the head is NULL. 15 | while(trav != NULL){ 16 | next = trav->next; 17 | trav->next = prev; 18 | prev = trav; 19 | trav = next; 20 | } 21 | list-> head = prev; 22 | } 23 | 24 | 25 | 26 | 27 | int main(){ 28 | List list; 29 | list.init_list(); 30 | list.add_end(1); 31 | list.add_end(3); 32 | list.add_end(5); 33 | list.add_end(6); 34 | list.add_end(-1); 35 | list.add_end(-2); 36 | list.print_list(); 37 | reverseList(&list); 38 | list.print_list(); 39 | list.delete_list(); 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Week6/readSquareSum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | // 2 5 | // 3 6 | // -1 -4 5 => 25 7 | // 4 8 | // 4 6 3 -10 => 61 (16 + 36 + 9) 9 | 10 | int readBlockNums(int n){ 11 | if(n == 0) return 0; 12 | 13 | // Read 14 | int a; 15 | scanf("%d", &a); 16 | 17 | // Return the output 18 | if(a > 0) 19 | return (a*a) + readBlockNums(n-1); 20 | else 21 | return readBlockNums(n-1); 22 | } 23 | 24 | void readBlock(int t){ 25 | // Base 26 | if(t == 0) return; 27 | 28 | // Read 29 | int n; 30 | scanf("%d", &n); 31 | 32 | // Calculate the answer 33 | int ans = readBlockNums(n); 34 | 35 | printf("%d\n", ans); 36 | 37 | // Recursive calls 38 | readBlock(t-1); 39 | 40 | } 41 | 42 | int main(){ 43 | int t; 44 | 45 | scanf("%d", &t); 46 | readBlock(t); 47 | 48 | return 0; 49 | 50 | } 51 | -------------------------------------------------------------------------------- /Week6/sumArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void fillArr(int i, int *myArr){ 6 | // Base Case 7 | if(i < 0){ 8 | return; 9 | } 10 | 11 | // Fill Arr 12 | myArr[i] = 10-i; 13 | 14 | // Call func 15 | fillArr(i-1, myArr); 16 | } 17 | 18 | void printArray(int i, int *myArr){ 19 | // Base Case 20 | if(i >= 10){ 21 | return; 22 | } 23 | 24 | // Print arr 25 | printf("%d ", myArr[i]); 26 | 27 | // Call func 28 | printArray(i + 1, myArr); 29 | } 30 | 31 | int sumArray(int i, int *myArr){ 32 | if(i >= 10){ 33 | return -10; 34 | } 35 | 36 | return myArr[i] + sumArray(i+1, myArr); 37 | } 38 | 39 | 40 | int main() { 41 | 42 | int myArr[10]; 43 | 44 | fillArr(9, myArr); 45 | 46 | printArray(0, myArr); 47 | 48 | printf("\n"); 49 | 50 | 51 | printf("%d\n", sumArray(0, myArr)); 52 | 53 | 54 | 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /Week1/arrayPush.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include // memcpy 3 | using namespace std; 4 | 5 | int currentSize = 0; 6 | int currentCapacity = 2; 7 | int *arr; 8 | 9 | void push(int val){ 10 | arr[currentSize] = val; 11 | 12 | currentSize++; 13 | if(currentSize >= currentCapacity){ 14 | currentCapacity *= 2; // Increase the capacity 15 | int *newArr = new int[currentCapacity]; // Allocate new dynamic array 16 | memcpy(newArr, arr, sizeof(int) * currentSize); // Copy old values to new array 17 | 18 | delete[] arr; // Delete old array 19 | arr = newArr; // Change the pointer 20 | } 21 | } 22 | 23 | int main() { 24 | arr = new int[currentCapacity]; 25 | push(1); 26 | push(2); 27 | push(3); 28 | push(4); 29 | push(1); 30 | push(8); 31 | push(1); 32 | push(10); 33 | 34 | for(int i=0;i 2 | 3 | using namespace std; 4 | 5 | void fillArr(int i, int *myArr){ 6 | // Base Case 7 | if(i < 0){ 8 | return; 9 | } 10 | 11 | // Fill Arr 12 | myArr[i] = 10-i; 13 | 14 | // Call func 15 | fillArr(i-1, myArr); 16 | } 17 | 18 | void printArray(int i, int *myArr){ 19 | // Base Case 20 | if(i >= 10){ 21 | return; 22 | } 23 | 24 | // Print arr 25 | printf("%d ", myArr[i]); 26 | 27 | // Call func 28 | printArray(i + 1, myArr); 29 | } 30 | 31 | // Tail recursive example 32 | int multiplyArray(int i, int totalMul, int *myArr){ 33 | if(i >= 10){ 34 | return totalMul; 35 | } 36 | 37 | return multiplyArray(i+1, totalMul * myArr[i], myArr); 38 | } 39 | 40 | 41 | int main() { 42 | 43 | int myArr[10]; 44 | 45 | fillArr(9, myArr); 46 | 47 | printArray(0, myArr); 48 | 49 | printf("\n"); 50 | 51 | 52 | printf("%d\n", multiplyArray(0, 1, myArr)); 53 | 54 | 55 | 56 | return 0; 57 | } 58 | 59 | -------------------------------------------------------------------------------- /Week4/stack_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define MAX_SIZE 100 4 | 5 | 6 | struct stack{ 7 | int size; 8 | int container[MAX_SIZE]; 9 | int sp; 10 | void init(){ 11 | size = 0; 12 | sp = 0; 13 | } 14 | void push(int inp){ 15 | if ( size < MAX_SIZE ){ 16 | container[sp] = inp; 17 | sp++; 18 | ++size; 19 | } 20 | } 21 | int top(){ 22 | return container[sp-1]; 23 | } 24 | void pop(){ 25 | if ( size > 0 ){ 26 | sp--; 27 | size--; 28 | } 29 | } 30 | void clear(){ 31 | sp = 0; 32 | } 33 | bool isEmpty(){ 34 | return size==0; 35 | } 36 | void dump(){ 37 | int trv = sp-1; 38 | while ( trv != -1 ){ 39 | std::cout << container[trv] << " "; 40 | trv--; 41 | } 42 | std::cout << std::endl; 43 | } 44 | }; 45 | 46 | 47 | int main(){ 48 | stack s; 49 | s.init(); 50 | s.push(1); 51 | s.push(2); 52 | s.push(3); 53 | s.push(5); 54 | 55 | 56 | s.dump(); 57 | s.clear(); 58 | return 0; 59 | } -------------------------------------------------------------------------------- /Week4/queue_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define MAX_SIZE 100 4 | 5 | /*Not good implementation*/ 6 | struct queue{ 7 | int size; 8 | int container[MAX_SIZE]; 9 | int qp; 10 | int head; 11 | void init(){ 12 | size = 0; 13 | qp = 0; 14 | head = 0; 15 | } 16 | void push(int inp){ 17 | if ( size < MAX_SIZE ){ 18 | container[qp] = inp; 19 | qp++; 20 | ++size; 21 | } 22 | } 23 | int top(){ 24 | return container[head]; 25 | } 26 | void pop(){ 27 | if ( size > 0 ){ 28 | head++; 29 | size--; 30 | } 31 | } 32 | void clear(){ 33 | qp = 0; 34 | head = 0; 35 | } 36 | bool isEmpty(){ 37 | return size==0; 38 | } 39 | void dump(){ 40 | int trv = head; 41 | while ( trv < qp ){ 42 | std::cout << container[trv] << " "; 43 | trv++; 44 | } 45 | std::cout << std::endl; 46 | } 47 | }; 48 | 49 | 50 | int main(){ 51 | queue q; 52 | q.init(); 53 | q.push(1); 54 | q.push(2); 55 | q.push(3); 56 | q.push(5); 57 | q.dump(); 58 | q.clear(); 59 | return 0; 60 | } -------------------------------------------------------------------------------- /Week1/LectureNotes/lecture6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int cap = 0; 6 | int size = 2; 7 | int *arr; 8 | 9 | void push(int a) { 10 | arr[cap] = a; 11 | cap++; 12 | if(cap >= size){ 13 | int *temp = new int[size]; 14 | for(int i=0;i 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | void dfs(int currentNode, vector< vector > &graph, vector &visited){ 8 | 9 | printf("%d ", currentNode); 10 | 11 | for(int i=0;i > graph; 24 | vector visited; 25 | 26 | 27 | // resize graph 28 | graph.resize(6); 29 | // create graph 30 | graph[0].push_back(1); 31 | graph[0].push_back(2); 32 | graph[0].push_back(4); 33 | 34 | graph[1].push_back(0); 35 | graph[1].push_back(2); 36 | 37 | graph[2].push_back(0); 38 | graph[2].push_back(1); 39 | 40 | 41 | graph[2].push_back(3); 42 | graph[2].push_back(4); 43 | 44 | graph[3].push_back(2); 45 | graph[3].push_back(5); 46 | 47 | graph[4].push_back(0); 48 | graph[4].push_back(2); 49 | graph[4].push_back(5); 50 | 51 | graph[5].push_back(3); 52 | graph[5].push_back(4); 53 | 54 | visited.resize(6,false); 55 | visited[0] = true; 56 | 57 | dfs(0); 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /Week8/vector.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | void takeVector(vector myVec){ 8 | printf("I am in the function -> %lf\n", myVec[0]); 9 | } 10 | 11 | bool mySortFunc(int a, int b){ 12 | return a < b; 13 | } 14 | int main() { 15 | // Create a vector 16 | vector vec(6, 5); 17 | 18 | // Loop through it 19 | for(int i=0;i %lf\n", vec[0]); 52 | // Clear the vector 53 | vec.clear(); 54 | 55 | printf("size -> %lu\n", vec.size()); 56 | printf("the capacity -> %lu\n", vec.capacity()); 57 | printf("is empty? -> %d\n", vec.empty()); 58 | 59 | 60 | } 61 | -------------------------------------------------------------------------------- /Week4/stack_list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | 5 | struct node{ 6 | int data; 7 | node *next; 8 | }; 9 | 10 | struct stack{ 11 | node *head; 12 | int size; 13 | 14 | void init(){ 15 | head = NULL; 16 | size = 0; 17 | } 18 | void push(int inp){ 19 | node *t = new node; 20 | 21 | t->data = inp; 22 | t->next = NULL; 23 | if (size > 0){ 24 | t->next = head; 25 | } 26 | head = t; 27 | ++size; 28 | } 29 | int top(){ 30 | return head->data; 31 | } 32 | void pop(){ 33 | if ( size > 0 ){ 34 | node *tmp = head->next; 35 | delete head; 36 | head = tmp; 37 | size--; 38 | } 39 | } 40 | void clear(){ 41 | while ( size > 0 ){ 42 | pop(); 43 | } 44 | } 45 | bool isEmpty(){ 46 | return size==0; 47 | } 48 | void dump(){ 49 | std::cout << "dumping\n"; 50 | node *trv = head; 51 | while ( trv!=NULL ){ 52 | std::cout << trv->data << " "; 53 | trv = trv->next; 54 | } 55 | std::cout << std::endl; 56 | } 57 | }; 58 | 59 | int main(){ 60 | /*Driver program for implemented stack*/ 61 | stack s; 62 | s.init(); 63 | s.push(1); 64 | s.push(2); 65 | s.push(3); 66 | 67 | 68 | s.dump(); 69 | s.clear(); 70 | return 0; 71 | } -------------------------------------------------------------------------------- /Week7/bfs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | 8 | void bfs(vector< vector > &graph){ 9 | queue q; 10 | q.push(0); 11 | 12 | vector visited(6, false); 13 | visited[0] = true; 14 | 15 | while(!q.empty()){ 16 | int currentNode = q.front(); 17 | q.pop(); 18 | 19 | printf("%d ", currentNode); 20 | 21 | for(int i=0;i > graph; 34 | // resize graph 35 | graph.resize(6); 36 | // create graph 37 | graph[0].push_back(1); 38 | graph[0].push_back(2); 39 | graph[0].push_back(4); 40 | 41 | graph[1].push_back(0); 42 | graph[1].push_back(2); 43 | 44 | graph[2].push_back(0); 45 | graph[2].push_back(1); 46 | graph[2].push_back(3); 47 | graph[2].push_back(4); 48 | 49 | graph[3].push_back(2); 50 | graph[3].push_back(5); 51 | 52 | graph[4].push_back(0); 53 | graph[4].push_back(2); 54 | graph[4].push_back(5); 55 | 56 | graph[5].push_back(3); 57 | graph[5].push_back(4); 58 | 59 | bfs(graph); 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /Week3/4_find_fib_example.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | using namespace std; 20 | 21 | struct Node { 22 | int val; 23 | Node *next; 24 | }; 25 | 26 | struct List { 27 | Node *head; 28 | void push(int val); 29 | void createFib(); 30 | bool printList(); 31 | }; 32 | 33 | void List::createFib() { 34 | Node *node = new Node; 35 | node->val = head->val + head->next->val; 36 | node->next = head; 37 | 38 | head = node; 39 | } 40 | void List::push(int val) { 41 | Node *tempNode = new Node; 42 | tempNode->next = head; 43 | tempNode->val = val; 44 | 45 | head = tempNode; 46 | } 47 | 48 | bool List::printList() { 49 | Node *traverse = head; 50 | 51 | while(traverse) { 52 | printf("%d ", traverse->val); 53 | traverse = traverse->next; 54 | } 55 | printf("\n"); 56 | } 57 | 58 | int main(){ 59 | List myList{}; 60 | 61 | // Init fib 62 | myList.push(1), myList.push(1); 63 | 64 | for(int i=0;i<6;i++) myList.createFib(); 65 | 66 | myList.printList(); 67 | 68 | 69 | 70 | 71 | return 0; 72 | } 73 | 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /Week4/queue_list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct node{ 4 | int data; 5 | node *next; 6 | }; 7 | 8 | struct queue{ 9 | node *head, *tail; 10 | int size; 11 | 12 | void init(){ 13 | head = NULL; 14 | tail = NULL; 15 | size = 0; 16 | } 17 | void push(int inp){ 18 | node *tmp = new node; 19 | 20 | tmp->next = NULL; 21 | tmp->data = inp; 22 | 23 | if ( size == 0 ){ 24 | head = tmp; 25 | tail = head; 26 | } 27 | 28 | if ( size > 0){ 29 | tail->next = tmp; 30 | tail = tmp; 31 | } 32 | size++; 33 | } 34 | int top(){ 35 | return head->data; 36 | } 37 | void pop(){ 38 | if ( size > 0 ){ 39 | node *tmp = head; 40 | delete head; 41 | head = tmp; 42 | size--; 43 | } 44 | } 45 | void clear(){ 46 | while ( size > 0 ){ 47 | pop(); 48 | } 49 | } 50 | bool isEmpty(){ 51 | return size==0; 52 | } 53 | void dump(){ 54 | std::cout << "dumping\n"; 55 | node *trv = head; 56 | while ( trv!=NULL ){ 57 | std::cout << trv->data << " "; 58 | trv = trv->next; 59 | } 60 | std::cout << std::endl; 61 | } 62 | }; 63 | 64 | int main(){ 65 | queue q; 66 | q.init(); 67 | q.push(1); 68 | q.push(2); 69 | q.push(3); 70 | q.push(5); 71 | q.push(3); 72 | 73 | 74 | q.dump(); 75 | q.clear(); 76 | return 0; 77 | } -------------------------------------------------------------------------------- /Week3/3_check_circular_example.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | using namespace std; 20 | 21 | 22 | // Find the cycle in given list 23 | 24 | struct Node { 25 | int val; 26 | Node *next; 27 | }; 28 | 29 | struct List { 30 | Node *head; 31 | void push(int val); 32 | bool cycleDetector(); 33 | }; 34 | 35 | void List::push(int val) { 36 | Node *tempNode = new Node; 37 | tempNode->next = head; 38 | tempNode->val = val; 39 | 40 | head = tempNode; 41 | } 42 | 43 | bool List::cycleDetector() { 44 | Node* fast = head; 45 | Node* slow = head; 46 | 47 | while(fast && slow && fast->next ){ 48 | fast = fast->next->next; 49 | slow = slow->next; 50 | 51 | if(slow == fast) return true; 52 | } 53 | return false; 54 | } 55 | 56 | int main(){ 57 | List myList{}; 58 | 59 | for(int i=0;i<6;i++) myList.push(i); 60 | 61 | Node *traverse = myList.head; 62 | while(traverse->next) traverse = traverse->next; 63 | traverse->next = myList.head->next; 64 | 65 | 66 | if(myList.cycleDetector()){ 67 | printf("Cycle has found!"); 68 | }else{ 69 | printf("There is no cycle"); 70 | } 71 | 72 | 73 | return 0; 74 | } 75 | 76 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ITU ACM 2018 - Data Structures Lecture 2 | 3 | ### Instructor 4 | 5 | **Sadık Ekin Özbay** 6 | 7 | *Computer Engineering #4 @I.T.U* 8 | 9 | [*LinkedIn*](https://www.linkedin.com/in/sadık-ekin-özbay/) 10 | 11 | ### Prerequisites 12 | 13 | 1. Very basic knowledge about any programming language(C++ is preferred). 14 | 15 | ### Goal 16 | This course has several aims. One of the aims is helping the enrolled students about the data structures course, that is given by the Istanbul Technical University. The second aim of this course is preparing the students about their possible interviews. 17 | 18 | A stable understanding of data structures is essential for every computer engineering student. Therefore, we as ITU ACM Student Branch members decided to open this kind of lectures to help Istanbul Technical University students. 19 | 20 | The third and maybe the most important aim of this course is helping the competitive programming community. Data structures knowledge is fundamental for competitive programming questions. 21 | 22 | 23 | 24 | ### Syllabus 25 | 26 | | Week | Topic | 27 | | :----------------- | ------------------------------------------------------------ | 28 | | Week 1 (11/10/18) | Dynamic Memory | 29 | | Week 2 (18/10/18) | Linked List 1 - Singular | 30 | | Week 3 (25/10/18) | Linked List 2 - Double, Circular | 31 | | Week 4 (01/11/18) | Stack, Queue and their applications | 32 | | Week 5 (08/11/18) | Mid-Break (Holiday) | 33 | | Week 6 (15/11/18) | Recursive programming - Contest-1 | 34 | | Week 7 (22/11/18) | Graph | 35 | | Week 8 (29/11/18) | STL - Contest-2 | 36 | | Week 9 (06/12/18) | Final Contest | 37 | 38 |

Lessons will be around 1.5 - 2 hours

39 | -------------------------------------------------------------------------------- /Week2/2_struct_func.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | struct Student{ 7 | string name; 8 | long number; 9 | int scores[3]; 10 | // A struct can have functions too. (Not in C language) 11 | void printStudent(); 12 | void enterScores(); 13 | void init_student(string , long); 14 | void add_scores(int, int, int); 15 | }; 16 | 17 | // Function of a Student struct. 'Student::' is added to that purpose. 18 | // If function is written inside of a struct, no need to pass a struct object to 19 | // function. Here print function does not have any parameter. 20 | void Student::printStudent(){ 21 | cout << "Name: "<name = name; 34 | this->number = number; 35 | // Here this pointer refers to a object that calls the funtion. Which is 36 | // student2 variable for our example 37 | } 38 | 39 | void Student::add_scores(int p1,int p2,int p3){ 40 | scores[0] = p1; 41 | this->scores[1] = p2; 42 | scores[2] = p3; 43 | } 44 | 45 | 46 | int main(){ 47 | 48 | Student student1; 49 | student1.name = "Burak Bekci"; 50 | student1.number = 12341234; 51 | student1.scores[0] = 12; 52 | student1.scores[1] = 62; 53 | student1.scores[2] = 92; 54 | student1.printStudent(); 55 | 56 | cout << endl << endl; 57 | Student student2; 58 | student2.init_student("Sadık Ekin",23241442); 59 | student2.add_scores(89,88,76); 60 | student2.printStudent(); 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /Week7/shortest_path_bfs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | //calculate the sorthest path from node-A to node-B in the directed unweighted graph. 21 | //1 22 | //4 6 3 4 23 | //1 2 24 | //1 3 25 | //1 4 26 | //3 1 27 | //3 2 28 | //2 4 29 | // Gives => 30 | //2 31 | 32 | using namespace std; 33 | 34 | int bfs(vector> &graph, int a, int b){ 35 | queue> q; 36 | q.push(make_pair(a, 0)); 37 | 38 | vector visited(graph.size(), false); 39 | visited[a] = true; 40 | 41 | int dist = INT_MAX; 42 | while(!q.empty()){ 43 | pair cNode = q.front(); q.pop(); 44 | 45 | if(cNode.first == b) dist = min(cNode.second, dist); 46 | 47 | for(auto node : graph[cNode.first]){ 48 | if(!visited[node]){ 49 | visited[node] = true; 50 | q.push(make_pair(node, cNode.second+1)); 51 | } 52 | } 53 | 54 | } 55 | 56 | return dist; 57 | } 58 | 59 | int main(){ 60 | int t,n,m,a,b; 61 | 62 | scanf("%d", &t); 63 | 64 | while(t--){ 65 | scanf("%d%d%d%d", &n, &m, &a, &b); 66 | vector> graph; 67 | graph.clear(); graph.resize((unsigned long)n + 5); 68 | for(int z,x,i=0;i 2 | 3 | using namespace std; 4 | /* 5 | Struct is a data type that can contain more than 1 different data types together. 6 | Here Person is the name of the struct. 7 | */ 8 | struct Person{ 9 | int age; 10 | float height; 11 | float weight; 12 | }; 13 | 14 | /* 15 | To access the member of the structs '.' operator is used. 16 | */ 17 | void print_Person(Person p){ 18 | cout << "Person with " << p.height << " m and " << p.weight << " kg at " << p.age << " age"<< endl; 19 | } 20 | /* 21 | Value of the Person(toUpdate) changed only in the function. 22 | It is because we pass its value to the function. Copy of the variable is created. 23 | */ 24 | void update_Person_height(float newHeight,Person toUpdate ){ 25 | toUpdate.height = newHeight; 26 | } 27 | /* 28 | Update Person using its address. 29 | Value changed everywhere of the code. 30 | When address of a struct element is used, '->' operator is used not '.'. 31 | */ 32 | void update_Person_height_pointer(float newHeight,Person *toUpdate ){ 33 | toUpdate->height = newHeight; 34 | } 35 | /* 36 | Update the value of an element using & operator. 37 | */ 38 | void update_Person_height_reference(float newHeight,Person &toUpdate ){ 39 | toUpdate.height = newHeight; 40 | } 41 | 42 | int main(){ 43 | Person person1; 44 | person1.age = 1; 45 | person1.height = 1.75; 46 | person1.weight = 80.2; 47 | cout << endl <<"Printing our person" << endl; 48 | print_Person(person1); 49 | cout << endl << "Let us try to change the height of the person to 1.93"< left: 1 2 15 27 and right: -1 3 10 11 19 7 | OUTPUT-> -1 1 2 3 10 11 15 19 27 8 | Initial link list should be empty at the end and no additional memory should be used. 9 | */ 10 | 11 | 12 | void merge_lists(List *left,List *right,List *merged){ 13 | 14 | Node *temp; // Used while insterting nodes. 15 | Node *trav = merged->head; 16 | while ( left->head != NULL && right->head != NULL ) { 17 | if ( left->head->val < right->head->val ) { // Add left head node 18 | temp = left->head; 19 | left->head = left->head->next; // Move left head to the next 20 | } 21 | else{ // Add right head node 22 | temp = right->head; 23 | right->head = right->head->next; // Move right head to the next 24 | } 25 | temp->next = NULL; 26 | if (merged->head == NULL) { 27 | merged->head = temp; 28 | } 29 | else{ 30 | trav = merged->head; 31 | while(trav->next != NULL){// Move to the end of the list. 32 | trav = trav->next; 33 | } 34 | trav->next = temp; 35 | } 36 | } 37 | // Elements of left or right has ended. Push remaining ones to merged list. 38 | Node *remainingList = right->head == NULL ? left->head : right->head; 39 | while(remainingList != NULL){ // Push remaining elements. 40 | temp = remainingList; 41 | remainingList = remainingList->next; 42 | temp->next = NULL; 43 | trav = merged->head; 44 | while(trav->next != NULL){ // Move to the end of the list. 45 | trav = trav->next; 46 | } 47 | trav->next = temp; 48 | } 49 | } 50 | 51 | 52 | int main(){ 53 | List listLeft,listRight,mergedList; 54 | listLeft.init_list(); 55 | listRight.init_list(); 56 | mergedList.init_list(); 57 | 58 | 59 | listLeft.add_end(1); 60 | listLeft.add_end(2); 61 | listLeft.add_end(15); 62 | listLeft.add_end(27); 63 | 64 | listRight.add_end(-1); 65 | listRight.add_end(3); 66 | listRight.add_end(10); 67 | listRight.add_end(11); 68 | listRight.add_end(19); 69 | 70 | listLeft.print_list(); 71 | listRight.print_list(); 72 | 73 | merge_lists(&listLeft,&listRight,&mergedList); 74 | 75 | mergedList.print_list(); 76 | 77 | mergedList.delete_list(); 78 | 79 | return 0; 80 | } 81 | -------------------------------------------------------------------------------- /Week7/mine_sweeper.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | //5 5 8 | //1 0 1 1 1 9 | //0 0 1 1 1 10 | //1 1 0 1 1 11 | //1 1 1 0 0 12 | //1 0 1 1 1 13 | //1 3 14 | 15 | int mineMovement(vector> matrix, int a, int b){ 16 | queue> q; 17 | q.push(make_pair(a,b)); 18 | 19 | vector> visited(matrix.size(), vector(matrix[0].size(), false)); 20 | 21 | visited[a][b] = true; 22 | 23 | int sum = 0; 24 | while(!q.empty()){ 25 | pair currentNode = q.front(); 26 | q.pop(); 27 | 28 | sum++; 29 | 30 | // UP 31 | int colm = currentNode.first - 1, row = currentNode.second; 32 | if(colm >= 0 && !visited[colm][row] && matrix[colm][row] != 0){ 33 | q.push(make_pair(colm, row)); 34 | visited[colm][row] = true; 35 | } 36 | 37 | // DOWN 38 | colm = currentNode.first + 1, row = currentNode.second; 39 | if(colm < matrix.size() && !visited[colm][row] && matrix[colm][row] != 0){ 40 | q.push(make_pair(colm, row)); 41 | visited[colm][row] = true; 42 | } 43 | 44 | 45 | // LEFT 46 | colm = currentNode.first, row = currentNode.second - 1; 47 | if(row >= 0 && !visited[colm][row] && matrix[colm][row] != 0){ 48 | q.push(make_pair(colm, row)); 49 | visited[colm][row] = true; 50 | } 51 | 52 | 53 | // RIGHT 54 | colm = currentNode.first, row = currentNode.second + 1; 55 | if(row < matrix[0].size() && !visited[colm][row] && matrix[colm][row] != 0){ 56 | q.push(make_pair(colm, row)); 57 | visited[colm][row] = true; 58 | } 59 | 60 | } 61 | 62 | return sum; 63 | 64 | } 65 | 66 | int main() { 67 | int n,m; 68 | scanf("%d %d", &n, &m); 69 | // Create the matrix 70 | vector> matrix(n, vector(m, 0)); 71 | 72 | int z; 73 | for(int i=0;inext = NULL; 6 | newnode->val = val; 7 | return newnode; 8 | } 9 | 10 | 11 | void List::init_list(){ 12 | head = NULL; 13 | nodeCount = 0; 14 | } 15 | //Add new node to start of the list. 16 | // New head will be this node. 17 | void List::add_start(int val){ 18 | Node *newnode = init_node(val); 19 | newnode->next = head; 20 | head = newnode; 21 | nodeCount++; 22 | } 23 | 24 | void List::print_list(){ 25 | Node *traverse = head; 26 | cout << "There are " << nodeCount << " items on the list."<val << " "; 29 | traverse = traverse->next; 30 | } 31 | cout << endl; 32 | } 33 | 34 | void List::add_end(int val){ 35 | Node *newnode = init_node(val); 36 | if(head == NULL) 37 | head = newnode; 38 | else{ 39 | Node *traverse = head; 40 | while(traverse->next != NULL){ // Move to the last index. 41 | traverse = traverse->next; 42 | } 43 | traverse->next = newnode; 44 | nodeCount++; 45 | } 46 | } 47 | 48 | void List::add_index(int val,int index){ 49 | if(index >= nodeCount) add_end(val); 50 | else if(index < 1) add_start(val); 51 | else{ 52 | Node *newnode = init_node(val); 53 | Node *traverse = head; // Move to the desired index. 54 | for(int i = 0 ;i < index-1;i++) 55 | traverse = traverse->next; 56 | // Pointer for the next element. 57 | Node *temp = traverse->next; 58 | //Order of the operations are important. 59 | newnode->next = temp; 60 | traverse->next = newnode; 61 | } 62 | } 63 | 64 | void List::delete_list(){ 65 | Node *traverse = head; 66 | while(traverse != NULL){ 67 | head = traverse->next; 68 | traverse->next = NULL; 69 | delete traverse; 70 | traverse = head; 71 | } 72 | delete head; 73 | } 74 | 75 | void List::delete_at_index(int index){ 76 | if(index < 0 || index > nodeCount) return; 77 | Node *traverse = head; // Will point to desired node to be deleted. 78 | if(index == 0){ 79 | head = traverse->next; 80 | } 81 | else{ 82 | Node *temp = head; // Will point the previous node. 83 | for(int i = 0 ; i< index ;i++){ 84 | temp = traverse; 85 | traverse = traverse->next; 86 | } 87 | temp->next = traverse->next; 88 | } 89 | traverse->next = NULL; 90 | delete traverse; 91 | nodeCount--; 92 | } 93 | -------------------------------------------------------------------------------- /Week4/expression_checker.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define MAX_LEN 2000 5 | 6 | struct node{ 7 | char data; 8 | node *next; 9 | }; 10 | 11 | struct stack{ 12 | node *head; 13 | int size; 14 | 15 | void init(){ 16 | head = NULL; 17 | size = 0; 18 | } 19 | void push(char inp){ 20 | node *t = new node; 21 | 22 | t->data = inp; 23 | t->next = NULL; 24 | if (size > 0){ 25 | t->next = head; 26 | } 27 | head = t; 28 | ++size; 29 | } 30 | char top(){ 31 | return head->data; 32 | } 33 | void pop(){ 34 | if ( size > 0 ){ 35 | node *tmp = head->next; 36 | delete head; 37 | head = tmp; 38 | size--; 39 | } 40 | } 41 | void clear(){ 42 | while ( size > 0 ){ 43 | pop(); 44 | } 45 | } 46 | bool isEmpty(){ 47 | return size==0; 48 | } 49 | void dump(){ 50 | std::cout << "dumping\n"; 51 | node *trv = head; 52 | while ( trv!=NULL ){ 53 | std::cout << trv->data << " "; 54 | trv = trv->next; 55 | } 56 | std::cout << std::endl; 57 | } 58 | }; 59 | 60 | 61 | bool isValid(char *expression ){ 62 | /* 63 | This procedure is simple, we will PUSH opening brackets to stack, 64 | and when we encounter closing bracket we will POP from stack then compare it to closing bracket to match them. 65 | */ 66 | stack s; 67 | s.init(); 68 | 69 | int length_of_string = strlen(expression); 70 | int i = 0; 71 | 72 | while ( i < length_of_string ){ 73 | char cur_char = expression[i]; 74 | if( cur_char == '(' || cur_char == '{' || cur_char == '[' ){ 75 | s.push( expression[i] ); 76 | } 77 | 78 | if( cur_char == ')' || cur_char == '}' || cur_char == ']' ){ 79 | if ( !s.isEmpty() ){ 80 | char c = s.top(); 81 | s.pop(); 82 | if ( !( (c == '(' && cur_char == ')') || ( c == '{' && cur_char == '}') || ( c == '[' && cur_char == ']')) ) { 83 | s.clear(); 84 | return false;; 85 | } 86 | } 87 | else{ 88 | s.clear(); 89 | return false; 90 | } 91 | } 92 | i++; 93 | } 94 | 95 | if ( !s.isEmpty() ){ 96 | s.clear(); 97 | return false; 98 | } 99 | s.clear(); 100 | return true; 101 | 102 | } 103 | int main(){ 104 | /*Driver program for experession checker*/ 105 | /* 106 | If an expression includes unmathced parantheses, it is unvalid. 107 | We will use stack data structure to check wheter it is valid or not. 108 | */ 109 | char expression[2000]; 110 | std::cin.getline(expression , MAX_LEN); 111 | std::cout << "Input expression is\t" << expression << std::endl; 112 | if ( isValid( expression ) ){ 113 | std::cout << "This expression is cool\n"; 114 | } 115 | else{ 116 | std::cout << "We can not evaluate it\n"; 117 | } 118 | stack s; 119 | 120 | return 0; 121 | } -------------------------------------------------------------------------------- /Week2/3_linked_list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | struct Node{ 7 | Node *next; // Keep address of next node. 8 | int val; //Could be anything. 9 | }; 10 | 11 | struct List{ 12 | Node *head; // Starting point of the list. 13 | int nodeCount; 14 | void init_list(); 15 | void add_end(int val); 16 | void add_start(int val); 17 | void add_index(int val,int index); 18 | void delete_at_index(int index); 19 | void print_list(); 20 | void delete_list(); 21 | }; 22 | 23 | // Allocate memory space for new node. 24 | Node* init_node(int val){ 25 | Node * newnode = new Node; 26 | newnode->next = NULL; 27 | newnode->val = val; 28 | return newnode; 29 | } 30 | 31 | void List::init_list(){ 32 | head = NULL; 33 | nodeCount = 0; 34 | } 35 | //Add new node to start of the list. 36 | // New head will be this node. 37 | void List::add_start(int val){ 38 | Node *newnode = init_node(val); 39 | newnode->next = head; 40 | head = newnode; 41 | nodeCount++; 42 | } 43 | 44 | void List::print_list(){ 45 | Node *traverse = head; 46 | cout << "There are " << nodeCount << " items on the list."<val << " "; 49 | traverse = traverse->next; 50 | } 51 | cout << endl; 52 | } 53 | 54 | void List::add_end(int val){ 55 | Node *newnode = init_node(val); 56 | if(head == NULL) 57 | head = newnode; 58 | else{ 59 | Node *traverse = head; 60 | while(traverse->next != NULL){ // Move to the last index. 61 | traverse = traverse->next; 62 | } 63 | traverse->next = newnode; 64 | nodeCount++; 65 | } 66 | } 67 | 68 | void List::add_index(int val,int index){ 69 | if(index >= nodeCount) add_end(val); 70 | else if(index < 1) add_start(val); 71 | else{ 72 | Node *newnode = init_node(val); 73 | Node *traverse = head; // Move to the desired index. 74 | for(int i = 0 ;i < index-1;i++) 75 | traverse = traverse->next; 76 | // Pointer for the next element. 77 | Node *temp = traverse->next; 78 | //Order of the operations are important. 79 | newnode->next = temp; 80 | traverse->next = newnode; 81 | } 82 | } 83 | 84 | void List::delete_list(){ 85 | Node *traverse = head; 86 | while(traverse != NULL){ 87 | head = traverse->next; 88 | traverse->next = NULL; 89 | delete traverse; 90 | traverse = head; 91 | } 92 | delete head; 93 | } 94 | 95 | void List::delete_at_index(int index){ 96 | if(index < 0 || index > nodeCount) return; 97 | Node *traverse = head; // Will point to desired node to be deleted. 98 | if(index == 0){ 99 | head = traverse->next; 100 | } 101 | else{ 102 | Node *temp = head; // Will point the previous node. 103 | for(int i = 0 ; i< index ;i++){ 104 | temp = traverse; 105 | traverse = traverse->next; 106 | } 107 | temp->next = traverse->next; 108 | } 109 | traverse->next = NULL; 110 | delete traverse; 111 | nodeCount--; 112 | } 113 | 114 | int main(){ 115 | List mList; 116 | mList.init_list(); 117 | cout << "Adding elements to first index..."< 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | #define N 15 8 | #define M 15 9 | 10 | 11 | struct node{ 12 | /* Position of node */ 13 | int x,y; 14 | node *next; 15 | }; 16 | 17 | struct stack{ 18 | node *head; 19 | int size; 20 | 21 | void init(){ 22 | head = NULL; 23 | size = 0; 24 | } 25 | void push(int x, int y){ 26 | node *t = new node; 27 | 28 | t->x = x; 29 | t->y = y; 30 | t->next = NULL; 31 | if (size > 0){ 32 | t->next = head; 33 | } 34 | head = t; 35 | ++size; 36 | } 37 | node top(){ 38 | node res; 39 | res.x = head->x; 40 | res.y = head->y; 41 | return res; 42 | } 43 | void pop(){ 44 | if ( size > 0 ){ 45 | node *tmp = head->next; 46 | delete head; 47 | head = tmp; 48 | size--; 49 | } 50 | } 51 | void clear(){ 52 | while ( size > 0 ){ 53 | pop(); 54 | } 55 | } 56 | bool isEmpty(){ 57 | return size==0; 58 | } 59 | void dump(){ 60 | std::cout << "dumping\n"; 61 | node *trv = head; 62 | while ( trv!=NULL ){ 63 | std::cout << trv->x << " " << trv->y << "\n"; 64 | trv = trv->next; 65 | } 66 | std::cout << std::endl; 67 | } 68 | }; 69 | 70 | bool isAvailable(char maze [N][M], int x, int y){ 71 | 72 | if ( x< 0 || y <0 || x>=N || y>=M) 73 | return false; 74 | if ( maze[x][y] == '#') 75 | return false; 76 | return true; 77 | } 78 | int moves[][2]= { {-1,0}, {0,-1}, {1,0}, {0,1}}; 79 | 80 | void print( char maze[N][M]){ 81 | for(int i=0; i< N; ++i){ 82 | for (int j=0; j < M; ++j){ 83 | std::cout << maze[i][j]; 84 | } 85 | std::cout << std::endl; 86 | } 87 | } 88 | 89 | bool dfs(int x, int y, char maze[N][M], bool visited[N][M], stack backtrack){ 90 | 91 | backtrack.push( x, y ); 92 | if ( maze[x][y] == '0' ) 93 | { 94 | backtrack.pop(); 95 | while( !backtrack.isEmpty() ){ 96 | 97 | std::cout << "Exit is " << x << " " << y << std::endl; 98 | print(maze); 99 | node step = backtrack.top(); 100 | backtrack.pop(); 101 | maze[step.x][step.y] = 'O'; 102 | if ( step.x == 0 && step.y == 0 ){ 103 | break; 104 | } 105 | system("sleep 0.01"); 106 | system("clear"); 107 | 108 | } 109 | 110 | backtrack.clear(); 111 | return true; 112 | } 113 | 114 | visited[x][y] = true; 115 | 116 | for (int i = 0; i < 4; ++i){ 117 | int new_x = x + moves[i][0]; 118 | int new_y = y + moves[i][1]; 119 | if ( isAvailable( maze, new_x, new_y ) && visited[ new_x ][ new_y ] == false ) { 120 | bool result = dfs( new_x, new_y, maze, visited, backtrack ); 121 | if (result){ 122 | return true; 123 | } 124 | } 125 | } 126 | 127 | return false; 128 | } 129 | int main(){ 130 | /* Escaping from maze */ 131 | /* 15 X 15 maze, # represent the blocks, we cannot move to there.*/ 132 | /* x = 0 ,y = 0 is our start point, for sake of simplicity 0 indexing is used and coordinates starts from top-left */ 133 | /* let 0 to our exit point here. */ 134 | /* Lets exit from maze */ 135 | 136 | char maze[N][M] = { 137 | {'*', '.', '.', '.', '.', '.', '.', '#', '#', '#', '.', '.', '.', '.', '.'}, 138 | {'.', '#', '#', '.', '.', '.', '.', '.', '#', '.', '.', '.', '.', '.', '.'}, 139 | {'.', '#', '#', '.', '.', '.', '.', '.', '.', '#', '#', '.', '#', '#', '.'}, 140 | {'.', '#', '#', '#', '.', '.', '.', '#', '.', '#', '#', '.', '#', '#', '.'}, 141 | {'#', '#', '#', '.', '.', '.', '#', '#', '.', '#', '#', '.', '#', '#', '.'}, 142 | {'.', '.', '#', '#', '.', '#', '.', '.', '.', '.', '#', '.', '#', '#', '.'}, 143 | {'.', '#', '#', '#', '.', '.', '.', '.', '.', '.', '.', '.', '.', '#', '.'}, 144 | {'.', '.', '.', '#', '.', '.', '.', '#', '.', '.', '.', '.', '#', '#', '.'}, 145 | {'.', '.', '.', '.', '.', '.', '.', '.', '#', '#', '#', '.', '#', '#', '.'}, 146 | {'.', '.', '.', '#', '.', '.', '#', '.', '#', '.', '#', '.', '.', '.', '#'}, 147 | {'.', '#', '.', '#', '.', '.', '#', '.', '#', '.', '#', '.', '.', '.', '#'}, 148 | {'.', '#', '.', '#', '.', '.', '.', '.', '#', '.', '#', '.', '.', '.', '.'}, 149 | {'.', '#', '.', '#', '.', '.', '.', '.', '#', '.', '#', '.', '0', '.', '.'}, 150 | {'.', '#', '.', '.', '.', '.', '.', '.', '#', '.', '#', '.', '.', '.', '.'}, 151 | {'.', '#', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.'} 152 | }; 153 | bool visited[N][M]; 154 | for (int i=0; i < N ; ++i) memset( *(visited+i), M, 0 ); 155 | stack s; 156 | s.init(); 157 | dfs(0,0,maze,visited ,s); 158 | 159 | 160 | return 0; 161 | } -------------------------------------------------------------------------------- /Week3/1_double_linked_list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | struct Node { 7 | Node *next; // Keep the address of the next node. 8 | Node *prev; // Keep the address of the prev node. 9 | int val; // Could be anything 10 | }; 11 | 12 | struct List { 13 | Node *head; // Starting point of the list 14 | int nodeCount; 15 | void init(); 16 | void addEnd(int val); 17 | void addStart(int val); 18 | void addIndex(int val, int index); 19 | void deleteEnd(); 20 | void deleteFront(); 21 | void deleteIndex(int index); 22 | void deleteList(); 23 | void printBack(); 24 | void printList(); 25 | 26 | }; 27 | 28 | // Create an not connected node. 29 | Node* initNode(int val){ 30 | Node* node = new Node; 31 | node->next = nullptr; 32 | node->prev = nullptr; 33 | node->val = val; 34 | return node; 35 | } 36 | 37 | // Init list. 38 | void List::init() { 39 | head = nullptr; 40 | nodeCount = 0; 41 | } 42 | 43 | // Add node to the end. 44 | void List::addEnd(int val) { 45 | if(nodeCount == 0){ // If list is empty make that node head. 46 | head = initNode(val); 47 | }else { // Otherwise go to the end. 48 | Node *traverse = head; 49 | 50 | while(traverse->next) 51 | traverse = traverse->next; 52 | 53 | Node *newNode = initNode(val); 54 | 55 | traverse->next = newNode; 56 | newNode->prev = traverse; 57 | } 58 | nodeCount++; 59 | } 60 | 61 | 62 | void List::addStart(int val) { 63 | if(nodeCount == 0){ // If list is empty make that node head. 64 | head = initNode(val); 65 | }else { // Otherwise add it to the start. 66 | Node *newNode = initNode(val); 67 | newNode->next = head; 68 | head->prev = newNode; 69 | head = newNode; 70 | nodeCount++; 71 | } 72 | 73 | } 74 | 75 | void List::addIndex(int val, int index) { 76 | if(index > nodeCount){ 77 | printf("Sorry dude, I do not have that many node\n"); 78 | return; 79 | } 80 | 81 | if(index == nodeCount){ // Index = nodeCount means addEnd; 82 | addEnd(val); 83 | return; 84 | } 85 | 86 | if(index == 0) { // Index = 0 means addStart 87 | addStart(val); 88 | return; 89 | } 90 | 91 | Node *traverse = head; 92 | 93 | while(index--) 94 | traverse = traverse->next; 95 | 96 | Node *newNode = initNode(val); 97 | newNode->prev = traverse->prev; 98 | newNode->next = traverse; 99 | 100 | traverse->prev->next = newNode; 101 | traverse->prev = newNode; 102 | 103 | nodeCount++; 104 | } 105 | 106 | // Go until the end and delete it. 107 | void List::deleteEnd() { 108 | if(head == nullptr) { 109 | printf("Really?! The list is already empty!\n"); 110 | return; 111 | } 112 | Node *traverse = head; 113 | 114 | while(traverse->next) 115 | traverse = traverse->next; 116 | 117 | traverse->prev->next = nullptr; 118 | delete traverse; 119 | nodeCount--; 120 | 121 | } 122 | 123 | void List::deleteFront() { 124 | Node *tempNode = head; 125 | 126 | 127 | tempNode->next->prev = nullptr; 128 | head = tempNode->next; 129 | 130 | delete tempNode; 131 | nodeCount--; 132 | 133 | } 134 | 135 | 136 | void List::deleteIndex(int index) { 137 | if(index > nodeCount){ 138 | printf("Sorry dude, I do not have that many node\n"); 139 | return; 140 | } 141 | 142 | if(index == 0) { 143 | deleteFront(); 144 | return; 145 | } 146 | 147 | if(index == nodeCount){ 148 | deleteEnd(); 149 | return; 150 | } 151 | 152 | Node *tempNode = head; 153 | while(index--) 154 | tempNode = tempNode->next; 155 | 156 | tempNode->prev->next = tempNode->next; 157 | tempNode->next->prev = tempNode->prev; 158 | 159 | delete tempNode; 160 | 161 | nodeCount--; 162 | } 163 | 164 | // Print from the front 165 | void List::printList() { 166 | 167 | if(nodeCount == 0){ 168 | printf("The list is empty\n"); 169 | return; 170 | } 171 | 172 | printf("The size of the list is %d\n", nodeCount); 173 | 174 | Node *tempNode = head; 175 | 176 | while(tempNode){ 177 | printf("%d ", tempNode->val); 178 | tempNode = tempNode->next; 179 | } 180 | printf("\n"); 181 | } 182 | 183 | // Delete all list 184 | void List::deleteList() { 185 | Node *tempNode = head; 186 | 187 | while(tempNode){ 188 | Node *newTemp = tempNode; 189 | tempNode = tempNode->next; 190 | delete newTemp; 191 | } 192 | 193 | nodeCount = 0; 194 | } 195 | 196 | // Print from the back. 197 | void List::printBack() { 198 | 199 | if(nodeCount == 0){ 200 | printf("The list is empty\n"); 201 | return; 202 | } 203 | 204 | printf("The size of the list is %d\n", nodeCount); 205 | 206 | Node *tempNode = head; 207 | 208 | while(tempNode->next) tempNode = tempNode->next; 209 | 210 | while(tempNode){ 211 | printf("%d ", tempNode->val); 212 | tempNode = tempNode->prev; 213 | } 214 | printf("\n"); 215 | } 216 | 217 | int main() { 218 | List myList; 219 | myList.init(); 220 | 221 | printf("\t Add End \n"); 222 | myList.addEnd(3), myList.addEnd(2), myList.addEnd(5), myList.addEnd(1); 223 | myList.printList(); 224 | 225 | printf("------------\n"); 226 | printf("\t Add Start \n"); 227 | 228 | myList.addStart(1), myList.addStart(2), myList.addStart(3); 229 | myList.printList(); 230 | 231 | printf("------------\n"); 232 | printf("\t Add to the Index \n"); 233 | 234 | myList.addIndex(10,0), myList.addIndex(30,1), myList.addIndex(50,1); 235 | myList.printList(); 236 | 237 | printf("------------\n"); 238 | printf("\t Delete from end \n"); 239 | 240 | myList.deleteEnd(), myList.deleteEnd(), myList.deleteEnd(); 241 | myList.printList(); 242 | 243 | printf("------------\n"); 244 | printf("\t Delete at index \n"); 245 | 246 | myList.deleteIndex(2); 247 | myList.printList(); 248 | 249 | printf("------------\n"); 250 | printf("\t Print backwards \n"); 251 | 252 | myList.printBack(); 253 | 254 | printf("------------\n"); 255 | printf("\t Delete all list \n"); 256 | 257 | myList.deleteList(); 258 | myList.printList(); 259 | 260 | printf("------------\n"); 261 | 262 | return 0; 263 | } 264 | -------------------------------------------------------------------------------- /Week3/2_circular_linkedList.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | using namespace std; 20 | 21 | struct Node { 22 | Node *next; // Keep the address of the next node. 23 | Node *prev; // Keep the address of the prev node. 24 | int val; // Could be anything 25 | }; 26 | 27 | struct List { 28 | Node *head; // Starting point of the list 29 | int nodeCount; 30 | void init(); 31 | void addEnd(int val); 32 | void addStart(int val); 33 | void addIndex(int val, int index); 34 | void deleteEnd(); 35 | void deleteFront(); 36 | void deleteIndex(int index); 37 | void deleteList(); 38 | void printBack(); 39 | void printList(); 40 | 41 | }; 42 | 43 | // Create an not connected node. 44 | Node* initNode(int val){ 45 | Node* node = new Node; 46 | node->next = nullptr; 47 | node->prev = nullptr; 48 | node->val = val; 49 | return node; 50 | } 51 | 52 | // Init list. 53 | void List::init() { 54 | head = nullptr; 55 | nodeCount = 0; 56 | } 57 | 58 | // Add node to the end. 59 | void List::addEnd(int val) { 60 | if(nodeCount == 0){ // If list is empty make that node head. 61 | head = initNode(val); 62 | head->next = head; 63 | head->prev = head; 64 | }else { // Otherwise go to the end. 65 | Node *traverse = head; 66 | 67 | while(traverse->next != head) 68 | traverse = traverse->next; 69 | 70 | Node *newNode = initNode(val); 71 | 72 | traverse->next = newNode; 73 | newNode->prev = traverse; 74 | 75 | newNode->next = head; 76 | head->prev = newNode; 77 | } 78 | nodeCount++; 79 | } 80 | 81 | 82 | void List::addStart(int val) { 83 | if(nodeCount == 0){ // If list is empty make that node head. 84 | head = initNode(val); 85 | head->next = head; 86 | head->prev = head; 87 | }else { // Otherwise add it to the start. 88 | Node *newNode = initNode(val); 89 | 90 | newNode->prev = head->prev; 91 | newNode->next = head; 92 | 93 | head->prev->next = newNode; 94 | head->prev = newNode; 95 | head = newNode; 96 | nodeCount++; 97 | } 98 | 99 | } 100 | 101 | void List::addIndex(int val, int index) { 102 | if(index > nodeCount){ 103 | printf("Sorry dude, I do not have that many node\n"); 104 | return; 105 | } 106 | 107 | if(index == nodeCount){ // Index = nodeCount means addEnd; 108 | addEnd(val); 109 | return; 110 | } 111 | 112 | if(index == 0) { // Index = 0 means addStart 113 | addStart(val); 114 | return; 115 | } 116 | 117 | Node *traverse = head; 118 | 119 | while(index--) 120 | traverse = traverse->next; 121 | 122 | Node *newNode = initNode(val); 123 | newNode->prev = traverse->prev; 124 | newNode->next = traverse; 125 | 126 | traverse->prev->next = newNode; 127 | traverse->prev = newNode; 128 | 129 | nodeCount++; 130 | } 131 | 132 | // Go until the end and delete it. 133 | void List::deleteEnd() { 134 | if(head == nullptr) { 135 | printf("Really?! The list is already empty!\n"); 136 | return; 137 | } 138 | Node *traverse = head; 139 | 140 | while(traverse->next != head) 141 | traverse = traverse->next; 142 | 143 | traverse->prev->next = head; 144 | delete traverse; 145 | nodeCount--; 146 | 147 | } 148 | 149 | void List::deleteFront() { 150 | Node *tempNode = head; 151 | 152 | 153 | tempNode->next->prev = head->prev; 154 | head = tempNode->next; 155 | 156 | delete tempNode; 157 | nodeCount--; 158 | 159 | } 160 | 161 | 162 | void List::deleteIndex(int index) { 163 | if(index > nodeCount){ 164 | printf("Sorry dude, I do not have that many node\n"); 165 | return; 166 | } 167 | 168 | if(index == 0) { 169 | deleteFront(); 170 | return; 171 | } 172 | 173 | if(index == nodeCount){ 174 | deleteEnd(); 175 | return; 176 | } 177 | 178 | Node *tempNode = head; 179 | while(index--) 180 | tempNode = tempNode->next; 181 | 182 | tempNode->prev->next = tempNode->next; 183 | tempNode->next->prev = tempNode->prev; 184 | 185 | delete tempNode; 186 | 187 | nodeCount--; 188 | } 189 | 190 | // Print from the front 191 | void List::printList() { 192 | 193 | if(nodeCount == 0){ 194 | printf("The list is empty\n"); 195 | return; 196 | } 197 | 198 | printf("The size of the list is %d\n", nodeCount); 199 | 200 | Node *tempNode = head; 201 | 202 | do { 203 | printf("%d ", tempNode->val); 204 | tempNode = tempNode->next; 205 | }while(tempNode != head); 206 | 207 | printf("\n"); 208 | } 209 | 210 | // Delete all list 211 | void List::deleteList() { 212 | Node *tempNode = head; 213 | 214 | while(nodeCount != 0){ 215 | Node *deletedNode = tempNode; 216 | tempNode = tempNode->next; 217 | delete deletedNode; 218 | nodeCount--; 219 | } 220 | 221 | 222 | 223 | } 224 | 225 | // Print from the back. 226 | void List::printBack() { 227 | 228 | if(nodeCount == 0){ 229 | printf("The list is empty\n"); 230 | return; 231 | } 232 | 233 | printf("The size of the list is %d\n", nodeCount); 234 | 235 | Node *tempNode = head; 236 | 237 | while(tempNode->next != head) tempNode = tempNode->next; 238 | 239 | do { 240 | printf("%d ", tempNode->val); 241 | tempNode = tempNode->prev; 242 | }while(tempNode != head); 243 | 244 | 245 | printf("\n"); 246 | } 247 | 248 | int main() { 249 | List myList; 250 | myList.init(); 251 | 252 | printf("\t Add End \n"); 253 | myList.addEnd(3), myList.addEnd(2), myList.addEnd(5), myList.addEnd(1); 254 | myList.printList(); 255 | 256 | printf("------------\n"); 257 | printf("\t Add Start \n"); 258 | 259 | myList.addStart(1), myList.addStart(2), myList.addStart(3); 260 | myList.printList(); 261 | 262 | printf("------------\n"); 263 | printf("\t Add to the Index \n"); 264 | 265 | myList.addIndex(10,0), myList.addIndex(30,1), myList.addIndex(50,1); 266 | myList.printList(); 267 | 268 | printf("------------\n"); 269 | printf("\t Delete from end \n"); 270 | 271 | myList.deleteEnd(), myList.deleteEnd(), myList.deleteEnd(); 272 | myList.printList(); 273 | 274 | printf("------------\n"); 275 | printf("\t Delete at index \n"); 276 | 277 | myList.deleteIndex(2); 278 | myList.printList(); 279 | 280 | printf("------------\n"); 281 | printf("\t Print backwards \n"); 282 | 283 | myList.printBack(); 284 | 285 | printf("------------\n"); 286 | printf("\t Delete all list \n"); 287 | 288 | myList.deleteList(); 289 | myList.printList(); 290 | 291 | printf("------------\n"); 292 | 293 | return 0; 294 | } 295 | --------------------------------------------------------------------------------