├── README.md ├── hashing ├── OpenAddressing │ ├── Demo.cs │ ├── HashTable.cs │ └── studentRecord.cs ├── OpenAddressing1 │ ├── Demo.cs │ ├── HashTable.cs │ └── studentRecord.cs ├── README.md └── SeparateChaining │ ├── Demo.cs │ ├── HashTable.cs │ ├── Node.cs │ ├── SingleLinkedList.cs │ └── StudentRecord.cs ├── linked-list ├── CircularLinkedList │ ├── CircularLinkedList.cs │ ├── Demo.cs │ └── Node.cs ├── CircularListConcatenateProject │ ├── CircularLinkedList.cs │ ├── Demo.cs │ └── Node.cs ├── DoubleLinkedListProject │ ├── Demo.cs │ ├── DoubleLinkedList.cs │ └── Node.cs ├── HeaderLinkedListProject │ ├── Demo.cs │ ├── HeaderLinkedList.cs │ └── Node.cs ├── README.md ├── SingleLinkedListProject │ ├── Demo.cs │ ├── Node.cs │ └── SingleLinkedList.cs ├── SingleListConcatenateProject │ ├── Demo.cs │ ├── Node.cs │ └── SingleLinkedList.cs ├── SingleListMergingProject │ ├── Demo.cs │ ├── Node.cs │ └── SingleLinkedList.cs └── SortedLinkedListProject │ ├── Demo.cs │ ├── Node.cs │ └── SortedLinkedList.cs ├── recursion ├── BaseConversion │ └── Program.cs ├── Display1toN │ └── Program.cs ├── Euclids │ └── Program.cs ├── Exponentiation │ └── Program.cs ├── Factorial │ └── Program.cs ├── Fibonacci │ └── Program.cs ├── README.md ├── SumOfDigits │ └── Program.cs └── TowerofHanoi │ └── Program.cs ├── searching ├── README.md ├── binary-search-recursive │ └── Program.cs ├── binary-search │ └── Program.cs ├── linear-search-sorted-array │ └── Program.cs ├── linear-search-with-sentinel │ └── Program.cs └── linear-search │ └── Program.cs ├── sorting ├── AddressCalculationSort │ ├── Node.cs │ ├── Program.cs │ └── SortedLinkedList.cs ├── BinaryTreeSort │ ├── BinarySearchTree.cs │ ├── Program.cs │ └── TreeNode.cs ├── BubbleSort │ └── BubbleSort.cs ├── HeapSort │ └── Program.cs ├── InsertionSort │ └── InsertionSort.cs ├── MergeSortIterative │ └── Program.cs ├── MergeSortRecursive │ └── Program.cs ├── Merging1Project │ └── Program.cs ├── MergingProject │ └── Program.cs ├── QuickSort │ └── Program.cs ├── README.md ├── RadixSortProject │ ├── Node.cs │ └── Program.cs ├── SelectionSort │ └── SelectionSort.cs └── ShellSort │ └── Program.cs ├── stack-queue ├── CircularQueueProject │ ├── CircularQueue.cs │ └── Program.cs ├── Deque │ ├── DequeA.cs │ └── Program.cs ├── ParenthesesProject │ ├── Demo.cs │ └── StackA.cs ├── PostfixProject │ ├── Demo.cs │ ├── StackChar.cs │ └── StackInt.cs ├── PriorityQueueProject │ ├── Node.cs │ ├── PriorityQueueL.cs │ └── Program.cs ├── QueueArrayProject │ ├── Demo.cs │ └── QueueA.cs ├── QueueLinkedListProject │ ├── Demo.cs │ ├── Node.cs │ └── QueueL.cs ├── QueuethruCircularLinkedList │ ├── Node.cs │ ├── Program.cs │ └── QueueCL.cs ├── README.md ├── StackArrayProject │ ├── Demo.cs │ └── StackA.cs └── StackLinkedListProject │ ├── Demo.cs │ ├── Node.cs │ └── StackL.cs └── tree ├── BinarySearchTreeProject ├── BinarySearchTree.cs ├── Demo.cs └── Node.cs ├── BinaryTreeProject ├── BinaryTree.cs ├── Demo.cs └── Node.cs ├── BuildHeapProject └── Program.cs ├── HeapProject ├── Demo.cs └── Heap.cs └── README.md /README.md: -------------------------------------------------------------------------------- 1 | # Data Structures and Algorithms in C# ( DSA ) 2 | 3 | This [Data Structures and Algorithms In C# (DSA)](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-csharp-masterclass?coupon=GITHUB50) repository is for the students of my online course available on Udemy. It contains the source code of all the programs used in the course. 4 | 5 | ### About the Course 6 | * Instructor - Deepali Srivastava, Author of [Ultimate Python Programming](https://www.amazon.in/Ultimate-Python-Programming-programs-questions/dp/935551655X) 7 | * Thoroughly detailed course with complete working programs 8 | * Contains lots of animations to help you visualize the concepts 9 | * Includes over 100 Quiz questions 10 | * Builds a solid foundation in Data Structures and Algorithms 11 | * Prepares you for coding interviews 12 | * Lifetime Access 13 | 14 | ### Courses by [Deepali Srivastava](https://www.udemy.com/user/deepalisrivastava/) 15 | 16 | [![data-structures-algorithms-csharp](https://user-images.githubusercontent.com/96913690/200234905-67b85dfd-20c4-4f4b-afd2-e10d3568fff8.jpg)](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-csharp-masterclass?coupon=GITHUB50) 17 | [![data-structures-algorithms-python](https://user-images.githubusercontent.com/96913690/200234827-86aec10a-bfab-4371-91fc-e2be855ff1ff.jpg)](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-python-masterclass?coupon=GITHUB50) 18 | [![data-structures-algorithms-java](https://user-images.githubusercontent.com/96913690/200234744-14a5ed97-085f-44f3-9298-979c2053c580.jpg)](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-java-masterclass?coupon=GITHUB50) 19 | [![data-structures-algorithms-c](https://user-images.githubusercontent.com/96913690/200234592-25d33957-0e9e-4cc0-b324-2a73325aca85.jpg)](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-c-masterclass?coupon=GITHUB50) 20 | 21 | 31 | 32 | ## Copyright 33 | © Copyright Deepali Srivastava : All rights reserved. 34 | Not to be used for commercial purposes. 35 | -------------------------------------------------------------------------------- /hashing/OpenAddressing/Demo.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace OpenAddressing 9 | { 10 | class Demo 11 | { 12 | static void Main(string[] args) 13 | { 14 | int id,choice; 15 | String name; 16 | 17 | Console.Write("Enter initial size of table : "); 18 | int size = Convert.ToInt32(Console.ReadLine()); 19 | 20 | HashTable table = new HashTable(size); 21 | 22 | while(true) 23 | { 24 | Console.WriteLine("1.Insert a record"); 25 | Console.WriteLine("2.Search a record"); 26 | Console.WriteLine("3.Delete a record"); 27 | Console.WriteLine("4.Display table"); 28 | Console.WriteLine("5.Exit"); 29 | 30 | Console.Write("Enter your choice : "); 31 | choice = Convert.ToInt32(Console.ReadLine()); 32 | 33 | if (choice == 5) 34 | break; 35 | 36 | switch(choice) 37 | { 38 | case 1 : 39 | Console.Write("Enter student id : "); 40 | id = Convert.ToInt32(Console.ReadLine()); 41 | Console.Write("Enter student name : "); 42 | name = Console.ReadLine(); 43 | 44 | studentRecord aRecord = new studentRecord(id,name); 45 | 46 | table.Insert(aRecord); 47 | break; 48 | case 2 : 49 | Console.Write("Enter a key to be searched : "); 50 | id = Convert.ToInt32(Console.ReadLine()); 51 | aRecord = table.Search(id); 52 | 53 | if( aRecord==null ) 54 | Console.WriteLine("Key not found"); 55 | else 56 | Console.WriteLine(aRecord.toString()); 57 | 58 | break; 59 | case 3: 60 | Console.Write("Enter a key to be deleted : "); 61 | id = Convert.ToInt32(Console.ReadLine()); 62 | table.Delete(id); 63 | break; 64 | case 4: 65 | table.DisplayTable(); 66 | break; 67 | 68 | } 69 | } 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /hashing/OpenAddressing/HashTable.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace OpenAddressing 9 | { 10 | public class HashTable 11 | { 12 | private studentRecord[] array; 13 | private int m; //size of the array 14 | int n; //number of records 15 | 16 | public HashTable() : this(11) 17 | { } 18 | 19 | public HashTable(int tableSize) 20 | { 21 | m = tableSize; 22 | array = new studentRecord[m]; 23 | n = 0; 24 | } 25 | 26 | int hash(int key) 27 | { 28 | return (key % m); 29 | } 30 | 31 | public void Insert(studentRecord newRecord) 32 | { 33 | int key = newRecord.getstudentId(); 34 | int h = hash(key); 35 | 36 | int location = h; 37 | 38 | for (int i = 1; i < m; i++) 39 | { 40 | if (array[location] == null || array[location].getstudentId() == -1) 41 | { 42 | array[location] = newRecord; 43 | n++; 44 | return; 45 | } 46 | 47 | if (array[location].getstudentId() == key) 48 | throw new System.InvalidOperationException("Duplicate key"); 49 | 50 | location = (h + i) % m; 51 | } 52 | Console.WriteLine("Table is full : Record can't be inserted "); 53 | } 54 | 55 | public studentRecord Search(int key) 56 | { 57 | int h = hash(key); 58 | int location = h; 59 | 60 | for (int i = 1; i < m; i++) 61 | { 62 | if (array[location] == null) 63 | return null; 64 | if (array[location].getstudentId() == key) 65 | return array[location]; 66 | location = (h + i) % m; 67 | } 68 | return null; 69 | } 70 | 71 | public void DisplayTable() 72 | { 73 | for (int i = 0; i < m; i++) 74 | { 75 | Console.Write("[" + i + "] --> "); 76 | 77 | if (array[i] != null && array[i].getstudentId() != -1) 78 | Console.WriteLine(array[i].toString()); 79 | else 80 | Console.WriteLine("___"); 81 | } 82 | } 83 | 84 | 85 | public studentRecord Delete(int key) 86 | { 87 | int h = hash(key); 88 | int location = h; 89 | 90 | for (int i = 1; i < m; i++) 91 | { 92 | if (array[location] == null) 93 | return null; 94 | if (array[location].getstudentId() == key) 95 | { 96 | studentRecord temp = array[location]; 97 | array[location].setstudentId(-1); 98 | n--; 99 | return temp; 100 | } 101 | location = (h + i) % m; 102 | } 103 | return null; 104 | } 105 | } 106 | } -------------------------------------------------------------------------------- /hashing/OpenAddressing/studentRecord.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace OpenAddressing 9 | { 10 | public class studentRecord 11 | { 12 | private int studentId; 13 | private String studentName; 14 | 15 | public studentRecord(int i, String name) 16 | { 17 | studentId = i; 18 | studentName = name; 19 | } 20 | public int getstudentId() 21 | { 22 | return studentId; 23 | } 24 | public void setstudentId(int i) 25 | { 26 | studentId = i; 27 | } 28 | public String toString() 29 | { 30 | return studentId + " " + studentName + " "; 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /hashing/OpenAddressing1/Demo.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace OpenAddressing1 9 | { 10 | class Demo 11 | { 12 | static void Main(string[] args) 13 | { 14 | int id,choice; 15 | String name; 16 | 17 | 18 | Console.Write("Enter initial size of table : "); 19 | int size = Convert.ToInt32(Console.ReadLine()); 20 | 21 | HashTable table = new HashTable(size); 22 | 23 | while(true) 24 | { 25 | Console.WriteLine("1.Insert a record"); 26 | Console.WriteLine("2.Search a record"); 27 | Console.WriteLine("3.Delete a record"); 28 | Console.WriteLine("4.Display table"); 29 | Console.WriteLine("5.Exit"); 30 | 31 | Console.Write("Enter your choice : "); 32 | choice = Convert.ToInt32(Console.ReadLine()); 33 | 34 | if (choice == 5) 35 | break; 36 | 37 | switch(choice) 38 | { 39 | case 1 : 40 | Console.Write("Enter student id : "); 41 | id = Convert.ToInt32(Console.ReadLine()); 42 | Console.Write("Enter student name : "); 43 | name = Console.ReadLine(); 44 | 45 | studentRecord aRecord = new studentRecord(id,name); 46 | 47 | table.Insert1(aRecord); 48 | break; 49 | case 2 : 50 | Console.Write("Enter a key to be searched : "); 51 | id = Convert.ToInt32(Console.ReadLine()); 52 | aRecord = table.Search(id); 53 | 54 | 55 | if( aRecord==null ) 56 | Console.WriteLine("Key not found"); 57 | else 58 | Console.WriteLine(aRecord); 59 | 60 | break; 61 | case 3: 62 | Console.Write("Enter a key to be deleted : "); 63 | id = Convert.ToInt32(Console.ReadLine()); 64 | table.Delete(id); 65 | break; 66 | case 4: 67 | table.DisplayTable(); 68 | break; 69 | 70 | } 71 | } 72 | } 73 | } 74 | } 75 | 76 | -------------------------------------------------------------------------------- /hashing/OpenAddressing1/HashTable.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace OpenAddressing1 9 | { 10 | public class HashTable 11 | { 12 | private studentRecord[] array; 13 | private int m; //size of the array 14 | int n; //number of records 15 | 16 | public HashTable() : this(11) 17 | { 18 | } 19 | 20 | public HashTable(int tableSize) 21 | { 22 | m = tableSize; 23 | array = new studentRecord[m]; 24 | n = 0; 25 | } 26 | 27 | public void DisplayTable() 28 | { 29 | for(int i=0; i " ); 32 | 33 | if(array[i]!=null && array[i].getstudentId()!=-1) 34 | Console.WriteLine( array[i].toString() ); 35 | else 36 | Console.WriteLine("___"); 37 | } 38 | } 39 | 40 | public studentRecord Search(int key) 41 | { 42 | int h = hash(key); 43 | int location = h; 44 | 45 | for (int i = 1; i < m; i++) 46 | { 47 | if (array[location] == null) 48 | return null; 49 | if (array[location].getstudentId() == key) 50 | return array[location]; 51 | location = (h + i) % m; 52 | } 53 | return null; 54 | } 55 | 56 | public void Insert1(studentRecord newRecord) 57 | { 58 | if (n >= m/2) 59 | { 60 | rehash( nextPrime(2*m) ); 61 | Console.WriteLine( "New Table Size is : " + m ); 62 | } 63 | Insert(newRecord); 64 | } 65 | 66 | public void Insert(studentRecord newRecord) 67 | { 68 | 69 | int key = newRecord.getstudentId(); 70 | int h = hash(key); 71 | 72 | int location = h; 73 | 74 | for (int i = 1; i < m; i++) 75 | { 76 | if (array[location] == null || array[location].getstudentId() == -1) 77 | { 78 | array[location] = newRecord; 79 | n++; 80 | return; 81 | } 82 | if (array[location].getstudentId() == key) 83 | throw new System.InvalidOperationException("Duplicate key"); 84 | location = (h + i) % m; 85 | } 86 | } 87 | 88 | public void Delete(int key) 89 | { 90 | int h = hash(key); 91 | int location=h; 92 | 93 | for(int i=1; i 0 && n <= m/8) 102 | { 103 | rehash(nextPrime(m/2)); 104 | Console.WriteLine( "New Table Size is : " + m ); 105 | } 106 | } 107 | location = (h+i)%m; 108 | } 109 | } 110 | 111 | int hash(int key) 112 | { 113 | return (key % m); 114 | } 115 | 116 | private void rehash(int newSize) 117 | { 118 | HashTable temp = new HashTable(newSize); 119 | 120 | for (int i = 0; i < m; i++) 121 | if (array[i] != null && array[i].getstudentId() != -1) 122 | temp.Insert(array[i]); 123 | 124 | array = temp.array; 125 | m = newSize; 126 | } 127 | 128 | int nextPrime(int x) 129 | { 130 | while (!isPrime(x)) 131 | x++; 132 | return x; 133 | } 134 | 135 | bool isPrime(int x) 136 | { 137 | for (int i = 2; i < x; i++) 138 | { 139 | if (x % i == 0) 140 | return false; 141 | } 142 | return true; 143 | } 144 | 145 | } 146 | } 147 | -------------------------------------------------------------------------------- /hashing/OpenAddressing1/studentRecord.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace OpenAddressing1 9 | { 10 | public class studentRecord 11 | { 12 | private int studentId; 13 | private String studentName; 14 | 15 | public studentRecord(int i, String name) 16 | { 17 | studentId = i; 18 | studentName = name; 19 | } 20 | 21 | public int getstudentId() 22 | { 23 | return studentId; 24 | } 25 | 26 | public void setstudentId(int i) 27 | { 28 | studentId = i; 29 | } 30 | 31 | public String toString() 32 | { 33 | return studentId + " " + studentName + " "; 34 | } 35 | 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /hashing/README.md: -------------------------------------------------------------------------------- 1 | # Data Structures and Algorithms in C# 2 | 3 | This [“Data Structures and Algorithms in C#”](https://www.udemy.com/course/data-structures-and-algorithms-in-csharp/?couponCode=GITHUBSTUDENT ) repository is for the students of my online course available on Udemy. It contains the source code of all the programs used in the course. 4 | 5 | ### About the Course 6 | * Thoroughly detailed course with complete working programs 7 | * Contains lots of animations to help you visualize the concepts 8 | * Includes over 100 Quiz questions 9 | * Builds a solid foundation in Data Structures and Algorithms 10 | * Prepares you for coding interviews 11 | * Lifetime Access 12 | 13 | ### Courses by [Deepali Srivastava](https://www.udemy.com/user/deepalisrivastava/) 14 | 15 | [![data-structures- and-algorithms-in-csharp](https://user-images.githubusercontent.com/98641125/153196407-99441e67-24a7-4fa0-aaea-78cb39743282.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-csharp/?couponCode=GITHUBSTUDENT) 16 | [![data-structures- and-algorithms-in-csharp-2](https://user-images.githubusercontent.com/98641125/153196486-9ce09c97-8724-4492-b5d6-4bb4aeb1d8bd.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-csharp-2/?couponCode=GITHUBSTUDENT) 17 | [![data-structures- and-algorithms-in-c-plus-plus](https://user-images.githubusercontent.com/98641125/153196522-2412c993-1055-4322-8487-4133537566c9.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-c-plus-plus/?couponCode=GITHUBSTUDENT) 18 | [![python-programming-in-depth](https://user-images.githubusercontent.com/98641125/153196166-45ef8461-adb1-4f9f-b9ee-e482a5ad54a7.png)]( https://www.udemy.com/course/python-programming-in-depth/?couponCode=GITHUBSTUDENT) 19 | [![linux-commands](https://user-images.githubusercontent.com/98641125/153196567-96b3396c-8ee3-4233-b8fc-66c6b3bd830c.png)]( https://www.udemy.com/course/linux-commands/?couponCode=GITHUBSTUDENT) 20 | [![data-structures-and-algorithms-in-c](https://user-images.githubusercontent.com/98641125/153195841-209d2615-ed5f-4007-ae54-539ac3c1538a.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-c/?couponCode=GITHUBSTUDENT) 21 | [![data-structures- and-algorithms-in-java](https://user-images.githubusercontent.com/98641125/153196280-c2028f4b-d27b-432d-ad5a-9b04be2a3717.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-java/?couponCode=GITHUBSTUDENT) 22 | [![data-structures- and-algorithms-in-java-2](https://user-images.githubusercontent.com/98641125/153196347-23003dc0-55b0-4315-8e52-425c51b2b5c4.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-java-2/?couponCode=GITHUBSTUDENT) 23 | 24 | ## Copyright 25 | © Copyright Deepali Srivastava : All rights reserved. 26 | Not to be used for commercial purposes. 27 | -------------------------------------------------------------------------------- /hashing/SeparateChaining/Demo.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace SeparateChaining 9 | { 10 | public class Demo 11 | { 12 | static void Main(string[] args) 13 | { 14 | int id, choice; 15 | String name; 16 | 17 | Console.Write("Enter initial size of table : "); 18 | int size = Convert.ToInt32(Console.ReadLine()); 19 | 20 | HashTable table = new HashTable(size); 21 | 22 | while (true) 23 | { 24 | Console.WriteLine("1.Insert a record"); 25 | Console.WriteLine("2.Search a record"); 26 | Console.WriteLine("3.Delete a record"); 27 | Console.WriteLine("4.Display table"); 28 | Console.WriteLine("5.Exit"); 29 | 30 | Console.Write("Enter your choice : "); 31 | choice = Convert.ToInt32(Console.ReadLine()); 32 | 33 | if (choice == 5) 34 | break; 35 | 36 | switch (choice) 37 | { 38 | case 1: 39 | Console.Write("Enter student id : "); 40 | id = Convert.ToInt32(Console.ReadLine()); 41 | Console.Write("Enter student name : "); 42 | name = Console.ReadLine(); 43 | 44 | studentRecord aRecord = new studentRecord(id, name); 45 | 46 | table.Insert(aRecord); 47 | break; 48 | case 2: 49 | Console.Write("Enter a key to be searched : "); 50 | id = Convert.ToInt32(Console.ReadLine()); 51 | aRecord = table.Search(id); 52 | 53 | if (aRecord == null) 54 | Console.WriteLine("Key not found"); 55 | else 56 | Console.WriteLine(aRecord.toString()); 57 | 58 | break; 59 | case 3: 60 | Console.Write("Enter a key to be deleted : "); 61 | id = Convert.ToInt32(Console.ReadLine()); 62 | table.Delete(id); 63 | break; 64 | case 4: 65 | table.DisplayTable(); 66 | break; 67 | } 68 | } 69 | } 70 | } 71 | } 72 | 73 | 74 | 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /hashing/SeparateChaining/HashTable.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace SeparateChaining 9 | { 10 | public class HashTable 11 | { 12 | private SingleLinkedList[] array; 13 | private int m; //size of the array 14 | private int n; //number of records 15 | 16 | public HashTable() : this(11) 17 | { 18 | } 19 | 20 | public HashTable(int tableSize) 21 | { 22 | m = tableSize; 23 | n = 0; 24 | array = new SingleLinkedList[tableSize]; 25 | } 26 | 27 | int hash(int key) 28 | { 29 | return (key % m); 30 | } 31 | 32 | public void DisplayTable() 33 | { 34 | for(int i=0; i "); 37 | 38 | if(array[i]!= null) 39 | array[i].displayList(); 40 | else 41 | Console.WriteLine("___"); 42 | } 43 | } 44 | 45 | public studentRecord Search(int key) 46 | { 47 | int h = hash(key); 48 | 49 | if (array[h] != null) 50 | return array[h].search(key); 51 | 52 | return null; 53 | } 54 | 55 | public void Insert(studentRecord newRecord) 56 | { 57 | int key = newRecord.getstudentId(); 58 | int h = hash(key); 59 | 60 | if (array[h] == null) 61 | array[h] = new SingleLinkedList(); 62 | 63 | array[h].insertInBeginning(newRecord); 64 | n++; 65 | } 66 | 67 | public void Delete(int key) 68 | { 69 | int h = hash(key); 70 | 71 | if (array[h] != null) 72 | { 73 | array[h].deleteNode(key); 74 | n--; 75 | } 76 | else 77 | Console.WriteLine("Value " + key + " not present\n"); 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /hashing/SeparateChaining/Node.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace SeparateChaining 9 | { 10 | public class Node 11 | { 12 | public studentRecord info; 13 | public Node link; 14 | 15 | public Node(studentRecord rec) 16 | { 17 | info = rec; 18 | link = null; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /hashing/SeparateChaining/SingleLinkedList.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace SeparateChaining 9 | { 10 | public class SingleLinkedList 11 | { 12 | private Node start; 13 | 14 | public SingleLinkedList() 15 | { 16 | start = null; 17 | } 18 | 19 | public void displayList() 20 | { 21 | Node p; 22 | if(start==null) 23 | { 24 | Console.WriteLine("___"); 25 | return; 26 | } 27 | p=start; 28 | while(p!=null) 29 | { 30 | Console.Write(p.info.toString() + " "); 31 | p=p.link; 32 | } 33 | Console.WriteLine(); 34 | } 35 | 36 | 37 | public studentRecord search(int x) 38 | { 39 | Node p=start; 40 | while(p!=null) 41 | { 42 | if(p.info.getstudentId()==x) 43 | break; 44 | p=p.link; 45 | } 46 | if(p==null) 47 | { 48 | Console.WriteLine(x + " not found in list"); 49 | return null; 50 | } 51 | else 52 | return p.info; 53 | } 54 | 55 | public void insertInBeginning(studentRecord data) 56 | { 57 | Node temp = new Node(data); 58 | temp.link = start; 59 | start = temp; 60 | } 61 | 62 | public void deleteNode(int x) 63 | { 64 | if(start==null) 65 | { 66 | Console.WriteLine("Value " + x + " not present\n"); 67 | return; 68 | } 69 | 70 | /*Deletion of first node*/ 71 | if(start.info.getstudentId()==x) 72 | { 73 | start=start.link; 74 | return; 75 | } 76 | 77 | /*Deletion in between or at the end*/ 78 | Node p=start; 79 | while(p.link!=null) 80 | { 81 | if(p.link.info.getstudentId()==x) 82 | break; 83 | p=p.link; 84 | } 85 | 86 | if(p.link==null) 87 | Console.WriteLine("Value " + x + " not present\n"); 88 | else 89 | p.link = p.link.link; 90 | } 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /hashing/SeparateChaining/StudentRecord.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace SeparateChaining 9 | { 10 | public class studentRecord 11 | { 12 | private int studentId; 13 | private String studentName; 14 | 15 | public studentRecord(int i, String name) 16 | { 17 | studentId=i; 18 | studentName = name; 19 | } 20 | public int getstudentId() 21 | { 22 | return studentId; 23 | } 24 | public void setstudentId(int i) 25 | { 26 | studentId = i; 27 | } 28 | public String toString() 29 | { 30 | return studentId + " " + studentName + " "; 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /linked-list/CircularLinkedList/CircularLinkedList.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace CircularLinkedListProject 9 | { 10 | class CircularLinkedList 11 | { 12 | private Node last; 13 | 14 | public CircularLinkedList() 15 | { 16 | last = null; 17 | } 18 | 19 | public void DisplayList() 20 | { 21 | if(last == null) 22 | { 23 | Console.WriteLine("List is empty\n"); 24 | return; 25 | } 26 | 27 | Node p = last.link; 28 | do 29 | { 30 | Console.Write(p.info + " "); 31 | p = p.link; 32 | }while( p != last.link ); 33 | 34 | Console.WriteLine(); 35 | } 36 | 37 | public void InsertInBeginning(int data) 38 | { 39 | Node temp = new Node(data); 40 | temp.link = last.link; 41 | last.link = temp; 42 | } 43 | 44 | public void InsertInEmptyList(int data) 45 | { 46 | Node temp = new Node(data); 47 | last = temp; 48 | last.link = last; 49 | } 50 | 51 | public void InsertAtEnd(int data) 52 | { 53 | Node temp = new Node(data); 54 | temp.link = last.link; 55 | last.link = temp; 56 | last = temp; 57 | } 58 | 59 | public void CreateList() 60 | { 61 | int i,n,data; 62 | 63 | Console.Write("Enter the number of nodes : "); 64 | n = Convert.ToInt32(Console.ReadLine()); 65 | 66 | if ( n == 0 ) 67 | return; 68 | Console.Write("Enter the element to be inserted : "); 69 | data = Convert.ToInt32(Console.ReadLine()); 70 | InsertInEmptyList(data); 71 | 72 | for(i = 2; i <= n; i++) 73 | { 74 | Console.Write("Enter the element to be inserted : "); 75 | data = Convert.ToInt32(Console.ReadLine()); 76 | InsertAtEnd(data); 77 | } 78 | } 79 | 80 | public void InsertAfter(int data, int x) 81 | { 82 | Node p = last.link; 83 | do 84 | { 85 | if(p.info == x) 86 | break; 87 | p = p.link; 88 | }while( p != last.link ); 89 | 90 | if( p == last.link && p.info != x ) 91 | Console.WriteLine(x + " not present in the list"); 92 | else 93 | { 94 | Node temp = new Node(data); 95 | temp.link = p.link; 96 | p.link = temp; 97 | if ( p == last ) 98 | last = temp; 99 | } 100 | } 101 | 102 | public void DeleteFirstNode() 103 | { 104 | if (last == null) /*List is empty*/ 105 | return; 106 | if (last.link == last) /*List has only one node*/ 107 | { 108 | last = null; 109 | return; 110 | } 111 | last.link = last.link.link; 112 | } 113 | 114 | public void DeleteLastNode() 115 | { 116 | if (last == null) /*List is empty*/ 117 | return; 118 | if (last.link == last) /*List has only one node*/ 119 | { 120 | last = null; 121 | return; 122 | } 123 | Node p = last.link; 124 | while (p.link != last) 125 | p = p.link; 126 | p.link = last.link; 127 | last = p; 128 | } 129 | 130 | public void DeleteNode(int x) 131 | { 132 | if ( last == null )/*List is empty*/ 133 | return; 134 | 135 | if ( last.link == last && last.info == x ) /*Deletion of only node*/ 136 | { 137 | last = null; 138 | return; 139 | } 140 | 141 | if ( last.link.info == x ) /*Deletion of first node*/ 142 | { 143 | last.link = last.link.link; 144 | return; 145 | } 146 | 147 | Node p = last.link; 148 | while ( p.link != last.link ) 149 | { 150 | if ( p.link.info == x ) 151 | break; 152 | p = p.link; 153 | } 154 | 155 | if( p.link == last.link ) 156 | Console.WriteLine(x + " not found in the list"); 157 | else 158 | { 159 | p.link = p.link.link; 160 | if( last.info == x ) 161 | last = p; 162 | } 163 | } 164 | 165 | public void Concatenate(CircularLinkedList list) 166 | { 167 | if (last == null) 168 | { 169 | last = list.last; 170 | return; 171 | } 172 | 173 | if (list.last == null) 174 | return; 175 | 176 | Node p = last.link; 177 | last.link = list.last.link; 178 | list.last.link = p; 179 | last = list.last; 180 | } 181 | } 182 | } 183 | -------------------------------------------------------------------------------- /linked-list/CircularLinkedList/Demo.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace CircularLinkedListProject 9 | { 10 | class Demo 11 | { 12 | static void Main(string[] args) 13 | { 14 | int choice,data,x; 15 | 16 | CircularLinkedList List = new CircularLinkedList(); 17 | 18 | List.CreateList(); 19 | 20 | while(true) 21 | { 22 | Console.WriteLine("1.Display List"); 23 | Console.WriteLine("2.Insert in empty list"); 24 | Console.WriteLine("3.Insert in the beginning"); 25 | Console.WriteLine("4.Insert at the end"); 26 | Console.WriteLine("5.Insert after a node"); 27 | Console.WriteLine("6.Delete first node"); 28 | Console.WriteLine("7.Delete last node"); 29 | Console.WriteLine("8.Delete any node"); 30 | Console.WriteLine("9.Quit"); 31 | 32 | Console.Write("Enter your choice : "); 33 | choice = Convert.ToInt32(Console.ReadLine()); 34 | 35 | if(choice==9) 36 | break; 37 | 38 | switch(choice) 39 | { 40 | case 1: 41 | List.DisplayList(); 42 | break; 43 | case 2: 44 | Console.Write("Enter the element to be inserted : "); 45 | data = Convert.ToInt32(Console.ReadLine()); 46 | List.InsertInEmptyList(data); 47 | break; 48 | case 3: 49 | Console.Write("Enter the element to be inserted : "); 50 | data = Convert.ToInt32(Console.ReadLine()); 51 | List.InsertInBeginning(data); 52 | break; 53 | case 4: 54 | Console.Write("Enter the element to be inserted : "); 55 | data = Convert.ToInt32(Console.ReadLine()); 56 | List.InsertAtEnd(data); 57 | break; 58 | case 5: 59 | Console.Write("Enter the element to be inserted : "); 60 | data = Convert.ToInt32(Console.ReadLine()); 61 | Console.Write("Enter the element after which to insert : "); 62 | x = Convert.ToInt32(Console.ReadLine()); 63 | List.InsertAfter(data,x); 64 | break; 65 | case 6: 66 | List.DeleteFirstNode(); 67 | break; 68 | case 7: 69 | List.DeleteLastNode(); 70 | break; 71 | case 8: 72 | Console.Write("Enter the element to be deleted : "); 73 | data = Convert.ToInt32(Console.ReadLine()); 74 | List.DeleteNode(data); 75 | break; 76 | default: 77 | Console.WriteLine("Wrong choice"); 78 | break; 79 | } 80 | Console.WriteLine(); 81 | } 82 | Console.WriteLine("Exiting"); 83 | } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /linked-list/CircularLinkedList/Node.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace CircularLinkedListProject 9 | { 10 | class Node 11 | { 12 | public int info; 13 | public Node link; 14 | 15 | public Node(int i) 16 | { 17 | info = i; 18 | link = null; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /linked-list/CircularListConcatenateProject/CircularLinkedList.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace CircularListConcatenateProject 9 | { 10 | class CircularLinkedList 11 | { 12 | private Node last; 13 | 14 | public CircularLinkedList() 15 | { 16 | last = null; 17 | } 18 | 19 | public void DisplayList() 20 | { 21 | Node p; 22 | if(last==null) 23 | { 24 | Console.WriteLine("List is empty\n"); 25 | return; 26 | } 27 | 28 | p=last.link; 29 | do 30 | { 31 | Console.Write(p.info + " "); 32 | p=p.link; 33 | }while(p!=last.link); 34 | Console.WriteLine(); 35 | } 36 | 37 | public void InsertInBeginning(int data) 38 | { 39 | Node temp = new Node(data); 40 | temp.link = last.link; 41 | last.link = temp; 42 | } 43 | 44 | public void InsertInEmptyList(int data) 45 | { 46 | Node temp = new Node(data); 47 | last = temp; 48 | last.link = last; 49 | }/*End of insertInEmptyList()*/ 50 | 51 | public void InsertAtEnd(int data) 52 | { 53 | Node temp = new Node(data); 54 | temp.link = last.link; 55 | last.link = temp; 56 | last = temp; 57 | }/*End of insertAtEnd( )*/ 58 | 59 | public void CreateList() 60 | { 61 | int i,n,data; 62 | 63 | Console.Write("Enter the number of nodes : "); 64 | n = Convert.ToInt32(Console.ReadLine()); 65 | 66 | if(n==0) 67 | return; 68 | Console.Write("Enter the element to be inserted : "); 69 | data = Convert.ToInt32(Console.ReadLine()); 70 | InsertInEmptyList(data); 71 | 72 | for(i=2; i<=n; i++) 73 | { 74 | Console.Write("Enter the element to be inserted : "); 75 | data = Convert.ToInt32(Console.ReadLine()); 76 | InsertAtEnd(data); 77 | } 78 | } 79 | 80 | public void InsertAfter(int data, int x) 81 | { 82 | Node p=last.link; 83 | do 84 | { 85 | if(p.info==x) 86 | break; 87 | p=p.link; 88 | }while(p!=last.link); 89 | 90 | if(p==last.link && p.info!=x) 91 | Console.WriteLine(x + " not present in the list"); 92 | else 93 | { 94 | Node temp=new Node(data); 95 | temp.link=p.link; 96 | p.link=temp; 97 | if(p==last) 98 | last=temp; 99 | } 100 | } 101 | 102 | public void DeleteFirstNode() 103 | { 104 | if (last == null) /*List is empty*/ 105 | return; 106 | if (last.link == last) /*List has only one node*/ 107 | { 108 | last = null; 109 | return; 110 | } 111 | last.link = last.link.link; 112 | } 113 | 114 | public void DeleteLastNode() 115 | { 116 | if (last == null) /*List is empty*/ 117 | return; 118 | if (last.link == last) /*List has only one node*/ 119 | { 120 | last = null; 121 | return; 122 | } 123 | Node p = last.link; 124 | while (p.link != last) 125 | p = p.link; 126 | p.link = last.link; 127 | last = p; 128 | } 129 | 130 | public void DeleteNode(int x) 131 | { 132 | if(last==null)/*List is empty*/ 133 | return; 134 | 135 | if(last.link==last && last.info==x) /*Deletion of only node*/ 136 | { 137 | last=null; 138 | return; 139 | } 140 | 141 | if(last.link.info==x) /*Deletion of first node*/ 142 | { 143 | last.link=last.link.link; 144 | return; 145 | } 146 | 147 | Node p=last.link; 148 | while(p.link!=last.link) 149 | { 150 | if(p.link.info==x) 151 | break; 152 | p=p.link; 153 | } 154 | 155 | if(p.link==last.link) 156 | Console.WriteLine(x + " not found in the list"); 157 | else 158 | { 159 | p.link=p.link.link; 160 | if(last.info==x) 161 | last=p; 162 | } 163 | }/*End of deleteNode()*/ 164 | 165 | public void Concatenate(CircularLinkedList list) 166 | { 167 | if (last == null) 168 | { 169 | last = list.last; 170 | return; 171 | } 172 | 173 | if (list.last == null) 174 | return; 175 | 176 | Node p = last.link; 177 | last.link = list.last.link; 178 | list.last.link = p; 179 | last = list.last; 180 | } 181 | } 182 | } 183 | -------------------------------------------------------------------------------- /linked-list/CircularListConcatenateProject/Demo.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace CircularListConcatenateProject 9 | { 10 | class Demo 11 | { 12 | static void Main(string[] args) 13 | { 14 | CircularLinkedList List1 = new CircularLinkedList(); 15 | CircularLinkedList List2 = new CircularLinkedList(); 16 | 17 | Console.WriteLine("Enter first list :- "); 18 | List1.CreateList(); 19 | Console.WriteLine("Enter second list :- "); 20 | List2.CreateList(); 21 | 22 | Console.Write("First "); 23 | List1.DisplayList(); 24 | Console.Write("Second "); 25 | List2.DisplayList(); 26 | 27 | List1.Concatenate(List2); 28 | Console.Write("First "); 29 | List1.DisplayList(); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /linked-list/CircularListConcatenateProject/Node.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace CircularListConcatenateProject 9 | { 10 | class Node 11 | { 12 | public int info; 13 | public Node link; 14 | 15 | public Node(int i) 16 | { 17 | info = i; 18 | link = null; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /linked-list/DoubleLinkedListProject/Demo.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace DoubleLinkedListProject 9 | { 10 | class Demo 11 | { 12 | static void Main(string[] args) 13 | { 14 | int choice, data, x; 15 | 16 | DoubleLinkedList list = new DoubleLinkedList(); 17 | list.CreateList(); 18 | 19 | while ( true ) 20 | { 21 | Console.WriteLine("1.Display List"); 22 | Console.WriteLine("2.Insert in empty list"); 23 | Console.WriteLine("3.Insert a node in beginning of the list"); 24 | Console.WriteLine("4.Insert a node at the end of the list"); 25 | Console.WriteLine("5.Insert a node after a specified node"); 26 | Console.WriteLine("6.Insert a node before a specified node"); 27 | Console.WriteLine("7.Delete first node"); 28 | Console.WriteLine("8.Delete last node"); 29 | Console.WriteLine("9.Delete any node"); 30 | Console.WriteLine("10.Reverse the list"); 31 | Console.WriteLine("11.Quit"); 32 | Console.WriteLine("Enter your choice : "); 33 | choice = Convert.ToInt32(Console.ReadLine()); 34 | 35 | if ( choice == 11 ) 36 | break; 37 | 38 | switch ( choice ) 39 | { 40 | case 1: 41 | list.DisplayList(); 42 | break; 43 | case 2: 44 | Console.Write("Enter the element to be inserted : "); 45 | data = Convert.ToInt32(Console.ReadLine()); 46 | list.InsertInEmptyList(data); 47 | break; 48 | case 3: 49 | Console.Write("Enter the element to be inserted : "); 50 | data = Convert.ToInt32(Console.ReadLine()); 51 | list.InsertInBeginning(data); 52 | break; 53 | case 4: 54 | Console.Write("Enter the element to be inserted : "); 55 | data = Convert.ToInt32(Console.ReadLine()); 56 | list.InsertAtEnd(data); 57 | break; 58 | case 5: 59 | Console.Write("Enter the element to be inserted : "); 60 | data = Convert.ToInt32(Console.ReadLine()); 61 | Console.Write("Enter the element after which to insert : "); 62 | x = Convert.ToInt32(Console.ReadLine()); 63 | list.InsertAfter(data,x); 64 | break; 65 | case 6: 66 | Console.Write("Enter the element to be inserted : "); 67 | data = Convert.ToInt32(Console.ReadLine()); 68 | Console.Write("Enter the element before which to insert : "); 69 | x = Convert.ToInt32(Console.ReadLine()); 70 | list.InsertBefore(data,x); 71 | break; 72 | case 7: 73 | list.DeleteFirstNode(); 74 | break; 75 | case 8: 76 | list.DeleteLastNode(); 77 | break; 78 | case 9: 79 | Console.Write("Enter the element to be deleted : "); 80 | data = Convert.ToInt32(Console.ReadLine()); 81 | list.DeleteNode(data); 82 | break; 83 | case 10: 84 | list.ReverseList(); 85 | break; 86 | default: 87 | Console.WriteLine("Wrong choice"); 88 | break; 89 | } 90 | Console.WriteLine(); 91 | } 92 | Console.WriteLine("Exiting"); 93 | } 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /linked-list/DoubleLinkedListProject/DoubleLinkedList.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace DoubleLinkedListProject 9 | { 10 | class DoubleLinkedList 11 | { 12 | private Node start; 13 | 14 | public DoubleLinkedList() 15 | { 16 | start = null; 17 | } 18 | 19 | public void DisplayList() 20 | { 21 | Node p; 22 | if ( start == null ) 23 | { 24 | Console.WriteLine("List is empty"); 25 | return; 26 | } 27 | 28 | p = start; 29 | Console.Write("List is : "); 30 | while ( p != null ) 31 | { 32 | Console.Write(p.info + " "); 33 | p = p.next; 34 | } 35 | Console.WriteLine(); 36 | } 37 | 38 | public void InsertInBeginning(int data) 39 | { 40 | Node temp = new Node(data); 41 | temp.next = start; 42 | start.prev = temp; 43 | start = temp; 44 | } 45 | 46 | public void InsertInEmptyList(int data) 47 | { 48 | Node temp = new Node(data); 49 | start = temp; 50 | } 51 | 52 | public void InsertAtEnd(int data) 53 | { 54 | Node temp = new Node(data); 55 | 56 | Node p = start; 57 | while (p.next != null ) 58 | p = p.next; 59 | 60 | p.next = temp; 61 | temp.prev = p; 62 | } 63 | 64 | public void CreateList() 65 | { 66 | int i,n,data; 67 | Console.Write("Enter the number of nodes : "); 68 | n = Convert.ToInt32(Console.ReadLine()); 69 | 70 | if(n == 0) 71 | return; 72 | 73 | Console.Write("Enter the first element to be inserted : "); 74 | data = Convert.ToInt32(Console.ReadLine()); 75 | InsertInEmptyList(data); 76 | 77 | for ( i = 2; i <= n; i++ ) 78 | { 79 | Console.Write("Enter the next element to be inserted : "); 80 | data = Convert.ToInt32(Console.ReadLine()); 81 | InsertAtEnd(data); 82 | } 83 | } 84 | 85 | public void InsertAfter(int data, int x) 86 | { 87 | Node temp = new Node(data); 88 | 89 | Node p = start; 90 | while ( p != null ) 91 | { 92 | if ( p.info == x ) 93 | break; 94 | p = p.next; 95 | } 96 | 97 | if(p == null) 98 | Console.WriteLine(x + " not present in the list"); 99 | else 100 | { 101 | temp.prev = p; 102 | temp.next = p.next; 103 | if ( p.next != null ) 104 | p.next.prev = temp; /*should not be done when p refers to last node*/ 105 | p.next = temp; 106 | } 107 | } 108 | 109 | public void InsertBefore(int data, int x) 110 | { 111 | if ( start == null ) 112 | { 113 | Console.WriteLine("List is empty"); 114 | return; 115 | } 116 | if ( start.info == x ) 117 | { 118 | Node temp = new Node(data); 119 | temp.next = start; 120 | start.prev = temp; 121 | start = temp; 122 | return; 123 | } 124 | 125 | Node p = start; 126 | while ( p != null ) 127 | { 128 | if( p.info == x ) 129 | break; 130 | p = p.next; 131 | } 132 | 133 | if ( p == null ) 134 | Console.WriteLine(x + " not present in the list"); 135 | else 136 | { 137 | Node temp = new Node(data); 138 | temp.prev = p.prev; 139 | temp.next = p; 140 | p.prev.next = temp; 141 | p.prev = temp; 142 | } 143 | } 144 | 145 | public void DeleteFirstNode() 146 | { 147 | if ( start == null ) /*list is empty*/ 148 | return; 149 | if ( start.next == null ) /*list has only one node*/ 150 | { 151 | start = null; 152 | return; 153 | } 154 | start = start.next; 155 | start.prev = null; 156 | } 157 | 158 | public void DeleteLastNode() 159 | { 160 | if (start == null) /*list is empty*/ 161 | return; 162 | if (start.next == null) /*list has only one node*/ 163 | { 164 | start = null; 165 | return; 166 | } 167 | Node p = start; 168 | while (p.next != null) 169 | p = p.next; 170 | p.prev.next = null; 171 | } 172 | 173 | public void DeleteNode(int x) 174 | { 175 | if( start == null ) /*list is empty*/ 176 | return; 177 | 178 | if ( start.next == null ) /*list has only one node*/ 179 | { 180 | if ( start.info == x ) 181 | start = null; 182 | else 183 | Console.WriteLine(x + " not found"); 184 | return; 185 | } 186 | 187 | /*Deletion of first node*/ 188 | if ( start.info == x ) 189 | { 190 | start = start.next; 191 | start.prev = null; 192 | return; 193 | } 194 | 195 | Node p = start.next; 196 | while ( p.next != null ) 197 | { 198 | if ( p.info == x ) 199 | break; 200 | p = p.next; 201 | } 202 | 203 | if ( p.next != null ) /*node to be deleted is in between*/ 204 | { 205 | p.prev.next = p.next; 206 | p.next.prev = p.prev; 207 | } 208 | else /*p refers to last node*/ 209 | { 210 | if ( p.info == x )/*node to be deleted is last node*/ 211 | p.prev.next = null; 212 | else 213 | Console.WriteLine(x + " not found"); 214 | } 215 | } 216 | 217 | public void ReverseList() 218 | { 219 | if (start == null) 220 | return; 221 | 222 | Node p1 = start; 223 | Node p2 = p1.next; 224 | p1.next = null; 225 | p1.prev = p2; 226 | while (p2 != null ) 227 | { 228 | p2.prev = p2.next; 229 | p2.next = p1; 230 | p1 = p2; 231 | p2 = p2.prev; 232 | } 233 | start = p1; 234 | } 235 | } 236 | } 237 | -------------------------------------------------------------------------------- /linked-list/DoubleLinkedListProject/Node.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace DoubleLinkedListProject 9 | { 10 | class Node 11 | { 12 | public Node prev; 13 | public int info; 14 | public Node next; 15 | 16 | public Node(int i) 17 | { 18 | info = i; 19 | prev = null; 20 | next = null; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /linked-list/HeaderLinkedListProject/Demo.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace HeaderLinkedListProject 9 | { 10 | class Demo 11 | { 12 | static void Main(string[] args) 13 | { 14 | int choice, data, x, k; 15 | 16 | HeaderLinkedList List = new HeaderLinkedList(); 17 | List.CreateList(); 18 | 19 | while(true) 20 | { 21 | Console.WriteLine("1.Display list"); 22 | Console.WriteLine("2.Insert a node at the end of the list"); 23 | Console.WriteLine("3.Insert a new node before a node"); 24 | Console.WriteLine("4.Insert at a given position"); 25 | Console.WriteLine("5.Delete a node"); 26 | Console.WriteLine("6.Reverse the list"); 27 | Console.WriteLine("7.Quit"); 28 | Console.Write("Enter your choice : "); 29 | choice=Convert.ToInt32(Console.ReadLine()); 30 | 31 | if(choice==7) 32 | break; 33 | 34 | switch(choice) 35 | { 36 | case 1: 37 | List.DisplayList(); 38 | break; 39 | case 2: 40 | Console.Write("Enter the element to be inserted : "); 41 | data = Convert.ToInt32(Console.ReadLine()); 42 | List.InsertAtEnd(data); 43 | break; 44 | case 3: 45 | Console.Write("Enter the element to be inserted : "); 46 | data = Convert.ToInt32(Console.ReadLine()); 47 | Console.Write("Enter the element before which to insert : "); 48 | x = Convert.ToInt32(Console.ReadLine()); 49 | List.InsertBefore(data,x); 50 | break; 51 | case 4: 52 | Console.Write("Enter the element to be inserted : "); 53 | data = Convert.ToInt32(Console.ReadLine()); 54 | Console.Write("Enter the position at which to insert : "); 55 | k = Convert.ToInt32(Console.ReadLine()); 56 | List.InsertAtPosition(data,k); 57 | break; 58 | case 5: 59 | Console.Write("Enter the element to be deleted : "); 60 | data = Convert.ToInt32(Console.ReadLine()); 61 | List.DeleteNode(data); 62 | break; 63 | case 6: 64 | List.Reverse(); 65 | break; 66 | default: 67 | Console.WriteLine("Wrong choice"); 68 | break; 69 | } 70 | } 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /linked-list/HeaderLinkedListProject/HeaderLinkedList.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace HeaderLinkedListProject 9 | { 10 | class HeaderLinkedList 11 | { 12 | private Node head; 13 | 14 | public HeaderLinkedList() 15 | { 16 | head = new Node(0); 17 | } 18 | 19 | public void DisplayList() 20 | { 21 | if ( head.link == null ) 22 | { 23 | Console.WriteLine("List is empty\n"); 24 | return; 25 | } 26 | Node p = head.link; 27 | Console.Write("List is :\n"); 28 | while( p != null ) 29 | { 30 | Console.Write(p.info + " "); 31 | p = p.link; 32 | } 33 | Console.WriteLine(); 34 | } 35 | 36 | public void InsertAtEnd(int data) 37 | { 38 | Node temp = new Node(data); 39 | 40 | Node p = head; 41 | while (p.link != null) 42 | p = p.link; 43 | 44 | p.link = temp; 45 | temp.link = null; 46 | } 47 | 48 | public void CreateList() 49 | { 50 | int i, n, data; 51 | 52 | Console.Write("Enter the number of nodes : "); 53 | n = Convert.ToInt32(Console.ReadLine()); 54 | 55 | for( i = 1; i <= n; i++ ) 56 | { 57 | Console.Write("Enter the element to be inserted : "); 58 | data = Convert.ToInt32( Console.ReadLine() ); 59 | InsertAtEnd( data ); 60 | } 61 | } 62 | 63 | public void InsertBefore(int data, int x) 64 | { 65 | Node temp; 66 | 67 | /*Find pointer to predecessor of node containing x*/ 68 | Node p = head; 69 | while( p.link != null ) 70 | { 71 | if ( p.link.info == x ) 72 | break; 73 | p = p.link; 74 | } 75 | 76 | if ( p.link == null ) 77 | Console.WriteLine(x + " not present in the list"); 78 | else 79 | { 80 | temp = new Node(data); 81 | temp.link = p.link; 82 | p.link = temp; 83 | } 84 | } 85 | 86 | public void InsertAtPosition(int data, int k) 87 | { 88 | Node temp; 89 | int i; 90 | 91 | Node p = head; 92 | for( i = 1; i <= k-1 && p != null; i++ ) 93 | p = p.link; 94 | 95 | if ( p == null ) 96 | Console.WriteLine("You can insert only upto " + (i-1) + "th position\n\n"); 97 | else 98 | { 99 | temp = new Node(data); 100 | temp.link = p.link; 101 | p.link = temp; 102 | } 103 | } 104 | 105 | public void DeleteNode(int data) 106 | { 107 | Node p = head; 108 | while(p.link != null) 109 | { 110 | if(p.link.info == data) 111 | break; 112 | p = p.link; 113 | } 114 | 115 | if(p.link == null) 116 | Console.WriteLine(data + "Element %d not found"); 117 | else 118 | p.link = p.link.link; 119 | } 120 | 121 | public void Reverse() 122 | { 123 | Node prev, p, next; 124 | prev = null; 125 | p = head.link; 126 | while (p != null) 127 | { 128 | next = p.link; 129 | p.link = prev; 130 | prev = p; 131 | p = next; 132 | } 133 | head.link = prev; 134 | } 135 | } 136 | } 137 | -------------------------------------------------------------------------------- /linked-list/HeaderLinkedListProject/Node.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace HeaderLinkedListProject 9 | { 10 | class Node 11 | { 12 | public int info; 13 | public Node link; 14 | 15 | public Node(int i) 16 | { 17 | info = i; 18 | link = null; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /linked-list/README.md: -------------------------------------------------------------------------------- 1 | # Data Structures and Algorithms in C# 2 | 3 | This [“Data Structures and Algorithms in C#”](https://www.udemy.com/course/data-structures-and-algorithms-in-csharp/?couponCode=GITHUBSTUDENT ) repository is for the students of my online course available on Udemy. It contains the source code of all the programs used in the course. 4 | 5 | ### About the Course 6 | * Thoroughly detailed course with complete working programs 7 | * Contains lots of animations to help you visualize the concepts 8 | * Includes over 100 Quiz questions 9 | * Builds a solid foundation in Data Structures and Algorithms 10 | * Prepares you for coding interviews 11 | * Lifetime Access 12 | 13 | ### Courses by [Deepali Srivastava](https://www.udemy.com/user/deepalisrivastava/) 14 | 15 | [![data-structures- and-algorithms-in-csharp](https://user-images.githubusercontent.com/98641125/153196407-99441e67-24a7-4fa0-aaea-78cb39743282.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-csharp/?couponCode=GITHUBSTUDENT) 16 | [![data-structures- and-algorithms-in-csharp-2](https://user-images.githubusercontent.com/98641125/153196486-9ce09c97-8724-4492-b5d6-4bb4aeb1d8bd.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-csharp-2/?couponCode=GITHUBSTUDENT) 17 | [![data-structures- and-algorithms-in-c-plus-plus](https://user-images.githubusercontent.com/98641125/153196522-2412c993-1055-4322-8487-4133537566c9.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-c-plus-plus/?couponCode=GITHUBSTUDENT) 18 | [![python-programming-in-depth](https://user-images.githubusercontent.com/98641125/153196166-45ef8461-adb1-4f9f-b9ee-e482a5ad54a7.png)]( https://www.udemy.com/course/python-programming-in-depth/?couponCode=GITHUBSTUDENT) 19 | [![linux-commands](https://user-images.githubusercontent.com/98641125/153196567-96b3396c-8ee3-4233-b8fc-66c6b3bd830c.png)]( https://www.udemy.com/course/linux-commands/?couponCode=GITHUBSTUDENT) 20 | [![data-structures-and-algorithms-in-c](https://user-images.githubusercontent.com/98641125/153195841-209d2615-ed5f-4007-ae54-539ac3c1538a.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-c/?couponCode=GITHUBSTUDENT) 21 | [![data-structures- and-algorithms-in-java](https://user-images.githubusercontent.com/98641125/153196280-c2028f4b-d27b-432d-ad5a-9b04be2a3717.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-java/?couponCode=GITHUBSTUDENT) 22 | [![data-structures- and-algorithms-in-java-2](https://user-images.githubusercontent.com/98641125/153196347-23003dc0-55b0-4315-8e52-425c51b2b5c4.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-java-2/?couponCode=GITHUBSTUDENT) 23 | 24 | ## Copyright 25 | © Copyright Deepali Srivastava : All rights reserved. 26 | Not to be used for commercial purposes. 27 | -------------------------------------------------------------------------------- /linked-list/SingleLinkedListProject/Demo.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace SingleLinkedListProject 9 | { 10 | class Demo 11 | { 12 | static void Main(string[] args) 13 | { 14 | int choice,data,k,x; 15 | 16 | SingleLinkedList list = new SingleLinkedList(); 17 | 18 | list.CreateList(); 19 | 20 | while (true) 21 | { 22 | Console.WriteLine("1.Display list"); 23 | Console.WriteLine("2.Count the number of nodes"); 24 | Console.WriteLine("3.Search for an element"); 25 | Console.WriteLine("4.Insert in empty list/Insert in beginning of the list"); 26 | Console.WriteLine("5.Insert a node at the end of the list"); 27 | Console.WriteLine("6.Insert a node after a specified node"); 28 | Console.WriteLine("7.Insert a node before a specified node"); 29 | Console.WriteLine("8.Insert a node at a given position"); 30 | Console.WriteLine("9.Delete first node"); 31 | Console.WriteLine("10.Delete last node"); 32 | Console.WriteLine("11.Delete any node"); 33 | Console.WriteLine("12.Reverse the list"); 34 | Console.WriteLine("13.Bubble sort by exchanging data"); 35 | Console.WriteLine("14.Bubble sort by exchanging links"); 36 | Console.WriteLine("15.MergeSort"); 37 | Console.WriteLine("16.Insert Cycle"); 38 | Console.WriteLine("17.Detect Cycle"); 39 | Console.WriteLine("18.Remove cycle"); 40 | Console.WriteLine("19.Quit"); 41 | 42 | Console.Write("Enter your choice : "); 43 | choice = Convert.ToInt32(Console.ReadLine()); 44 | 45 | if (choice == 19) 46 | break; 47 | 48 | switch(choice) 49 | { 50 | case 1: 51 | list.DisplayList(); 52 | break; 53 | case 2: 54 | list.CountNodes(); 55 | break; 56 | case 3: 57 | Console.Write("Enter the element to be searched : "); 58 | data = Convert.ToInt32(Console.ReadLine()); 59 | list.Search(data); 60 | break; 61 | case 4: 62 | Console.Write("Enter the element to be inserted : "); 63 | data = Convert.ToInt32(Console.ReadLine()); 64 | list.InsertInBeginning(data); 65 | break; 66 | case 5: 67 | Console.Write("Enter the element to be inserted : "); 68 | data = Convert.ToInt32(Console.ReadLine()); 69 | list.InsertAtEnd(data); 70 | break; 71 | case 6: 72 | Console.Write("Enter the element to be inserted : "); 73 | data = Convert.ToInt32(Console.ReadLine()); 74 | Console.Write("Enter the element after which to insert : "); 75 | x = Convert.ToInt32(Console.ReadLine()); 76 | list.InsertAfter(data,x); 77 | break; 78 | case 7: 79 | Console.Write("Enter the element to be inserted : "); 80 | data = Convert.ToInt32(Console.ReadLine()); 81 | Console.Write("Enter the element before which to insert : "); 82 | x = Convert.ToInt32(Console.ReadLine()); 83 | list.InsertBefore(data,x); 84 | break; 85 | case 8: 86 | Console.Write("Enter the element to be inserted : "); 87 | data = Convert.ToInt32(Console.ReadLine()); 88 | Console.Write("Enter the position at which to insert : "); 89 | k = Convert.ToInt32(Console.ReadLine()); 90 | list.InsertAtPosition(data,k); 91 | break; 92 | case 9: 93 | list.DeleteFirstNode(); 94 | break; 95 | case 10: 96 | list.DeleteLastNode(); 97 | break; 98 | case 11: 99 | Console.Write("Enter the element to be deleted : "); 100 | data = Convert.ToInt32(Console.ReadLine()); 101 | list.DeleteNode(data); 102 | break; 103 | case 12: 104 | list.ReverseList(); 105 | break; 106 | case 13: 107 | list.BubbleSortExData(); 108 | break; 109 | case 14: 110 | list.BubbleSortExLinks(); 111 | break; 112 | case 15: 113 | list.MergeSort(); 114 | break; 115 | case 16: 116 | Console.Write("Enter the element at which the cycle has to be inserted : "); 117 | data = Convert.ToInt32(Console.ReadLine()); 118 | list.InsertCycle(data); 119 | break; 120 | case 17: 121 | if (list.HasCycle()) 122 | Console.WriteLine("List has a cycle"); 123 | else 124 | Console.WriteLine("List does not have a cycle"); 125 | break; 126 | case 18: 127 | list.RemoveCycle(); 128 | break; 129 | default: 130 | Console.WriteLine("Wrong choice"); 131 | break; 132 | } 133 | Console.WriteLine(); 134 | } 135 | Console.WriteLine("Exiting"); 136 | } 137 | } 138 | } 139 | 140 | 141 | 142 | -------------------------------------------------------------------------------- /linked-list/SingleLinkedListProject/Node.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace SingleLinkedListProject 9 | { 10 | class Node 11 | { 12 | public int info; 13 | public Node link; 14 | 15 | public Node(int i) 16 | { 17 | info = i; 18 | link = null; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /linked-list/SingleListConcatenateProject/Demo.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace SingleListConcatenateProject 9 | { 10 | class Demo 11 | { 12 | static void Main(string[] args) 13 | { 14 | SingleLinkedList List1 = new SingleLinkedList(); 15 | SingleLinkedList List2 = new SingleLinkedList(); 16 | 17 | Console.WriteLine("Enter first list :- "); 18 | List1.CreateList(); 19 | Console.WriteLine("Enter second list :- "); 20 | List2.CreateList(); 21 | 22 | Console.Write("First "); 23 | List1.DisplayList(); 24 | Console.Write("Second "); 25 | List2.DisplayList(); 26 | 27 | List1.Concatenate(List2); 28 | Console.Write("First "); 29 | List1.DisplayList(); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /linked-list/SingleListConcatenateProject/Node.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace SingleListConcatenateProject 9 | { 10 | class Node 11 | { 12 | public int info; 13 | public Node link; 14 | 15 | public Node(int i) 16 | { 17 | info = i; 18 | link = null; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /linked-list/SingleListMergingProject/Demo.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace SingleListMergingProject 9 | { 10 | class Demo 11 | { 12 | static void Main(string[] args) 13 | { 14 | SingleLinkedList list1 = new SingleLinkedList(); 15 | SingleLinkedList list2 = new SingleLinkedList(); 16 | 17 | list1.CreateList(); 18 | list2.CreateList(); 19 | 20 | list1.BubbleSortExData(); 21 | list2.BubbleSortExData(); 22 | 23 | Console.WriteLine("First List - "); list1.DisplayList(); 24 | Console.WriteLine("Second List - "); list2.DisplayList(); 25 | 26 | SingleLinkedList list3; 27 | 28 | list3 = list1.Merge1(list2); 29 | Console.WriteLine("Merged List - "); list3.DisplayList(); 30 | 31 | Console.WriteLine("First List - "); list1.DisplayList(); 32 | Console.WriteLine("Second List - "); list2.DisplayList(); 33 | 34 | list3 = list1.Merge2(list2); 35 | Console.WriteLine("Merged List - "); list3.DisplayList(); 36 | 37 | Console.WriteLine("First List - "); list1.DisplayList(); 38 | Console.WriteLine("Second List - "); list2.DisplayList(); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /linked-list/SingleListMergingProject/Node.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | 9 | namespace SingleListMergingProject 10 | { 11 | class Node 12 | { 13 | public int info; 14 | public Node link; 15 | 16 | public Node(int i) 17 | { 18 | info = i; 19 | link = null; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /linked-list/SortedLinkedListProject/Demo.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace SortedLinkedListProject 9 | { 10 | class Demo 11 | { 12 | static void Main(string[] args) 13 | { 14 | int choice,data; 15 | 16 | SortedLinkedList List = new SortedLinkedList(); 17 | List.CreateList(); 18 | 19 | while(true) 20 | { 21 | Console.WriteLine("1.Display List"); 22 | Console.WriteLine("2.Insert a new node"); 23 | Console.WriteLine("3.Search"); 24 | Console.WriteLine("4.Exit"); 25 | Console.Write("Enter your choice : "); 26 | choice = Convert.ToInt32(Console.ReadLine()); 27 | if(choice==4) 28 | break; 29 | 30 | switch(choice) 31 | { 32 | case 1: 33 | List.DisplayList(); 34 | break; 35 | case 2: 36 | Console.WriteLine("Enter the element to be inserted : "); 37 | data = Convert.ToInt32(Console.ReadLine()); 38 | List.InsertInOrder(data); 39 | break; 40 | case 3: 41 | Console.WriteLine("Enter the element to be searched : "); 42 | data=Convert.ToInt32(Console.ReadLine()); 43 | List.Search(data); 44 | break; 45 | default: 46 | Console.WriteLine("Wrong choice\n"); 47 | break; 48 | } 49 | }/*End of while*/ 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /linked-list/SortedLinkedListProject/Node.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace SortedLinkedListProject 9 | { 10 | class Node 11 | { 12 | public int info; 13 | public Node link; 14 | 15 | public Node(int i) 16 | { 17 | info = i; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /linked-list/SortedLinkedListProject/SortedLinkedList.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace SortedLinkedListProject 9 | { 10 | class SortedLinkedList 11 | { 12 | private Node start; 13 | 14 | public SortedLinkedList() 15 | { 16 | start = null; 17 | } 18 | 19 | public void InsertInOrder(int data) 20 | { 21 | Node temp = new Node(data); 22 | 23 | /*List empty or new node to be inserted before first node*/ 24 | if (start == null || data < start.info) 25 | { 26 | temp.link = start; 27 | start = temp; 28 | return; 29 | } 30 | 31 | Node p = start; 32 | while (p.link != null && p.link.info <= data) 33 | p = p.link; 34 | 35 | temp.link = p.link; 36 | p.link = temp; 37 | } 38 | 39 | public void CreateList() 40 | { 41 | int i,n,data; 42 | 43 | Console.Write("Enter the number of nodes : "); 44 | n = Convert.ToInt32(Console.ReadLine()); 45 | 46 | if(n == 0) 47 | return; 48 | 49 | for(i = 1; i <= n; i++) 50 | { 51 | Console.Write("Enter the element to be inserted : "); 52 | data = Convert.ToInt32(Console.ReadLine()); 53 | InsertInOrder(data); 54 | } 55 | } 56 | 57 | public void Search(int x) 58 | { 59 | if( start == null ) 60 | { 61 | Console.WriteLine("List is empty"); 62 | return; 63 | } 64 | 65 | Node p = start; 66 | int position = 1; 67 | while( p != null && p.info <= x) 68 | { 69 | if ( p.info == x ) 70 | break; 71 | position++; 72 | p = p.link; 73 | } 74 | if ( p == null || p.info != x ) 75 | Console.WriteLine(x + " not found in list"); 76 | else 77 | Console.WriteLine(x + " is at position " + position); 78 | } 79 | 80 | public void DisplayList() 81 | { 82 | Node p; 83 | if ( start == null ) 84 | { 85 | Console.WriteLine("List is empty"); 86 | return; 87 | } 88 | Console.Write("List is : "); 89 | p = start; 90 | while (p != null ) 91 | { 92 | Console.Write(p.info + " "); 93 | p = p.link; 94 | } 95 | Console.WriteLine(); 96 | } 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /recursion/BaseConversion/Program.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace BaseConversion 9 | { 10 | class Program 11 | { 12 | public static void ToBinary(int n) 13 | { 14 | if( n == 0 ) 15 | return; 16 | ToBinary( n/2 ); 17 | Console.Write( n%2 ); 18 | } 19 | 20 | public static void ConvertBase(int n, int b) 21 | { 22 | if( n == 0 ) 23 | return; 24 | ConvertBase(n/b, b); 25 | 26 | int remainder = n % b; 27 | if( remainder < 10 ) 28 | Console.Write(remainder); 29 | else 30 | Console.Write((char) ( remainder - 10 + 'A' )); 31 | } 32 | 33 | static void Main(string[] args) 34 | { 35 | int n; 36 | 37 | Console.Write("Enter a positive decimal number : "); 38 | n = Convert.ToInt32(Console.ReadLine()); 39 | 40 | Console.Write("Binary form : "); ToBinary(n); 41 | Console.WriteLine(); 42 | Console.Write("Binary form : "); ConvertBase(n, 2); 43 | Console.WriteLine(); 44 | Console.Write("Octal form : "); ConvertBase(n, 8); 45 | Console.WriteLine(); 46 | Console.Write("Hexadecimal form : "); ConvertBase(n, 16); 47 | Console.WriteLine(); 48 | } 49 | } 50 | } 51 | 52 | -------------------------------------------------------------------------------- /recursion/Display1toN/Program.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace Display1toN 9 | { 10 | class Program 11 | { 12 | public static void Print1(int n) 13 | { 14 | if(n==0) 15 | return; 16 | Console.Write(n + " "); 17 | Print1(n-1); 18 | } 19 | 20 | public static void Print2(int n) 21 | { 22 | if(n==0) 23 | return; 24 | Print2(n-1); 25 | Console.Write(n + " "); 26 | } 27 | 28 | static void Main(string[] args) 29 | { 30 | int n; 31 | Console.WriteLine("Enter value of n : "); 32 | n = Convert.ToInt32(Console.ReadLine()); 33 | 34 | Print1(n); 35 | Console.WriteLine(); 36 | 37 | Print2(n); 38 | Console.WriteLine(); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /recursion/Euclids/Program.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace Euclids 9 | { 10 | class Program 11 | { 12 | public static int GCD(int a, int b) 13 | { 14 | if (b == 0) 15 | return a; 16 | 17 | return GCD(b, a % b); 18 | } 19 | 20 | static void Main(string[] args) 21 | { 22 | int a,b; 23 | 24 | Console.Write("Enter values for a and b : "); 25 | a = Convert.ToInt32(Console.ReadLine()); 26 | b = Convert.ToInt32(Console.ReadLine()); 27 | 28 | Console.WriteLine("GCD of " + a + " and " + b + " is " + GCD(a,b)); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /recursion/Exponentiation/Program.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace Exponentiation 9 | { 10 | class Program 11 | { 12 | public static double Power(double x, int n) 13 | { 14 | if (n == 0) 15 | return 1; 16 | return x * Power(x, n - 1); 17 | } 18 | 19 | static void Main(string[] args) 20 | { 21 | double x; 22 | int n; 23 | 24 | Console.Write("Enter values for x and n : "); 25 | 26 | x = Convert.ToDouble(Console.ReadLine()); 27 | 28 | n = Convert.ToInt32(Console.ReadLine()); 29 | 30 | Console.WriteLine(x + "^" + n + "=" + Power(x,n)); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /recursion/Factorial/Program.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace Factorial 9 | { 10 | class Program 11 | { 12 | public static long Factorial(int n) 13 | { 14 | if (n == 0) 15 | return 1; 16 | return n * Factorial(n - 1); 17 | } 18 | 19 | static void Main(string[] args) 20 | { 21 | int n; 22 | Console.Write("Enter a number greater than or equal to zero : "); 23 | n = Convert.ToInt32(Console.ReadLine()); 24 | 25 | Console.WriteLine("Factorial of " + n + " is " + Factorial(n)); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /recursion/Fibonacci/Program.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace Fibonacci 9 | { 10 | class Program 11 | { 12 | public static int Fib(int n) 13 | { 14 | if (n == 0) 15 | return 0; 16 | if (n == 1) 17 | return 1; 18 | return Fib(n - 1) + Fib(n - 2); 19 | } 20 | 21 | static void Main(string[] args) 22 | { 23 | Console.WriteLine("Enter number of terms : "); 24 | int terms = Convert.ToInt32(Console.ReadLine()); 25 | 26 | for( int i = 0; i <= terms; i++ ) 27 | Console.Write(Fib(i)+" "); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /recursion/README.md: -------------------------------------------------------------------------------- 1 | # Data Structures and Algorithms in C# 2 | 3 | This [“Data Structures and Algorithms in C#”](https://www.udemy.com/course/data-structures-and-algorithms-in-csharp/?couponCode=GITHUBSTUDENT ) repository is for the students of my online course available on Udemy. It contains the source code of all the programs used in the course. 4 | 5 | ### About the Course 6 | * Thoroughly detailed course with complete working programs 7 | * Contains lots of animations to help you visualize the concepts 8 | * Includes over 100 Quiz questions 9 | * Builds a solid foundation in Data Structures and Algorithms 10 | * Prepares you for coding interviews 11 | * Lifetime Access 12 | 13 | ### Courses by [Deepali Srivastava](https://www.udemy.com/user/deepalisrivastava/) 14 | 15 | [![data-structures- and-algorithms-in-csharp](https://user-images.githubusercontent.com/98641125/153196407-99441e67-24a7-4fa0-aaea-78cb39743282.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-csharp/?couponCode=GITHUBSTUDENT) 16 | [![data-structures- and-algorithms-in-csharp-2](https://user-images.githubusercontent.com/98641125/153196486-9ce09c97-8724-4492-b5d6-4bb4aeb1d8bd.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-csharp-2/?couponCode=GITHUBSTUDENT) 17 | [![data-structures- and-algorithms-in-c-plus-plus](https://user-images.githubusercontent.com/98641125/153196522-2412c993-1055-4322-8487-4133537566c9.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-c-plus-plus/?couponCode=GITHUBSTUDENT) 18 | [![python-programming-in-depth](https://user-images.githubusercontent.com/98641125/153196166-45ef8461-adb1-4f9f-b9ee-e482a5ad54a7.png)]( https://www.udemy.com/course/python-programming-in-depth/?couponCode=GITHUBSTUDENT) 19 | [![linux-commands](https://user-images.githubusercontent.com/98641125/153196567-96b3396c-8ee3-4233-b8fc-66c6b3bd830c.png)]( https://www.udemy.com/course/linux-commands/?couponCode=GITHUBSTUDENT) 20 | [![data-structures-and-algorithms-in-c](https://user-images.githubusercontent.com/98641125/153195841-209d2615-ed5f-4007-ae54-539ac3c1538a.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-c/?couponCode=GITHUBSTUDENT) 21 | [![data-structures- and-algorithms-in-java](https://user-images.githubusercontent.com/98641125/153196280-c2028f4b-d27b-432d-ad5a-9b04be2a3717.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-java/?couponCode=GITHUBSTUDENT) 22 | [![data-structures- and-algorithms-in-java-2](https://user-images.githubusercontent.com/98641125/153196347-23003dc0-55b0-4315-8e52-425c51b2b5c4.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-java-2/?couponCode=GITHUBSTUDENT) 23 | 24 | ## Copyright 25 | © Copyright Deepali Srivastava : All rights reserved. 26 | Not to be used for commercial purposes. 27 | -------------------------------------------------------------------------------- /recursion/SumOfDigits/Program.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace SumOfDigits 9 | { 10 | class Program 11 | { 12 | public static int SumDigits(int n) 13 | { 14 | if (n / 10 == 0) 15 | return n; 16 | return SumDigits(n / 10) + n % 10; 17 | } 18 | 19 | static void Main(string[] args) 20 | { 21 | int n; 22 | Console.Write("Enter a number : "); 23 | n = Convert.ToInt32(Console.ReadLine()); 24 | Console.WriteLine(SumDigits(n)); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /recursion/TowerofHanoi/Program.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace TowerofHanoi 9 | { 10 | class Program 11 | { 12 | public static void Hanoi(int n, char source, char temp, char dest) 13 | { 14 | if(n==1) 15 | { 16 | Console.WriteLine("Move Disk " + n + " from " + source + "-->" + dest); 17 | return; 18 | } 19 | Hanoi(n-1, source, dest, temp); 20 | Console.WriteLine("Move Disk " + n + " from " + source + "-->" + dest); 21 | Hanoi(n-1, temp, source, dest); 22 | } 23 | 24 | static void Main(string[] args) 25 | { 26 | int n; 27 | Console.Write("Enter the number of disks : "); 28 | n = Convert.ToInt32(Console.ReadLine()); 29 | 30 | Hanoi(n, 'A', 'B', 'C'); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /searching/README.md: -------------------------------------------------------------------------------- 1 | # Data Structures and Algorithms in C# 2 | 3 | This [“Data Structures and Algorithms in C#”](https://www.udemy.com/course/data-structures-and-algorithms-in-csharp/?couponCode=GITHUBSTUDENT ) repository is for the students of my online course available on Udemy. It contains the source code of all the programs used in the course. 4 | 5 | ### About the Course 6 | * Thoroughly detailed course with complete working programs 7 | * Contains lots of animations to help you visualize the concepts 8 | * Includes over 100 Quiz questions 9 | * Builds a solid foundation in Data Structures and Algorithms 10 | * Prepares you for coding interviews 11 | * Lifetime Access 12 | 13 | ### Courses by [Deepali Srivastava](https://www.udemy.com/user/deepalisrivastava/) 14 | 15 | [![data-structures- and-algorithms-in-csharp](https://user-images.githubusercontent.com/98641125/153196407-99441e67-24a7-4fa0-aaea-78cb39743282.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-csharp/?couponCode=GITHUBSTUDENT) 16 | [![data-structures- and-algorithms-in-csharp-2](https://user-images.githubusercontent.com/98641125/153196486-9ce09c97-8724-4492-b5d6-4bb4aeb1d8bd.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-csharp-2/?couponCode=GITHUBSTUDENT) 17 | [![data-structures- and-algorithms-in-c-plus-plus](https://user-images.githubusercontent.com/98641125/153196522-2412c993-1055-4322-8487-4133537566c9.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-c-plus-plus/?couponCode=GITHUBSTUDENT) 18 | [![python-programming-in-depth](https://user-images.githubusercontent.com/98641125/153196166-45ef8461-adb1-4f9f-b9ee-e482a5ad54a7.png)]( https://www.udemy.com/course/python-programming-in-depth/?couponCode=GITHUBSTUDENT) 19 | [![linux-commands](https://user-images.githubusercontent.com/98641125/153196567-96b3396c-8ee3-4233-b8fc-66c6b3bd830c.png)]( https://www.udemy.com/course/linux-commands/?couponCode=GITHUBSTUDENT) 20 | [![data-structures-and-algorithms-in-c](https://user-images.githubusercontent.com/98641125/153195841-209d2615-ed5f-4007-ae54-539ac3c1538a.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-c/?couponCode=GITHUBSTUDENT) 21 | [![data-structures- and-algorithms-in-java](https://user-images.githubusercontent.com/98641125/153196280-c2028f4b-d27b-432d-ad5a-9b04be2a3717.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-java/?couponCode=GITHUBSTUDENT) 22 | [![data-structures- and-algorithms-in-java-2](https://user-images.githubusercontent.com/98641125/153196347-23003dc0-55b0-4315-8e52-425c51b2b5c4.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-java-2/?couponCode=GITHUBSTUDENT) 23 | 24 | ## Copyright 25 | © Copyright Deepali Srivastava : All rights reserved. 26 | Not to be used for commercial purposes. 27 | -------------------------------------------------------------------------------- /searching/binary-search-recursive/Program.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | class Program 9 | { 10 | public static int Search(int[] a, int n, int searchValue) 11 | { 12 | return Search(a, 0, n-1, searchValue); 13 | } 14 | 15 | public static int Search(int[] a, int first, int last, int searchValue) 16 | { 17 | if (first > last) 18 | return -1; 19 | 20 | int mid = (first + last) / 2; 21 | 22 | if (searchValue > a[mid]) /*Search in right half */ 23 | return Search(a, mid+1, last, searchValue); 24 | else if (searchValue < a[mid]) /*Search in left half */ 25 | return Search(a, first, mid-1, searchValue); 26 | else 27 | return mid; 28 | } 29 | 30 | static void Main(string[] args) 31 | { 32 | int i, n, searchValue, index; 33 | 34 | int[] a = new int[100]; 35 | 36 | Console.Write("Enter the number of elements : "); 37 | n = Convert.ToInt32(Console.ReadLine()); 38 | 39 | Console.WriteLine("Enter the elements in sorted order - "); 40 | for (i = 0; i < n; i++) 41 | a[i] = Convert.ToInt32(Console.ReadLine()); 42 | 43 | Console.Write("Enter the search value : "); 44 | searchValue = Convert.ToInt32(Console.ReadLine()); 45 | 46 | index = Search(a, n, searchValue); 47 | 48 | if (index == -1) 49 | Console.WriteLine("Value " + searchValue + " not present in the array"); 50 | else 51 | Console.WriteLine("Value " + searchValue + " present at index " + index); 52 | } 53 | } 54 | 55 | -------------------------------------------------------------------------------- /searching/binary-search/Program.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | class Program 9 | { 10 | public static int Search(int[] a, int n, int searchValue) 11 | { 12 | int first=0, last=n-1, mid; 13 | 14 | while( first <= last ) 15 | { 16 | mid = (first + last)/2; 17 | 18 | if( searchValue < a[mid] ) 19 | last = mid-1; /* Search in left half */ 20 | else if( searchValue > a[mid] ) 21 | first = mid+1; /* Search in right half */ 22 | else 23 | return mid; /* searchValue present at index mid */ 24 | } 25 | return -1; 26 | } 27 | 28 | static void Main(string[] args) 29 | { 30 | int i,n,searchValue, index; 31 | 32 | int[] a = new int[100]; 33 | 34 | Console.Write("Enter the number of elements : "); 35 | n = Convert.ToInt32(Console.ReadLine()); 36 | 37 | Console.WriteLine("Enter the elements in sorted order - "); 38 | for(i=0; i= searchValue) 16 | break; 17 | } 18 | if (a[i] == searchValue) 19 | return i; 20 | else 21 | return -1; 22 | } 23 | 24 | static void Main(string[] args) 25 | { 26 | int i, n, searchValue, index; 27 | 28 | int[] a = new int[100]; 29 | 30 | Console.Write("Enter the number of elements : "); 31 | n = Convert.ToInt32(Console.ReadLine()); 32 | 33 | Console.WriteLine("Enter the elements in sorted order - "); 34 | for (i = 0; i < n; i++) 35 | a[i] = Convert.ToInt32(Console.ReadLine()); 36 | 37 | Console.Write("Enter the search value : "); 38 | searchValue = Convert.ToInt32(Console.ReadLine()); 39 | 40 | index = Search(a, n, searchValue); 41 | 42 | if (index == -1) 43 | Console.WriteLine("Value " + searchValue + " not present in the array"); 44 | else 45 | Console.WriteLine("Value " + searchValue + " present at index " + index); 46 | } 47 | } 48 | 49 | -------------------------------------------------------------------------------- /searching/linear-search-with-sentinel/Program.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | class Program 9 | { 10 | public static int Search(int[] a, int n, int searchValue) 11 | { 12 | a[n] = searchValue; 13 | 14 | int i = 0; 15 | while (searchValue != a[i]) 16 | i++; 17 | 18 | if (i < n) 19 | return i; 20 | else 21 | return -1; 22 | 23 | } 24 | 25 | static void Main(string[] args) 26 | { 27 | int i, n, searchValue, index; 28 | 29 | int[] a = new int[100]; 30 | 31 | Console.Write("Enter the number of elements : "); 32 | n = Convert.ToInt32(Console.ReadLine()); 33 | 34 | Console.WriteLine("Enter the elements - "); 35 | for (i = 0; i < n; i++) 36 | a[i] = Convert.ToInt32(Console.ReadLine()); 37 | 38 | Console.Write("Enter the search value : "); 39 | searchValue = Convert.ToInt32(Console.ReadLine()); 40 | 41 | index = Search(a, n, searchValue); 42 | 43 | if (index == -1) 44 | Console.WriteLine("Value " + searchValue + " not present in the array"); 45 | else 46 | Console.WriteLine("Value " + searchValue + " present at index " + index); 47 | } 48 | } 49 | 50 | -------------------------------------------------------------------------------- /searching/linear-search/Program.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | class Program 9 | { 10 | public static int Search(int[] a, int n, int searchValue) 11 | { 12 | for(int i=0; i large) 24 | large = a[i]; 25 | } 26 | 27 | for (i = 0; i < n; i++) 28 | { 29 | x = Hash(a[i], large); 30 | List[x].InsertInOrder(a[i]); 31 | } 32 | 33 | /*Elements of linked lists are copied to array*/ 34 | Node p; 35 | i = 0; 36 | for (j = 0; j <= 5; j++) 37 | { 38 | p = List[j].GetStart(); 39 | while (p != null) 40 | { 41 | a[i++] = p.info; 42 | p = p.link; 43 | } 44 | } 45 | } 46 | 47 | public static int Hash(int x, int large) 48 | { 49 | float temp; 50 | temp = (float)x / large; 51 | return (int)(temp * 5); 52 | } 53 | 54 | static void Main(string[] args) 55 | { 56 | int i,n; 57 | int[] a = new int[20]; 58 | 59 | Console.Write("Enter the number of elements : "); 60 | n = Convert.ToInt32(Console.ReadLine()); 61 | 62 | for( i = 0; i < n; i++ ) 63 | { 64 | Console.Write("Enter element " + (i+1) + " : "); 65 | a[i] = Convert.ToInt32(Console.ReadLine()); 66 | } 67 | 68 | Sort(a, n); 69 | 70 | Console.WriteLine("Sorted array is : "); 71 | for( i = 0; i < n; i++ ) 72 | Console.Write(a[i] + " "); 73 | Console.WriteLine(); 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /sorting/AddressCalculationSort/SortedLinkedList.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace AddressCalculationSort 9 | { 10 | class SortedLinkedList 11 | { 12 | private Node start; 13 | 14 | public SortedLinkedList() 15 | { 16 | start = null; 17 | } 18 | 19 | public void InsertInOrder(int data) 20 | { 21 | Node p, temp; 22 | temp = new Node(data); 23 | 24 | /*List empty or new node to be inserted before first node*/ 25 | if (start == null || data < start.info) 26 | { 27 | temp.link = start; 28 | start = temp; 29 | return; 30 | } 31 | 32 | p = start; 33 | while (p.link != null && p.link.info <= data) 34 | p = p.link; 35 | 36 | temp.link = p.link; 37 | p.link = temp; 38 | 39 | } 40 | 41 | public void CreateList() 42 | { 43 | int i, n, data; 44 | 45 | Console.Write("Enter the number of nodes : "); 46 | n = Convert.ToInt32(Console.ReadLine()); 47 | 48 | if(n == 0) 49 | return; 50 | 51 | for( i = 1; i <= n; i++ ) 52 | { 53 | Console.Write("Enter the element to be inserted : "); 54 | data = Convert.ToInt32(Console.ReadLine()); 55 | InsertInOrder(data); 56 | } 57 | } 58 | 59 | public void Search(int x) 60 | { 61 | Node p; 62 | int position; 63 | 64 | if( start == null ) 65 | { 66 | Console.WriteLine("List is empty\n"); 67 | return; 68 | } 69 | 70 | p = start; 71 | position = 1; 72 | while( p != null && p.info <= x ) 73 | { 74 | if( p.info == x ) 75 | break; 76 | position++; 77 | p = p.link; 78 | } 79 | 80 | if(p == null || p.info != x ) 81 | Console.WriteLine(x + " not found in list"); 82 | else 83 | Console.WriteLine(x + " is at position " + position); 84 | } 85 | 86 | public void DisplayList() 87 | { 88 | Node p; 89 | if( start == null ) 90 | { 91 | Console.WriteLine("List is empty"); 92 | return; 93 | } 94 | Console.Write("List is : "); 95 | p = start; 96 | while(p != null) 97 | { 98 | Console.Write(p.info + " "); 99 | p = p.link; 100 | } 101 | Console.WriteLine(); 102 | } 103 | 104 | public Node GetStart() 105 | { 106 | return start; 107 | } 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /sorting/BinaryTreeSort/BinarySearchTree.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace BinaryTreeSort 9 | { 10 | class BinarySearchTree 11 | { 12 | private TreeNode root; 13 | private static int k; 14 | 15 | public BinarySearchTree() 16 | { 17 | root = null; 18 | } 19 | 20 | public void Inorder(int[] a) 21 | { 22 | k = 0; 23 | Inorder(root, a); 24 | } 25 | 26 | private void Inorder(TreeNode p, int[] a) 27 | { 28 | if (p == null) 29 | return; 30 | Inorder(p.lchild, a); 31 | a[k++] = p.info; 32 | Inorder(p.rchild, a); 33 | } 34 | 35 | bool IsEmpty() 36 | { 37 | return (root == null); 38 | } 39 | 40 | public void Insert(int x) 41 | { 42 | root = Insert(root, x); 43 | } 44 | 45 | private TreeNode Insert(TreeNode p, int x) 46 | { 47 | if (p == null) 48 | p = new TreeNode(x); 49 | else if (x < p.info) 50 | p.lchild = Insert(p.lchild, x); 51 | else 52 | p.rchild = Insert(p.rchild, x); 53 | return p; 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /sorting/BinaryTreeSort/Program.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace BinaryTreeSort 9 | { 10 | class Program 11 | { 12 | public static void Sort(int[] a, int n) 13 | { 14 | BinarySearchTree tree = new BinarySearchTree(); 15 | 16 | for (int i = 0; i < n; i++) 17 | tree.Insert(a[i]); 18 | 19 | tree.Inorder(a); 20 | } 21 | 22 | static void Main(string[] args) 23 | { 24 | int i,n; 25 | int[] a = new int[20]; 26 | 27 | Console.Write("Enter the number of elements : "); 28 | n = Convert.ToInt32(Console.ReadLine()); 29 | 30 | for(i=0; i= 0; x--) 15 | { 16 | swaps = 0; 17 | for (j = 0; j <= x; j++) 18 | { 19 | if (a[j] > a[j + 1]) 20 | { 21 | temp = a[j]; 22 | a[j] = a[j + 1]; 23 | a[j + 1] = temp; 24 | swaps++; 25 | } 26 | } 27 | if (swaps == 0) 28 | break; 29 | } 30 | } 31 | 32 | static void Main(string[] args) 33 | { 34 | int i,n; 35 | int[] a = new int[20]; 36 | 37 | Console.Write("Enter the number of elements : "); 38 | n = Convert.ToInt32(Console.ReadLine()); 39 | 40 | for(i=0; i 1) 23 | { 24 | maxValue = a[1]; 25 | a[1] = a[n]; 26 | a[n] = maxValue; 27 | n--; 28 | RestoreDown(1, a, n); 29 | } 30 | } 31 | 32 | public static void BuildHeap_BottomUp(int[] arr, int n) 33 | { 34 | int i; 35 | for (i = n/2; i >= 1; i--) 36 | RestoreDown(i, arr, n); 37 | } 38 | 39 | private static void RestoreDown(int i, int[] a, int n) 40 | { 41 | int k = a[i]; 42 | int lchild = 2 * i, rchild = lchild + 1; 43 | 44 | while (rchild <= n) 45 | { 46 | if (k >= a[lchild] && k >= a[rchild]) 47 | { 48 | a[i] = k; 49 | return; 50 | } 51 | else if (a[lchild] > a[rchild]) 52 | { 53 | a[i] = a[lchild]; 54 | i = lchild; 55 | } 56 | else 57 | { 58 | a[i] = a[rchild]; 59 | i = rchild; 60 | } 61 | lchild = 2 * i; 62 | rchild = lchild + 1; 63 | } 64 | 65 | /*If number of nodes is even*/ 66 | if (lchild == n && k < a[lchild]) 67 | { 68 | a[i] = a[lchild]; 69 | i = lchild; 70 | } 71 | a[i] = k; 72 | } 73 | 74 | static void Main(string[] args) 75 | { 76 | int i,n; 77 | int[] a = new int[20]; 78 | 79 | Console.Write("Enter the number of elements : "); 80 | n = Convert.ToInt32(Console.ReadLine()); 81 | 82 | for( i = 1; i <= n; i++ ) 83 | { 84 | Console.Write("Enter element " + i + " : "); 85 | a[i] = Convert.ToInt32(Console.ReadLine()); 86 | } 87 | 88 | Sort(a,n); 89 | 90 | Console.WriteLine("Sorted array is : "); 91 | for(i = 1; i <= n; i++) 92 | Console.Write(a[i] + " "); 93 | Console.WriteLine(); 94 | } 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /sorting/InsertionSort/InsertionSort.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | class InsertionSort 9 | { 10 | public static void Sort(int[] a, int n) 11 | { 12 | int i, j, temp; 13 | for (i = 1; i < n; i++) 14 | { 15 | temp = a[i]; 16 | 17 | for (j = i - 1; j >= 0 && a[j] > temp; j--) 18 | a[j + 1] = a[j]; 19 | 20 | a[j + 1] = temp; 21 | } 22 | } 23 | 24 | static void Main(string[] args) 25 | { 26 | int i,n; 27 | int[] a = new int[20]; 28 | 29 | Console.Write("Enter the number of elements : "); 30 | n = Convert.ToInt32(Console.ReadLine()); 31 | 32 | for(i = 0; i < n; i++) 33 | { 34 | Console.Write("Enter element " + (i + 1) + " : "); 35 | a[i] = Convert.ToInt32(Console.ReadLine()); 36 | } 37 | 38 | Sort(a,n); 39 | 40 | Console.WriteLine("Sorted array is : "); 41 | for(i = 0; i < n; i++) 42 | Console.Write(a[i] + " "); 43 | Console.WriteLine(); 44 | } 45 | } 46 | 47 | -------------------------------------------------------------------------------- /sorting/MergeSortIterative/Program.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace MergeSortIterative 9 | { 10 | class Program 11 | { 12 | public static void Sort(int[] a, int n) 13 | { 14 | int[] temp = new int[n]; 15 | int size = 1; 16 | while( size <= n-1 ) 17 | { 18 | SortPass(a, temp, size, n); 19 | size = size * 2; 20 | } 21 | } 22 | 23 | private static void SortPass(int[] a, int[] temp, int size, int n) 24 | { 25 | int i, low1, up1, low2, up2; 26 | 27 | low1 = 0; 28 | 29 | while( low1+size <= n-1 ) 30 | { 31 | up1 = low1 + size - 1; 32 | low2 = low1 + size; 33 | up2 = low2 + size - 1; 34 | 35 | if( up2 >= n )/*if length of last sublist is less than size*/ 36 | up2 = n-1; 37 | 38 | merge(a, temp, low1, up1, low2, up2); 39 | 40 | low1 = up2 + 1; /*Take next two sublists for merging*/ 41 | } 42 | 43 | for( i = low1; i <= n-1; i++) 44 | temp[i] = a[i]; /*If any sublist is left alone*/ 45 | 46 | copy(a, temp, n); 47 | } 48 | 49 | 50 | /* a[low1]...a[up1] and a[low2]...a[up2] merged to temp[low1]...temp[up2] */ 51 | private static void merge( int[] a, int[] temp, int low1, int up1, int low2, int up2 ) 52 | { 53 | int i = low1; 54 | int j = low2; 55 | int k = low1; 56 | 57 | while( (i <= up1) && (j <= up2) ) 58 | { 59 | if( a[i] <= a[j] ) 60 | temp[k++] = a[i++]; 61 | else 62 | temp[k++] = a[j++]; 63 | } 64 | 65 | while( i <= up1 ) 66 | temp[k++] = a[i++]; 67 | 68 | while( j <= up2 ) 69 | temp[k++] = a[j++]; 70 | } 71 | 72 | /*copies temp[low]....temp[up] to a[low]...a[up]*/ 73 | private static void copy(int[] a, int[] temp, int n) 74 | { 75 | for(int i = 0; i < n; i++) 76 | a[i] = temp[i]; 77 | } 78 | 79 | static void Main(string[] args) 80 | { 81 | int i, n; 82 | int[] a = new int[20]; 83 | 84 | Console.Write("Enter the number of elements : "); 85 | n = Convert.ToInt32(Console.ReadLine()); 86 | 87 | for (i = 0; i < n; i++) 88 | { 89 | Console.Write("Enter element " + (i + 1) + " : "); 90 | a[i] = Convert.ToInt32(Console.ReadLine()); 91 | } 92 | 93 | Sort(a, n); 94 | 95 | Console.WriteLine("Sorted array is : "); 96 | for (i = 0; i < n; i++) 97 | Console.Write(a[i] + " "); 98 | Console.WriteLine(); 99 | } 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /sorting/MergeSortRecursive/Program.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace MergeSortRecursive 9 | { 10 | class Program 11 | { 12 | public static void Sort(int[] a, int n) 13 | { 14 | int[] temp = new int[n]; 15 | Sort ( a, temp, 0, n-1 ); 16 | } 17 | 18 | private static void Sort(int[] a, int[] temp, int low, int up) 19 | { 20 | if ( low == up ) /*only one element*/ 21 | return; 22 | 23 | int mid = (low + up)/2; 24 | 25 | Sort(a, temp, low, mid); /* Sort a[low]....a[mid] */ 26 | Sort(a, temp, mid+1, up); /* Sort a[mid+1]....a[up] */ 27 | 28 | /* Merge a[low]...a[mid] and a[mid+1]....a[up] to temp[low]...temp[up] */ 29 | Merge(a, temp, low, mid, mid+1, up); 30 | 31 | /* Copy temp[low]...temp[up] to a[low]...a[up] */ 32 | Copy(a, temp, low, up); 33 | } 34 | 35 | /* a[low1]...a[up1] and a[low2]...a[up2] merged to temp[low1]...temp[up2] */ 36 | private static void Merge( int[] a, int[] temp, int low1, int up1, int low2, int up2 ) 37 | { 38 | int i = low1; 39 | int j = low2; 40 | int k = low1; 41 | 42 | while( (i<=up1) && (j<=up2) ) 43 | { 44 | if( a[i] <= a[j] ) 45 | temp[k++] = a[i++]; 46 | else 47 | temp[k++] = a[j++]; 48 | } 49 | 50 | while( i <= up1 ) 51 | temp[k++] = a[i++]; 52 | 53 | while(j <= up2) 54 | temp[k++] = a[j++]; 55 | } 56 | 57 | /*copies temp[low]....temp[up] to a[low]...a[up]*/ 58 | private static void Copy(int[] a, int[] temp, int low, int up) 59 | { 60 | for(int i = low; i <= up; i++) 61 | a[i] = temp[i]; 62 | } 63 | 64 | static void Main(string[] args) 65 | { 66 | int i,n; 67 | int[] a = new int[20]; 68 | 69 | Console.Write("Enter the number of elements : "); 70 | n = Convert.ToInt32(Console.ReadLine()); 71 | 72 | for( i = 0; i < n; i++ ) 73 | { 74 | Console.Write("Enter element " + (i+1) + " : "); 75 | a[i] = Convert.ToInt32( Console.ReadLine() ); 76 | } 77 | 78 | Sort(a, n); 79 | 80 | Console.WriteLine("Sorted array is : "); 81 | for( i = 0; i < n; i++) 82 | Console.Write( a[i] + " " ); 83 | Console.WriteLine(); 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /sorting/Merging1Project/Program.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace Merging1Project 9 | { 10 | class Program 11 | { 12 | /* a[low1]...a[up1] and a[low2]...a[up2] merged to temp[low1]...temp[up2] */ 13 | public static void Merge(int[] a, int[] temp, int low1, int up1, int low2, int up2) 14 | { 15 | int i = low1; 16 | int j = low2; 17 | int k = low1; 18 | 19 | while ((i <= up1) && (j <= up2)) 20 | { 21 | if (a[i] <= a[j]) 22 | temp[k++] = a[i++]; 23 | else 24 | temp[k++] = a[j++]; 25 | } 26 | 27 | while (i <= up1) 28 | temp[k++] = a[i++]; 29 | 30 | while (j <= up2) 31 | temp[k++] = a[j++]; 32 | } 33 | 34 | static void Main(string[] args) 35 | { 36 | int i; 37 | int[] a = {1,3,5,7, 2,4,6,9,11,14}; 38 | int[] temp = new int[20]; 39 | 40 | Merge(a, temp, 0, 3, 4, 9); 41 | 42 | Console.WriteLine("Array a is : "); 43 | for(i = 0; i <= 9; i++) 44 | Console.Write(a[i] + " "); 45 | Console.WriteLine(); 46 | 47 | Console.WriteLine("Merged array temp is : "); 48 | for( i = 0; i <= 9; i++ ) 49 | Console.Write(temp[i] + " "); 50 | Console.WriteLine(); 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /sorting/MergingProject/Program.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace MergingProject 9 | { 10 | class Program 11 | { 12 | public static void Merge(int[] a1, int[] a2, int[] temp, int n1, int n2) 13 | { 14 | int i = 0, j = 0, k = 0; 15 | 16 | while ((i <= n1-1) && (j <= n2-1)) 17 | { 18 | if (a1[i] < a2[j]) 19 | temp[k++] = a1[i++]; 20 | else 21 | temp[k++] = a2[j++]; 22 | } 23 | 24 | /*copy remaining elements of a1, array a2 finished */ 25 | while (i <= n1 - 1) 26 | temp[k++] = a1[i++]; 27 | 28 | /*copy remaining elements of a2, array a1 finished */ 29 | while (j <= n2 - 1) 30 | temp[k++] = a2[j++]; 31 | } 32 | 33 | static void Main(string[] args) 34 | { 35 | int i,n1,n2; 36 | int[] a1 = new int[20]; 37 | int[] a2 = new int[20]; 38 | int[] temp = new int[40]; 39 | 40 | Console.Write("Enter the number of elements in array a1 : "); 41 | n1 = Convert.ToInt32(Console.ReadLine()); 42 | Console.WriteLine("Enter elements in sorted order : " ); 43 | for( i = 0; i < n1; i++) 44 | { 45 | Console.Write("Enter element " + (i+1) + " : "); 46 | a1[i] = Convert.ToInt32(Console.ReadLine()); 47 | } 48 | 49 | Console.Write("Enter the number of elements in array a2 : "); 50 | n2 = Convert.ToInt32(Console.ReadLine()); 51 | Console.WriteLine("Enter elements in sorted order : "); 52 | for( i = 0; i < n2; i++ ) 53 | { 54 | Console.Write("Enter element " + (i+1) + " : "); 55 | a2[i] = Convert.ToInt32(Console.ReadLine()); 56 | } 57 | 58 | Merge(a1, a2, temp, n1, n2); 59 | 60 | Console.WriteLine("Merged array temp is : "); 61 | for( i = 0; i < n1+n2; i++ ) 62 | Console.Write(temp[i] + " "); 63 | Console.WriteLine(); 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /sorting/QuickSort/Program.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace QuickSort 9 | { 10 | class Program 11 | { 12 | public static void Sort(int[] a, int n) 13 | { 14 | Sort(a, 0, n - 1); 15 | } 16 | 17 | private static void Sort(int[] a, int low, int up) 18 | { 19 | if ( low >= up ) /*zero or one element in sublist*/ 20 | return; 21 | int p = Partition( a, low, up ); 22 | Sort( a, low, p-1 ); /*Sort left sublist*/ 23 | Sort( a, p+1, up ); /*Sort right sublist*/ 24 | } 25 | 26 | private static int Partition(int[] a, int low, int up) 27 | { 28 | int temp, i, j, pivot; 29 | 30 | pivot = a[low]; 31 | 32 | i = low+1; /*moves from left to right*/ 33 | j = up; /*moves from right to left*/ 34 | 35 | while ( i <= j ) 36 | { 37 | while ( a[i] < pivot && i < up ) 38 | i++; 39 | 40 | while ( a[j] > pivot ) 41 | j--; 42 | 43 | if ( i < j ) /*swap a[i] and a[j]*/ 44 | { 45 | temp = a[i]; 46 | a[i] = a[j]; 47 | a[j] = temp; 48 | i++; 49 | j--; 50 | } 51 | else /*found proper place for pivot*/ 52 | break; 53 | } 54 | 55 | /* Proper place for pivot is j */ 56 | a[low] = a[j]; 57 | a[j] = pivot; 58 | 59 | return j; 60 | } 61 | 62 | static void Main(string[] args) 63 | { 64 | int i, n; 65 | int[] a = new int[20]; 66 | 67 | Console.Write("Enter the number of elements : "); 68 | n = Convert.ToInt32(Console.ReadLine()); 69 | 70 | for (i = 0; i < n; i++) 71 | { 72 | Console.Write("Enter element " + (i + 1) + " : "); 73 | a[i] = Convert.ToInt32(Console.ReadLine()); 74 | } 75 | 76 | Sort(a, n); 77 | 78 | Console.WriteLine("Sorted array is : "); 79 | for (i = 0; i < n; i++) 80 | Console.Write(a[i] + " "); 81 | Console.WriteLine(); 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /sorting/README.md: -------------------------------------------------------------------------------- 1 | # Data Structures and Algorithms in C# 2 | 3 | This [“Data Structures and Algorithms in C#”](https://www.udemy.com/course/data-structures-and-algorithms-in-csharp/?couponCode=GITHUBSTUDENT ) repository is for the students of my online course available on Udemy. It contains the source code of all the programs used in the course. 4 | 5 | ### About the Course 6 | * Thoroughly detailed course with complete working programs 7 | * Contains lots of animations to help you visualize the concepts 8 | * Includes over 100 Quiz questions 9 | * Builds a solid foundation in Data Structures and Algorithms 10 | * Prepares you for coding interviews 11 | * Lifetime Access 12 | 13 | ### Courses by [Deepali Srivastava](https://www.udemy.com/user/deepalisrivastava/) 14 | 15 | [![data-structures- and-algorithms-in-csharp](https://user-images.githubusercontent.com/98641125/153196407-99441e67-24a7-4fa0-aaea-78cb39743282.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-csharp/?couponCode=GITHUBSTUDENT) 16 | [![data-structures- and-algorithms-in-csharp-2](https://user-images.githubusercontent.com/98641125/153196486-9ce09c97-8724-4492-b5d6-4bb4aeb1d8bd.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-csharp-2/?couponCode=GITHUBSTUDENT) 17 | [![data-structures- and-algorithms-in-c-plus-plus](https://user-images.githubusercontent.com/98641125/153196522-2412c993-1055-4322-8487-4133537566c9.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-c-plus-plus/?couponCode=GITHUBSTUDENT) 18 | [![python-programming-in-depth](https://user-images.githubusercontent.com/98641125/153196166-45ef8461-adb1-4f9f-b9ee-e482a5ad54a7.png)]( https://www.udemy.com/course/python-programming-in-depth/?couponCode=GITHUBSTUDENT) 19 | [![linux-commands](https://user-images.githubusercontent.com/98641125/153196567-96b3396c-8ee3-4233-b8fc-66c6b3bd830c.png)]( https://www.udemy.com/course/linux-commands/?couponCode=GITHUBSTUDENT) 20 | [![data-structures-and-algorithms-in-c](https://user-images.githubusercontent.com/98641125/153195841-209d2615-ed5f-4007-ae54-539ac3c1538a.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-c/?couponCode=GITHUBSTUDENT) 21 | [![data-structures- and-algorithms-in-java](https://user-images.githubusercontent.com/98641125/153196280-c2028f4b-d27b-432d-ad5a-9b04be2a3717.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-java/?couponCode=GITHUBSTUDENT) 22 | [![data-structures- and-algorithms-in-java-2](https://user-images.githubusercontent.com/98641125/153196347-23003dc0-55b0-4315-8e52-425c51b2b5c4.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-java-2/?couponCode=GITHUBSTUDENT) 23 | 24 | ## Copyright 25 | © Copyright Deepali Srivastava : All rights reserved. 26 | Not to be used for commercial purposes. 27 | -------------------------------------------------------------------------------- /sorting/RadixSortProject/Node.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | using System.Collections.Generic; 8 | using System.Linq; 9 | using System.Text; 10 | 11 | namespace RadixSortProject 12 | { 13 | class Node 14 | { 15 | public int info; 16 | public Node link; 17 | 18 | public Node(int i) 19 | { 20 | info = i; 21 | link = null; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /sorting/RadixSortProject/Program.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace RadixSortProject 9 | { 10 | class Program 11 | { 12 | public static Node Sort(Node start) 13 | { 14 | Node[] rear = new Node[10]; 15 | Node[] front = new Node[10]; 16 | 17 | int leastSigPos = 1; 18 | int mostSigPos = DigitsInLargest(start); 19 | 20 | int i, dig; 21 | Node p; 22 | for (int k = leastSigPos; k <= mostSigPos; k++) 23 | { 24 | /*Making all the queues empty at the beginning of each pass*/ 25 | for (i = 0; i <= 9; i++) 26 | { 27 | rear[i] = null; 28 | front[i] = null; 29 | } 30 | 31 | for (p = start; p != null; p = p.link) 32 | { 33 | /*Find kth digit from right in the number*/ 34 | dig = Digit(p.info, k); 35 | 36 | /*Insert the node in Queue(dig) */ 37 | if (front[dig] == null) 38 | front[dig] = p; 39 | else 40 | rear[dig].link = p; 41 | rear[dig] = p; 42 | } 43 | 44 | /*Join all queues to form new linked list*/ 45 | i = 0; 46 | while (front[i] == null) /*Finding first non empty queue*/ 47 | i++; 48 | start = front[i]; 49 | while (i <= 8) 50 | { 51 | if (rear[i + 1] != null) /*if (i+1)th queue is not empty*/ 52 | rear[i].link = front[i + 1]; /*join end of ith queue to start of (i+1)th queue*/ 53 | else 54 | rear[i + 1] = rear[i]; /*continue with rear[i]*/ 55 | i++; 56 | } 57 | rear[9].link = null; 58 | } 59 | return start; 60 | }/*End of sort*/ 61 | 62 | /*Returns number of digits in the largest element of the list */ 63 | public static int DigitsInLargest(Node start) 64 | { 65 | /*Find largest element*/ 66 | int large = 0; 67 | Node p = start; 68 | while (p != null) 69 | { 70 | if (p.info > large) 71 | large = p.info; 72 | p = p.link; 73 | } 74 | 75 | /*Find number of digits in largest element*/ 76 | int ndigits = 0; 77 | while (large != 0) 78 | { 79 | ndigits++; 80 | large /= 10; 81 | } 82 | return ndigits; 83 | } 84 | 85 | /*Returns kth digit from right in n*/ 86 | public static int Digit(int n, int k) 87 | { 88 | int d = 0, i; 89 | for (i = 1; i <= k; i++) 90 | { 91 | d = n % 10; 92 | n /= 10; 93 | } 94 | return d; 95 | } 96 | 97 | static void Main(string[] args) 98 | { 99 | Node temp, p; 100 | int i, n, data; 101 | 102 | Console.WriteLine("Enter number of elements in the list : "); 103 | n = Convert.ToInt32(Console.ReadLine()); 104 | 105 | Node start = null; 106 | for(i = 1; i <= n; i++ ) /*Inserting elements in linked list*/ 107 | { 108 | Console.Write("Enter element " + i + " : "); 109 | data = Convert.ToInt32(Console.ReadLine()); 110 | 111 | temp = new Node( data ); 112 | if ( start == null ) 113 | start = temp; 114 | else 115 | { 116 | p = start; 117 | while( p.link != null ) 118 | p = p.link; 119 | p.link = temp; 120 | } 121 | } 122 | 123 | start = Sort(start); 124 | 125 | Console.WriteLine("Sorted list is : "); 126 | p = start; 127 | while( p != null ) 128 | { 129 | Console.Write(p.info + " "); 130 | p = p.link; 131 | } 132 | Console.WriteLine(); 133 | } 134 | } 135 | } 136 | 137 | -------------------------------------------------------------------------------- /sorting/SelectionSort/SelectionSort.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | class SelectionSort 9 | { 10 | public static void Sort(int[] a, int n) 11 | { 12 | int minIndex, temp, i, j; 13 | 14 | for (i = 0; i < n - 1; i++) 15 | { 16 | minIndex = i; 17 | for (j = i + 1; j < n; j++) 18 | { 19 | if (a[j] < a[minIndex]) 20 | minIndex = j; 21 | } 22 | if (i != minIndex) 23 | { 24 | temp = a[i]; 25 | a[i] = a[minIndex]; 26 | a[minIndex] = temp; 27 | } 28 | } 29 | } 30 | static void Main(string[] args) 31 | { 32 | int i,n; 33 | int[] a = new int[20]; 34 | 35 | Console.Write("Enter the number of elements : "); 36 | n = Convert.ToInt32(Console.ReadLine()); 37 | 38 | for (i = 0; i < n; i++) 39 | { 40 | Console.Write("Enter element " + (i + 1) + " : "); 41 | a[i] = Convert.ToInt32(Console.ReadLine()); 42 | } 43 | 44 | Sort(a, n); 45 | 46 | Console.WriteLine("Sorted array is : "); 47 | for ( i = 0; i < n; i++) 48 | Console.Write(a[i] + " "); 49 | Console.WriteLine(); 50 | 51 | } 52 | } 53 | 54 | -------------------------------------------------------------------------------- /sorting/ShellSort/Program.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace ShellSort 9 | { 10 | class Program 11 | { 12 | public static void Sort(int[] a, int n) 13 | { 14 | int i, j, temp, h; 15 | 16 | Console.Write("Enter maximum increment(odd value) : "); 17 | h = Convert.ToInt32( Console.ReadLine() ); 18 | 19 | while ( h >= 1 ) 20 | { 21 | for (i = h; i < n; i++) 22 | { 23 | temp = a[i]; 24 | for (j = i-h; j>=0 && a[j]>temp; j = j-h) 25 | a[j+h] = a[j]; 26 | a[j+h] = temp; 27 | } 28 | h = h-2; 29 | } 30 | } 31 | 32 | static void Main(string[] args) 33 | { 34 | int i, n; 35 | int[] a = new int[20]; 36 | 37 | Console.Write("Enter the number of elements : "); 38 | n = Convert.ToInt32(Console.ReadLine()); 39 | 40 | for (i = 0; i < n; i++) 41 | { 42 | Console.Write("Enter element " + (i + 1) + " : "); 43 | a[i] = Convert.ToInt32(Console.ReadLine()); 44 | } 45 | 46 | Sort(a, n); 47 | 48 | Console.WriteLine("Sorted array is : "); 49 | for (i = 0; i < n; i++) 50 | Console.Write(a[i] + " "); 51 | Console.WriteLine(); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /stack-queue/CircularQueueProject/CircularQueue.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace CircularQueueProject 9 | { 10 | class CircularQueue 11 | { 12 | private int[] queueArray; 13 | private int front; 14 | private int rear; 15 | 16 | public CircularQueue() 17 | { 18 | queueArray = new int[10]; 19 | front = -1; 20 | rear = -1; 21 | } 22 | 23 | public CircularQueue(int maxSize) 24 | { 25 | queueArray = new int[maxSize]; 26 | front = -1; 27 | rear = -1; 28 | } 29 | 30 | public bool IsEmpty() 31 | { 32 | return (front == -1); 33 | } 34 | 35 | public bool IsFull() 36 | { 37 | return ((front == 0 && rear == queueArray.Length-1) || (front == rear+1)); 38 | } 39 | 40 | public void Insert(int x) 41 | { 42 | if(IsFull()) 43 | { 44 | Console.WriteLine("Queue Overflow\n"); 45 | return; 46 | } 47 | if( front == -1 ) 48 | front = 0; 49 | if( rear == queueArray.Length-1 ) 50 | rear = 0; 51 | else 52 | rear = rear+1; 53 | queueArray[rear] = x; 54 | } 55 | 56 | public int Delete() 57 | { 58 | if( IsEmpty() ) 59 | throw new System.InvalidOperationException("Queue Underflow"); 60 | 61 | int x = queueArray[front]; 62 | 63 | if( front == rear ) /*queue has only one element*/ 64 | { 65 | front = -1; 66 | rear = -1; 67 | } 68 | else if( front == queueArray.Length-1 ) 69 | front = 0; 70 | else 71 | front = front+1; 72 | return x; 73 | } 74 | 75 | public int Peek() 76 | { 77 | if( IsEmpty() ) 78 | throw new System.InvalidOperationException("Queue Underflow"); 79 | 80 | return queueArray[front]; 81 | } 82 | 83 | public void Display() 84 | { 85 | if( IsEmpty() ) 86 | { 87 | Console.WriteLine("Queue is empty"); 88 | return; 89 | } 90 | 91 | Console.WriteLine("Queue is : "); 92 | 93 | int i = front; 94 | if( front <= rear ) 95 | { 96 | while( i <= rear ) 97 | Console.Write(queueArray[i++] + " "); 98 | } 99 | else 100 | { 101 | while( i <= queueArray.Length-1 ) 102 | Console.WriteLine(queueArray[i++] + " "); 103 | i = 0; 104 | while( i <= rear ) 105 | Console.WriteLine(queueArray[i++] + " "); 106 | } 107 | Console.WriteLine(); 108 | } 109 | 110 | public int Size() 111 | { 112 | if ( IsEmpty() ) 113 | return 0; 114 | if ( IsFull() ) 115 | return queueArray.Length; 116 | 117 | int i = front; 118 | int sz = 0; 119 | if (front <= rear) 120 | while (i <= rear) 121 | { 122 | sz++; 123 | i++; 124 | } 125 | else 126 | { 127 | while (i <= queueArray.Length - 1) 128 | { 129 | sz++; 130 | i++; 131 | } 132 | i = 0; 133 | while (i <= rear) 134 | { 135 | sz++; 136 | i++; 137 | } 138 | } 139 | return sz; 140 | } 141 | } 142 | } 143 | -------------------------------------------------------------------------------- /stack-queue/CircularQueueProject/Program.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace CircularQueueProject 9 | { 10 | class Program 11 | { 12 | static void Main(string[] args) 13 | { 14 | int choice, x; 15 | 16 | CircularQueue qu = new CircularQueue(8); 17 | 18 | while (true) 19 | { 20 | Console.WriteLine("1.Insert an element in the queue"); 21 | Console.WriteLine("2.Delete an element from the queue"); 22 | Console.WriteLine("3.Display element at the front"); 23 | Console.WriteLine("4.Display all elements of the queue"); 24 | Console.WriteLine("5.Display size of the queue"); 25 | Console.WriteLine("6.Quit"); 26 | Console.Write("Enter your choice : "); 27 | choice = Convert.ToInt32(Console.ReadLine()); 28 | if(choice == 6) 29 | break; 30 | 31 | switch (choice) 32 | { 33 | case 1: 34 | Console.Write("Enter the element to be inserted : "); 35 | x = Convert.ToInt32(Console.ReadLine()); 36 | qu.Insert(x); 37 | break; 38 | case 2: 39 | x = qu.Delete(); 40 | Console.WriteLine("Element deleted is : " + x); 41 | break; 42 | case 3: 43 | Console.WriteLine("Element at the front is : " + qu.Peek()); 44 | break; 45 | case 4: 46 | qu.Display(); 47 | break; 48 | case 5: 49 | Console.WriteLine("Size of queue is " + qu.Size()); 50 | break; 51 | default: 52 | Console.WriteLine("Wrong choice"); 53 | break; 54 | } 55 | Console.WriteLine(); 56 | } 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /stack-queue/Deque/DequeA.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace Deque 9 | { 10 | class DequeA 11 | { 12 | private int[] queueArray; 13 | private int front; 14 | private int rear; 15 | 16 | public DequeA() 17 | { 18 | queueArray = new int[10]; 19 | front = -1; 20 | rear = -1; 21 | } 22 | public DequeA(int maxSize) 23 | { 24 | queueArray = new int[maxSize]; 25 | front = -1; 26 | rear = -1; 27 | } 28 | public void InsertFront(int x) 29 | { 30 | if( IsFull() ) 31 | { 32 | Console.WriteLine("Queue Overflow"); 33 | return; 34 | } 35 | if( front == -1 ) 36 | { 37 | front = 0; 38 | rear = 0; 39 | } 40 | else if( front == 0 ) 41 | front = queueArray.Length-1; 42 | else 43 | front = front-1; 44 | queueArray[front] = x; 45 | } 46 | 47 | public void InsertRear(int x) 48 | { 49 | if( IsFull() ) 50 | { 51 | Console.WriteLine("Queue Overflow"); 52 | return; 53 | } 54 | if ( front == -1 ) 55 | front = 0; 56 | 57 | if( rear == queueArray.Length-1 ) 58 | rear = 0; 59 | else 60 | rear = rear+1; 61 | queueArray[rear] = x; 62 | } 63 | 64 | public int DeleteFront() 65 | { 66 | int x; 67 | if( IsEmpty() ) 68 | throw new System.InvalidOperationException("Queue Underflow"); 69 | 70 | x = queueArray[front]; 71 | 72 | if( front == rear ) /*only one element*/ 73 | { 74 | front = -1; 75 | rear = -1; 76 | } 77 | else if( front == queueArray.Length-1 ) 78 | front = 0; 79 | else 80 | front=front+1; 81 | return x; 82 | } 83 | 84 | public int DeleteRear() 85 | { 86 | int x; 87 | 88 | if( IsEmpty() ) 89 | throw new System.InvalidOperationException("Queue Underflow"); 90 | 91 | x = queueArray[rear]; 92 | 93 | if( front == rear ) /*only one element*/ 94 | { 95 | front = -1; 96 | rear = -1; 97 | } 98 | else if( rear == 0 ) 99 | rear = queueArray.Length-1; 100 | else 101 | rear = rear-1; 102 | return x; 103 | } 104 | 105 | public bool IsFull() 106 | { 107 | return ((front == 0 && rear == queueArray.Length - 1) || (front == rear + 1)); 108 | } 109 | 110 | public bool IsEmpty() 111 | { 112 | return (front == -1); 113 | } 114 | 115 | public void Display() 116 | { 117 | int i; 118 | 119 | if( IsEmpty() ) 120 | { 121 | Console.WriteLine("Queue is empty"); 122 | return; 123 | } 124 | 125 | Console.WriteLine("Queue is : "); 126 | i = front; 127 | if( front <= rear ) 128 | { 129 | while( i <= rear ) 130 | Console.Write(queueArray[i++] + " "); 131 | } 132 | else 133 | { 134 | while( i <= queueArray.Length-1 ) 135 | Console.Write( queueArray[i++] + " "); 136 | i = 0; 137 | while(i <= rear) 138 | Console.Write( queueArray[i++] + " "); 139 | } 140 | Console.WriteLine(); 141 | } 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /stack-queue/Deque/Program.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace Deque 9 | { 10 | class Program 11 | { 12 | static void Main(string[] args) 13 | { 14 | int choice,x; 15 | 16 | DequeA dq = new DequeA(8); 17 | 18 | while( true ) 19 | { 20 | Console.WriteLine("1.Insert at the front end"); 21 | Console.WriteLine("2.Insert at the rear end"); 22 | Console.WriteLine("3.Delete from front end"); 23 | Console.WriteLine("4.Delete from rear end"); 24 | Console.WriteLine("5.Display all elements of deque"); 25 | Console.WriteLine("6.Quit"); 26 | Console.Write("Enter your choice : "); 27 | choice = Convert.ToInt32(Console.ReadLine()); 28 | 29 | if(choice==6) 30 | break; 31 | 32 | switch(choice) 33 | { 34 | case 1: 35 | Console.Write("Enter the element to be inserted : "); 36 | x = Convert.ToInt32(Console.ReadLine()); 37 | dq.InsertFront(x); 38 | break; 39 | case 2: 40 | Console.Write("Enter the element to be inserted : "); 41 | x = Convert.ToInt32(Console.ReadLine()); 42 | dq.InsertRear(x); 43 | break; 44 | case 3: 45 | Console.WriteLine("Element deleted from front end is " + dq.DeleteFront()); 46 | break; 47 | case 4: 48 | Console.WriteLine("Element deleted from rear end is " + dq.DeleteRear()); 49 | break; 50 | case 5: 51 | dq.Display(); 52 | break; 53 | default: 54 | Console.WriteLine("Wrong choice"); 55 | break; 56 | } 57 | } 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /stack-queue/ParenthesesProject/Demo.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace ParenthesesProject 9 | { 10 | class Demo 11 | { 12 | static void Main(string[] args) 13 | { 14 | String expression; 15 | 16 | Console.Write("Enter an expression with parentheses : "); 17 | expression = Console.ReadLine(); 18 | 19 | if ( IsValid(expression) ) 20 | Console.WriteLine("Valid expression"); 21 | else 22 | Console.WriteLine("Invalid expression"); 23 | } 24 | 25 | static bool IsValid(String expr) 26 | { 27 | StackA st = new StackA(); 28 | 29 | char ch; 30 | 31 | for ( int i = 0; i < expr.Length; i++) 32 | { 33 | if ( expr[i] == '(' || expr[i] == '{' || expr[i] == '[' ) 34 | st.Push(expr[i]); 35 | 36 | if ( expr[i] == ')' || expr[i] == '}' || expr[i] == ']') 37 | if ( st.IsEmpty() ) 38 | { 39 | Console.WriteLine("Right parentheses are more than left parentheses"); 40 | return false; 41 | } 42 | else 43 | { 44 | ch = st.Pop(); 45 | if ( !MatchParentheses(ch,expr[i]) ) 46 | { 47 | Console.WriteLine("Mismatched parentheses are : "); 48 | Console.WriteLine(ch + " and " + expr[i]); 49 | return false; 50 | } 51 | } 52 | } 53 | 54 | if ( st.IsEmpty() ) 55 | { 56 | Console.WriteLine("Balanced Parentheses"); 57 | return true; 58 | } 59 | else 60 | { 61 | Console.WriteLine("Left parentheses are more than right parentheses"); 62 | return false; 63 | } 64 | } 65 | 66 | static bool MatchParentheses(char leftPar, char rightPar) 67 | { 68 | if ( leftPar == '[' && rightPar == ']' ) 69 | return true; 70 | if ( leftPar == '{' && rightPar == '}' ) 71 | return true; 72 | if ( leftPar == '(' && rightPar == ')' ) 73 | return true; 74 | return false; 75 | } 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /stack-queue/ParenthesesProject/StackA.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace ParenthesesProject 9 | { 10 | class StackA 11 | { 12 | private char[] stackArray; 13 | private int top; 14 | 15 | public StackA() 16 | { 17 | stackArray = new char[10]; 18 | top = -1; 19 | } 20 | 21 | public StackA(int maxSize) 22 | { 23 | stackArray = new char[maxSize]; 24 | top = -1; 25 | } 26 | 27 | public int Size() 28 | { 29 | return top + 1; 30 | } 31 | 32 | public bool IsEmpty() 33 | { 34 | return ( top == -1 ); 35 | } 36 | 37 | public bool IsFull() 38 | { 39 | return ( top == stackArray.Length - 1 ); 40 | } 41 | 42 | public void Push(char x) 43 | { 44 | if( IsFull() ) 45 | { 46 | Console.WriteLine("Stack Overflow\n"); 47 | return; 48 | } 49 | top = top+1; 50 | stackArray[top] = x; 51 | } 52 | 53 | public char Pop() 54 | { 55 | char x; 56 | if( IsEmpty() ) 57 | { 58 | Console.WriteLine("Stack Underflow\n"); 59 | throw new System.InvalidOperationException(); 60 | } 61 | x = stackArray[top]; 62 | top = top-1; 63 | return x; 64 | } 65 | 66 | public char peek() 67 | { 68 | if ( IsEmpty() ) 69 | { 70 | Console.WriteLine("Stack Underflow\n"); 71 | throw new System.InvalidOperationException(); 72 | } 73 | return stackArray[top]; 74 | } 75 | 76 | public void Display() 77 | { 78 | int i; 79 | 80 | Console.WriteLine("top= " + top); 81 | 82 | if ( IsEmpty() ) 83 | { 84 | Console.WriteLine("Stack is empty"); 85 | return; 86 | } 87 | Console.WriteLine("Stack is : "); 88 | for ( i = top; i >= 0; i-- ) 89 | Console.WriteLine(stackArray[i] + " "); 90 | Console.WriteLine(); 91 | } 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /stack-queue/PostfixProject/Demo.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace PostfixProject 9 | { 10 | class Demo 11 | { 12 | static void Main(string[] args) 13 | { 14 | String infix; 15 | 16 | Console.Write("Enter infix expression : "); 17 | infix = Console.ReadLine(); 18 | 19 | String postfix = infixToPostfix(infix); 20 | 21 | Console.WriteLine("Postfix expression is : " + postfix); 22 | 23 | Console.WriteLine("Value of expression : " + evaluatePostfix(postfix)); 24 | } 25 | 26 | public static String infixToPostfix(String infix) 27 | { 28 | String postfix = ""; 29 | 30 | StackChar st = new StackChar(20); 31 | 32 | char next, symbol; 33 | 34 | for (int i = 0; i < infix.Length; i++) 35 | { 36 | symbol = infix[i]; 37 | 38 | if (symbol == ' ' || symbol == '\t') /*ignore blanks and tabs*/ 39 | continue; 40 | 41 | switch (symbol) 42 | { 43 | case '(': 44 | st.Push(symbol); 45 | break; 46 | case ')': 47 | while ((next = st.Pop()) != '(') 48 | postfix = postfix + next; 49 | break; 50 | case '+': 51 | case '-': 52 | case '*': 53 | case '/': 54 | case '%': 55 | case '^': 56 | while (!st.IsEmpty() && Precedence(st.Peek()) >= Precedence(symbol)) 57 | postfix = postfix + st.Pop(); 58 | st.Push(symbol); 59 | break; 60 | default: /*operand*/ 61 | postfix = postfix + symbol; 62 | break; 63 | } 64 | } 65 | while (!st.IsEmpty()) 66 | postfix = postfix + st.Pop(); 67 | return postfix; 68 | } 69 | 70 | public static int Precedence(char symbol) 71 | { 72 | switch (symbol) 73 | { 74 | case '(': 75 | return 0; 76 | case '+': 77 | case '-': 78 | return 1; 79 | case '*': 80 | case '/': 81 | case '%': 82 | return 2; 83 | case '^': 84 | return 3; 85 | default: 86 | return 0; 87 | } 88 | } 89 | 90 | public static int evaluatePostfix(String postfix) 91 | { 92 | StackInt st = new StackInt(20); 93 | 94 | int x, y; 95 | for (int i = 0; i < postfix.Length; i++) 96 | { 97 | if (Char.IsDigit(postfix[i])) 98 | st.Push (Convert.ToInt32(Char.GetNumericValue(postfix[i]))); 99 | else 100 | { 101 | x = st.Pop(); 102 | y = st.Pop(); 103 | switch (postfix[i]) 104 | { 105 | case '+': 106 | st.Push(y + x); break; 107 | case '-': 108 | st.Push(y - x); break; 109 | case '*': 110 | st.Push(y * x); break; 111 | case '/': 112 | st.Push(y / x); break; 113 | case '%': 114 | st.Push(y % x); break; 115 | case '^': 116 | st.Push(power(y, x)); 117 | break; 118 | } 119 | } 120 | } 121 | return st.Pop(); 122 | } 123 | 124 | public static int power(int b, int a) 125 | { 126 | int i, x = 1; 127 | for (i = 1; i <= a; i++) 128 | x = x * b; 129 | return x; 130 | } 131 | } 132 | } 133 | -------------------------------------------------------------------------------- /stack-queue/PostfixProject/StackChar.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace PostfixProject 9 | { 10 | class StackChar 11 | { 12 | private char[] stackArray; 13 | private int top; 14 | 15 | public StackChar() 16 | { 17 | stackArray = new char[10]; 18 | top = -1; 19 | } 20 | 21 | public StackChar(int maxSize) 22 | { 23 | stackArray = new char[maxSize]; 24 | top = -1; 25 | } 26 | 27 | public int Size() 28 | { 29 | return top + 1; 30 | } 31 | 32 | public bool IsEmpty() 33 | { 34 | return (top == -1); 35 | } 36 | 37 | public bool IsFull() 38 | { 39 | return (top == stackArray.Length - 1); 40 | } 41 | 42 | public void Push(char x) 43 | { 44 | if( IsFull() ) 45 | { 46 | Console.WriteLine("Stack Overflow\n"); 47 | return; 48 | } 49 | top = top+1; 50 | stackArray[top] = x; 51 | } 52 | 53 | public char Pop() 54 | { 55 | char x; 56 | if ( IsEmpty() ) 57 | { 58 | Console.WriteLine("Stack Underflow\n"); 59 | throw new System.InvalidOperationException(); 60 | } 61 | x = stackArray[top]; 62 | top = top-1; 63 | return x; 64 | } 65 | 66 | public char Peek() 67 | { 68 | if ( IsEmpty() ) 69 | { 70 | Console.WriteLine("Stack Underflow\n"); 71 | throw new System.InvalidOperationException(); 72 | } 73 | return stackArray[top]; 74 | } 75 | 76 | public void Display() 77 | { 78 | int i; 79 | Console.WriteLine("top= " + top); 80 | 81 | if ( IsEmpty() ) 82 | { 83 | Console.WriteLine("Stack is empty"); 84 | return; 85 | } 86 | Console.WriteLine("Stack is : "); 87 | for ( i = top; i >= 0; i--) 88 | Console.WriteLine(stackArray[i] + " "); 89 | Console.WriteLine(); 90 | } 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /stack-queue/PostfixProject/StackInt.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace PostfixProject 9 | { 10 | class StackInt 11 | { 12 | private int[] stackArray; 13 | private int top; 14 | 15 | public StackInt() 16 | { 17 | stackArray = new int[10]; 18 | top = -1; 19 | } 20 | 21 | public StackInt(int maxSize) 22 | { 23 | stackArray = new int[maxSize]; 24 | top = -1; 25 | } 26 | 27 | public int Size() 28 | { 29 | return top + 1; 30 | } 31 | 32 | public bool IsEmpty() 33 | { 34 | return (top == -1); 35 | } 36 | 37 | public bool IsFull() 38 | { 39 | return ( top == stackArray.Length - 1 ); 40 | } 41 | 42 | public void Push(int x) 43 | { 44 | if ( IsFull() ) 45 | { 46 | Console.WriteLine("Stack Overflow\n"); 47 | return; 48 | } 49 | top = top + 1; 50 | stackArray[top] = x; 51 | } 52 | 53 | public int Pop() 54 | { 55 | int x; 56 | if ( IsEmpty() ) 57 | { 58 | Console.WriteLine("Stack Underflow\n"); 59 | throw new System.InvalidOperationException(); 60 | } 61 | x = stackArray[top]; 62 | top = top-1; 63 | return x; 64 | } 65 | 66 | public int Peek() 67 | { 68 | if ( IsEmpty() ) 69 | { 70 | Console.WriteLine("Stack Underflow\n"); 71 | throw new System.InvalidOperationException(); 72 | } 73 | return stackArray[top]; 74 | } 75 | 76 | public void display() 77 | { 78 | int i; 79 | 80 | Console.WriteLine("top= " + top); 81 | 82 | if ( IsEmpty() ) 83 | { 84 | Console.WriteLine("Stack is empty"); 85 | return; 86 | } 87 | Console.WriteLine("Stack is : "); 88 | for ( i = top; i >= 0; i-- ) 89 | Console.WriteLine( stackArray[i] + " " ); 90 | Console.WriteLine(); 91 | } 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /stack-queue/PriorityQueueProject/Node.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | 9 | namespace PriorityQueueProject 10 | { 11 | class Node 12 | { 13 | public int priority; 14 | public int info; 15 | public Node link; 16 | 17 | public Node(int i, int pr) 18 | { 19 | info = i; 20 | priority = pr; 21 | link = null; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /stack-queue/PriorityQueueProject/PriorityQueueL.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace PriorityQueueProject 9 | { 10 | class PriorityQueueL 11 | { 12 | private Node front; 13 | 14 | public PriorityQueueL() 15 | { 16 | front = null; 17 | } 18 | 19 | public void Insert(int element, int elementPriority) 20 | { 21 | Node temp, p; 22 | 23 | temp = new Node(element, elementPriority); 24 | 25 | /*Queue is empty or element to be added has priority more than first element*/ 26 | if (IsEmpty() || elementPriority < front.priority) 27 | { 28 | temp.link = front; 29 | front = temp; 30 | } 31 | else 32 | { 33 | p = front; 34 | while (p.link != null && p.link.priority <= elementPriority) 35 | p = p.link; 36 | temp.link = p.link; 37 | p.link = temp; 38 | } 39 | } 40 | 41 | public int Delete() 42 | { 43 | int element; 44 | if( IsEmpty() ) 45 | throw new System.InvalidOperationException("Queue Underflow"); 46 | else 47 | { 48 | element = front.info; 49 | front = front.link; 50 | } 51 | return element; 52 | } 53 | 54 | public bool IsEmpty() 55 | { 56 | return (front == null); 57 | } 58 | 59 | public void Display() 60 | { 61 | Node p = front; 62 | if( IsEmpty() ) 63 | Console.WriteLine("Queue is empty\n"); 64 | else 65 | { 66 | Console.WriteLine("Queue is :"); 67 | Console.WriteLine("Element Priority"); 68 | while( p != null ) 69 | { 70 | Console.WriteLine(p.info + " " + p.priority ); 71 | p=p.link; 72 | } 73 | } 74 | Console.WriteLine(""); 75 | } 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /stack-queue/PriorityQueueProject/Program.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace PriorityQueueProject 9 | { 10 | class Program 11 | { 12 | static void Main(string[] args) 13 | { 14 | int choice, element, elementPriority; 15 | 16 | PriorityQueueL pq = new PriorityQueueL(); 17 | 18 | while( true ) 19 | { 20 | Console.WriteLine("1.Insert a new element"); 21 | Console.WriteLine("2.Delete an element"); 22 | Console.WriteLine("3.Display the queue"); 23 | Console.WriteLine("4.Quit"); 24 | Console.Write("Enter your choice : "); 25 | choice = Convert.ToInt32(Console.ReadLine()); 26 | if(choice == 4) 27 | break; 28 | 29 | switch( choice ) 30 | { 31 | case 1: 32 | Console.WriteLine("Enter the element to be inserted : "); 33 | element = Convert.ToInt32(Console.ReadLine()); 34 | Console.WriteLine("Enter its priority : "); 35 | elementPriority = Convert.ToInt32(Console.ReadLine()); 36 | pq.Insert(element, elementPriority); 37 | break; 38 | case 2: 39 | Console.WriteLine("Deleted element is " + pq.Delete()); 40 | break; 41 | case 3: 42 | pq.Display(); 43 | break; 44 | default : 45 | Console.WriteLine("Wrong choice"); 46 | break; 47 | } 48 | } 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /stack-queue/QueueArrayProject/Demo.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace QueueArrayProject 9 | { 10 | class Demo 11 | { 12 | static void Main(string[] args) 13 | { 14 | int choice, x; 15 | 16 | QueueA qu = new QueueA(8); 17 | 18 | while (true) 19 | { 20 | Console.WriteLine("1.Insert an element in the queue"); 21 | Console.WriteLine("2.Delete an element from the queue"); 22 | Console.WriteLine("3.Display element at the front"); 23 | Console.WriteLine("4.Display all elements of the queue"); 24 | Console.WriteLine("5.Display size of the queue"); 25 | Console.WriteLine("6.Quit"); 26 | Console.Write("Enter your choice : "); 27 | choice = Convert.ToInt32(Console.ReadLine()); 28 | if (choice == 6) 29 | break; 30 | 31 | switch (choice) 32 | { 33 | case 1: 34 | Console.Write("Enter the element to be inserted : "); 35 | x = Convert.ToInt32(Console.ReadLine()); 36 | qu.Insert(x); 37 | break; 38 | case 2: 39 | x = qu.Delete(); 40 | Console.WriteLine("Element deleted is : " + x); 41 | break; 42 | case 3: 43 | Console.WriteLine("Element at the front is : " + qu.Peek()); 44 | break; 45 | case 4: 46 | qu.Display(); 47 | break; 48 | case 5: 49 | Console.WriteLine("Size of queue is " + qu.Size()); 50 | break; 51 | default: 52 | Console.WriteLine("Wrong choice"); 53 | break; 54 | } 55 | Console.WriteLine(); 56 | } 57 | } 58 | } 59 | } 60 | 61 | -------------------------------------------------------------------------------- /stack-queue/QueueArrayProject/QueueA.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace QueueArrayProject 9 | { 10 | class QueueA 11 | { 12 | private int[] queueArray; 13 | private int front; 14 | private int rear; 15 | 16 | public QueueA() 17 | { 18 | queueArray = new int[10]; 19 | front = -1; 20 | rear = -1; 21 | } 22 | public QueueA(int maxSize) 23 | { 24 | queueArray = new int[maxSize]; 25 | front = -1; 26 | rear = -1; 27 | } 28 | public bool IsEmpty() 29 | { 30 | return (front == -1 || front == rear + 1); 31 | } 32 | 33 | public bool IsFull() 34 | { 35 | return (rear == queueArray.Length - 1); 36 | } 37 | 38 | public int Size() 39 | { 40 | if ( IsEmpty() ) 41 | return 0; 42 | else 43 | return rear - front + 1; 44 | } 45 | 46 | public void Insert(int x) 47 | { 48 | if ( IsFull() ) 49 | { 50 | Console.WriteLine("Queue Overflow\n"); 51 | return; 52 | } 53 | if ( front==-1 ) 54 | front = 0; 55 | rear = rear + 1; 56 | queueArray[rear] = x; 57 | } 58 | 59 | public int Delete() 60 | { 61 | int x; 62 | if( IsEmpty() ) 63 | throw new System.InvalidOperationException("Queue Underflow"); 64 | x = queueArray[front]; 65 | front = front+1; 66 | return x; 67 | } 68 | 69 | public int Peek() 70 | { 71 | if ( IsEmpty() ) 72 | throw new System.InvalidOperationException("Queue Underflow"); 73 | return queueArray[front]; 74 | } 75 | 76 | public void Display() 77 | { 78 | if ( IsEmpty() ) 79 | { 80 | Console.WriteLine("Queue is empty\n"); 81 | return; 82 | } 83 | 84 | Console.WriteLine("Queue is :\n\n"); 85 | for (int i = front; i <= rear; i++ ) 86 | Console.Write(queueArray[i] + " "); 87 | 88 | Console.WriteLine(); 89 | } 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /stack-queue/QueueLinkedListProject/Demo.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace QueueLinkedListProject 9 | { 10 | class Demo 11 | { 12 | static void Main(string[] args) 13 | { 14 | int choice,x; 15 | 16 | QueueL qu = new QueueL(); 17 | 18 | while(true) 19 | { 20 | Console.WriteLine("1.Insert an element in the queue"); 21 | Console.WriteLine("2.Delete an element from the queue"); 22 | Console.WriteLine("3.Display element at the front"); 23 | Console.WriteLine("4.Display all elements of the queue"); 24 | Console.WriteLine("5.Display size of the queue"); 25 | Console.WriteLine("6.Quit"); 26 | Console.Write("Enter your choice : "); 27 | choice = Convert.ToInt32(Console.ReadLine()); 28 | if ( choice == 6 ) 29 | break; 30 | 31 | switch( choice ) 32 | { 33 | case 1: 34 | Console.Write("Enter the element to be inserted : "); 35 | x = Convert.ToInt32(Console.ReadLine()); 36 | qu.Insert(x); 37 | break; 38 | case 2: 39 | x=qu.Delete(); 40 | Console.WriteLine("Element deleted is : " + x); 41 | break; 42 | case 3: 43 | Console.WriteLine("Element at the front is : " + qu.Peek()); 44 | break; 45 | case 4: 46 | qu.Display(); 47 | break; 48 | case 5: 49 | Console.WriteLine("Size of queue is " + qu.Size()); 50 | break; 51 | default: 52 | Console.WriteLine("Wrong choice"); 53 | break; 54 | } 55 | Console.WriteLine(); 56 | } 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /stack-queue/QueueLinkedListProject/Node.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace QueueLinkedListProject 9 | { 10 | class Node 11 | { 12 | public int info; 13 | public Node link; 14 | 15 | public Node(int i) 16 | { 17 | info = i; 18 | link = null; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /stack-queue/QueueLinkedListProject/QueueL.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace QueueLinkedListProject 9 | { 10 | class QueueL 11 | { 12 | private Node front; 13 | private Node rear; 14 | 15 | public QueueL() 16 | { 17 | front = null; 18 | rear = null; 19 | } 20 | public int Size() 21 | { 22 | int s = 0; 23 | Node p = front; 24 | while (p != null ) 25 | { 26 | s++; 27 | p = p.link; 28 | } 29 | return s; 30 | } 31 | 32 | public void Insert(int x) 33 | { 34 | Node temp; 35 | temp = new Node(x); 36 | 37 | if (front == null) /*If Queue is empty*/ 38 | front = temp; 39 | else 40 | rear.link = temp; 41 | rear = temp; 42 | } 43 | 44 | public int Delete() 45 | { 46 | int x; 47 | if( IsEmpty() ) 48 | throw new System.InvalidOperationException("Queue Underflow"); 49 | x = front.info; 50 | front = front.link; 51 | return x; 52 | } 53 | 54 | public int Peek() 55 | { 56 | if( IsEmpty() ) 57 | throw new System.InvalidOperationException("Queue Underflow"); 58 | return front.info; 59 | } 60 | 61 | public bool IsEmpty() 62 | { 63 | return ( front == null ); 64 | } 65 | 66 | public void Display() 67 | { 68 | Node p = front; 69 | if( IsEmpty() ) 70 | { 71 | Console.WriteLine("Queue is empty"); 72 | return; 73 | } 74 | 75 | Console.WriteLine("Queue is : "); 76 | while ( p != null ) 77 | { 78 | Console.Write(p.info + " "); 79 | p = p.link; 80 | } 81 | Console.WriteLine(); 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /stack-queue/QueuethruCircularLinkedList/Node.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace QueuethruCircularLinkedList 9 | { 10 | class Node 11 | { 12 | public int info; 13 | public Node link; 14 | 15 | public Node(int i) 16 | { 17 | info = i; 18 | link = null; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /stack-queue/QueuethruCircularLinkedList/Program.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace QueuethruCircularLinkedList 9 | { 10 | class Program 11 | { 12 | static void Main(string[] args) 13 | { 14 | int choice,x; 15 | 16 | QueueCL qu = new QueueCL(); 17 | 18 | while(true) 19 | { 20 | Console.WriteLine("1.Insert an element in the queue"); 21 | Console.WriteLine("2.Delete an element from the queue"); 22 | Console.WriteLine("3.Display element at the front"); 23 | Console.WriteLine("4.Display all elements of the queue"); 24 | Console.WriteLine("5.Display size of the queue"); 25 | Console.WriteLine("6.Quit"); 26 | Console.Write("Enter your choice : "); 27 | choice = Convert.ToInt32(Console.ReadLine()); 28 | if( choice == 6 ) 29 | break; 30 | 31 | switch(choice) 32 | { 33 | case 1: 34 | Console.Write("Enter the element to be inserted : "); 35 | x = Convert.ToInt32(Console.ReadLine()); 36 | qu.Insert(x); 37 | break; 38 | case 2: 39 | x = qu.Delete(); 40 | Console.WriteLine("Element deleted is : " + x); 41 | break; 42 | case 3: 43 | Console.WriteLine("Element at the front is : " + qu.Peek()); 44 | break; 45 | case 4: 46 | qu.Display(); 47 | break; 48 | case 5: 49 | Console.WriteLine("Size of queue is " + qu.Size()); 50 | break; 51 | default: 52 | Console.WriteLine("Wrong choice"); 53 | break; 54 | } 55 | Console.WriteLine(); 56 | } 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /stack-queue/QueuethruCircularLinkedList/QueueCL.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace QueuethruCircularLinkedList 9 | { 10 | class QueueCL 11 | { 12 | private Node rear; 13 | 14 | public QueueCL() 15 | { 16 | rear = null; 17 | } 18 | 19 | public bool IsEmpty() 20 | { 21 | return (rear == null); 22 | } 23 | 24 | public void Insert(int x) 25 | { 26 | Node temp = new Node(x); 27 | 28 | if (IsEmpty()) 29 | { 30 | rear = temp; 31 | rear.link = rear; 32 | } 33 | else 34 | { 35 | temp.link = rear.link; 36 | rear.link = temp; 37 | rear = temp; 38 | } 39 | } 40 | 41 | public int Delete() 42 | { 43 | if( IsEmpty() ) 44 | throw new System.InvalidOperationException("Queue Underflow"); 45 | 46 | Node temp; 47 | if( rear.link == rear ) /*If only one element*/ 48 | { 49 | temp = rear; 50 | rear = null; 51 | } 52 | else 53 | { 54 | temp = rear.link; 55 | rear.link = temp.link; 56 | } 57 | return temp.info; 58 | } 59 | 60 | public int Peek() 61 | { 62 | if ( IsEmpty() ) 63 | throw new System.InvalidOperationException("Queue Underflow"); 64 | return rear.link.info; 65 | } 66 | 67 | public void Display() 68 | { 69 | if ( IsEmpty() ) 70 | { 71 | Console.WriteLine("Queue is empty"); 72 | return; 73 | } 74 | 75 | Console.WriteLine("Queue is : "); 76 | Node p = rear.link; 77 | do 78 | { 79 | Console.Write(p.info + " "); 80 | p = p.link; 81 | }while( p != rear.link ); 82 | Console.WriteLine(); 83 | } 84 | 85 | public int Size() 86 | { 87 | if ( IsEmpty() ) 88 | return 0; 89 | int s = 0; 90 | Node p = rear.link; 91 | do 92 | { 93 | s++; 94 | p = p.link; 95 | } while (p != rear.link); 96 | return s; 97 | } 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /stack-queue/README.md: -------------------------------------------------------------------------------- 1 | # Data Structures and Algorithms in C# 2 | 3 | This [“Data Structures and Algorithms in C#”](https://www.udemy.com/course/data-structures-and-algorithms-in-csharp/?couponCode=GITHUBSTUDENT ) repository is for the students of my online course available on Udemy. It contains the source code of all the programs used in the course. 4 | 5 | ### About the Course 6 | * Thoroughly detailed course with complete working programs 7 | * Contains lots of animations to help you visualize the concepts 8 | * Includes over 100 Quiz questions 9 | * Builds a solid foundation in Data Structures and Algorithms 10 | * Prepares you for coding interviews 11 | * Lifetime Access 12 | 13 | ### Courses by [Deepali Srivastava](https://www.udemy.com/user/deepalisrivastava/) 14 | 15 | [![data-structures- and-algorithms-in-csharp](https://user-images.githubusercontent.com/98641125/153196407-99441e67-24a7-4fa0-aaea-78cb39743282.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-csharp/?couponCode=GITHUBSTUDENT) 16 | [![data-structures- and-algorithms-in-csharp-2](https://user-images.githubusercontent.com/98641125/153196486-9ce09c97-8724-4492-b5d6-4bb4aeb1d8bd.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-csharp-2/?couponCode=GITHUBSTUDENT) 17 | [![data-structures- and-algorithms-in-c-plus-plus](https://user-images.githubusercontent.com/98641125/153196522-2412c993-1055-4322-8487-4133537566c9.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-c-plus-plus/?couponCode=GITHUBSTUDENT) 18 | [![python-programming-in-depth](https://user-images.githubusercontent.com/98641125/153196166-45ef8461-adb1-4f9f-b9ee-e482a5ad54a7.png)]( https://www.udemy.com/course/python-programming-in-depth/?couponCode=GITHUBSTUDENT) 19 | [![linux-commands](https://user-images.githubusercontent.com/98641125/153196567-96b3396c-8ee3-4233-b8fc-66c6b3bd830c.png)]( https://www.udemy.com/course/linux-commands/?couponCode=GITHUBSTUDENT) 20 | [![data-structures-and-algorithms-in-c](https://user-images.githubusercontent.com/98641125/153195841-209d2615-ed5f-4007-ae54-539ac3c1538a.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-c/?couponCode=GITHUBSTUDENT) 21 | [![data-structures- and-algorithms-in-java](https://user-images.githubusercontent.com/98641125/153196280-c2028f4b-d27b-432d-ad5a-9b04be2a3717.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-java/?couponCode=GITHUBSTUDENT) 22 | [![data-structures- and-algorithms-in-java-2](https://user-images.githubusercontent.com/98641125/153196347-23003dc0-55b0-4315-8e52-425c51b2b5c4.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-java-2/?couponCode=GITHUBSTUDENT) 23 | 24 | ## Copyright 25 | © Copyright Deepali Srivastava : All rights reserved. 26 | Not to be used for commercial purposes. 27 | -------------------------------------------------------------------------------- /stack-queue/StackArrayProject/Demo.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace StackArrayProject 9 | { 10 | class Demo 11 | { 12 | static void Main(string[] args) 13 | { 14 | int choice,x; 15 | 16 | StackA st = new StackA(8); 17 | 18 | while(true) 19 | { 20 | Console.WriteLine("1.Push an element on the stack"); 21 | Console.WriteLine("2.Pop an element from the stack"); 22 | Console.WriteLine("3.Display the top element"); 23 | Console.WriteLine("4.Display all stack elements"); 24 | Console.WriteLine("5.Display size of the stack"); 25 | Console.WriteLine("6.Quit"); 26 | Console.Write("Enter your choice : "); 27 | choice = Convert.ToInt32(Console.ReadLine()); 28 | 29 | if(choice==6) 30 | break; 31 | 32 | switch(choice) 33 | { 34 | case 1 : 35 | Console.Write("Enter the element to be pushed : "); 36 | x=Convert.ToInt32(Console.ReadLine()); 37 | st.Push(x); 38 | break; 39 | case 2: 40 | x=st.Pop(); 41 | Console.WriteLine("Popped element is : " + x); 42 | break; 43 | case 3: 44 | Console.WriteLine("Element at the top is : " + st.Peek()); 45 | break; 46 | case 4: 47 | st.Display(); 48 | break; 49 | case 5: 50 | Console.WriteLine("Size of stack " + st.Size()); 51 | break; 52 | default: 53 | Console.WriteLine("Wrong choice"); 54 | break; 55 | } 56 | Console.WriteLine(""); 57 | } 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /stack-queue/StackArrayProject/StackA.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace StackArrayProject 9 | { 10 | class StackA 11 | { 12 | private int[] stackArray; 13 | private int top; 14 | 15 | public StackA() 16 | { 17 | stackArray = new int[10]; 18 | top = -1; 19 | } 20 | 21 | public StackA(int maxSize) 22 | { 23 | stackArray = new int[maxSize]; 24 | top = -1; 25 | } 26 | 27 | public int Size() 28 | { 29 | return top + 1; 30 | } 31 | 32 | public bool IsEmpty() 33 | { 34 | return ( top == -1 ); 35 | } 36 | 37 | public bool IsFull() 38 | { 39 | return ( top == stackArray.Length-1 ); 40 | } 41 | 42 | public void Push(int x) 43 | { 44 | if(IsFull()) 45 | { 46 | Console.WriteLine("Stack Overflow"); 47 | return; 48 | } 49 | top = top + 1; 50 | stackArray[top] = x; 51 | } 52 | 53 | public int Pop() 54 | { 55 | int x; 56 | if( IsEmpty() ) 57 | throw new System.InvalidOperationException("Stack Underflow"); 58 | x=stackArray[top]; 59 | top=top-1; 60 | return x; 61 | } 62 | 63 | public int Peek() 64 | { 65 | if ( IsEmpty() ) 66 | throw new System.InvalidOperationException("Stack Underflow"); 67 | return stackArray[top]; 68 | } 69 | 70 | public void Display() 71 | { 72 | if( IsEmpty() ) 73 | { 74 | Console.WriteLine("Stack is empty"); 75 | return; 76 | } 77 | Console.WriteLine("Stack is : "); 78 | for ( int i = top; i >= 0; i--) 79 | Console.WriteLine(stackArray[i] + " "); 80 | Console.WriteLine(); 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /stack-queue/StackLinkedListProject/Demo.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace StackLinkedListProject 9 | { 10 | class Demo 11 | { 12 | static void Main(string[] args) 13 | { 14 | int choice,x; 15 | 16 | StackL st = new StackL(); 17 | 18 | while(true) 19 | { 20 | Console.WriteLine("1.Push an element on the stack"); 21 | Console.WriteLine("2.Pop an element from the stack"); 22 | Console.WriteLine("3.Display the top element"); 23 | Console.WriteLine("4.Display all stack elements"); 24 | Console.WriteLine("5.Display size of the stack"); 25 | Console.WriteLine("6.Quit"); 26 | Console.Write("Enter your choice : "); 27 | choice = Convert.ToInt32(Console.ReadLine()); 28 | 29 | if ( choice == 6 ) 30 | break; 31 | switch( choice ) 32 | { 33 | case 1 : 34 | Console.Write("Enter the element to be pushed : "); 35 | x = Convert.ToInt32(Console.ReadLine()); 36 | st.Push(x); 37 | break; 38 | case 2: 39 | x = st.Pop(); 40 | Console.WriteLine("Popped element is : " + x); 41 | break; 42 | case 3: 43 | Console.WriteLine("Element at the top is : " + st.Peek()); 44 | break; 45 | case 4: 46 | st.Display(); 47 | break; 48 | case 5: 49 | Console.WriteLine("Size of stack " + st.Size()); 50 | break; 51 | default: 52 | Console.WriteLine("Wrong choice"); 53 | break; 54 | } 55 | Console.WriteLine(""); 56 | } 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /stack-queue/StackLinkedListProject/Node.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace StackLinkedListProject 9 | { 10 | class Node 11 | { 12 | public int info; 13 | public Node link; 14 | 15 | public Node(int i) 16 | { 17 | info = i; 18 | link = null; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /stack-queue/StackLinkedListProject/StackL.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace StackLinkedListProject 9 | { 10 | class StackL 11 | { 12 | private Node top; 13 | 14 | public StackL() 15 | { 16 | top = null; 17 | } 18 | 19 | public int Size() 20 | { 21 | int s = 0; 22 | Node p = top; 23 | while (p != null) 24 | { 25 | p = p.link; 26 | s++; 27 | } 28 | return s; 29 | } 30 | 31 | public void Push(int x) 32 | { 33 | Node temp = new Node(x); 34 | temp.link = top; 35 | top = temp; 36 | } 37 | 38 | public int Pop() 39 | { 40 | int x; 41 | if ( IsEmpty() ) 42 | throw new System.InvalidOperationException("Stack Underflow"); 43 | x = top.info; 44 | top = top.link; 45 | return x; 46 | } 47 | 48 | public int Peek() 49 | { 50 | if ( IsEmpty() ) 51 | throw new System.InvalidOperationException("Stack Underflow"); 52 | return top.info; 53 | } 54 | 55 | public bool IsEmpty() 56 | { 57 | return ( top == null ); 58 | } 59 | 60 | public void Display() 61 | { 62 | Node p = top; 63 | if ( IsEmpty() ) 64 | { 65 | Console.WriteLine("Stack is empty"); 66 | return; 67 | } 68 | 69 | Console.WriteLine("Stack is : "); 70 | while( p != null ) 71 | { 72 | Console.WriteLine(p.info + " "); 73 | p=p.link; 74 | } 75 | Console.WriteLine(); 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /tree/BinarySearchTreeProject/BinarySearchTree.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace BinarySearchTreeProject 9 | { 10 | class BinarySearchTree 11 | { 12 | private Node root; 13 | 14 | public BinarySearchTree() 15 | { 16 | root = null; 17 | } 18 | 19 | public bool IsEmpty() 20 | { 21 | return (root == null); 22 | } 23 | 24 | public void Insert(int x) 25 | { 26 | root = Insert(root, x); 27 | } 28 | 29 | private Node Insert(Node p, int x) 30 | { 31 | if (p == null) 32 | p = new Node(x); 33 | else if ( x < p.info ) 34 | p.lchild = Insert(p.lchild, x); 35 | else if ( x > p.info ) 36 | p.rchild = Insert(p.rchild, x); 37 | else 38 | Console.WriteLine(x + " already present in tree"); 39 | return p; 40 | } 41 | 42 | public void Insert1(int x) 43 | { 44 | Node p = root; 45 | Node par = null; 46 | 47 | while( p != null ) 48 | { 49 | par = p; 50 | if(x < p.info) 51 | p = p.lchild; 52 | else if(x > p.info) 53 | p = p.rchild; 54 | else 55 | { 56 | Console.WriteLine(x + " already present in the tree"); 57 | return; 58 | } 59 | } 60 | 61 | Node temp = new Node(x); 62 | 63 | if(par == null) 64 | root = temp; 65 | else if(x < par.info) 66 | par.lchild = temp; 67 | else 68 | par.rchild = temp; 69 | } 70 | 71 | public bool Search(int x) 72 | { 73 | return (Search(root, x) != null); 74 | } 75 | 76 | private Node Search(Node p, int x) 77 | { 78 | if (p == null) 79 | return null; /*key not found*/ 80 | if (x < p.info)/*search in left subtree*/ 81 | return Search(p.lchild, x); 82 | if (x > p.info)/*search in right subtree*/ 83 | return Search(p.rchild, x); 84 | return p; /*key found*/ 85 | } 86 | 87 | public bool Search1(int x) 88 | { 89 | Node p = root; 90 | while (p != null) 91 | { 92 | if (x < p.info) 93 | p = p.lchild; /*Move to left child*/ 94 | else if (x > p.info) 95 | p = p.rchild; /*Move to right child */ 96 | else /*x found*/ 97 | return true; 98 | } 99 | return false; 100 | } 101 | 102 | public void Delete(int x) 103 | { 104 | root = Delete(root, x); 105 | } 106 | 107 | private Node Delete(Node p, int x) 108 | { 109 | Node ch,s; 110 | 111 | if(p == null) 112 | { 113 | Console.WriteLine(x + " not found"); 114 | return p; 115 | } 116 | if(x < p.info) /*delete from left subtree*/ 117 | p.lchild = Delete(p.lchild, x); 118 | else if(x > p.info) /*delete from right subtree*/ 119 | p.rchild = Delete(p.rchild, x); 120 | else 121 | { 122 | /*key to be deleted is found*/ 123 | if( p.lchild != null && p.rchild != null ) /*2 children*/ 124 | { 125 | s = p.rchild; 126 | while(s.lchild != null) 127 | s = s.lchild; 128 | p.info = s.info; 129 | p.rchild = Delete(p.rchild,s.info); 130 | } 131 | else /*1 child or no child*/ 132 | { 133 | if(p.lchild != null) /*only left child*/ 134 | ch = p.lchild; 135 | else /*only right child or no child*/ 136 | ch = p.rchild; 137 | p = ch; 138 | } 139 | } 140 | return p; 141 | } 142 | 143 | public void Delete1(int x) 144 | { 145 | Node p = root; 146 | Node par = null; 147 | 148 | while(p != null) 149 | { 150 | if(x == p.info) 151 | break; 152 | par = p; 153 | if(x < p.info) 154 | p = p.lchild; 155 | else 156 | p = p.rchild; 157 | } 158 | 159 | if(p == null) 160 | { 161 | Console.WriteLine(x + " not found"); 162 | return; 163 | } 164 | 165 | /*Case C: 2 children*/ 166 | /*Find inorder successor and its parent*/ 167 | Node s,ps; 168 | if( p.lchild != null && p.rchild != null ) 169 | { 170 | ps = p; 171 | s = p.rchild; 172 | 173 | while( s.lchild != null ) 174 | { 175 | ps = s; 176 | s = s.lchild; 177 | } 178 | p.info = s.info; 179 | p = s; 180 | par = ps; 181 | } 182 | 183 | /*Case B and Case A : 1 or no child*/ 184 | Node ch; 185 | if( p.lchild != null ) /*node to be deleted has left child */ 186 | ch = p.lchild; 187 | else /*node to be deleted has right child or no child*/ 188 | ch = p.rchild; 189 | 190 | if( par == null ) /*node to be deleted is root node*/ 191 | root = ch; 192 | else if( p == par.lchild )/*node is left child of its parent*/ 193 | par.lchild = ch; 194 | else /*node is right child of its parent*/ 195 | par.rchild = ch; 196 | } 197 | 198 | public int Min() 199 | { 200 | if (IsEmpty()) 201 | throw new InvalidOperationException("Tree is empty"); 202 | return Min(root).info; 203 | } 204 | 205 | private Node Min(Node p) 206 | { 207 | if (p.lchild == null) 208 | return p; 209 | return Min(p.lchild); 210 | } 211 | 212 | public int Max() 213 | { 214 | if (IsEmpty()) 215 | throw new InvalidOperationException("Tree is empty"); 216 | return Max(root).info; 217 | } 218 | 219 | private Node Max(Node p) 220 | { 221 | if (p.rchild == null) 222 | return p; 223 | return Max(p.rchild); 224 | } 225 | 226 | public int Min1() 227 | { 228 | if (IsEmpty()) 229 | throw new InvalidOperationException("Tree is empty"); 230 | Node p = root; 231 | while (p.lchild != null) 232 | p = p.lchild; 233 | return p.info; 234 | } 235 | 236 | public int Max1() 237 | { 238 | if (IsEmpty()) 239 | throw new InvalidOperationException("Tree is empty"); 240 | Node p = root; 241 | while (p.rchild != null) 242 | p = p.rchild; 243 | return p.info; 244 | } 245 | 246 | public void Display() 247 | { 248 | Display(root,0); 249 | Console.WriteLine(); 250 | } 251 | 252 | 253 | 254 | private void Display(Node p, int level) 255 | { 256 | int i; 257 | if(p == null) 258 | return; 259 | 260 | Display(p.rchild, level+1); 261 | Console.WriteLine(); 262 | 263 | for(i=0; i hR) 331 | return 1 + hL; 332 | else 333 | return 1 + hR; 334 | } 335 | } 336 | } 337 | -------------------------------------------------------------------------------- /tree/BinarySearchTreeProject/Demo.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace BinarySearchTreeProject 9 | { 10 | class Demo 11 | { 12 | static void Main(string[] args) 13 | { 14 | BinarySearchTree bt = new BinarySearchTree(); 15 | int choice,x; 16 | 17 | while(true) 18 | { 19 | Console.WriteLine("1.Display Tree"); 20 | Console.WriteLine("2.Search"); 21 | Console.WriteLine("3.Insert a new node"); 22 | Console.WriteLine("4.Delete a node"); 23 | Console.WriteLine("5.Preorder Traversal"); 24 | Console.WriteLine("6.Inorder Traversal"); 25 | Console.WriteLine("7.Postorder Traversal"); 26 | Console.WriteLine("8.Height of tree"); 27 | Console.WriteLine("9.Find Minimum key"); 28 | Console.WriteLine("10.Find Maximum key"); 29 | Console.WriteLine("11.Quit"); 30 | Console.Write("Enter your choice : "); 31 | choice = Convert.ToInt32(Console.ReadLine()); 32 | 33 | if(choice == 11) 34 | break; 35 | 36 | switch( choice ) 37 | { 38 | case 1: 39 | bt.Display(); 40 | break; 41 | case 2: 42 | Console.Write("Enter the key to be searched : "); 43 | x = Convert.ToInt32(Console.ReadLine()); 44 | 45 | if ( bt.Search(x) ) 46 | Console.WriteLine("Key found"); 47 | else 48 | Console.WriteLine("Key not found"); 49 | break; 50 | case 3: 51 | Console.Write("Enter the key to be inserted : "); 52 | x = Convert.ToInt32(Console.ReadLine()); 53 | bt.Insert(x); 54 | break; 55 | case 4: 56 | Console.Write("Enter the key to be deleted : "); 57 | x = Convert.ToInt32(Console.ReadLine()); 58 | bt.Delete(x); 59 | break; 60 | case 5: 61 | bt.Preorder(); 62 | break; 63 | case 6: 64 | bt.Inorder(); 65 | break; 66 | case 7: 67 | bt.Postorder(); 68 | break; 69 | case 8: 70 | Console.WriteLine("Height of tree is " + bt.Height()); 71 | break; 72 | case 9: 73 | Console.WriteLine("Minimum key is " + bt.Min()); 74 | break; 75 | case 10: 76 | Console.WriteLine("Maximum key is " + bt.Max()); 77 | break; 78 | } 79 | } 80 | } 81 | } 82 | } 83 | 84 | -------------------------------------------------------------------------------- /tree/BinarySearchTreeProject/Node.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace BinarySearchTreeProject 9 | { 10 | class Node 11 | { 12 | public Node lchild; 13 | public int info; 14 | public Node rchild; 15 | 16 | public Node(int i) 17 | { 18 | info = i; 19 | lchild = null; 20 | rchild = null; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /tree/BinaryTreeProject/BinaryTree.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | using System.Collections.Generic; 8 | 9 | namespace BinaryTreeProject 10 | { 11 | class BinaryTree 12 | { 13 | private Node root; 14 | 15 | public BinaryTree() 16 | { 17 | root = null; 18 | } 19 | 20 | public void Display() 21 | { 22 | Display(root,0); 23 | Console.WriteLine(); 24 | } 25 | 26 | private void Display(Node p, int level) 27 | { 28 | int i; 29 | if ( p == null ) 30 | return; 31 | 32 | Display(p.rchild, level+1); 33 | Console.WriteLine(); 34 | 35 | for (i = 0; i < level; i++) 36 | Console.Write(" "); 37 | Console.Write(p.info); 38 | 39 | Display(p.lchild, level+1); 40 | } 41 | 42 | public void Preorder() 43 | { 44 | Preorder(root); 45 | Console.WriteLine(); 46 | } 47 | 48 | private void Preorder(Node p) 49 | { 50 | if ( p == null ) 51 | return; 52 | Console.Write(p.info + " "); 53 | Preorder(p.lchild); 54 | Preorder(p.rchild); 55 | } 56 | 57 | public void Inorder() 58 | { 59 | Inorder(root); 60 | Console.WriteLine(); 61 | } 62 | 63 | private void Inorder(Node p) 64 | { 65 | if ( p == null ) 66 | return; 67 | Inorder(p.lchild); 68 | Console.Write(p.info + " "); 69 | Inorder(p.rchild); 70 | } 71 | 72 | public void Postorder() 73 | { 74 | Postorder(root); 75 | Console.WriteLine(); 76 | } 77 | 78 | private void Postorder(Node p) 79 | { 80 | if ( p == null ) 81 | return; 82 | Postorder(p.lchild); 83 | Postorder(p.rchild); 84 | Console.Write(p.info + " "); 85 | } 86 | 87 | public void LevelOrder() 88 | { 89 | if ( root == null ) 90 | { 91 | Console.WriteLine("Tree is empty"); 92 | return; 93 | } 94 | 95 | Queue qu = new Queue(); 96 | qu.Enqueue(root); 97 | 98 | Node p; 99 | while ( qu.Count!=0 ) 100 | { 101 | p = qu.Dequeue(); 102 | Console.Write(p.info + " "); 103 | if ( p.lchild != null ) 104 | qu.Enqueue(p.lchild); 105 | if ( p.rchild != null ) 106 | qu.Enqueue(p.rchild); 107 | } 108 | Console.WriteLine(); 109 | } 110 | 111 | public int Height() 112 | { 113 | return Height(root); 114 | } 115 | 116 | private int Height(Node p) 117 | { 118 | if ( p == null ) 119 | return 0; 120 | 121 | int hL = Height(p.lchild); 122 | int hR = Height(p.rchild); 123 | 124 | if (hL > hR) 125 | return 1 + hL; 126 | else 127 | return 1 + hR; 128 | } 129 | 130 | public void CreateTree() 131 | { 132 | root = new Node('P'); 133 | root.lchild = new Node('Q'); 134 | root.rchild = new Node('R'); 135 | root.lchild.lchild = new Node('A'); 136 | root.lchild.rchild = new Node('B'); 137 | root.rchild.lchild = new Node('X'); 138 | 139 | } 140 | } 141 | } 142 | -------------------------------------------------------------------------------- /tree/BinaryTreeProject/Demo.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace BinaryTreeProject 9 | { 10 | class Demo 11 | { 12 | static void Main(string[] args) 13 | { 14 | BinaryTree bt = new BinaryTree(); 15 | 16 | bt.CreateTree(); 17 | 18 | bt.Display(); 19 | Console.WriteLine(); 20 | 21 | Console.WriteLine("Preorder : "); 22 | bt.Preorder(); 23 | Console.WriteLine(""); 24 | 25 | Console.WriteLine("Inorder : "); 26 | bt.Inorder(); 27 | Console.WriteLine(); 28 | 29 | Console.WriteLine("Postorder : "); 30 | bt.Postorder(); 31 | Console.WriteLine(); 32 | 33 | Console.WriteLine("Level order : "); 34 | bt.LevelOrder(); 35 | Console.WriteLine(); 36 | 37 | Console.WriteLine("Height of tree is " + bt.Height()); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /tree/BinaryTreeProject/Node.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace BinaryTreeProject 9 | { 10 | class Node 11 | { 12 | public Node lchild; 13 | public char info; 14 | public Node rchild; 15 | 16 | public Node(char ch) 17 | { 18 | info = ch; 19 | lchild = null; 20 | rchild = null; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /tree/BuildHeapProject/Program.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace BuildHeapProject 9 | { 10 | class Program 11 | { 12 | public static void BuildHeap_TopDown(int[] a, int n) 13 | { 14 | for (int i = 2; i <= n; i++) 15 | RestoreUp(i, a); 16 | } 17 | 18 | public static void BuildHeap_BottomUp(int[] a, int n) 19 | { 20 | for (int i = n / 2; i >= 1; i--) 21 | RestoreDown(i, a, n); 22 | } 23 | 24 | private static void RestoreUp(int i, int[] a) 25 | { 26 | int k = a[i]; 27 | int iparent = i / 2; 28 | 29 | while (a[iparent] < k) 30 | { 31 | a[i] = a[iparent]; 32 | i = iparent; 33 | iparent = i / 2; 34 | } 35 | a[i] = k; 36 | } 37 | 38 | private static void RestoreDown(int i, int[] a, int n) 39 | { 40 | int k = a[i]; 41 | int lchild = 2 * i, rchild = lchild + 1; 42 | 43 | while (rchild <= n) 44 | { 45 | if (k >= a[lchild] && k >= a[rchild]) 46 | { 47 | a[i] = k; 48 | return; 49 | } 50 | else if (a[lchild] > a[rchild]) 51 | { 52 | a[i] = a[lchild]; 53 | i = lchild; 54 | } 55 | else 56 | { 57 | a[i] = a[rchild]; 58 | i = rchild; 59 | } 60 | lchild = 2 * i; 61 | rchild = lchild + 1; 62 | } 63 | 64 | /*If number of nodes is even*/ 65 | if (lchild == n && k < a[lchild]) 66 | { 67 | a[i] = a[lchild]; 68 | i = lchild; 69 | } 70 | a[i] = k; 71 | } 72 | 73 | static void Main(string[] args) 74 | { 75 | int[] a1 = {99999,1,4,5,7,9,10}; 76 | int n1 = a1.Length-1; 77 | 78 | BuildHeap_BottomUp(a1,n1); 79 | 80 | for ( int i = 1; i <= n1; i++ ) 81 | Console.Write( a1[i] + " "); 82 | Console.WriteLine(); 83 | 84 | 85 | int[] a2 = {99999,1,4,5,7,9,10}; 86 | int n2 = a2.Length-1; 87 | 88 | BuildHeap_TopDown(a2,n2); 89 | 90 | for (int i = 1; i <= n2; i++ ) 91 | Console.Write(a2[i] + " "); 92 | Console.WriteLine(); 93 | int x = Convert.ToInt32(Console.ReadLine());//to stop window 94 | } 95 | 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /tree/HeapProject/Demo.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace HeapProject 9 | { 10 | class Demo 11 | { 12 | static void Main(string[] args) 13 | { 14 | Heap h = new Heap(20); 15 | 16 | int choice,value; 17 | 18 | while(true) 19 | { 20 | Console.WriteLine("1.Insert"); 21 | Console.WriteLine("2.Delete root"); 22 | Console.WriteLine("3.Display"); 23 | Console.WriteLine("4.Exit"); 24 | Console.Write("Enter your choice : "); 25 | choice = Convert.ToInt32(Console.ReadLine()); 26 | 27 | if(choice==4) 28 | break; 29 | 30 | switch(choice) 31 | { 32 | case 1: 33 | Console.Write("Enter the value to be inserted : "); 34 | value = Convert.ToInt32(Console.ReadLine()); 35 | h.Insert(value); 36 | break; 37 | case 2: 38 | Console.WriteLine("Maximum value is " + h.DeleteRoot()); 39 | break; 40 | case 3: 41 | h.Display(); 42 | break; 43 | default: 44 | Console.WriteLine("Wrong choice"); 45 | break; 46 | } 47 | } 48 | } 49 | } 50 | } 51 | 52 | -------------------------------------------------------------------------------- /tree/HeapProject/Heap.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | using System; 7 | 8 | namespace HeapProject 9 | { 10 | class Heap 11 | { 12 | private int[] a; 13 | private int n; 14 | 15 | public Heap() 16 | { 17 | a = new int[10]; 18 | n = 0; 19 | a[0] = 99999; 20 | } 21 | 22 | public Heap(int maxSize) 23 | { 24 | a = new int[maxSize]; 25 | n = 0; 26 | a[0] = 99999; 27 | } 28 | 29 | public void Insert(int value) 30 | { 31 | n++; 32 | a[n] = value; 33 | RestoreUp(n); 34 | } 35 | 36 | private void RestoreUp(int i) 37 | { 38 | int k = a[i]; 39 | int iparent = i / 2; 40 | 41 | while (a[iparent] < k) /*No sentinel - while(iparent>=1 && a[iparent]= a[lchild] && k >= a[rchild]) 70 | { 71 | a[i] = k; 72 | return; 73 | } 74 | else if (a[lchild] > a[rchild]) 75 | { 76 | a[i] = a[lchild]; 77 | i = lchild; 78 | } 79 | else 80 | { 81 | a[i] = a[rchild]; 82 | i = rchild; 83 | } 84 | lchild = 2 * i; 85 | rchild = lchild + 1; 86 | } 87 | 88 | /*If number of nodes is even*/ 89 | if (lchild == n && k < a[lchild]) 90 | { 91 | a[i] = a[lchild]; 92 | i = lchild; 93 | } 94 | a[i] = k; 95 | } 96 | 97 | public void Display() 98 | { 99 | if (n == 0) 100 | { 101 | Console.WriteLine("Heap is empty"); 102 | return; 103 | } 104 | Console.WriteLine("Heap size = " + n); 105 | for (int i = 1; i <= n; i++ ) 106 | Console.Write(a[i] + " "); 107 | Console.WriteLine(); 108 | } 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /tree/README.md: -------------------------------------------------------------------------------- 1 | # Data Structures and Algorithms in C# 2 | 3 | This [“Data Structures and Algorithms in C#”](https://www.udemy.com/course/data-structures-and-algorithms-in-csharp/?couponCode=GITHUBSTUDENT ) repository is for the students of my online course available on Udemy. It contains the source code of all the programs used in the course. 4 | 5 | ### About the Course 6 | * Thoroughly detailed course with complete working programs 7 | * Contains lots of animations to help you visualize the concepts 8 | * Includes over 100 Quiz questions 9 | * Builds a solid foundation in Data Structures and Algorithms 10 | * Prepares you for coding interviews 11 | * Lifetime Access 12 | 13 | ### Courses by [Deepali Srivastava](https://www.udemy.com/user/deepalisrivastava/) 14 | 15 | [![data-structures- and-algorithms-in-csharp](https://user-images.githubusercontent.com/98641125/153196407-99441e67-24a7-4fa0-aaea-78cb39743282.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-csharp/?couponCode=GITHUBSTUDENT) 16 | [![data-structures- and-algorithms-in-csharp-2](https://user-images.githubusercontent.com/98641125/153196486-9ce09c97-8724-4492-b5d6-4bb4aeb1d8bd.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-csharp-2/?couponCode=GITHUBSTUDENT) 17 | [![data-structures- and-algorithms-in-c-plus-plus](https://user-images.githubusercontent.com/98641125/153196522-2412c993-1055-4322-8487-4133537566c9.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-c-plus-plus/?couponCode=GITHUBSTUDENT) 18 | [![python-programming-in-depth](https://user-images.githubusercontent.com/98641125/153196166-45ef8461-adb1-4f9f-b9ee-e482a5ad54a7.png)]( https://www.udemy.com/course/python-programming-in-depth/?couponCode=GITHUBSTUDENT) 19 | [![linux-commands](https://user-images.githubusercontent.com/98641125/153196567-96b3396c-8ee3-4233-b8fc-66c6b3bd830c.png)]( https://www.udemy.com/course/linux-commands/?couponCode=GITHUBSTUDENT) 20 | [![data-structures-and-algorithms-in-c](https://user-images.githubusercontent.com/98641125/153195841-209d2615-ed5f-4007-ae54-539ac3c1538a.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-c/?couponCode=GITHUBSTUDENT) 21 | [![data-structures- and-algorithms-in-java](https://user-images.githubusercontent.com/98641125/153196280-c2028f4b-d27b-432d-ad5a-9b04be2a3717.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-java/?couponCode=GITHUBSTUDENT) 22 | [![data-structures- and-algorithms-in-java-2](https://user-images.githubusercontent.com/98641125/153196347-23003dc0-55b0-4315-8e52-425c51b2b5c4.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-java-2/?couponCode=GITHUBSTUDENT) 23 | 24 | ## Copyright 25 | © Copyright Deepali Srivastava : All rights reserved. 26 | Not to be used for commercial purposes. 27 | --------------------------------------------------------------------------------