├── Dynamic-Memory-Allocation ├── Tuple Python │ ├── tuple1.py │ ├── tuple2.py │ ├── tuple3.py │ ├── tuple5.py │ └── tuple4.py ├── Dictionary Python │ ├── dictionary5.py │ ├── dictionary1.py │ ├── dictionary4.py │ ├── dictionary3.py │ └── dictionary2.py ├── Python programs │ ├── delDataArr.py │ ├── findMin.py │ ├── findPos.py │ ├── max2DArr.py │ ├── min2DArr.py │ ├── modifySize.py │ ├── searchEle.py │ ├── search2DArr.py │ ├── mergeUnsorted.py │ └── sortedMerge.py ├── Set Python │ ├── set4.py │ ├── set2.py │ ├── set3.py │ ├── set1.py │ └── set5.py ├── List Python │ ├── concatenateList.py │ ├── removeItem.py │ ├── reverseList.py │ ├── sqList.py │ └── addItem.py └── C programs │ ├── findMin.c │ ├── modifySize.c │ ├── max2DArr.c │ ├── min2DArr.c │ ├── searchEle.c │ ├── delDataArr.c │ ├── search2DArr.c │ ├── findPos.c │ ├── sortedMerge.c │ └── mergeUnsorted.c ├── Questions ├── README9.md ├── README8.md ├── README10.md ├── README5.md ├── README2.md ├── README6.md ├── Assignment-10@DSALAB.txt ├── README7.md ├── Assignment-7@DSALAB.txt ├── Assignment-9@DSALAB.txt ├── README4.md ├── Assignment-6@DSALAB.txt ├── README3.md ├── Assignment-8@DSALAB.txt ├── Assignment-1@DSALAB.txt ├── Assignment-4@DSALAB.txt ├── Assignment-3@DSALAB.txt ├── Assignment-2@DSALAB.txt └── README1.md ├── Graph ├── Python Programs │ ├── adjacencyMatrix.py │ └── adjacencyList.py └── C Programs │ ├── adjacencyMatrix.c │ ├── prim.c │ ├── DFS.c │ └── kruskal.c ├── Linked List ├── Algorithms 📝 │ ├── ReverseAlgo.txt │ ├── Sorted Merge Algo.txt │ ├── DeleteAlgo.txt │ ├── InsertAlgo.txt │ └── DLL Algo.txt ├── C Programs ☠️ │ ├── Singly │ │ ├── reverse_SLL.c │ │ ├── swapNodes.c │ │ ├── singlylinkedlist.h │ │ ├── leftShift_SLL.c │ │ ├── countNodes.c │ │ ├── polynomialLL.h │ │ ├── detectCycle_SLL.c │ │ ├── sortedMerge_SLL.c │ │ ├── polyMultiplication_LL.c │ │ ├── polynomial_LL.c │ │ ├── polyAdd_SLL.c │ │ └── oddEvenNode_LL.c │ └── Doubly │ │ └── sortedMerge_DLL.c └── Python Programs 🐍 │ └── Singly │ ├── countNodes.py │ ├── reverse_SLL.py │ ├── polynomial_SL.py │ ├── swapNodes_SLL.py │ ├── oddEvenNode.py │ ├── sortedMerge_SLL.py │ ├── detectCycle_SLL.py │ ├── delete_SL.py │ ├── insert_SL.py │ └── polyAdd_SLL.py ├── Searching & Sorting ├── Python Programs │ ├── bubbleSort.py │ ├── linearSearch.py │ ├── insertionSort.py │ ├── selectionSort.py │ ├── binarySearch.py │ └── mergeSort.py └── C Programs │ ├── linearSearch.c │ ├── insertionSort.c │ ├── bubbleSort.c │ ├── selectionSort.c │ ├── binarySearch.c │ ├── shellSort.c │ ├── interpolationSearch.c │ ├── mergeSort.c │ ├── quickSort.c │ ├── heapSort.c │ ├── radixSort.c │ └── hashing.c ├── Stack ├── Python Programs │ ├── revString.py │ ├── factorial.py │ ├── parenthesis.py │ ├── stack.py │ ├── color.py │ └── towerOfHanoi.py └── C programs │ ├── factorial.c │ ├── revString.c │ ├── parenthesis.c │ ├── stack_SLL.c │ ├── stack_Arr.c │ ├── HTMLtag.c │ ├── evaluatePostfix.c │ ├── pallindrome.c │ ├── infixToPostfix.c │ ├── towerOfHanoi.c │ └── doubleStack.c └── Queue ├── Python Programs ├── queue.py └── circularQueue.py └── C Programs ├── priorityQueue.c ├── queueUsingStack.c ├── queue_SLL.c ├── queue_Arr.c └── circularQueue.c /Dynamic-Memory-Allocation/Tuple Python/tuple1.py: -------------------------------------------------------------------------------- 1 | # Access value 20 from the tuple. 2 | 3 | num = (10, 20, 30, 40) 4 | val = num[1] 5 | print(val) 6 | 7 | # OUTPUT 8 | # 20 9 | -------------------------------------------------------------------------------- /Dynamic-Memory-Allocation/Tuple Python/tuple2.py: -------------------------------------------------------------------------------- 1 | # Unpack the tuple into 4 variables. 2 | 3 | num = (10, 20, 30, 40) 4 | a, b, c, d = num 5 | print(a, b, c, d) 6 | 7 | # OUTPUT 8 | # 10 20 30 40 9 | -------------------------------------------------------------------------------- /Dynamic-Memory-Allocation/Dictionary Python/dictionary5.py: -------------------------------------------------------------------------------- 1 | # Check if a value exists in a dictionary. 2 | 3 | dict1 = {'a': 1, 'b': 2, 'c': 3, 'd': 4} 4 | print(3 in dict1.values()) 5 | 6 | # OUTPUT 7 | # True 8 | -------------------------------------------------------------------------------- /Dynamic-Memory-Allocation/Python programs/delDataArr.py: -------------------------------------------------------------------------------- 1 | # 9. Write a python program to delete a range of data from a dynamic array. 2 | 3 | list1 = [1, 2, 3, 4, 5, 6, 7, 8] 4 | del list1[2:6] 5 | print(list1) 6 | 7 | # OUTPUT 8 | # [1, 2, 7, 8] 9 | -------------------------------------------------------------------------------- /Dynamic-Memory-Allocation/Set Python/set4.py: -------------------------------------------------------------------------------- 1 | # Return a set of elements present in Set A or B, but not both. 2 | 3 | num = {1, 2, 3, 4, 5, 6} 4 | even = {2, 4, 6} 5 | num.difference_update(even) 6 | print(num) 7 | 8 | # OUTPUT 9 | # {1, 3, 5} 10 | -------------------------------------------------------------------------------- /Dynamic-Memory-Allocation/Dictionary Python/dictionary1.py: -------------------------------------------------------------------------------- 1 | # Convert two lists into a dictionary. 2 | 3 | l = ["blue", "red", "yellow"] 4 | n = [3, 1, 6] 5 | d = {l[i]: n[i] for i in range(len(l))} 6 | print(d) 7 | 8 | # OUTPUT 9 | # {'blue': 3, 'red': 1, 'yellow': 6} 10 | -------------------------------------------------------------------------------- /Dynamic-Memory-Allocation/Dictionary Python/dictionary4.py: -------------------------------------------------------------------------------- 1 | # Delete a list of keys from a dictionary. 2 | 3 | dict1 = {'a': 1, 'b': 2, 'c': 3, 'd': 4} 4 | keys = ['a', 'c'] 5 | for k in keys: 6 | dict1.pop(k) 7 | print(dict1) 8 | 9 | # OUTPUT 10 | # {'b': 2, 'd': 4} 11 | -------------------------------------------------------------------------------- /Dynamic-Memory-Allocation/Set Python/set2.py: -------------------------------------------------------------------------------- 1 | # Get Only unique items from two sets. 2 | 3 | set1 = {1, 3, 7} 4 | set2 = {3, 5, 8} 5 | diff = set1.difference(set2) 6 | diff2 = set2.difference(set1) 7 | diff.update(diff2) 8 | print(diff) 9 | 10 | # OUTPUT 11 | # {8, 1, 5, 7} 12 | -------------------------------------------------------------------------------- /Dynamic-Memory-Allocation/Dictionary Python/dictionary3.py: -------------------------------------------------------------------------------- 1 | # Create a dictionary by extracting the keys from a given dictionary. 2 | 3 | dict1 = {'a': 1, 'b': 2, 'c': 3, 'd': 4} 4 | keys = ['a', 'c'] 5 | dict2 = {k: dict1[k] for k in keys} 6 | print(dict2) 7 | 8 | # OUTPUT 9 | # {'a': 1, 'c': 3} 10 | -------------------------------------------------------------------------------- /Questions/README9.md: -------------------------------------------------------------------------------- 1 |

Assignment 9

2 | 3 | > 💠 Binary Search Tree
👉🏼 [Question](/Questions/Assignment-9%40DSALAB.txt) 4 | 5 | --- 6 | 7 | 1. [Binary Search Tree (C)](/Binary%20Tree/C%20Programs/BST.c) 8 | 1. [Binary Search Tree (Py)](/Binary%20Tree/Python%20Programs/BST.py) 9 | -------------------------------------------------------------------------------- /Graph/Python Programs/adjacencyMatrix.py: -------------------------------------------------------------------------------- 1 | # Write a Python program to implement Graph using Adjacency Matrix along with the following function: 2 | # a. To count number of vertices and edges present in a graph. 3 | # b. To find the adjacent vertices of a given vertex. 4 | # c. To search a node in a given graph. 5 | -------------------------------------------------------------------------------- /Dynamic-Memory-Allocation/Set Python/set3.py: -------------------------------------------------------------------------------- 1 | # Remove items from the set at once. 2 | 3 | set1 = {6, 4, 2, 7, 9} 4 | print("Original Set:", set1) 5 | rem = [2, 4, 8] 6 | res = set1-set(rem) 7 | print("Updated Set:", res) 8 | 9 | # OUTPUT 10 | # Original Set: {2, 4, 6, 7, 9} 11 | # Updated Set: {9, 6, 7} 12 | -------------------------------------------------------------------------------- /Dynamic-Memory-Allocation/Tuple Python/tuple3.py: -------------------------------------------------------------------------------- 1 | # Copy specific elements from one tuple to a new tuple. 2 | 3 | num = (1, 2, 3, 4, 5, 6, 7, 8, 9) 4 | n = list(num) 5 | o = [] 6 | for i in range(0, 9, 2): 7 | o.append(n[i]) 8 | odd = tuple(o) 9 | print(odd) 10 | 11 | # OUTPUT 12 | # (1, 3, 5, 7, 9) 13 | -------------------------------------------------------------------------------- /Dynamic-Memory-Allocation/Set Python/set1.py: -------------------------------------------------------------------------------- 1 | # Add a list of elements to a set. 2 | 3 | num = {1, 3, 5, 7, 9} 4 | e = [2, 4, 6, 8] 5 | 6 | print("Before adding List:", num) 7 | num.update(e) 8 | print("Updated Set:", num) 9 | 10 | 11 | # OUTPUT 12 | # Before adding List: {1, 3, 5, 7, 9} 13 | # Updated Set: {1, 2, 3, 4, 5, 6, 7, 8, 9} 14 | -------------------------------------------------------------------------------- /Dynamic-Memory-Allocation/Dictionary Python/dictionary2.py: -------------------------------------------------------------------------------- 1 | # Merge two Python dictionaries into one. 2 | 3 | def merge(dict1, dict2): 4 | return (dict2.update(dict1)) 5 | 6 | 7 | dict1 = {'a': 1, 'b': 4} 8 | dict2 = {'p': 3, 'q': 7} 9 | merge(dict1, dict2) 10 | print(dict2) 11 | 12 | # OUTPUT 13 | # {'p': 3, 'q': 7, 'a': 1, 'b': 4} 14 | -------------------------------------------------------------------------------- /Dynamic-Memory-Allocation/Set Python/set5.py: -------------------------------------------------------------------------------- 1 | # Check if two sets have any elements in common. If yes, display the common elements. 2 | 3 | set1 = {1, 2, 3, 4, 5, 6} 4 | set2 = {2, 4, 6} 5 | if set1.isdisjoint(set2): 6 | print("No elements in common") 7 | else: 8 | set3 = set1.intersection(set2) 9 | print(set3) 10 | 11 | # OUTPUT 12 | # {2, 4, 6} 13 | -------------------------------------------------------------------------------- /Dynamic-Memory-Allocation/Python programs/findMin.py: -------------------------------------------------------------------------------- 1 | # 3. Write a python program to find the minimum element 2 | # in an array using dynamic memory allocation. 3 | 4 | list = [3, 8, 4, 2, 1, 7, 9] 5 | min = list[0] 6 | for i in list: 7 | if i < min: 8 | min = i 9 | print(min, "is minimum in the list") 10 | 11 | # OUTPUT 12 | # 1 is minimum in the list 13 | -------------------------------------------------------------------------------- /Dynamic-Memory-Allocation/Tuple Python/tuple5.py: -------------------------------------------------------------------------------- 1 | # Check if all items in the tuple are the same. 2 | 3 | n = (1, 1, 1, 1) 4 | c = n[0] 5 | flag = 1 6 | for i in range(4): 7 | if(c != n[i]): 8 | flag = 0 9 | if(flag == 0): 10 | print("All values are not same") 11 | else: 12 | print("All values are same") 13 | 14 | # OUTPUT 15 | # All values are same 16 | -------------------------------------------------------------------------------- /Dynamic-Memory-Allocation/Python programs/findPos.py: -------------------------------------------------------------------------------- 1 | # 2. Write a python program to find the 3rd/user defined position based 2 | # maximum element in an array using dynamic memory allocation. 3 | 4 | list = [3, 8, 4, 2, 5, 7, 9] 5 | max = list[0] 6 | for i in list: 7 | if i > max: 8 | max = i 9 | print(max, "is largest in the list") 10 | 11 | # OUTPUT 12 | # 9 is largest in the list 13 | -------------------------------------------------------------------------------- /Linked List/Algorithms 📝/ReverseAlgo.txt: -------------------------------------------------------------------------------- 1 | *** Reverse a Linked List *** 2 | 3 | Step 1: INITIALIZE CURR = HEAD 4 | Step 2: INITIALIZE PREV = NULL 5 | Step 3: Repeat Steps 4, 5, 6 and 7 while CURR != NULL 6 | Step 4: SET TEMP = CURR -> NEXT 7 | Step 5: SET CURR -> NEXT = PREV 8 | Step 6: SET PREV = CURR 9 | Step 7: SET CURR = TEMP 10 | [END OF LOOP] 11 | Step 8: RETURN PREV As HEAD 12 | -------------------------------------------------------------------------------- /Dynamic-Memory-Allocation/Tuple Python/tuple4.py: -------------------------------------------------------------------------------- 1 | # Counts the number of occurrences of item ‘x’ from a tuple. 2 | 3 | t = (10, 20, 10, 30, 40, 50, 20, 10, 10, 30, 30) 4 | n = len(t) 5 | count = 0 6 | c = int(input("Enter the number to be checked: ")) 7 | for i in range(n): 8 | if(t[i] == c): 9 | count += 1 10 | print("Frequency: "+str(count)) 11 | 12 | # OUTPUT 13 | # Enter the number to be checked: 10 14 | # Frequency: 4 15 | -------------------------------------------------------------------------------- /Graph/C Programs/adjacencyMatrix.c: -------------------------------------------------------------------------------- 1 | // Write a C program to implement Graph using Adjacency Matrix along with the following function: 2 | // a. To count number of vertices and edges present in a graph. 3 | // b. To find the adjacent vertices of a given vertex. 4 | // c. To search a node in a given graph. 5 | 6 | /* 7 | 8 | 5 9 | / \ 10 | / \ 11 | 1 --- 2 12 | | \ | 13 | | \ | 14 | | \ | 15 | 3 --- 4 16 | 17 | */ -------------------------------------------------------------------------------- /Dynamic-Memory-Allocation/Python programs/max2DArr.py: -------------------------------------------------------------------------------- 1 | # 5. Write a python program to find the maximum element 2 | # in a 2D-array using dynamic memory allocation. 3 | 4 | matrix = [[1, 2, 3], [7, 8, 9], [4, 5, 6]] 5 | max = matrix[0][0] 6 | for i in range(len(matrix)): 7 | for j in range(len(matrix[i])): 8 | if(matrix[i][j] > max): 9 | max = matrix[i][j] 10 | print("Maximum Element:", max) 11 | 12 | # OUTPUT 13 | # Maximum Element: 9 14 | -------------------------------------------------------------------------------- /Dynamic-Memory-Allocation/Python programs/min2DArr.py: -------------------------------------------------------------------------------- 1 | # 6. Write a python program to find the minimum element 2 | # in a 2D-array using dynamic memory allocation. 3 | 4 | matrix = [[7, 8, 9], [1, 2, 3], [4, 5, 6]] 5 | min = matrix[0][0] 6 | for i in range(len(matrix)): 7 | for j in range(len(matrix[i])): 8 | if(matrix[i][j] < min): 9 | min = matrix[i][j] 10 | print("Minimum Element:", min) 11 | 12 | # OUTPUT 13 | # Minimum Element: 1 14 | -------------------------------------------------------------------------------- /Questions/README8.md: -------------------------------------------------------------------------------- 1 |

Assignment 8

2 | 3 | > 💠 Binary Tree
👉🏼 [Question](/Questions/Assignment-8%40DSALAB.txt) 4 | 5 | --- 6 | 7 | ## C Programs 👽 8 | 9 | 1. [Binary Tree using Array](/Binary%20Tree/C%20Programs/binaryTreeArr.c) 10 | 1. [Binary Tree using LL](/Binary%20Tree/C%20Programs/binaryTreeLL.c) 11 | 12 | --- 13 | 14 | ## Python Programs 🤖 15 | 16 | 1. [Binary Tree using LL](/Binary%20Tree/Python%20Programs/binaryTree.py) 17 | -------------------------------------------------------------------------------- /Searching & Sorting/Python Programs/bubbleSort.py: -------------------------------------------------------------------------------- 1 | # Write a program to implement Bubble Sort. 2 | 3 | def bubbleSort(arr): 4 | n = len(arr) 5 | for i in range(n): 6 | for j in range(0, n-i-1): 7 | if arr[j] > arr[j+1]: 8 | (arr[j], arr[j+1]) = (arr[j+1], arr[j]) 9 | 10 | 11 | arr = [6, 2, 5, 7, 1, 4, 3] 12 | bubbleSort(arr) 13 | print("Sorted Array is:") 14 | print(arr) 15 | 16 | # OUTPUT 17 | # Sorted Array is: 18 | # [1, 2, 3, 4, 5, 6, 7] 19 | -------------------------------------------------------------------------------- /Searching & Sorting/Python Programs/linearSearch.py: -------------------------------------------------------------------------------- 1 | # Write a program to implement linear search. 2 | 3 | def linearSearch(arr, key): 4 | for i in range(len(arr)): 5 | if arr[i] == key: 6 | return i 7 | return -1 8 | 9 | 10 | arr = [3, 9, 2, 5, 4] 11 | key = 5 12 | idx = linearSearch(arr, key) 13 | if idx == -1: 14 | print("Item Not Found") 15 | else: 16 | print(str(key), "found at index:", str(idx)) 17 | 18 | 19 | # OUTPUT 20 | # 5 found at index: 3 21 | -------------------------------------------------------------------------------- /Dynamic-Memory-Allocation/Python programs/modifySize.py: -------------------------------------------------------------------------------- 1 | # 10. Write a python program to modify the size 2 | # of an array and utilize that during run time. 3 | 4 | arr = [1, 2, 3, 4] 5 | n = int(input("Enter number of extra elements: ")) 6 | print("Enter the elements: ") 7 | for i in range(n): 8 | ele = int(input()) 9 | arr.append(ele) 10 | print(arr) 11 | 12 | # OUTPUT 13 | # Enter number of extra elements: 3 14 | # Enter the elements: 15 | # 9 16 | # 8 17 | # 7 18 | # [1, 2, 3, 4, 9, 8, 7] 19 | -------------------------------------------------------------------------------- /Dynamic-Memory-Allocation/Python programs/searchEle.py: -------------------------------------------------------------------------------- 1 | # 1. Write a python program to search an element 2 | # in an Array using dynamic memory allocation. 3 | 4 | list1 = [1, 2, 3, 4, 5] 5 | key = int(input("Enter the key: ")) 6 | flag = 0 7 | for i in range(len(list1)): 8 | if list1[i] == key: 9 | flag = 1 10 | break 11 | if flag == 1: 12 | print(key, "found in index:", i) 13 | else: 14 | print("Not in list") 15 | 16 | # OUTPUT 17 | # Enter the key: 4 18 | # 4 found in index: 3 19 | -------------------------------------------------------------------------------- /Dynamic-Memory-Allocation/List Python/concatenateList.py: -------------------------------------------------------------------------------- 1 | # Concatenate two lists index-wise. 2 | 3 | list1 = ["My ", "i", "Indranil "] 4 | list2 = ["name", "s", "Saha"] 5 | 6 | print("Original List1 is: "+str(list1)) 7 | print("Original List2 is: "+str(list2)) 8 | 9 | res = [i+j for i, j in zip(list1, list2)] 10 | print("Concated List: "+str(res)) 11 | 12 | # OUTPUT 13 | # Original List1 is : ['My ', 'i', 'Indranil '] 14 | # Original List2 is : ['name', 's', 'Saha'] 15 | # Concated List: ['My name', 'is', 'Indranil Saha'] 16 | -------------------------------------------------------------------------------- /Searching & Sorting/Python Programs/insertionSort.py: -------------------------------------------------------------------------------- 1 | # Write a program to implement Insertion Sort. 2 | 3 | def insertionSort(arr): 4 | size = len(arr) 5 | for i in range(1, size): 6 | curr = arr[i] 7 | j = i-1 8 | while j >= 0 and curr < arr[j]: 9 | arr[j+1] = arr[j] 10 | j -= 1 11 | arr[j+1] = curr 12 | 13 | 14 | arr = [6, 2, 5, 7, 1, 4, 3] 15 | insertionSort(arr) 16 | print("Sorted Array is:") 17 | print(arr) 18 | 19 | # OUTPUT 20 | # Sorted Array is: 21 | # [1, 2, 3, 4, 5, 6, 7] 22 | -------------------------------------------------------------------------------- /Dynamic-Memory-Allocation/List Python/removeItem.py: -------------------------------------------------------------------------------- 1 | # Remove all ocurrences of a specific item from a list. 2 | 3 | def remove_items(list1, item): 4 | res = [i for i in list1 if i != item] 5 | return res 6 | 7 | 8 | if __name__ == "__main__": 9 | list1 = [1, 2, 5, 1, 1, 0, 7, 1] 10 | item = 1 11 | print("Original list is: "+str(list1)) 12 | res = remove_items(list1, item) 13 | print("After Removing Elements: "+str(res)) 14 | 15 | # OUTPUT 16 | # Original list is : [1, 2, 5, 1, 1, 0, 7, 1] 17 | # After Removing Elements: [2, 5, 0, 7] 18 | -------------------------------------------------------------------------------- /Dynamic-Memory-Allocation/List Python/reverseList.py: -------------------------------------------------------------------------------- 1 | # Reverse a list in Python. 2 | 3 | list1 = [] 4 | n = int(input("Enter number of elements in list: ")) 5 | for i in range(0, n): 6 | ele = int(input("Enter Numbers["+str(i)+"]: ")) 7 | list1.append(ele) 8 | 9 | list2 = [] 10 | for i in list1: 11 | list2.insert(0, i) 12 | print("Reversed List is: "+str(list2)) 13 | 14 | # OUTPUT 15 | # Enter number of elements in list: 4 16 | # Enter List[0]: 7 17 | # Enter List[1]: 8 18 | # Enter List[2]: 9 19 | # Enter List[3]: 10 20 | # Reversed List is: [10, 9, 8, 7] 21 | -------------------------------------------------------------------------------- /Searching & Sorting/Python Programs/selectionSort.py: -------------------------------------------------------------------------------- 1 | # Write a program to implement Selection Sort. 2 | 3 | def selectionSort(arr): 4 | size = len(arr) 5 | for i in range(size-1): 6 | smallest = i 7 | for j in range(i + 1, size): 8 | if arr[j] < arr[smallest]: 9 | smallest = j 10 | (arr[i], arr[smallest]) = (arr[smallest], arr[i]) 11 | 12 | 13 | arr = [6, 2, 5, 7, 1, 4, 3] 14 | selectionSort(arr) 15 | print("Sorted Array is:") 16 | print(arr) 17 | 18 | 19 | # OUTPUT 20 | # Sorted Array is: 21 | # [1, 2, 3, 4, 5, 6, 7] 22 | -------------------------------------------------------------------------------- /Dynamic-Memory-Allocation/List Python/sqList.py: -------------------------------------------------------------------------------- 1 | # Turn every item of a list into its square 2 | 3 | numbers = [] 4 | n = int(input("Enter number of elements in list: ")) 5 | for i in range(0, n): 6 | ele = int(input("Enter Numbers["+str(i)+"]: ")) 7 | numbers.append(ele) 8 | 9 | sqnums = [number**2 for number in numbers] 10 | print("Squared Number List: "+str(sqnums)) 11 | 12 | # OUTPUT 13 | # Enter number of elements in list: 4 14 | # Enter Numbers[0]: -5 15 | # Enter Numbers[1]: 2 16 | # Enter Numbers[2]: 0 17 | # Enter Numbers[3]: 6 18 | # Squared Number List: [25, 4, 0, 36] 19 | -------------------------------------------------------------------------------- /Dynamic-Memory-Allocation/Python programs/search2DArr.py: -------------------------------------------------------------------------------- 1 | # 4. Write a python program to search an element in a 2 | # 2D-Array using dynamic memory allocation. 3 | 4 | def search(arr, target): 5 | for i in range(len(arr)): 6 | for j in range(len(arr[i])): 7 | if(arr[i][j] == target): 8 | return [i, j] 9 | return [-1, -1] 10 | 11 | 12 | matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] 13 | target = 7 14 | ans = search(matrix, target) 15 | print(f"Element found at index: [{ans[0]} {ans[1]}]") 16 | 17 | 18 | # OUTPUT 19 | # Element found at index: [2 0] 20 | -------------------------------------------------------------------------------- /Stack/Python Programs/revString.py: -------------------------------------------------------------------------------- 1 | # Write a Python program to reverse a string using stack (using array). (Assignment 2) 2 | # Write a program to reverse a string using stack. (Assignment 6) 3 | 4 | def reverse_string(str): 5 | print("Original String: "+str) 6 | print("Reversed String: ", end='') 7 | stack = [] 8 | for i in str: 9 | stack.append(i) 10 | while len(stack) > 0: 11 | print(stack.pop(), end="") 12 | print() 13 | 14 | 15 | reverse_string("hello world") 16 | 17 | 18 | # OUTPUT 19 | # Original String: hello world 20 | # Reversed String: dlrow olleh 21 | -------------------------------------------------------------------------------- /Stack/Python Programs/factorial.py: -------------------------------------------------------------------------------- 1 | # Write a program to calculate factorial with the help of stack. 2 | 3 | stack1 = [] 4 | 5 | 6 | def push(item): 7 | stack1.append(item) 8 | return stack1 9 | 10 | 11 | def pop(): 12 | if len(stack1) == 0: 13 | return "Stack is empty" 14 | else: 15 | return stack1.pop() 16 | 17 | 18 | if __name__ == '__main__': 19 | n = int(input("Enter a Number: ")) 20 | push(1) 21 | for i in range(2, n+1): 22 | temp = pop() 23 | push(temp*i) 24 | print("Factorial: "+str(pop())) 25 | 26 | 27 | # OUTPUT 28 | # Enter a Number: 6 29 | # Factorial: 720 30 | -------------------------------------------------------------------------------- /Dynamic-Memory-Allocation/Python programs/mergeUnsorted.py: -------------------------------------------------------------------------------- 1 | # 8. Write a python program to merge two 2 | # unsorted dynamic array in sorted order. 3 | 4 | 5 | def merge(arr1, arr2, n1, n2): 6 | i = j = k = 0 7 | arr3 = [None]*(n1+n2) 8 | while i < n1: 9 | arr3[k] = arr1[i] 10 | i += 1 11 | k += 1 12 | while j < n2: 13 | arr3[k] = arr2[j] 14 | j += 1 15 | k += 1 16 | arr3.sort() 17 | return arr3 18 | 19 | 20 | arr1 = [2, 7, 3, 1] 21 | arr2 = [5, 8, 4, 9, 6] 22 | res = merge(arr1, arr2, len(arr1), len(arr2)) 23 | print(res) 24 | 25 | # OUTPUT 26 | # [1, 2, 3, 4, 5, 6, 7, 8, 9] 27 | -------------------------------------------------------------------------------- /Searching & Sorting/Python Programs/binarySearch.py: -------------------------------------------------------------------------------- 1 | # Write a program to implement binary search. 2 | 3 | def binarySearch(arr, key): 4 | low = 0 5 | high = len(arr) - 1 6 | mid = 0 7 | while low <= high: 8 | mid = (high + low) // 2 9 | if arr[mid] < key: 10 | low = mid + 1 11 | elif arr[mid] > key: 12 | high = mid - 1 13 | else: 14 | return mid 15 | return -1 16 | 17 | 18 | arr = [1, 2, 3, 4, 5] 19 | key = 4 20 | idx = binarySearch(arr, key) 21 | if idx == -1: 22 | print("Item Not Found") 23 | else: 24 | print(str(key), "found at index:", str(idx)) 25 | 26 | # OUTPUT 27 | # 4 found at index: 3 28 | -------------------------------------------------------------------------------- /Questions/README10.md: -------------------------------------------------------------------------------- 1 |

Assignment 10

2 | 3 | > 💠 Graph
👉🏼 [Question](/Questions/Assignment-10%40DSALAB.txt) 4 | 5 | --- 6 | 7 | ## C Programs 👽 8 | 9 | 1. [Graph using Adjacency Matrix](/Graph/C%20Programs/adjacencyMatrix.c) 10 | 1. [Graph using Adjacency List](/Graph/C%20Programs/adjacencyList.c) 11 | 1. [DFS](/Graph/C%20Programs/DFS.c) (**EXTRA**) 12 | 1. [BFS](/Graph/C%20Programs/BFS.c) (**EXTRA**) 13 | 1. [Kruskal's Algo](/Graph/C%20Programs/kruskal.c) (**EXTRA**) 14 | 1. [Prim's Algo](/Graph/C%20Programs/prim.c) (**EXTRA**) 15 | 16 | --- 17 | 18 | ## Python Programs 🤖 19 | 20 | 1. [Graph using Adjacency Matrix](/Graph/Python%20Programs/adjacencyMatrix.py) 21 | 1. [Graph using Adjacency List](/Graph/Python%20Programs/adjacencyList.py) 22 | -------------------------------------------------------------------------------- /Questions/README5.md: -------------------------------------------------------------------------------- 1 |

Assignment 5

2 | 3 | > 💠 Doubly Linked List
💠 Doubly Circular Linked List
👉🏼 [Question](/Questions/Assignment-5%40DSALAB.txt) 4 | 5 | --- 6 | 7 | ## C Programs 🐷 8 | 9 | 1. [Doubly_LL](/Linked%20List/C%20Programs%20%E2%98%A0%EF%B8%8F/Doubly/doubly_LL.c) & [DoublyLL Algo](/Linked%20List/Algorithms%20%F0%9F%93%9D/DLL%20Algo.txt) 10 | 1. [SortedMerged_DLL](/Linked%20List/C%20Programs%20%E2%98%A0%EF%B8%8F/Doubly/sortedMerge_DLL.c) 11 | 1. [DoublyCircular_LL](/Linked%20List/C%20Programs%20%E2%98%A0%EF%B8%8F/Doubly%20Circular/doublyCircular_LL.c) 12 | 13 |
14 | 15 | ## Python Programs 🐍 16 | 17 | 1. [Doubly_LL](/Linked%20List/Python%20Programs%20%F0%9F%90%8D/Doubly/doubly_LL.py) 18 | 1. [DoublyCircular_LL](/Linked%20List/Python%20Programs%20%F0%9F%90%8D/Doubly%20Circular/doublyCircular_LL.py) 19 | -------------------------------------------------------------------------------- /Linked List/Algorithms 📝/Sorted Merge Algo.txt: -------------------------------------------------------------------------------- 1 | *** Algorithm for Merging Two Sorted List *** 2 | 3 | Step 1: GET Two Sorted Linked List In HEAD1 And HEAD2 4 | Step 2: IF HEAD1 = NULL 5 | SET RES = HEAD2 6 | Go To Step 13 7 | [END OF IF] 8 | Step 3: IF HEAD2 = NULL 9 | SET RES = HEAD1 10 | Go To Step 13 11 | [END OF IF] 12 | Step 4: IF HEAD1 -> DATA > HEAD2 -> DATA 13 | SWAP HEAD1 And HEAD2 14 | [END OF IF] 15 | Step 5: SET RES = HEAD1 16 | Step 6: Repeat Steps 7-12 while HEAD1 != NULL AND HEAD2 != NULL 17 | Step 7: SET TEMP = NULL 18 | Step 8: Repeat Steps 9,10 while HEAD1 != NULL AND HEAD1 -> DATA <= HEAD2 -> DATA 19 | Step 9: SET TEMP = HEAD1 20 | Step 10: SET HEAD1 = HEAD1 -> NEXT 21 | [END OF LOOP] 22 | Step 11: SET TEMP -> NEXT = HEAD2 23 | Step 12: SWAP HEAD1 And HEAD2 24 | [END OF LOOP] 25 | Step 13: RETURN RES 26 | 27 | -------------------------------------------------------------------------------- /Dynamic-Memory-Allocation/Python programs/sortedMerge.py: -------------------------------------------------------------------------------- 1 | # 7. Write a python program to merge two sorted dynamic array. 2 | 3 | def merge(arr1, arr2, n1, n2): 4 | i = j = k = 0 5 | arr3 = [None]*(n1+n2) 6 | while i < n1 and j < n2: 7 | if arr1[i] < arr2[j]: 8 | arr3[k] = arr1[i] 9 | i += 1 10 | k += 1 11 | else: 12 | arr3[k] = arr2[j] 13 | j += 1 14 | k += 1 15 | 16 | while i < n1: 17 | arr3[k] = arr1[i] 18 | i += 1 19 | k += 1 20 | 21 | while j < n2: 22 | arr3[k] = arr2[j] 23 | j += 1 24 | k += 1 25 | 26 | return arr3 27 | 28 | 29 | arr1 = [1, 5, 8, 9] 30 | arr2 = [2, 3, 6, 7, 10, 11] 31 | arr3 = merge(arr1, arr2, len(arr1), len(arr2)) 32 | print(arr3) 33 | 34 | 35 | # OUTPUT 36 | # [1, 2, 3, 5, 6, 7, 8, 9, 10, 11] 37 | -------------------------------------------------------------------------------- /Searching & Sorting/C Programs/linearSearch.c: -------------------------------------------------------------------------------- 1 | // Write a program to implement linear search. 2 | 3 | #include 4 | int linearSearch(int arr[], int, int); 5 | int main() 6 | { 7 | int arr[] = {3, 9, 2, 5, 4}; 8 | int size = sizeof(arr) / sizeof(arr[0]); 9 | int key = 5; 10 | int index = linearSearch(arr, size, key); 11 | if (index == -1) 12 | printf("Item not found!"); 13 | else 14 | printf("%d found at Index: %d", key, index); 15 | return 0; 16 | } 17 | 18 | int linearSearch(int arr[], int size, int key) 19 | { 20 | int i; 21 | for (i = 0; i < size; i++) 22 | { 23 | if (arr[i] == key) 24 | return i; 25 | } 26 | return -1; 27 | } 28 | 29 | // OUTPUT 30 | // 5 found at Index: 3 31 | 32 | // can be implemented on both sorted & unsorted array 33 | // Worst complexity: O(n) 34 | // Average complexity: O(n) 35 | // Space complexity: O(1) -------------------------------------------------------------------------------- /Questions/README2.md: -------------------------------------------------------------------------------- 1 |

Assignment 2

2 | 3 | > 💠 Stack & Queue
👉🏼 [Question](/Questions/Assignment-2%40DSALAB.txt) 4 | 5 | --- 6 | 7 | ## C Programs 8 | 9 | 1. [Stack using Array](/Stack/C%20programs/stack_Arr.c) 10 | 1. [Queue using Array](/Queue/C%20Programs/queue_Arr.c) 11 | 1. [Reverse String](/Stack/C%20programs/revString.c) 12 | 1. [Double Stack](/Stack/C%20programs/doubleStack.c) 13 | 1. [Pallindrome](/Stack/C%20programs/pallindrome.c) 14 | 1. [Queue Using Stack](/Queue/C%20Programs/queueUsingStack.c) 15 | 1. [Circular Queue](/Queue/C%20Programs/circularQueue.c) 16 | 17 | --- 18 | 19 | ## Python Programs 20 | 21 | 1. [Stack](/Stack/Python%20Programs/stack.py) 22 | 1. [Queue](/Queue/Python%20Programs/queue.py) 23 | 1. [Reverse String](/Stack/Python%20Programs/revString.py) 24 | 1. [Circular Queue](/Queue/Python%20Programs/circularQueue.py) 25 | 1. [Color](/Stack/Python%20Programs/color.py) 26 | -------------------------------------------------------------------------------- /Questions/README6.md: -------------------------------------------------------------------------------- 1 |

Assignment 6

2 | 3 | > 💠 Stack & Queue
👉🏼 [Question](/Questions/Assignment-6%40DSALAB.txt) 4 | 5 | --- 6 | 7 | ## C Programs 8 | 9 | 1. [Evaluate Postfix](/Stack/C%20programs/evaluatePostfix.c) 10 | 1. [Infix to Postfix](/Stack/C%20programs/infixToPostfix.c) 11 | 1. [Priority Queue](/Queue/C%20Programs/priorityQueue.c) 12 | 1. [Factorial](/Stack/C%20programs/factorial.c) 13 | 1. [Tower of Hanoi](/Stack/C%20programs/towerOfHanoi.c) 14 | 1. [Reverse String](/Stack/C%20programs/revString.c) 15 | 1. [Validate Parenthesis](/Stack/C%20programs/parenthesis.c) 16 | 1. Validate HTML [**Skip it**] 17 | 18 | --- 19 | 20 | ## Python Programs 21 | 22 | 1. [Factorial](/Stack/Python%20Programs/factorial.py) 23 | 1. [Tower of Hanoi](/Stack/Python%20Programs/towerOfHanoi.py) 24 | 1. [Reverse String](/Stack/Python%20Programs/revString.py) 25 | 1. [Validate Parenthesis](/Stack/Python%20Programs/parenthesis.py) 26 | -------------------------------------------------------------------------------- /Stack/Python Programs/parenthesis.py: -------------------------------------------------------------------------------- 1 | # Write a program to validate the parenthesis of an expression. 2 | 3 | open_list = ["(", "{", "["] 4 | close_list = [")", "}", "]"] 5 | 6 | 7 | def validate(str): 8 | stack = [] 9 | for i in str: 10 | if i in open_list: 11 | stack.append(i) 12 | elif i in close_list: 13 | pos = close_list.index(i) 14 | if((len(stack) > 0) and (open_list[pos] == stack[len(stack)-1])): 15 | stack.pop() 16 | else: 17 | return "Parenthesis are not matching" 18 | if len(stack) == 0: 19 | return "Parenthesis are matching!!!" 20 | else: 21 | return "Parenthesis are not matching" 22 | 23 | 24 | str = input("Enter Parenthesis: ") 25 | print(validate(str)) 26 | 27 | # OUTPUT1 28 | # Enter Parenthesis: {} () [{()}] 29 | # Parenthesis are matching!!! 30 | 31 | # OUTPUT2 32 | # Enter Parenthesis: (({}) 33 | # Parenthesis are not matching 34 | -------------------------------------------------------------------------------- /Stack/Python Programs/stack.py: -------------------------------------------------------------------------------- 1 | # Write a Python program to implement a stack. 2 | 3 | stack = [] 4 | 5 | 6 | def push(item): 7 | stack.append(item) 8 | return stack 9 | 10 | 11 | def pop(): 12 | if len(stack) == 0: 13 | return "Stack is empty" 14 | else: 15 | return stack.pop() 16 | 17 | 18 | def peek(): 19 | if len(stack) == 0: 20 | return "Stack is empty" 21 | else: 22 | return stack[-1] 23 | 24 | 25 | choice = int(input("1. Push\n2. Pop\n3. Peek\n4. Exit\nEnter your choice: ")) 26 | while choice != 4: 27 | if choice == 1: 28 | item = input("Enter the item to push: ") 29 | push(item) 30 | print("Stack: ", stack) 31 | elif choice == 2: 32 | print("Popped item: ", pop()) 33 | print("Stack: ", stack) 34 | elif choice == 3: 35 | print("Peeked item: ", peek()) 36 | print("Stack: ", stack) 37 | else: 38 | print("Invalid choice") 39 | choice = int( 40 | input("1. Push\n2. Pop\n3. Peek\n4. Exit\nEnter your choice: ")) 41 | -------------------------------------------------------------------------------- /Searching & Sorting/C Programs/insertionSort.c: -------------------------------------------------------------------------------- 1 | // Write a program to implement Insertion Sort. 2 | 3 | #include 4 | void insertionSort(int arr[], int); 5 | 6 | int main() 7 | { 8 | int arr[] = {3, 7, 5, 9, 1, 6, 2, 8, 4}; 9 | int size = sizeof(arr) / sizeof(arr[0]); 10 | insertionSort(arr, size); 11 | printf("Sorted Array is\n"); 12 | for (int i = 0; i < size; i++) 13 | printf("%d ", arr[i]); 14 | return 0; 15 | } 16 | 17 | void insertionSort(int arr[], int size) 18 | { 19 | int i, j, curr; 20 | for (i = 1; i < size; i++) 21 | { 22 | curr = arr[i]; 23 | j = i - 1; 24 | while (j >= 0 && curr < arr[j]) 25 | { 26 | arr[j + 1] = arr[j]; 27 | j--; 28 | } 29 | arr[j + 1] = curr; 30 | } 31 | } 32 | 33 | // OUTPUT 34 | // Sorted Array is 35 | // 1 2 3 4 5 6 7 8 9 36 | 37 | // Worst complexity: n^2 38 | // Average complexity: n^2 39 | // Best complexity: n 40 | // Space complexity: 1 41 | // Method: Insertion 42 | // Stable: Yes 43 | // Class: Comparison sort -------------------------------------------------------------------------------- /Stack/C programs/factorial.c: -------------------------------------------------------------------------------- 1 | // Write a C program to calculate factorial with the help of stack. 2 | 3 | #include 4 | #define MAX 2 5 | void push(int st[], int *, int); 6 | int pop(int st[], int *); 7 | 8 | int main() 9 | { 10 | int st[MAX], top = -1, n, temp; 11 | printf("Enter a Number: "); 12 | scanf("%d", &n); 13 | push(st, &top, 1); 14 | for (int i = 2; i <= n; i++) 15 | { 16 | temp = pop(st, &top); 17 | push(st, &top, temp * i); 18 | } 19 | printf("Factorial: %d", pop(st, &top)); 20 | return 0; 21 | } 22 | 23 | void push(int st[], int *top, int val) 24 | { 25 | if (*top == MAX - 1) 26 | printf("Stack Overflow!"); 27 | else 28 | st[++(*top)] = val; 29 | } 30 | 31 | int pop(int st[], int *top) 32 | { 33 | if (*top == -1) 34 | { 35 | printf("Stack Underflow!"); 36 | return -1; 37 | } 38 | else 39 | return st[(*top)--]; 40 | } 41 | 42 | // OUTPUT 1 43 | // Enter a Number: 0 44 | // Factorial: 1 45 | 46 | // OUTPUT 2 47 | // Enter a Number: 5 48 | // Factorial: 120 -------------------------------------------------------------------------------- /Searching & Sorting/Python Programs/mergeSort.py: -------------------------------------------------------------------------------- 1 | # Write a program to implement Merge Sort. 2 | 3 | def mergeSort(arr): 4 | if len(arr) > 1: 5 | mid = len(arr)//2 6 | sub_array1 = arr[:mid] 7 | sub_array2 = arr[mid:] 8 | 9 | mergeSort(sub_array1) 10 | mergeSort(sub_array2) 11 | 12 | i = j = k = 0 13 | 14 | while i < len(sub_array1) and j < len(sub_array2): 15 | if sub_array1[i] < sub_array2[j]: 16 | arr[k] = sub_array1[i] 17 | i += 1 18 | else: 19 | arr[k] = sub_array2[j] 20 | j += 1 21 | k += 1 22 | 23 | while i < len(sub_array1): 24 | arr[k] = sub_array1[i] 25 | i += 1 26 | k += 1 27 | 28 | while j < len(sub_array2): 29 | arr[k] = sub_array2[j] 30 | j += 1 31 | k += 1 32 | 33 | 34 | arr = [6, 2, 5, 7, 1, 4, 3] 35 | mergeSort(arr) 36 | print("Sorted Array is:") 37 | print(arr) 38 | 39 | 40 | # OUTPUT 41 | # Sorted Array is: 42 | # [1, 2, 3, 4, 5, 6, 7] 43 | -------------------------------------------------------------------------------- /Searching & Sorting/C Programs/bubbleSort.c: -------------------------------------------------------------------------------- 1 | // Write a program to implement Bubble Sort. 2 | 3 | #include 4 | void bubbleSort(int arr[], int); 5 | 6 | int main() 7 | { 8 | int arr[] = {3, 7, 5, 9, 1, 6, 2, 8, 4}; 9 | int size = sizeof(arr) / sizeof(arr[0]); 10 | bubbleSort(arr, size); 11 | printf("Sorted Array is\n"); 12 | for (int i = 0; i < size; i++) 13 | printf("%d ", arr[i]); 14 | return 0; 15 | } 16 | 17 | void bubbleSort(int arr[], int size) 18 | { 19 | int i, j, swap; 20 | for (i = 0; i < size - 1; i++) 21 | { 22 | for (j = 0; j < size - i - 1; j++) 23 | { 24 | if (arr[j] > arr[j + 1]) 25 | { 26 | swap = arr[j]; 27 | arr[j] = arr[j + 1]; 28 | arr[j + 1] = swap; 29 | } 30 | } 31 | } 32 | } 33 | 34 | // OUTPUT 35 | // Sorted Array is 36 | // 1 2 3 4 5 6 7 8 9 37 | 38 | // Worst complexity: n^2 39 | // Average complexity: n^2 40 | // Best complexity: n 41 | // Space complexity: 1 42 | // Method: Exchanging 43 | // Stable: Yes 44 | // Class: Comparison sort -------------------------------------------------------------------------------- /Queue/Python Programs/queue.py: -------------------------------------------------------------------------------- 1 | # Write a Python program to implement a queue. 2 | # write a program to implement a queue 3 | 4 | queue = [] 5 | 6 | 7 | def enqueue(item): 8 | queue.append(item) 9 | return queue 10 | 11 | 12 | def dequeue(): 13 | if len(queue) == 0: 14 | return "Queue is empty" 15 | else: 16 | return queue.pop(0) 17 | 18 | 19 | def peek(): 20 | if len(queue) == 0: 21 | return "Queue is empty" 22 | else: 23 | return queue[0] 24 | 25 | 26 | choice = int( 27 | input("1. Enqueue\n2. Dequeue\n3. Peek\n4. Exit\nEnter your choice: ")) 28 | while choice != 4: 29 | if choice == 1: 30 | item = input("Enter the item to enqueue: ") 31 | enqueue(item) 32 | print("Queue: ", queue) 33 | elif choice == 2: 34 | print("Dequeued item: ", dequeue()) 35 | print("Queue: ", queue) 36 | elif choice == 3: 37 | print("Peeked item: ", peek()) 38 | print("Queue: ", queue) 39 | else: 40 | print("Invalid choice") 41 | choice = int( 42 | input("1. Enqueue\n2. Dequeue\n3. Peek\n4. Exit\nEnter your choice: ")) 43 | -------------------------------------------------------------------------------- /Dynamic-Memory-Allocation/List Python/addItem.py: -------------------------------------------------------------------------------- 1 | # Add new item to list after a specified item 2 | 3 | list1 = [] 4 | n = int(input("Enter number of elements in list: ")) 5 | for i in range(0, n): 6 | ele = int(input("Enter Numbers["+str(i)+"]: ")) 7 | list1.append(ele) 8 | print(list1) 9 | 10 | key = int(input("Enter Specified item: ")) 11 | 12 | flag = 0 13 | for i in range(0, n): 14 | if list1[i] == key: 15 | val = int(input("Enter value to insert: ")) 16 | list1.insert(i+1, val) 17 | flag = 1 18 | break 19 | 20 | if flag == 0: 21 | print("Specified item not found!") 22 | else: 23 | print("New list is: "+str(list1)) 24 | 25 | 26 | # OUTPUT 1 27 | # Enter number of elements in list: 4 28 | # Enter Numbers[0]: 1 29 | # Enter Numbers[1]: 2 30 | # Enter Numbers[2]: 3 31 | # Enter Numbers[3]: 4 32 | # [1, 2, 3, 4] 33 | # Enter Specified item: 2 34 | # Enter value to insert: 10 35 | # New list is: [1, 2, 10, 3, 4] 36 | 37 | # OUTPUT 2 38 | # Enter number of elements in list: 3 39 | # Enter Numbers[0]: 1 40 | # Enter Numbers[1]: 2 41 | # Enter Numbers[2]: 3 42 | # [1, 2, 3] 43 | # Enter Specified item: 6 44 | # Specified item not found! 45 | -------------------------------------------------------------------------------- /Searching & Sorting/C Programs/selectionSort.c: -------------------------------------------------------------------------------- 1 | // Write a program to implement Selection Sort. 2 | 3 | #include 4 | void selectionSort(int arr[], int); 5 | 6 | int main() 7 | { 8 | int arr[] = {3, 7, 5, 9, 1, 6, 2, 8, 4}; 9 | int size = sizeof(arr) / sizeof(arr[0]); 10 | selectionSort(arr, size); 11 | printf("Sorted Array is\n"); 12 | for (int i = 0; i < size; i++) 13 | printf("%d ", arr[i]); 14 | return 0; 15 | } 16 | 17 | void selectionSort(int arr[], int size) 18 | { 19 | int i, j, smallest, swap; 20 | for (i = 0; i < size - 1; i++) 21 | { 22 | smallest = i; 23 | for (j = i + 1; j < size; j++) 24 | { 25 | if (arr[j] < arr[smallest]) 26 | { 27 | smallest = j; 28 | } 29 | } 30 | swap = arr[i]; 31 | arr[i] = arr[smallest]; 32 | arr[smallest] = swap; 33 | } 34 | } 35 | 36 | // OUTPUT 37 | // Sorted Array is 38 | // 1 2 3 4 5 6 7 8 9 39 | 40 | // Worst complexity: n^2 41 | // Average complexity: n^2 42 | // Best complexity: n^2 43 | // Space complexity: 1 44 | // Method: Selection 45 | // Stable: No 46 | // Class: Comparison sort -------------------------------------------------------------------------------- /Linked List/C Programs ☠️/Singly/reverse_SLL.c: -------------------------------------------------------------------------------- 1 | // Write a C program to reverse an already created single linked list. 2 | 3 | #include "singlylinkedlist.h" 4 | 5 | NODE *reverseList(NODE *); 6 | 7 | int main() 8 | { 9 | NODE *head = NULL; 10 | head = createList(head); 11 | printf("Original List:\n"); 12 | displayList(head); 13 | 14 | head = reverseList(head); 15 | printf("Reversed List:\n"); 16 | displayList(head); 17 | 18 | return 0; 19 | } 20 | 21 | NODE *reverseList(NODE *head) 22 | { 23 | NODE *curr = head; 24 | NODE *prev = NULL; 25 | while (curr != NULL) 26 | { 27 | NODE *temp = curr->next; 28 | curr->next = prev; 29 | prev = curr; 30 | curr = temp; 31 | } 32 | return prev; 33 | } 34 | 35 | // OUTPUT 36 | // Creating Linked List... 37 | // Enter -1 to end 38 | // Enter the data: 5 39 | // Enter the data: 10 40 | // Enter the data: 15 41 | // Enter the data: 20 42 | // Enter the data: 25 43 | // Enter the data: -1 44 | // Original List: 45 | // HEAD -> 5 -> 10 -> 15 -> 20 -> 25 -> NULL 46 | // Reversed List: 47 | // HEAD -> 25 -> 20 -> 15 -> 10 -> 5 -> NULL -------------------------------------------------------------------------------- /Linked List/Algorithms 📝/DeleteAlgo.txt: -------------------------------------------------------------------------------- 1 | *** Delete from Beginning *** 2 | Step 1: IF HEAD = NULL 3 | Write UNDERFLOW 4 | Go to Step 5 5 | [END OF IF] 6 | Step 2: SET PTR = HEAD 7 | Step 3: SET HEAD = HEAD -> NEXT 8 | Step 4: FREE PTR 9 | Step 5: EXIT 10 | 11 | 12 | *** Delete from End *** 13 | Step 1: IF HEAD = NULL 14 | Write UNDERFLOW 15 | Go to Step 8 16 | [END OF IF] 17 | Step 2: SET PTR = HEAD 18 | Step 3: Repeat Steps 4 and 5 while PTR -> NEXT != NULL 19 | Step 4: SET PREPTR = PTR 20 | Step 5: SET PTR = PTR -> NEXT 21 | [END OF LOOP] 22 | Step 6: SET PREPTR -> NEXT = NULL 23 | Step 7: FREE PTR 24 | Step 8: EXIT 25 | 26 | 27 | *** Delete from Any *** 28 | Step 1: IF HEAD = NULL 29 | Write UNDERFLOW 30 | Go to Step 10 31 | [END OF IF] 32 | Step 2: SET PTR = HEAD 33 | Step 3: SET I = 0 34 | Step 4: Repeat Steps 5, 6 and 7 while I < POSITION - 1 35 | Step 5: SET PREPTR = PTR 36 | Step 6: SET PTR = PTR -> NEXT 37 | Step 7: SET I = I + 1 38 | [END OF LOOP] 39 | Step 8: SET PREPTR -> NEXT = PTR -> NEXT 40 | Step 9: FREE TEMP 41 | Step 10 : EXIT -------------------------------------------------------------------------------- /Dynamic-Memory-Allocation/C programs/findMin.c: -------------------------------------------------------------------------------- 1 | // 3. Write a C program to find the minimum element 2 | // in an array using dynamic memory allocation. 3 | 4 | #include 5 | #include 6 | 7 | int main() 8 | { 9 | int n, i, *arr, min; 10 | printf("Enter Total Number of Elements: "); 11 | scanf("%d", &n); 12 | 13 | // Allocating Memory for n elements 14 | arr = (int *)malloc(n * sizeof(int)); 15 | if (arr == NULL) 16 | { 17 | printf("Memory is not allocated"); 18 | exit(0); 19 | } 20 | 21 | // storing numbers entered by the user 22 | for (i = 0; i < n; i++) 23 | { 24 | printf("Enter Array[%d]: ", i); 25 | scanf("%d", arr + i); 26 | } 27 | 28 | min = *(arr + 0); 29 | for (i = 1; i < n; i++) 30 | { 31 | if (min > *(arr + i)) 32 | { 33 | min = *(arr + i); 34 | } 35 | } 36 | 37 | free(arr); 38 | printf("Minimum Element is: %d", min); 39 | return 0; 40 | } 41 | 42 | // OUTPUT 43 | // Enter Total Number of Elements: 5 44 | // Enter Array[0]: 9 45 | // Enter Array[1]: 8 46 | // Enter Array[2]: 2 47 | // Enter Array[3]: 7 48 | // Enter Array[4]: 4 49 | // Minimum Element is: 2 -------------------------------------------------------------------------------- /Searching & Sorting/C Programs/binarySearch.c: -------------------------------------------------------------------------------- 1 | // Write a program to implement binary search. 2 | 3 | #include 4 | 5 | int binarySearch(int arr[], int n, int key); 6 | 7 | int main() 8 | { 9 | int n, key, index; 10 | int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; 11 | n = sizeof(arr) / sizeof(arr[0]); 12 | key = 6; 13 | index = binarySearch(arr, n, key); 14 | if (index == -1) 15 | printf("Item not found!"); 16 | else 17 | printf("%d found at Index: %d", key, index); 18 | return 0; 19 | } 20 | 21 | int binarySearch(int arr[], int n, int key) 22 | { 23 | int low = 0, mid, high = n - 1; 24 | while (low <= high) 25 | { 26 | mid = low + (high - low) / 2; 27 | if (arr[mid] == key) 28 | return mid; 29 | else if (arr[mid] < key) 30 | low = mid + 1; 31 | else if (arr[mid] > key) 32 | high = mid - 1; 33 | } 34 | return -1; 35 | } 36 | 37 | // OUTPUT 38 | // 6 found at Index: 5 39 | 40 | // Works if array is already sorted 41 | // Worst complexity: O(log n) 42 | // Average complexity: O(log n) 43 | // Best complexity: O(1) 44 | // Space complexity: O(1) 45 | // Data structure: Array 46 | // Class: Search algorithm -------------------------------------------------------------------------------- /Stack/C programs/revString.c: -------------------------------------------------------------------------------- 1 | // Write a C program to reverse a string using appropriate ADT. (Assignment 2) 2 | // Write a C program to reverse a string using stack. (Assignment 6) 3 | 4 | #include 5 | #include 6 | #define MAX 10 7 | 8 | char stack[MAX]; 9 | int top = -1; 10 | void push(char); 11 | char pop(void); 12 | 13 | int main() 14 | { 15 | char str[MAX]; 16 | int len, i; 17 | printf("Enter a String: "); 18 | gets(str); 19 | len = strlen(str); 20 | for (i = 0; i < len; i++) 21 | { 22 | push(str[i]); 23 | } 24 | for (i = 0; i <= len; i++) 25 | { 26 | str[i] = pop(); 27 | } 28 | printf("Reversed String is: %s", str); 29 | return 0; 30 | } 31 | 32 | void push(char c) 33 | { 34 | if (top == MAX - 1) 35 | printf("Stack Overflow!"); 36 | else 37 | { 38 | top++; 39 | stack[top] = c; 40 | } 41 | } 42 | 43 | char pop(void) 44 | { 45 | char c; 46 | if (top == -1) 47 | { 48 | return '\0'; 49 | } 50 | else 51 | { 52 | c = stack[top]; 53 | top--; 54 | return c; 55 | } 56 | } 57 | 58 | // OUTPUT 59 | // Enter a String: good day 60 | // Reversed String is: yad doog -------------------------------------------------------------------------------- /Dynamic-Memory-Allocation/C programs/modifySize.c: -------------------------------------------------------------------------------- 1 | // 10. Write a C program to modify the size of an array and utilize that during run time. 2 | 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | int *arr, n, m; 9 | printf("Enter size of the array: "); 10 | scanf("%d", &n); 11 | arr = (int *)malloc(n * sizeof(int)); 12 | 13 | for (int i = 0; i < n; i++) 14 | { 15 | printf("Enter Array[%d]: ", i); 16 | scanf("%d", arr + i); 17 | } 18 | 19 | printf("\nHow many extra elements you want to add: "); 20 | scanf("%d", &m); 21 | arr = (int *)realloc(arr, sizeof(int) * (m + n)); 22 | 23 | for (int i = n; i < m + n; i++) 24 | { 25 | printf("Enter Array[%d]: ", i); 26 | scanf("%d", arr + i); 27 | } 28 | 29 | printf("\nNew array after reallocation is\n"); 30 | for (int i = 0; i < m + n; i++) 31 | { 32 | printf("%d ", *(arr + i)); 33 | } 34 | 35 | free(arr); 36 | return 0; 37 | } 38 | 39 | // OUTPUT 40 | // Enter size of the array: 2 41 | // Enter Array[0]: 2 42 | // Enter Array[1]: 4 43 | 44 | // How many extra elements you want to add: 2 45 | // Enter Array[2]: 6 46 | // Enter Array[3]: 8 47 | 48 | // New array after reallocation is 49 | // 2 4 6 8 -------------------------------------------------------------------------------- /Linked List/C Programs ☠️/Singly/swapNodes.c: -------------------------------------------------------------------------------- 1 | // Given a singly linked list of size N. The task 2 | // is to swap elements in the linked list pairwise. 3 | 4 | #include "singlylinkedlist.h" 5 | 6 | NODE *swapNodes(NODE *head); 7 | 8 | int main() 9 | { 10 | NODE *head = NULL; 11 | head = createList(head); 12 | printf("Original List:\n"); 13 | displayList(head); 14 | 15 | head = swapNodes(head); 16 | printf("After Swapping:\n"); 17 | displayList(head); 18 | 19 | return 0; 20 | } 21 | 22 | NODE *swapNodes(NODE *head) 23 | { 24 | NODE *dummy = (NODE *)malloc(sizeof(NODE)); 25 | dummy->next = head; 26 | NODE *prev = dummy; 27 | NODE *curr = head; 28 | while (curr != NULL && curr->next != NULL) 29 | { 30 | prev->next = curr->next; 31 | curr->next = curr->next->next; 32 | prev->next->next = curr; 33 | curr = curr->next; 34 | prev = prev->next->next; 35 | } 36 | return dummy->next; 37 | } 38 | 39 | // OUTPUT 40 | // Creating Linked List... 41 | // Enter -1 to end 42 | // Enter the data: 1 43 | // Enter the data: 2 44 | // Enter the data: 3 45 | // Enter the data: 4 46 | // Enter the data: 5 47 | // Enter the data: -1 48 | // Original List: 49 | // HEAD -> 1 -> 2 -> 3 -> 4 -> 5 -> NULL 50 | // After Swapping: 51 | // HEAD -> 2 -> 1 -> 4 -> 3 -> 5 -> NULL -------------------------------------------------------------------------------- /Dynamic-Memory-Allocation/C programs/max2DArr.c: -------------------------------------------------------------------------------- 1 | // 5. Write a C program to find the maximum element in 2 | // a 2D - array using dynamic memory allocation. 3 | 4 | #include 5 | #include 6 | 7 | int main() 8 | { 9 | int row, col, *arr, max; 10 | printf("Enter Number of Rows: "); 11 | scanf("%d", &row); 12 | printf("Enter Number of Columns: "); 13 | scanf("%d", &col); 14 | 15 | arr = (int *)malloc(row * col * sizeof(int)); 16 | 17 | // storing numbers entered by the user 18 | for (int i = 0; i < row; i++) 19 | { 20 | for (int j = 0; j < col; j++) 21 | { 22 | printf("Enter Array[%d][%d]: ", i, j); 23 | scanf("%d", arr + (i * col + j)); 24 | } 25 | } 26 | 27 | // searching max element 28 | max = *(arr + 0); 29 | for (int i = 0; i < row; i++) 30 | { 31 | for (int j = 0; j < col; j++) 32 | { 33 | if (*(arr + (i * col + j)) > max) 34 | { 35 | max = *(arr + (i * col + j)); 36 | } 37 | } 38 | } 39 | printf("Maximum Element is %d", max); 40 | free(arr); 41 | return 0; 42 | } 43 | 44 | // OUTPUT 45 | // Enter Number of Rows: 2 46 | // Enter Number of Columns: 2 47 | // Enter Array[0][0]: 8 48 | // Enter Array[0][1]: 4 49 | // Enter Array[1][0]: 6 50 | // Enter Array[1][1]: 7 51 | // Maximum Element is 8 -------------------------------------------------------------------------------- /Dynamic-Memory-Allocation/C programs/min2DArr.c: -------------------------------------------------------------------------------- 1 | // 6. Write a C program to find the minimum element in 2 | // a 2D - array using dynamic memory allocation. 3 | 4 | #include 5 | #include 6 | 7 | int main() 8 | { 9 | int row, col, *arr, min; 10 | printf("Enter Number of Rows: "); 11 | scanf("%d", &row); 12 | printf("Enter Number of Columns: "); 13 | scanf("%d", &col); 14 | 15 | arr = (int *)malloc(row * col * sizeof(int)); 16 | 17 | // storing numbers entered by the user 18 | for (int i = 0; i < row; i++) 19 | { 20 | for (int j = 0; j < col; j++) 21 | { 22 | printf("Enter Array[%d][%d]: ", i, j); 23 | scanf("%d", arr + (i * col + j)); 24 | } 25 | } 26 | 27 | // searching min element 28 | min = *(arr + 0); 29 | for (int i = 0; i < row; i++) 30 | { 31 | for (int j = 0; j < col; j++) 32 | { 33 | if (*(arr + (i * col + j)) < min) 34 | { 35 | min = *(arr + (i * col + j)); 36 | } 37 | } 38 | } 39 | printf("Minimum Element is %d", min); 40 | free(arr); 41 | return 0; 42 | } 43 | 44 | // OUTPUT 45 | // Enter Number of Rows: 2 46 | // Enter Number of Columns: 2 47 | // Enter Array[0][0]: 5 48 | // Enter Array[0][1]: 7 49 | // Enter Array[1][0]: 3 50 | // Enter Array[1][1]: 9 51 | // Minimum Element is 3 -------------------------------------------------------------------------------- /Searching & Sorting/C Programs/shellSort.c: -------------------------------------------------------------------------------- 1 | // Write a program to implement Shell sort. 2 | 3 | // Shell Sort in C programming 4 | 5 | #include 6 | 7 | void shellSort(int[], int); 8 | void printArray(int[], int); 9 | 10 | int main() 11 | { 12 | int data[] = {9, 8, 3, 7, 5, 6, 4, 1}; 13 | int size = sizeof(data) / sizeof(data[0]); 14 | shellSort(data, size); 15 | printf("Sorted array: \n"); 16 | printArray(data, size); 17 | } 18 | 19 | void shellSort(int array[], int n) 20 | { 21 | // Rearrange elements at each n/2, n/4, n/8, ... intervals 22 | for (int interval = n / 2; interval > 0; interval /= 2) 23 | { 24 | for (int i = interval; i < n; i += 1) 25 | { 26 | int temp = array[i]; 27 | int j; 28 | for (j = i; j >= interval && array[j - interval] > temp; j -= interval) 29 | { 30 | array[j] = array[j - interval]; 31 | } 32 | array[j] = temp; 33 | } 34 | } 35 | } 36 | 37 | void printArray(int array[], int size) 38 | { 39 | for (int i = 0; i < size; ++i) 40 | { 41 | printf("%d ", array[i]); 42 | } 43 | printf("\n"); 44 | } 45 | 46 | // OUTPUT 47 | // Sorted array: 48 | // 1 3 4 5 6 7 8 9 49 | 50 | // Best Time: O(nlog n) 51 | // Average Time: O(nlog n) 52 | // Worst Time: O(n2) 53 | // Space Complexity: O(1) 54 | // Stability: No -------------------------------------------------------------------------------- /Linked List/Algorithms 📝/InsertAlgo.txt: -------------------------------------------------------------------------------- 1 | *** Insert at the Beginning *** 2 | Step 1: IF AVAIL = NULL 3 | Write OVERFLOW 4 | Go to Step 7 5 | [END OF IF] 6 | Step 2: SET NEW_NODE = AVAIL 7 | Step 3: SET AVAIL = AVAIL NEXT 8 | Step 4: SET DATA = VAL 9 | Step 5: SET NEW_NODE NEXT = START 10 | Step 6: SET START = NEW_NODE 11 | Step 7: EXIT 12 | 13 | 14 | *** Insert at the End *** 15 | Step 1: IF AVAIL = NULL 16 | Write OVERFLOW 17 | Go to Step 10 18 | [END OF IF] 19 | Step 2: SET NEW_NODE = AVAIL 20 | Step 3: SET AVAIL = AVAIL -> NEXT 21 | Step 4: SET NEW_NODE -> DATA = VAL 22 | Step 5: SET NEW_NODE -> NEXT = NULL 23 | Step 6: SET PTR = HEAD 24 | Step 7: Repeat Step 8 while PTR -> NEXT != NULL 25 | Step 8: SET PTR = PTR -> NEXT 26 | [END OF LOOP] 27 | Step 9: SET PTR -> NEXT = NEW_NODE 28 | Step 10: EXIT 29 | 30 | 31 | *** Insert at Any *** 32 | Step 1: IF AVAIL = NULL 33 | Write OVERFLOW 34 | Go to Step 12 35 | [END OF IF] 36 | Step 2: SET NEW_NODE = AVAIL 37 | Step 3: SET AVAIL = AVAIL -> NEXT 38 | Step 4: SET NEW_NODE -> DATA = VAL 39 | Step 5: SET PTR = HEAD 40 | Step 6: SET I = 0 41 | Step 7: Repeat Steps 8 and 9 while I < POSITION-1 42 | Step 8: SET PTR = PTR -> NEXT 43 | Step 9: SET I = I + 1 44 | [END OF LOOP] 45 | Step 10: SET NEW_NODE -> NEXT = PTR -> NEXT 46 | Step 11: SET PTR -> NEXT = NEW_NODE 47 | Step 12: EXIT -------------------------------------------------------------------------------- /Dynamic-Memory-Allocation/C programs/searchEle.c: -------------------------------------------------------------------------------- 1 | // 1. Write a C program to search an element in an Array using dynamic memory allocation. 2 | 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | int n, *arr, key, flag = 0; 9 | printf("Enter Total Number of Elements: "); 10 | scanf("%d", &n); 11 | 12 | // Allocating Memory for n elements 13 | arr = (int *)malloc(n * sizeof(int)); 14 | if (arr == NULL) 15 | { 16 | printf("Memory is not allocated"); 17 | exit(0); 18 | } 19 | 20 | // storing numbers entered by the user 21 | for (int i = 0; i < n; i++) 22 | { 23 | printf("Enter Array[%d]: ", i); 24 | scanf("%d", arr + i); 25 | } 26 | 27 | printf("Enter an Element to find index: "); 28 | scanf("%d", &key); 29 | 30 | // Searching the element 31 | for (int i = 0; i < n; i++) 32 | { 33 | if (*(arr + i) == key) 34 | { 35 | flag = 1; 36 | printf("Element found at index %d", i); 37 | break; 38 | } 39 | } 40 | if (flag == 0) 41 | { 42 | printf("Number is not available"); 43 | } 44 | 45 | free(arr); 46 | return 0; 47 | } 48 | 49 | // OUTPUT 50 | // Enter Total Number of Elements: 5 51 | // Enter Array[0]: 1 52 | // Enter Array[1]: 2 53 | // Enter Array[2]: 3 54 | // Enter Array[3]: 4 55 | // Enter Array[4]: 5 56 | // Enter an Element to find index: 3 57 | // Element found at index 2 -------------------------------------------------------------------------------- /Linked List/Python Programs 🐍/Singly/countNodes.py: -------------------------------------------------------------------------------- 1 | # Write a function to count the number 2 | # of node present in a linked list. 3 | 4 | class Node: 5 | def __init__(self, data): 6 | self.data = data 7 | self.next = None 8 | 9 | 10 | class LinkedList: 11 | def __init__(self): 12 | self.head = None 13 | 14 | def createList(self): 15 | print("Creating Linked List...") 16 | print("Enter -1 to end") 17 | num = int(input("Enter the data: ")) 18 | while(num != -1): 19 | newNode = Node(num) 20 | if self.head is None: 21 | self.head = newNode 22 | else: 23 | last = self.head 24 | while last.next: 25 | last = last.next 26 | last.next = newNode 27 | num = int(input("Enter the data: ")) 28 | 29 | def size(self): 30 | count = 0 31 | temp = self.head 32 | while temp: 33 | count += 1 34 | temp = temp.next 35 | return count 36 | 37 | 38 | myList = LinkedList() 39 | myList.createList() 40 | n = myList.size() 41 | print("Number of Nodes is: " + str(n)) 42 | 43 | 44 | # OUTPUT 45 | # Creating Linked List... 46 | # Enter -1 to end 47 | # Enter the data: 6 48 | # Enter the data: 3 49 | # Enter the data: 8 50 | # Enter the data: 2 51 | # Enter the data: -1 52 | # Number of Nodes is: 4 53 | -------------------------------------------------------------------------------- /Searching & Sorting/C Programs/interpolationSearch.c: -------------------------------------------------------------------------------- 1 | // Write a program to implement interpolation search. 2 | 3 | #include 4 | int interpolationSearch(int arr[], int, int, int); 5 | int main() 6 | { 7 | int arr[] = {1, 3, 5, 7, 9, 11, 13}; 8 | int size = sizeof(arr) / sizeof(arr[0]); 9 | int key = 9; 10 | int index = interpolationSearch(arr, 0, size - 1, key); 11 | if (index == -1) 12 | printf("Item not found!"); 13 | else 14 | printf("%d found at Index: %d", key, index); 15 | return 0; 16 | } 17 | 18 | int interpolationSearch(int arr[], int low, int high, int key) 19 | { 20 | int mid; 21 | while (low <= high) 22 | { 23 | mid = low + (high - low) * ((key - arr[low]) / (arr[high] - arr[low])); 24 | if (key == arr[mid]) 25 | return mid; 26 | if (key < arr[mid]) 27 | high = mid - 1; 28 | else 29 | low = mid + 1; 30 | } 31 | return -1; 32 | } 33 | 34 | // OUTPUT 35 | // 9 found at Index: 4 36 | 37 | // When n elements of a list to be sorted are uniformly distributed (average case), interpolation 38 | // search makes about log(log n) comparisons. However, in the worst case, that is when the elements 39 | // increase exponentially, the algorithm can make up to O(n) comparisons. 40 | // Class : Search algorithm 41 | // Data structure : Array 42 | // Worst-case performance : O(n) 43 | // Best-case performance : O(1) 44 | // Average performance : O(log(log(n))) 45 | // Worst-case space complexity : O(1) -------------------------------------------------------------------------------- /Dynamic-Memory-Allocation/C programs/delDataArr.c: -------------------------------------------------------------------------------- 1 | // 9. Write a C program to delete a range 2 | // of data from a dynamic array. 3 | 4 | #include 5 | #include 6 | 7 | int main() 8 | { 9 | int *arr, n, i, left, right; 10 | printf("Enter Size of the array: "); 11 | scanf("%d", &n); 12 | arr = (int *)malloc(n * sizeof(int)); 13 | 14 | for (i = 0; i < n; i++) 15 | { 16 | printf("Enter Array[%d]: ", i); 17 | scanf("%d", arr + i); 18 | } 19 | 20 | printf("Enter left index to delete: "); // Should be between 0 to n and <=right index 21 | scanf("%d", &left); 22 | printf("Enter right index to delete: "); // Should be between 0 to n and >=left index 23 | scanf("%d", &right); 24 | 25 | for (i = right; i < n; i++) 26 | { 27 | *(arr + left) = *(arr + (right + 1)); 28 | left++; 29 | right++; 30 | } 31 | arr = (int *)realloc(arr, (n + left - right - 1) * sizeof(int)); 32 | 33 | printf("\nFinal Array is\n"); 34 | for (i = 0; i < n + left - right - 1; i++) 35 | { 36 | printf("%d ", *(arr + i)); 37 | } 38 | 39 | free(arr); 40 | return 0; 41 | } 42 | 43 | // OUTPUT 44 | // Enter Size of the array: 7 45 | // Enter Array[0]: 2 46 | // Enter Array[1]: 4 47 | // Enter Array[2]: 6 48 | // Enter Array[3]: 8 49 | // Enter Array[4]: 10 50 | // Enter Array[5]: 12 51 | // Enter Array[6]: 14 52 | // Enter left index to delete: 2 53 | // Enter right index to delete: 4 54 | 55 | // Final Array is 56 | // 2 4 12 14 -------------------------------------------------------------------------------- /Questions/Assignment-10@DSALAB.txt: -------------------------------------------------------------------------------- 1 | UEM New Logo 2 | 3 | University of Engineering & Management, Kolkata 4 | Course: B.Tech (CSE / CSE(AIML) / CSE(IOT-CYS-BCT) 5 | Semester: 3rd 6 | Paper Name: Data Structure & Algorithm Laboratory 7 | Paper Code: PCC - CS391 8 | Date of Assignment - 18.10.2022 9 | Date of Submission - 18.10.2022 10 | Section: CSE A, CSE B 11 | 12 | 13 | 14 | Assignment – X 15 | (All programs to be implemented in C & Python) 16 | 17 | Topic: Graph 18 | 1. Write a C program to implement Graph using Adjacency Matrix along with the following function: 19 | a. To count number of vertices and edges present in a graph. 20 | b. To find the adjacent vertices of a given vertex. 21 | c. To search a node in a given graph. 22 | 23 | 24 | 2. Write a C program to implement Graph using Adjacency list along with the following function: 25 | a. To count number of vertices and edges present in a graph. 26 | b. To find the adjacent vertices of a given vertex. 27 | c. To search a node in a given graph. 28 | 29 | 30 | 31 | 32 | 33 | 34 | Instruction: 35 | 1. Mention the following thing using A4 pages: 36 | a. Program Objective 37 | b. Algorithm (if it is asked) 38 | c. Program Code. 39 | d. Output 40 | 2. Handwritten assignment solutions are required. No printout is allowed. 41 | 3. Submit all the assignments using the channel file. 42 | 4. One Index page must be attached there. 43 | 5. Late submission will not be entertained. Maintain the deadline. -------------------------------------------------------------------------------- /Stack/C programs/parenthesis.c: -------------------------------------------------------------------------------- 1 | // Write a C program to validate the parenthesis of an expression. 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | bool isValid(char *s); 8 | 9 | int main() 10 | { 11 | char str[10]; 12 | printf("Enter a String: "); 13 | gets(str); 14 | if (isValid(str)) 15 | printf("Parenthesis is Matching!"); 16 | else 17 | printf("Parenthesis is not Matching"); 18 | return 0; 19 | } 20 | 21 | bool isValid(char *s) 22 | { 23 | int s_len = strlen(s); 24 | if (s_len < 2) 25 | return false; 26 | char *open = "({["; 27 | char *close = ")}]"; 28 | char *stack = malloc(sizeof(char) * (s_len)); 29 | int top = -1; 30 | while (*s != '\0') 31 | { 32 | for (int i = 0; i < 3; i++) 33 | { 34 | if (*s == open[i]) 35 | { 36 | stack[++top] = *s; 37 | break; 38 | } 39 | if (*s == close[i]) 40 | { 41 | if (top == -1) 42 | return false; 43 | if (stack[top] == open[i]) 44 | { 45 | top--; 46 | break; 47 | } 48 | else 49 | return false; 50 | } 51 | } 52 | s++; 53 | } 54 | return (top == -1) ? true : false; 55 | } 56 | 57 | // OUTPUT 58 | 59 | // Enter a String: [5*{7+1-(4/(2))}] 60 | // Parenthesis is Matching! 61 | 62 | // Enter a String: ({[]}] 63 | // Parenthesis is not Matching -------------------------------------------------------------------------------- /Queue/C Programs/priorityQueue.c: -------------------------------------------------------------------------------- 1 | // Write a C program to implement Priority Queue. 2 | 3 | #include 4 | #include 5 | 6 | struct item 7 | { 8 | int value; 9 | int priority; 10 | }; 11 | struct item q[10]; 12 | int size = -1; 13 | 14 | void enqueue(int, int); 15 | int peek(void); 16 | void dequeue(void); 17 | 18 | int main() 19 | { 20 | enqueue(10, 2); 21 | enqueue(15, 4); 22 | enqueue(20, 1); 23 | enqueue(25, 4); 24 | enqueue(30, 3); 25 | dequeue(); 26 | dequeue(); 27 | dequeue(); 28 | dequeue(); 29 | dequeue(); 30 | dequeue(); 31 | return 0; 32 | } 33 | 34 | void enqueue(int value, int priority) 35 | { 36 | size++; 37 | q[size].value = value; 38 | q[size].priority = priority; 39 | } 40 | 41 | int peek() 42 | { 43 | int highestPriority = INT_MIN; 44 | int ind = -1; 45 | for (int i = 0; i <= size; i++) 46 | { 47 | if (highestPriority < q[i].priority) 48 | { 49 | highestPriority = q[i].priority; 50 | ind = i; 51 | } 52 | } 53 | return ind; 54 | } 55 | 56 | void dequeue() 57 | { 58 | if (size == -1) 59 | { 60 | printf("Priority Queue is Empty!"); 61 | return; 62 | } 63 | int ind = peek(); 64 | printf("%d is Removed from Queue\n", q[ind].value); 65 | for (int i = ind; i < size; i++) 66 | { 67 | q[i] = q[i + 1]; 68 | } 69 | size--; 70 | } 71 | 72 | // OUTPUT 73 | // 15 is Removed from Queue 74 | // 25 is Removed from Queue 75 | // 30 is Removed from Queue 76 | // 10 is Removed from Queue 77 | // 20 is Removed from Queue 78 | // Priority Queue is Empty! -------------------------------------------------------------------------------- /Searching & Sorting/C Programs/mergeSort.c: -------------------------------------------------------------------------------- 1 | // Write a program to implement Merge Sort. 2 | 3 | #include 4 | void mergeSort(int arr[], int, int); 5 | void merge(int arr[], int, int, int); 6 | 7 | int main() 8 | { 9 | int arr[] = {3, 7, 5, 1, 6, 2, 4}; 10 | int size = sizeof(arr) / sizeof(arr[0]); 11 | mergeSort(arr, 0, size - 1); 12 | printf("Sorted Array is\n"); 13 | for (int i = 0; i < size; i++) 14 | printf("%d ", arr[i]); 15 | return 0; 16 | } 17 | 18 | void mergeSort(int arr[], int start, int end) 19 | { 20 | if (start >= end) 21 | return; 22 | int mid = start + (end - start) / 2; 23 | mergeSort(arr, start, mid); 24 | mergeSort(arr, mid + 1, end); 25 | merge(arr, start, mid, end); 26 | } 27 | 28 | void merge(int arr[], int start, int mid, int end) 29 | { 30 | int merged[end - start + 1]; 31 | int idx1 = start, idx2 = mid + 1; 32 | int i = 0, j; 33 | 34 | while (idx1 <= mid && idx2 <= end) 35 | { 36 | if (arr[idx1] <= arr[idx2]) 37 | merged[i++] = arr[idx1++]; 38 | else 39 | merged[i++] = arr[idx2++]; 40 | } 41 | while (idx1 <= mid) 42 | merged[i++] = arr[idx1++]; 43 | while (idx2 <= end) 44 | merged[i++] = arr[idx2++]; 45 | 46 | int size = sizeof(merged) / sizeof(merged[0]); 47 | for (int i = 0, j = start; i < size; i++, j++) 48 | arr[j] = merged[i]; 49 | } 50 | 51 | // OUTPUT 52 | // Sorted Array is 53 | // 1 2 3 4 5 6 7 54 | 55 | // Worst complexity: n*log(n) 56 | // Average complexity: n*log(n) 57 | // Best complexity: n*log(n) 58 | // Space complexity: n 59 | // Method: Merging 60 | // Stable: Yes -------------------------------------------------------------------------------- /Searching & Sorting/C Programs/quickSort.c: -------------------------------------------------------------------------------- 1 | // Write a program to implement Quick sort. 2 | 3 | #include 4 | 5 | int partition(int arr[], int, int); 6 | void quickSort(int arr[], int, int); 7 | void swap(int *, int *); 8 | 9 | int main() 10 | { 11 | int arr[] = {3, 7, 5, 9, 1, 6, 2, 8, 4}; 12 | int size = sizeof(arr) / sizeof(arr[0]); 13 | quickSort(arr, 0, size - 1); 14 | printf("Sorted Array is\n"); 15 | for (int i = 0; i < size; i++) 16 | printf("%d ", arr[i]); 17 | return 0; 18 | } 19 | 20 | void quickSort(int arr[], int low, int high) 21 | { 22 | if (low < high) 23 | { 24 | int pi = partition(arr, low, high); 25 | quickSort(arr, low, pi - 1); 26 | quickSort(arr, pi + 1, high); 27 | } 28 | } 29 | 30 | int partition(int arr[], int low, int high) 31 | { 32 | int pivot = arr[high]; 33 | int i = (low - 1); 34 | for (int j = low; j < high; j++) 35 | { 36 | if (arr[j] <= pivot) 37 | { 38 | i++; 39 | swap(&arr[i], &arr[j]); 40 | } 41 | } 42 | swap(&arr[i + 1], &arr[high]); 43 | return (i + 1); 44 | } 45 | 46 | void swap(int *a, int *b) 47 | { 48 | int temp = *a; 49 | *a = *b; 50 | *b = temp; 51 | } 52 | 53 | // OUTPUT 54 | // Sorted Array is 55 | // 1 2 3 4 5 6 7 8 9 56 | 57 | // Class Sorting algorithm 58 | // Worst-case performance O(n^2) 59 | // Best-case performance O(nlog n) (simple partition) or O(n) (three-way partition and equal keys) 60 | // Average performance O(nlog n) 61 | // Worst-case space complexity O(n) auxiliary(naive) O(log n) auxiliary(Hoare 1962) 62 | -------------------------------------------------------------------------------- /Dynamic-Memory-Allocation/C programs/search2DArr.c: -------------------------------------------------------------------------------- 1 | // 4. Write a C program to search an element in a 2 | // 2D - Array using dynamic memory allocation. 3 | 4 | #include 5 | #include 6 | 7 | int main() 8 | { 9 | int row, col, *arr, key, flag = 0; 10 | printf("Enter Number of Rows: "); 11 | scanf("%d", &row); 12 | printf("Enter Number of Columns: "); 13 | scanf("%d", &col); 14 | 15 | arr = (int *)malloc(row * col * sizeof(int)); 16 | 17 | // storing numbers entered by the user 18 | for (int i = 0; i < row; i++) 19 | { 20 | for (int j = 0; j < col; j++) 21 | { 22 | printf("Enter Array[%d][%d]: ", i, j); 23 | scanf("%d", arr + (i * col + j)); 24 | } 25 | } 26 | 27 | printf("Enter an Element to find index: "); 28 | scanf("%d", &key); 29 | 30 | // searching element 31 | for (int i = 0; i < row; i++) 32 | { 33 | for (int j = 0; j < col; j++) 34 | { 35 | if (*(arr + (i * col + j)) == key) 36 | { 37 | flag = 1; 38 | printf("\nElement found at Array[%d][%d]", i, j); 39 | break; 40 | } 41 | } 42 | } 43 | 44 | if (flag == 0) 45 | { 46 | printf("\nNumber is not available"); 47 | } 48 | 49 | free(arr); 50 | return 0; 51 | } 52 | 53 | // OUTPUT 54 | // Enter Number of Rows: 2 55 | // Enter Number of Columns: 3 56 | // Enter Array[0][0]: 1 57 | // Enter Array[0][1]: 2 58 | // Enter Array[0][2]: 3 59 | // Enter Array[1][0]: 4 60 | // Enter Array[1][1]: 5 61 | // Enter Array[1][2]: 6 62 | // Enter an Element to find index: 4 63 | 64 | // Element found at Array[1][0] -------------------------------------------------------------------------------- /Questions/README7.md: -------------------------------------------------------------------------------- 1 |

Assignment 7

2 | 3 | > 💠 Sorting & Searching
👉🏼 [Question](/Questions/Assignment-7%40DSALAB.txt) 4 | 5 | --- 6 | 7 | ## C Programs 👽 8 | 9 | 1. [Bubble Sort](/Searching%20%26%20Sorting/C%20Programs/bubbleSort.c) 10 | 1. [Selection Sort](/Searching%20%26%20Sorting/C%20Programs/selectionSort.c) 11 | 1. [Insertion Sort](/Searching%20%26%20Sorting/C%20Programs/insertionSort.c) 12 | 1. [Merge Sort](/Searching%20%26%20Sorting/C%20Programs/mergeSort.c) 13 | 1. [Quick Sort](/Searching%20%26%20Sorting/C%20Programs/quickSort.c) (**EXTRA**) 14 | 1. [Shell Sort](/Searching%20%26%20Sorting/C%20Programs/shellSort.c) (**EXTRA**) 15 | 1. [Radix Sort](/Searching%20%26%20Sorting/C%20Programs/radixSort.c) (**EXTRA**) 16 | 1. [Heap Sort](/Searching%20%26%20Sorting/C%20Programs/heapSort.c) (**EXTRA**) 17 | 1. [Linear Search](/Searching%20%26%20Sorting/C%20Programs/linearSearch.c) 18 | 1. [Binary Search](/Searching%20%26%20Sorting/C%20Programs/binarySearch.c) 19 | 1. [Interpolation Search](/Searching%20%26%20Sorting/C%20Programs/interpolationSearch.c) (**EXTRA**) 20 | 1. [Hashing](/Searching%20%26%20Sorting/C%20Programs/hashing.c) 21 | 22 | --- 23 | 24 | ## Python Programs 🤖 25 | 26 | 1. [Bubble Sort](/Searching%20%26%20Sorting/Python%20Programs/bubbleSort.py) 27 | 1. [Selection Sort](/Searching%20%26%20Sorting/Python%20Programs/selectionSort.py) 28 | 1. [Insertion Sort](/Searching%20%26%20Sorting/Python%20Programs/insertionSort.py) 29 | 1. [Merge Sort](/Searching%20%26%20Sorting/Python%20Programs/mergeSort.py) 30 | 1. [Linear Search](/Searching%20%26%20Sorting/Python%20Programs/linearSearch.py) 31 | 1. [Binary Search](/Searching%20%26%20Sorting/Python%20Programs/binarySearch.py) 32 | -------------------------------------------------------------------------------- /Linked List/C Programs ☠️/Singly/singlylinkedlist.h: -------------------------------------------------------------------------------- 1 | // Please write down this header file one time and 2 | // mention #include "singlylinkedlist.h" in all other programs 3 | // We don't have to write this 50 lines of code in every code 4 | // 👇👇 Start writing from here 👇👇 5 | 6 | // This is singlylinkedlist.h file 7 | // This header file contains Structure of linked list, 8 | // createList and displayList function 9 | 10 | #include 11 | #include 12 | 13 | typedef struct node 14 | { 15 | int data; 16 | struct node *next; 17 | } NODE; 18 | 19 | NODE *createList(NODE *head) 20 | { 21 | NODE *newNode, *ptr; 22 | int num; 23 | printf("Creating Linked List...\n"); 24 | printf("Enter -1 to end\n"); 25 | printf("Enter the data: "); 26 | scanf("%d", &num); 27 | while (num != -1) 28 | { 29 | newNode = (NODE *)malloc(sizeof(NODE)); 30 | newNode->data = num; 31 | if (head == NULL) 32 | { 33 | newNode->next = NULL; 34 | head = newNode; 35 | } 36 | else 37 | { 38 | ptr = head; 39 | while (ptr->next != NULL) 40 | { 41 | ptr = ptr->next; 42 | } 43 | ptr->next = newNode; 44 | newNode->next = NULL; 45 | } 46 | printf("Enter the data: "); 47 | scanf("%d", &num); 48 | } 49 | return head; 50 | } 51 | 52 | void displayList(NODE *head) 53 | { 54 | NODE *ptr; 55 | ptr = head; 56 | printf("HEAD"); 57 | while (ptr != NULL) 58 | { 59 | printf(" -> %d", ptr->data); 60 | ptr = ptr->next; 61 | } 62 | printf(" -> NULL\n"); 63 | } 64 | -------------------------------------------------------------------------------- /Stack/Python Programs/color.py: -------------------------------------------------------------------------------- 1 | # Given a stack of boxes in different colors. Write a python function 2 | # that accepts the stack of boxes and removes those boxes having 3 | # color other than the primary colors(Red, Green and Blue) from the stack. 4 | # The removed boxes should be en-queued into a new queue and returned. 5 | # The original stack should have only the boxes having primary colors 6 | # and the order must be maintained. 7 | # Perform case sensitive string comparison wherever necessary. 8 | 9 | 10 | def remove_color(stack): 11 | new_stack = [] 12 | new_queue = [] 13 | while stack: 14 | box = stack.pop() 15 | if box == 'Red' or box == 'Green' or box == 'Blue': 16 | new_stack.append(box) 17 | new_queue.append(box) 18 | return new_stack, new_queue 19 | 20 | 21 | if __name__ == '__main__': 22 | a = int(input("Enter the number of boxes: ")) 23 | stack = [] 24 | for i in range(a): 25 | stack.append(input("Enter the color of the box: ")) 26 | print("The stack is: ", stack) 27 | new_stack, new_queue = remove_color(stack) 28 | print("The new stack is: ", new_stack) 29 | print("The new queue is: ", new_queue) 30 | print("The original stack is: ", stack) 31 | print("The original queue is: ", stack) 32 | 33 | # Enter the number of boxes: 5 34 | # Enter the color of the box: Red 35 | # Enter the color of the box: Yellow 36 | # Enter the color of the box: Blue 37 | # Enter the color of the box: Green 38 | # Enter the color of the box: RedThe stack is: ['Red', 'Yellow', 'Blue', 'Green', 'Red'] 39 | # The new stack is: ['Red', 'Green', 'Blue', 'Red'] 40 | # The new queue is: ['Red', 'Green', 'Blue', 'Red'] 41 | # The original stack is: [] 42 | # The original queue is: [] 43 | -------------------------------------------------------------------------------- /Dynamic-Memory-Allocation/C programs/findPos.c: -------------------------------------------------------------------------------- 1 | // 2. Write a C program to find the 3rd / user defined position based 2 | // maximum element in an array using dynamic memory allocation. 3 | 4 | #include 5 | #include 6 | 7 | int main() 8 | { 9 | int n, *arr, rank; 10 | int i, j, temp; 11 | printf("Enter Total Number of Elements (Minimum 3): "); 12 | scanf("%d", &n); 13 | 14 | // Allocating Memory for n elements 15 | arr = (int *)malloc(n * sizeof(int)); 16 | if (arr == NULL) 17 | { 18 | printf("Memory is not allocated"); 19 | exit(0); 20 | } 21 | 22 | // storing numbers entered by the user 23 | for (int i = 0; i < n; i++) 24 | { 25 | printf("Enter Array[%d]: ", i); 26 | scanf("%d", arr + i); 27 | } 28 | printf("Enter Rank: "); 29 | scanf("%d", &rank); 30 | if (rank > n) 31 | { 32 | printf("Error!"); 33 | exit(0); 34 | } 35 | 36 | for (i = 0; i < n; i++) 37 | { 38 | for (j = 0; j < n - i - 1; j++) 39 | { 40 | if (*(arr + j) > *(arr + (j + 1))) 41 | { 42 | temp = *(arr + j); 43 | *(arr + j) = *(arr + (j + 1)); 44 | *(arr + (j + 1)) = temp; 45 | } 46 | } 47 | } 48 | 49 | printf("3rd Maximum Number: %d \n", *(arr + (n - 3))); 50 | printf("%dth Maximum Number: %d \n", rank, *(arr + (n - rank))); 51 | free(arr); 52 | return 0; 53 | } 54 | 55 | // OUTPUT 56 | // Enter Total Number of Elements (Minimum 3): 5 57 | // Enter Array[0]: 7 58 | // Enter Array[1]: 4 59 | // Enter Array[2]: 9 60 | // Enter Array[3]: 1 61 | // Enter Array[4]: 3 62 | // Enter Rank: 4 63 | // 3rd Maximum Number: 4 64 | // 4th Maximum Number: 3 -------------------------------------------------------------------------------- /Questions/Assignment-7@DSALAB.txt: -------------------------------------------------------------------------------- 1 | UEM New Logo 2 | 3 | University of Engineering & Management, Kolkata 4 | Course: B.Tech (CSE / CSE(AIML) / CSE(IOT-CYS-BCT) 5 | Semester: 3rd 6 | Paper Name: Data Structure & Algorithm Laboratory 7 | Paper Code: PCC - CS391 8 | Date of Assignment - 17.09.2022 9 | Date of Submission - 17.09.2022 10 | Section: CSE A, CSE B 11 | 12 | 13 | 14 | Assignment – VII 15 | (All programs to be implemented in C) 16 | 17 | Topic: Searching & Sorting 18 | 1. Write a program to implement Bubble Sort. 19 | 2. Write a program to implement Selection Sort. 20 | 3. Write a program to implement Insertion Sort. 21 | 4. Write a program to implement Merge Sort. 22 | 5. Write a program to implement linear search. 23 | 6. Write a program to implement binary search. 24 | 7. Write a program to implement the concept of Hashing. 25 | 26 | 27 | Assignment – VII 28 | (All programs to be implemented in Python) 29 | 30 | Topic: Searching & Sorting 31 | 1. Write a program to implement Bubble Sort. 32 | 2. Write a program to implement Selection Sort. 33 | 3. Write a program to implement Insertion Sort. 34 | 4. Write a program to implement Merge Sort. 35 | 5. Write a program to implement linear search. 36 | 6. Write a program to implement binary search. 37 | 38 | 39 | 40 | 41 | Instruction: 42 | 1. Mention the following thing using A4 pages: 43 | a. Program Objective 44 | b. Algorithm (if it is asked) 45 | c. Program Code. 46 | d. Output 47 | 2. Handwritten assignment solutions are required. No printout is allowed. 48 | 3. Submit all the assignments using the channel file. 49 | 4. One Index page must be attached there. 50 | 5. Late submission will not be entertained. Maintain the deadline. -------------------------------------------------------------------------------- /Questions/Assignment-9@DSALAB.txt: -------------------------------------------------------------------------------- 1 | UEM New Logo 2 | 3 | University of Engineering & Management, Kolkata 4 | Course: B.Tech (CSE / CSE(AIML) / CSE(IOT-CYS-BCT) 5 | Semester: 3rd 6 | Paper Name: Data Structure & Algorithm Laboratory 7 | Paper Code: PCC - CS391 8 | Date of Assignment - 11.10.2022 9 | Date of Submission - 11.10.2022 10 | Section: CSE A, CSE B 11 | 12 | 13 | 14 | Assignment – IX 15 | (All programs to be implemented in C & Python) 16 | 17 | Topic: Binary Search Tree 18 | 1. Write a program to implement Binary Search Tree using linked list along with the following functions: 19 | a. To create a binary search tree. 20 | b. To display tree using inorder. 21 | c. To display tree using preorder. 22 | d. To display tree using postorder. 23 | e. To count number of node present in the tree. 24 | f. To find the height of the tree. 25 | g. To find the number of leaf node. 26 | h. To find the number of internal node. 27 | i. To search a data present in the tree. 28 | j. To check complete binary tree. 29 | k. To insert a node. 30 | l. To delete a node. 31 | m. To check the balance factor of a node. 32 | n. To find the minimum value present in the tree. 33 | o. To find the maximum value present in the tree. 34 | p. To count number of NULL pointer present in the tree. 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | Instruction: 44 | 1. Mention the following thing using A4 pages: 45 | a. Program Objective 46 | b. Algorithm (if it is asked) 47 | c. Program Code. 48 | d. Output 49 | 2. Handwritten assignment solutions are required. No printout is allowed. 50 | 3. Submit all the assignments using the channel file. 51 | 4. One Index page must be attached there. 52 | 5. Late submission will not be entertained. Maintain the deadline. -------------------------------------------------------------------------------- /Linked List/C Programs ☠️/Singly/leftShift_SLL.c: -------------------------------------------------------------------------------- 1 | // Given a singly linked list of size N. The task is 2 | // to left-shift the linked list by k nodes, 3 | // where k is a given positive integer smaller 4 | // than or equal to length of the linked list. 5 | 6 | #include "singlylinkedlist.h" 7 | 8 | NODE *leftShift(NODE *, int, int); 9 | 10 | int main() 11 | { 12 | NODE *head = NULL; 13 | head = createList(head); 14 | displayList(head); 15 | 16 | NODE *curr = head; 17 | int n = 1, k; 18 | while (curr->next && ++n) 19 | curr = curr->next; 20 | printf("Enter k (should be <=%d): ", n); 21 | scanf("%d", &k); 22 | 23 | head = leftShift(head, n, k); 24 | printf("\nLinked List Left shifted by %d nodes\n", k); 25 | displayList(head); 26 | 27 | return 0; 28 | } 29 | 30 | NODE *leftShift(NODE *head, int n, int k) 31 | { 32 | if (k < 0 || k > n) 33 | { 34 | printf("Invalid!"); 35 | exit(1); 36 | } 37 | if (!head || !head->next || k == 0 || k == n) 38 | { 39 | return head; 40 | } 41 | NODE *tail = head; 42 | while (tail->next != NULL) 43 | tail = tail->next; 44 | tail->next = head; 45 | NODE *curr = head; 46 | for (int i = 1; i < k; i++) 47 | { 48 | curr = curr->next; 49 | } 50 | NODE *res = curr->next; 51 | curr->next = NULL; 52 | return res; 53 | } 54 | 55 | // OUTPUT 56 | // Creating Linked List... 57 | // Enter -1 to end 58 | // Enter the data: 1 59 | // Enter the data: 2 60 | // Enter the data: 3 61 | // Enter the data: 4 62 | // Enter the data: 5 63 | // Enter the data: -1 64 | // HEAD -> 1 -> 2 -> 3 -> 4 -> 5 -> NULL 65 | // Enter k (should be <=5): 2 66 | 67 | // Linked List Left shifted by 2 nodes 68 | // HEAD -> 3 -> 4 -> 5 -> 1 -> 2 -> NULL -------------------------------------------------------------------------------- /Stack/C programs/stack_SLL.c: -------------------------------------------------------------------------------- 1 | // Write a C program to implement the concept 2 | // of stack using single linked list. 3 | 4 | #include 5 | #include 6 | 7 | typedef struct Node 8 | { 9 | int data; 10 | struct Node *next; 11 | } NODE; 12 | 13 | NODE *push(NODE *, int); 14 | NODE *pop(NODE *); 15 | void display(NODE *); 16 | 17 | int main() 18 | { 19 | NODE *top = NULL; 20 | display(top); 21 | top = pop(top); 22 | 23 | top = push(top, 11); 24 | top = push(top, 12); 25 | top = push(top, 13); 26 | top = push(top, 14); 27 | display(top); 28 | 29 | top = pop(top); 30 | top = pop(top); 31 | top = push(top, 15); 32 | display(top); 33 | 34 | return 0; 35 | } 36 | 37 | NODE *push(NODE *top, int val) 38 | { 39 | NODE *newNode; 40 | newNode = (NODE *)malloc(sizeof(NODE)); 41 | newNode->data = val; 42 | if (top == NULL) 43 | { 44 | newNode->next = NULL; 45 | top = newNode; 46 | } 47 | else 48 | { 49 | newNode->next = top; 50 | top = newNode; 51 | } 52 | return top; 53 | } 54 | 55 | NODE *pop(NODE *top) 56 | { 57 | NODE *ptr; 58 | ptr = top; 59 | if (top == NULL) 60 | printf("Stack Underflow!\n"); 61 | else 62 | { 63 | top = top->next; 64 | free(ptr); 65 | } 66 | return top; 67 | } 68 | 69 | void display(NODE *top) 70 | { 71 | NODE *ptr; 72 | ptr = top; 73 | if (ptr == NULL) 74 | { 75 | printf("Stack is Empty\n"); 76 | return; 77 | } 78 | printf("TOP"); 79 | while (ptr != NULL) 80 | { 81 | printf(" -> %d", ptr->data); 82 | ptr = ptr->next; 83 | } 84 | printf(" -> NULL\n"); 85 | } 86 | 87 | // OUTPUT 88 | // Stack is Empty 89 | // Stack Underflow! 90 | // TOP -> 14 -> 13 -> 12 -> 11 -> NULL 91 | // TOP -> 15 -> 12 -> 11 -> NULL -------------------------------------------------------------------------------- /Questions/README4.md: -------------------------------------------------------------------------------- 1 |

Assignment 4

2 | 3 | > 💠 Singly Linked List
👉🏼 [Question](/Questions/Assignment-4%40DSALAB.txt) 4 | 5 | --- 6 | 7 | ## C Programs🐸 8 | 9 | _Write down this two header files before writing these C Programs
✴️[**singlylinkedlist.h**](/Linked%20List/C%20Programs%20%E2%98%A0%EF%B8%8F/Singly/singlylinkedlist.h) & ✴️[**polynomialLL.h**](/Linked%20List/C%20Programs%20%E2%98%A0%EF%B8%8F/Singly/polynomialLL.h)_ 10 | 11 | 1. [Reverse SLL](/Linked%20List/C%20Programs%20%E2%98%A0%EF%B8%8F/Singly/reverse_SLL.c) & [Reverse Algo](/Linked%20List/Algorithms%20%F0%9F%93%9D/ReverseAlgo.txt) 12 | 1. [Sorted Merge SLL](/Linked%20List/C%20Programs%20%E2%98%A0%EF%B8%8F/Singly/sortedMerge_SLL.c) & [Sorted Merge Algo](/Linked%20List/Algorithms%20%F0%9F%93%9D/Sorted%20Merge%20Algo.txt) 13 | 1. [Left Shift SLL](/Linked%20List/C%20Programs%20%E2%98%A0%EF%B8%8F/Singly/leftShift_SLL.c) 14 | 1. [Poly Add SLL](/Linked%20List/C%20Programs%20%E2%98%A0%EF%B8%8F/Singly/polyAdd_SLL.c) 15 | 1. [Swap Nodes SLL](/Linked%20List/C%20Programs%20%E2%98%A0%EF%B8%8F/Singly/swapNodes.c) 16 | 1. [Detect Cycle SLL](/Linked%20List/C%20Programs%20%E2%98%A0%EF%B8%8F/Singly/detectCycle_SLL.c) 17 | 1. [Poly Multiplication LL](/Linked%20List/C%20Programs%20%E2%98%A0%EF%B8%8F/Singly/polyMultiplication_LL.c) 18 | 1. [Stack using SLL](/Stack/C%20programs/stack_SLL.c) 19 | 1. [Queue using SLL](/Queue/C%20Programs/queue_SLL.c) 20 | 21 | --- 22 | 23 | ## Python Programs 🐍 24 | 25 | 1. [Reverse SLL](/Linked%20List/Python%20Programs%20%F0%9F%90%8D/Singly/reverse_SLL.py) 26 | 1. [Sorted Merge SLL](/Linked%20List/Python%20Programs%20%F0%9F%90%8D/Singly/sortedMerge_SLL.py) 27 | 1. [Poly Add SLL](/Linked%20List/Python%20Programs%20%F0%9F%90%8D/Singly/polyAdd_SLL.py) 28 | 1. [Swap Nodes SLL](/Linked%20List/Python%20Programs%20%F0%9F%90%8D/Singly/swapNodes_SLL.py) 29 | 1. [Detect Cycle SLL](/Linked%20List/Python%20Programs%20%F0%9F%90%8D/Singly/detectCycle_SLL.py) 30 | -------------------------------------------------------------------------------- /Linked List/C Programs ☠️/Singly/countNodes.c: -------------------------------------------------------------------------------- 1 | // Write a function to count the number of node present in a linked list. 2 | 3 | #include 4 | #include 5 | 6 | typedef struct node 7 | { 8 | int data; 9 | struct node *next; 10 | } NODE; 11 | 12 | NODE *createList(NODE *); 13 | int countNodes(NODE *); 14 | 15 | int main() 16 | { 17 | int n; 18 | NODE *head = NULL; 19 | head = createList(head); 20 | n = countNodes(head); 21 | printf("Number Of Nodes in this Linked List is %d", n); 22 | return 0; 23 | } 24 | 25 | NODE *createList(NODE *head) 26 | { 27 | NODE *newNode, *ptr; 28 | int num; 29 | printf("Creating Linked List...\n"); 30 | printf("Enter -1 to end\n"); 31 | printf("Enter the data: "); 32 | scanf("%d", &num); 33 | while (num != -1) 34 | { 35 | newNode = (NODE *)malloc(sizeof(NODE)); 36 | newNode->data = num; 37 | if (head == NULL) 38 | { 39 | newNode->next = NULL; 40 | head = newNode; 41 | } 42 | else 43 | { 44 | ptr = head; 45 | while (ptr->next != NULL) 46 | { 47 | ptr = ptr->next; 48 | } 49 | ptr->next = newNode; 50 | newNode->next = NULL; 51 | } 52 | printf("Enter the data: "); 53 | scanf("%d", &num); 54 | } 55 | return head; 56 | } 57 | 58 | int countNodes(NODE *head) 59 | { 60 | int count = 0; 61 | NODE *ptr = head; 62 | while (ptr != NULL) 63 | { 64 | count++; 65 | ptr = ptr->next; 66 | } 67 | return count; 68 | } 69 | 70 | // OUTPUT 71 | // Creating Linked List... 72 | // Enter -1 to end 73 | // Enter the data: 5 74 | // Enter the data: 6 75 | // Enter the data: 7 76 | // Enter the data: 8 77 | // Enter the data: -1 78 | // Number Of Nodes in this Linked List is 4 -------------------------------------------------------------------------------- /Linked List/C Programs ☠️/Singly/polynomialLL.h: -------------------------------------------------------------------------------- 1 | // 👇👇 Start writing from here 👇👇 2 | 3 | // This is polynomialLL.h header file 4 | // for polynomial linked list creation and display 5 | 6 | #include 7 | #include 8 | 9 | typedef struct node 10 | { 11 | int coeff; 12 | int expo; 13 | struct node *next; 14 | } NODE; 15 | 16 | NODE *insert(NODE *head, int co, int ex) 17 | { 18 | NODE *newNode = (NODE *)malloc(sizeof(NODE)); 19 | NODE *ptr; 20 | newNode->coeff = co; 21 | newNode->expo = ex; 22 | 23 | if (head == NULL || ex > head->expo) 24 | { 25 | newNode->next = head; 26 | head = newNode; 27 | } 28 | else 29 | { 30 | ptr = head; 31 | while (ptr->next != NULL && ex < ptr->next->expo) 32 | { 33 | ptr = ptr->next; 34 | } 35 | newNode->next = ptr->next; 36 | ptr->next = newNode; 37 | } 38 | return head; 39 | } 40 | 41 | NODE *create(NODE *head) 42 | { 43 | int n, i, coeff, expo; 44 | printf("Enter the Number of Terms: "); 45 | scanf("%d", &n); 46 | 47 | for (i = 0; i < n; i++) 48 | { 49 | printf("Enter the coefficient of term %d: ", i + 1); 50 | scanf("%d", &coeff); 51 | 52 | printf("Enter the Exponent of term %d: ", i + 1); 53 | scanf("%d", &expo); 54 | 55 | head = insert(head, coeff, expo); 56 | } 57 | return head; 58 | } 59 | 60 | void display(NODE *head) 61 | { 62 | if (head == NULL) 63 | { 64 | printf("No Polynomial"); 65 | exit(1); 66 | } 67 | printf("Polynomial Equation is\n"); 68 | NODE *ptr = head; 69 | while (ptr != NULL) 70 | { 71 | printf("( %dx^%d )", ptr->coeff, ptr->expo); 72 | ptr = ptr->next; 73 | if (ptr != NULL) 74 | { 75 | printf(" + "); 76 | } 77 | } 78 | } -------------------------------------------------------------------------------- /Searching & Sorting/C Programs/heapSort.c: -------------------------------------------------------------------------------- 1 | // Write a program to implement Heap sort. 2 | 3 | #include 4 | 5 | void heapSort(int[], int); 6 | void heapify(int[], int, int); 7 | void swap(int *, int *); 8 | void printArray(int[], int); 9 | 10 | int main() 11 | { 12 | int arr[] = {1, 12, 9, 5, 6, 10}; 13 | int n = sizeof(arr) / sizeof(arr[0]); 14 | 15 | heapSort(arr, n); 16 | 17 | printf("Sorted array is \n"); 18 | printArray(arr, n); 19 | } 20 | 21 | void printArray(int arr[], int n) 22 | { 23 | for (int i = 0; i < n; ++i) 24 | printf("%d ", arr[i]); 25 | printf("\n"); 26 | } 27 | 28 | // Main function to do heap sort 29 | void heapSort(int arr[], int n) 30 | { 31 | // Build max heap 32 | for (int i = n / 2 - 1; i >= 0; i--) 33 | heapify(arr, n, i); 34 | 35 | // Heap sort 36 | for (int i = n - 1; i >= 0; i--) 37 | { 38 | swap(&arr[0], &arr[i]); 39 | 40 | // Heapify root element to get highest element at root again 41 | heapify(arr, i, 0); 42 | } 43 | } 44 | 45 | void heapify(int arr[], int n, int i) 46 | { 47 | // Find largest among root, left child and right child 48 | int largest = i; 49 | int left = 2 * i + 1; 50 | int right = 2 * i + 2; 51 | 52 | if (left < n && arr[left] > arr[largest]) 53 | largest = left; 54 | 55 | if (right < n && arr[right] > arr[largest]) 56 | largest = right; 57 | 58 | // Swap and continue heapifying if root is not largest 59 | if (largest != i) 60 | { 61 | swap(&arr[i], &arr[largest]); 62 | heapify(arr, n, largest); 63 | } 64 | } 65 | 66 | void swap(int *a, int *b) 67 | { 68 | int temp = *a; 69 | *a = *b; 70 | *b = temp; 71 | } 72 | 73 | // OUTPUT 74 | // Sorted array is 75 | // 1 5 6 9 10 12 76 | 77 | // Worst complexity: n*log(n) 78 | // Average complexity: n*log(n) 79 | // Best complexity: n*log(n) 80 | // Space complexity: 1 81 | // Method: Selection 82 | // Stable: No -------------------------------------------------------------------------------- /Queue/Python Programs/circularQueue.py: -------------------------------------------------------------------------------- 1 | class MyCircularQueue(): 2 | def __init__(self, k): 3 | self.k = k # size of the circular queue 4 | # array to store the elements of the circular queue 5 | self.queue = [0] * k 6 | self.front = self.rear = -1 7 | 8 | def enqueue(self, data): 9 | 10 | if ((self.rear + 1) % self.k == self.front): 11 | print("The circular queue is full\n") 12 | 13 | elif (self.front == -1): 14 | self.front = 0 15 | self.rear = 0 16 | self.queue[self.rear] = data 17 | else: 18 | self.rear = (self.rear + 1) % self.k 19 | self.queue[self.rear] = data 20 | 21 | def dequeue(self): 22 | if (self.front == -1): 23 | print("The circular queue is empty\n") 24 | 25 | elif (self.front == self.rear): 26 | temp = self.queue[self.front] 27 | self.front = -1 28 | self.rear = -1 29 | return temp 30 | else: 31 | temp = self.queue[self.front] 32 | self.front = (self.front + 1) % self.k 33 | return temp 34 | 35 | def printCQueue(self): 36 | if(self.front == -1): 37 | print("No element in the circular queue") 38 | 39 | elif (self.rear >= self.front): 40 | for i in range(self.front, self.rear + 1): 41 | print(self.queue[i], end=" ") 42 | print() 43 | else: 44 | for i in range(self.front, self.k): 45 | print(self.queue[i], end=" ") 46 | for i in range(0, self.rear + 1): 47 | print(self.queue[i], end=" ") 48 | print() 49 | 50 | 51 | obj = MyCircularQueue(5) 52 | obj.enqueue(1) 53 | obj.enqueue(2) 54 | obj.enqueue(3) 55 | obj.enqueue(4) 56 | obj.enqueue(5) 57 | print("Initial queue") 58 | obj.printCQueue() 59 | 60 | obj.dequeue() 61 | print("After removing an element from the queue") 62 | obj.printCQueue() 63 | -------------------------------------------------------------------------------- /Queue/C Programs/queueUsingStack.c: -------------------------------------------------------------------------------- 1 | // C Program to Implement Queues using Stacks 2 | 3 | #include 4 | #include 5 | 6 | void push1(int); 7 | void push2(int); 8 | int pop1(); 9 | int pop2(); 10 | void enqueue(int); 11 | int dequeue(); 12 | void display(); 13 | 14 | int st1[10], st2[10]; 15 | int top1 = -1, top2 = -1; 16 | 17 | int main() 18 | { 19 | enqueue(10); 20 | enqueue(20); 21 | enqueue(30); 22 | dequeue(); 23 | enqueue(40); 24 | display(); 25 | return 0; 26 | } 27 | 28 | /*Function to push the element on to the stack*/ 29 | void push1(int data) 30 | { 31 | st1[++top1] = data; 32 | } 33 | 34 | /*Function to pop the element from the stack*/ 35 | int pop1() 36 | { 37 | return (st1[top1--]); 38 | } 39 | 40 | /*Function to push an element on to stack*/ 41 | void push2(int data) 42 | { 43 | st2[++top2] = data; 44 | } 45 | 46 | /*Function to pop an element from the stack*/ 47 | 48 | int pop2() 49 | { 50 | return (st2[top2--]); 51 | } 52 | 53 | /*Function to add an element into the queue using stack*/ 54 | void enqueue(int data) 55 | { 56 | push1(data); 57 | printf("\n%d inserted to queue", data); 58 | } 59 | 60 | /*Function to delete an element from the queue using stack*/ 61 | 62 | int dequeue() 63 | { 64 | int i, val; 65 | while (top1 != -1) 66 | { 67 | push2(pop1()); 68 | } 69 | val = pop2(); 70 | printf("\n%d deleted from queue", val); 71 | while (top2 != -1) 72 | { 73 | push1(pop2()); 74 | } 75 | return val; 76 | } 77 | 78 | /*Function to display the elements in the stack*/ 79 | 80 | void display() 81 | { 82 | int i; 83 | printf("\nFRONT--> "); 84 | for (i = 0; i <= top1; i++) 85 | { 86 | printf(" %d ", st1[i]); 87 | } 88 | printf(" <--REAR"); 89 | } 90 | 91 | // OUTPUT 92 | // 10 inserted to queue 93 | // 20 inserted to queue 94 | // 30 inserted to queue 95 | // 10 deleted from queue 96 | // 40 inserted to queue 97 | // FRONT--> 20 30 40 <--REAR -------------------------------------------------------------------------------- /Linked List/C Programs ☠️/Singly/detectCycle_SLL.c: -------------------------------------------------------------------------------- 1 | // Write a C program to detect a loop in a single linked list. 2 | 3 | #include "singlylinkedlist.h" 4 | 5 | void detectCycle(NODE *); 6 | 7 | int main() 8 | { 9 | NODE *head = NULL; 10 | printf("Please Enter More Than 3 data\n"); 11 | head = createList(head); 12 | displayList(head); 13 | detectCycle(head); 14 | 15 | NODE *tail = head; 16 | int size = 1; 17 | while (tail->next != NULL) 18 | { 19 | tail = tail->next; 20 | size++; 21 | } 22 | 23 | if (size >= 3) 24 | { 25 | tail->next = head->next->next; 26 | printf("\nLinked List Successfully Created with Cycle"); 27 | detectCycle(head); 28 | } 29 | return 0; 30 | } 31 | 32 | void detectCycle(NODE *head) 33 | { 34 | if (head == NULL || head->next == NULL) 35 | { 36 | printf("\nNo Cycle in Linked List"); 37 | return; 38 | } 39 | 40 | NODE *slow = head; 41 | NODE *fast = head; 42 | NODE *entry = head; 43 | 44 | while (fast->next && fast->next->next) 45 | { 46 | slow = slow->next; 47 | fast = fast->next->next; 48 | if (slow == fast) 49 | { 50 | while (slow != entry) 51 | { 52 | slow = slow->next; 53 | entry = entry->next; 54 | } 55 | printf("\nCycle Detected at Node Data: %d", entry->data); 56 | return; 57 | } 58 | } 59 | printf("\nNo Cycle in Linked List"); 60 | } 61 | 62 | // OUTPUT 63 | // Please Enter More Than 3 data 64 | // Creating Linked List... 65 | // Enter -1 to end 66 | // Enter the data: 16 67 | // Enter the data: 51 68 | // Enter the data: 29 69 | // Enter the data: 47 70 | // Enter the data: -1 71 | // HEAD -> 16 -> 51 -> 29 -> 47 -> NULL 72 | 73 | // No Cycle in Linked List 74 | // Linked List Successfully Created with Cycle 75 | // Cycle Detected at Node Data: 29 -------------------------------------------------------------------------------- /Linked List/C Programs ☠️/Singly/sortedMerge_SLL.c: -------------------------------------------------------------------------------- 1 | // Write a C program to merge two already sorted list 2 | 3 | #include "singlylinkedlist.h" 4 | 5 | NODE *sortedMerge(NODE *, NODE *); 6 | 7 | int main() 8 | { 9 | NODE *head1 = NULL, *head2 = NULL; 10 | printf("Enter First List\n"); 11 | head1 = createList(head1); 12 | displayList(head1); 13 | 14 | printf("\nEnter Second List\n"); 15 | head2 = createList(head2); 16 | displayList(head2); 17 | 18 | NODE *res = sortedMerge(head1, head2); 19 | printf("\nSorted Merge list is\n"); 20 | displayList(res); 21 | 22 | return 0; 23 | } 24 | 25 | NODE *sortedMerge(NODE *list1, NODE *list2) 26 | { 27 | if (list1 == NULL) 28 | return list2; 29 | if (list2 == NULL) 30 | return list1; 31 | if (list1->data > list2->data) 32 | { 33 | NODE *temp = list1; 34 | list1 = list2; 35 | list2 = temp; 36 | } 37 | 38 | NODE *res = list1; 39 | while (list1 != NULL && list2 != NULL) 40 | { 41 | NODE *tmp = NULL; 42 | while (list1 != NULL && list1->data <= list2->data) 43 | { 44 | tmp = list1; 45 | list1 = list1->next; 46 | } 47 | tmp->next = list2; 48 | 49 | // swap 50 | NODE *temp = list1; 51 | list1 = list2; 52 | list2 = temp; 53 | } 54 | return res; 55 | } 56 | 57 | // OUTPUT 58 | 59 | // Enter First List 60 | // Creating Linked List... 61 | // Enter -1 to end 62 | // Enter the data: 5 63 | // Enter the data: 7 64 | // Enter the data: 9 65 | // Enter the data: -1 66 | // HEAD -> 5 -> 7 -> 9 -> NULL 67 | 68 | // Enter Second List 69 | // Creating Linked List... 70 | // Enter -1 to end 71 | // Enter the data: 3 72 | // Enter the data: 4 73 | // Enter the data: 8 74 | // Enter the data: 10 75 | // Enter the data: -1 76 | // HEAD -> 3 -> 4 -> 8 -> 10 -> NULL 77 | 78 | // Sorted Merge list is 79 | // HEAD -> 3 -> 4 -> 5 -> 7 -> 8 -> 9 -> 10 -> NULL -------------------------------------------------------------------------------- /Questions/Assignment-6@DSALAB.txt: -------------------------------------------------------------------------------- 1 | University of Engineering & Management, Kolkata 2 | Course: B.Tech (CSE / CSE(AIML) / CSE(IOT-CYS-BCT) 3 | Semester: 3rd 4 | Paper Name: Data Structure & Algorithm Laboratory 5 | Paper Code: PCC - CS391 6 | Date of Assignment - 07.09.2022 7 | Date of Submission - 07.09.2022 8 | Section: CSE A, CSE B 9 | 10 | 11 | Assignment – VI 12 | (All programs to be implemented in C) 13 | 14 | Topic: Application of Stack & Queue 15 | 1. Write a C program to evaluate a postfix expression. 16 | 2. Write a C program to convert an infix expression to postfix expression. 17 | 3. Write a C program to implement Priority Queue. 18 | 4. Write a C program to calculate factorial with the help of stack. 19 | 5. Write a C program to solve the tower of Hanoi using stack. 20 | 6. Write a C program to reverse a string using stack. 21 | 7. Write a C program to validate the parenthesis of an expression. 22 | 8. Write a C program to validate an html tag. 23 | 24 | 25 | 26 | Assignment – VI 27 | (All programs to be implemented in Python) 28 | 29 | Topic: Application of Stack & Queue 30 | 1. Write a Python program to calculate factorial with the help of stack. 31 | 2. Write a Python program to solve the tower of Hanoi using stack. 32 | 3. Write a Python program to reverse a string using stack. 33 | 4. Write a Python program to validate the parenthesis of an expression. 34 | 35 | 36 | 37 | Instruction: 38 | 1. Mention the following thing using A4 pages: 39 | a. Program Objective 40 | b. Algorithm (if it is asked) 41 | c. Program Code. 42 | d. Output 43 | 44 | 45 | 2. Handwritten assignment solutions are required. No printout is allowed. 46 | 3. Submit all the assignments using the channel file. 47 | 4. One Index page must be attached there. 48 | 5. Late submission will not be entertained. Maintain the deadline. 49 | -------------------------------------------------------------------------------- /Graph/Python Programs/adjacencyList.py: -------------------------------------------------------------------------------- 1 | # Write a Python program to implement Graph using Adjacency list along with the following function: 2 | # a. To count number of vertices and edges present in a graph. 3 | # b. To find the adjacent vertices of a given vertex. 4 | # c. To search a node in a given graph. 5 | 6 | 7 | class AdjNode: 8 | def __init__(self, value): 9 | self.vertex = value 10 | self.next = None 11 | 12 | 13 | class Graph: 14 | def __init__(self, num): 15 | self.V = num 16 | self.graph = [None] * self.V 17 | 18 | def addEdge(self, s, d): 19 | node = AdjNode(d) 20 | node.next = self.graph[s] 21 | self.graph[s] = node 22 | 23 | node = AdjNode(s) 24 | node.next = self.graph[d] 25 | self.graph[d] = node 26 | 27 | def printGraph(self): 28 | for i in range(self.V): 29 | print("\nVertex", i, end="") 30 | temp = self.graph[i] 31 | while temp: 32 | print(" -> {}".format(temp.vertex), end="") 33 | temp = temp.next 34 | 35 | 36 | if __name__ == "__main__": 37 | n = int(input("Enter Number of Vertices: ")) 38 | m = int(input("Enter Number of Edges: ")) 39 | graph = Graph(n) 40 | for i in range(m): 41 | u, v = map(int, input("Add Edge Between: ").split()) 42 | graph.addEdge(u, v) 43 | graph.printGraph() 44 | 45 | 46 | """ 47 | 0-----1 48 | / \ | 6 49 | 3 \ | / \ 50 | | \ | 7----5 51 | 4 ---- 2 52 | 53 | """ 54 | 55 | # OUTPUT 56 | # Enter Number of Vertices: 8 57 | # Enter Number of Edges: 9 58 | # Add Edge Between: 0 1 59 | # Add Edge Between: 0 2 60 | # Add Edge Between: 0 3 61 | # Add Edge Between: 1 2 62 | # Add Edge Between: 2 4 63 | # Add Edge Between: 3 4 64 | # Add Edge Between: 5 6 65 | # Add Edge Between: 5 7 66 | # Add Edge Between: 6 7 67 | 68 | # Vertex 0 -> 3 -> 2 -> 1 69 | # Vertex 1 -> 2 -> 0 70 | # Vertex 2 -> 4 -> 1 -> 0 71 | # Vertex 3 -> 4 -> 0 72 | # Vertex 4 -> 3 -> 2 73 | # Vertex 5 -> 7 -> 6 74 | # Vertex 6 -> 7 -> 5 75 | # Vertex 7 -> 6 -> 5 76 | -------------------------------------------------------------------------------- /Linked List/Python Programs 🐍/Singly/reverse_SLL.py: -------------------------------------------------------------------------------- 1 | # Write a python program to reverse an 2 | # already created single linked list. 3 | 4 | class Node: 5 | def __init__(self, data): 6 | self.data = data 7 | self.next = None 8 | 9 | 10 | class LinkedList: 11 | def __init__(self): 12 | self.head = None 13 | 14 | def createList(self): 15 | print("Creating Linked List...") 16 | print("Enter -1 to end") 17 | num = int(input("Enter the data: ")) 18 | while(num != -1): 19 | newNode = Node(num) 20 | if self.head is None: 21 | self.head = newNode 22 | else: 23 | last = self.head 24 | while last.next: 25 | last = last.next 26 | last.next = newNode 27 | num = int(input("Enter the data: ")) 28 | 29 | def displayList(self): 30 | if self.head is None: 31 | print("List is empty") 32 | return 33 | print("HEAD", end='') 34 | temp = self.head 35 | while temp: 36 | print(" -> " + str(temp.data), end='') 37 | temp = temp.next 38 | print(" -> NULL\n") 39 | 40 | def reverse(self): 41 | curr = self.head 42 | prev = None 43 | while curr is not None: 44 | temp = curr.next 45 | curr.next = prev 46 | prev = curr 47 | curr = temp 48 | self.head = prev 49 | 50 | 51 | myList = LinkedList() 52 | myList.createList() 53 | print("Original List:") 54 | myList.displayList() 55 | myList.reverse() 56 | print("Reversed List:") 57 | myList.displayList() 58 | 59 | 60 | # OUTPUT 61 | # Creating Linked List... 62 | # Enter - 1 to end 63 | # Enter the data: 1 64 | # Enter the data: 2 65 | # Enter the data: 3 66 | # Enter the data: 4 67 | # Enter the data: 5 68 | # Enter the data: -1 69 | # Original List: 70 | # HEAD -> 1 -> 2 -> 3 -> 4 -> 5 -> NULL 71 | 72 | # Reversed List: 73 | # HEAD -> 5 -> 4 -> 3 -> 2 -> 1 -> NULL 74 | -------------------------------------------------------------------------------- /Stack/C programs/stack_Arr.c: -------------------------------------------------------------------------------- 1 | // Write a C program to implement stack using dynamic array and perform 2 | // the following operation • PUSH() • POP() • PEEK() • DISPLAY() 3 | 4 | #include 5 | #include 6 | 7 | int *st = NULL, top = -1, size = 0; 8 | void push(int); 9 | int pop(void); 10 | int peek(void); 11 | void display(void); 12 | 13 | int main() 14 | { 15 | int n; 16 | st = (int *)malloc(size * sizeof(int)); 17 | push(10); 18 | push(20); 19 | push(30); 20 | display(); 21 | n = pop(); 22 | printf("\n%d is poped from stack", n); 23 | n = peek(); 24 | printf("\n%d is peek element", n); 25 | 26 | free(st); 27 | return 0; 28 | } 29 | 30 | void push(int val) 31 | { 32 | if (top == size - 1) 33 | { 34 | size++; 35 | st = (int *)realloc(st, size * sizeof(int)); 36 | } 37 | top++; 38 | *(st + top) = val; 39 | printf("\n%d is pushed to the stack", val); 40 | } 41 | 42 | int pop(void) 43 | { 44 | int val; 45 | if (top == -1) 46 | { 47 | printf("Stack is underflowed"); 48 | return -1; 49 | } 50 | else 51 | { 52 | val = *(st + top); 53 | top--; 54 | size--; 55 | st = (int *)realloc(st, size * sizeof(int)); 56 | return val; 57 | } 58 | } 59 | 60 | int peek(void) 61 | { 62 | if (top == -1) 63 | { 64 | printf("Stack is empty"); 65 | return -1; 66 | } 67 | else 68 | { 69 | return *(st + top); 70 | } 71 | } 72 | 73 | void display(void) 74 | { 75 | if (top == -1) 76 | { 77 | printf("Stack is empty"); 78 | } 79 | else 80 | { 81 | for (int i = top; i >= 0; i--) 82 | { 83 | printf("\nStack[%d]: %d", i, *(st + i)); 84 | } 85 | } 86 | } 87 | 88 | // OUTPUT 89 | // 10 is pushed to the stack 90 | // 20 is pushed to the stack 91 | // 30 is pushed to the stack 92 | // Stack[2]: 30 93 | // Stack[1]: 20 94 | // Stack[0]: 10 95 | // 30 is poped from stack 96 | // 20 is peek element 97 | 98 | // Write the algorithms of push and pop 99 | // after writing the output -------------------------------------------------------------------------------- /Dynamic-Memory-Allocation/C programs/sortedMerge.c: -------------------------------------------------------------------------------- 1 | // 7. Write a C program to merge two sorted dynamic array. 2 | 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | int *arr1, *arr2, *arr3; 9 | int m, n, i; 10 | printf("Enter Size of first Array: "); 11 | scanf("%d", &m); 12 | printf("Enter Size of second Array: "); 13 | scanf("%d", &n); 14 | arr1 = (int *)malloc(m * sizeof(int)); 15 | arr2 = (int *)malloc(n * sizeof(int)); 16 | arr3 = (int *)malloc((m + n) * sizeof(int)); 17 | 18 | // Getting array elements 19 | for (i = 0; i < m; i++) 20 | { 21 | printf("Enter first Array[%d]: ", i); 22 | scanf("%d", arr1 + i); 23 | } 24 | printf("\n"); 25 | for (i = 0; i < n; i++) 26 | { 27 | printf("Enter Second Array[%d]: ", i); 28 | scanf("%d", arr2 + i); 29 | } 30 | 31 | // Merging Both Arrays 32 | i = 0; 33 | int j = 0, k = 0; 34 | while (i < m && j < n) 35 | { 36 | if (*(arr1 + i) < *(arr2 + j)) 37 | { 38 | *(arr3 + k) = *(arr1 + i); 39 | k++; 40 | i++; 41 | } 42 | else 43 | { 44 | *(arr3 + k) = *(arr2 + j); 45 | k++; 46 | j++; 47 | } 48 | } 49 | while (i < m) 50 | { 51 | *(arr3 + k) = *(arr1 + i); 52 | k++; 53 | i++; 54 | } 55 | while (j < n) 56 | { 57 | *(arr3 + k) = *(arr2 + j); 58 | k++; 59 | j++; 60 | } 61 | 62 | printf("\nPrintng Merged Array\n"); 63 | for (i = 0; i < m + n; i++) 64 | { 65 | printf("%d ", *(arr3 + i)); 66 | } 67 | 68 | free(arr1); 69 | free(arr2); 70 | free(arr3); 71 | return 0; 72 | } 73 | 74 | // OUTPUT 75 | // Enter Size of first Array: 4 76 | // Enter Size of second Array: 3 77 | // Enter first Array[0]: 1 78 | // Enter first Array[1]: 4 79 | // Enter first Array[2]: 9 80 | // Enter first Array[3]: 10 81 | 82 | // Enter Second Array[0]: 2 83 | // Enter Second Array[1]: 3 84 | // Enter Second Array[2]: 12 85 | 86 | // Printng Merged Array 87 | // 1 2 3 4 9 10 12 -------------------------------------------------------------------------------- /Linked List/C Programs ☠️/Singly/polyMultiplication_LL.c: -------------------------------------------------------------------------------- 1 | // Write a C program to perform multiplication 2 | // of two polynomials using linked list 3 | 4 | #include "polynomialLL.h" 5 | 6 | NODE *polyMult(NODE *, NODE *); 7 | 8 | int main() 9 | { 10 | NODE *poly1 = NULL, *poly2 = NULL; 11 | 12 | printf("Enter First Polynomial\n"); 13 | poly1 = create(poly1); 14 | display(poly1); 15 | 16 | printf("\nEnter Second Polynomial\n"); 17 | poly2 = create(poly2); 18 | display(poly2); 19 | 20 | NODE *product = polyMult(poly1, poly2); 21 | printf("\nPolynomial Multiplied\n"); 22 | display(product); 23 | return 0; 24 | } 25 | 26 | NODE *polyMult(NODE *p1, NODE *p2) 27 | { 28 | NODE *product = NULL; 29 | if (p1 == NULL || p2 == NULL) 30 | { 31 | printf("Cannot Multiply"); 32 | return product; 33 | } 34 | NODE *poly1 = p1, *poly2 = p2; 35 | int co, ex; 36 | while (poly1 != NULL) 37 | { 38 | while (poly2 != NULL) 39 | { 40 | co = poly1->coeff * poly2->coeff; 41 | ex = poly1->expo + poly2->expo; 42 | product = insert(product, co, ex); 43 | poly2 = poly2->next; 44 | } 45 | poly1 = poly1->next; 46 | poly2 = p2; 47 | } 48 | return product; 49 | } 50 | 51 | // OUTPUT 52 | // Enter First Polynomial 53 | // Enter the Number of Terms: 3 54 | // Enter the coefficient of term 1: 4 55 | // Enter the Exponent of term 1: 2 56 | // Enter the coefficient of term 2: 7 57 | // Enter the Exponent of term 2: 1 58 | // Enter the coefficient of term 3: 9 59 | // Enter the Exponent of term 3: 0 60 | // Polynomial Equation is 61 | // ( 4x^2 ) + ( 7x^1 ) + ( 9x^0 ) 62 | // Enter Second Polynomial 63 | // Enter the Number of Terms: 2 64 | // Enter the coefficient of term 1: 6 65 | // Enter the Exponent of term 1: 1 66 | // Enter the coefficient of term 2: 3 67 | // Enter the Exponent of term 2: 0 68 | // Polynomial Equation is 69 | // ( 6x^1 ) + ( 3x^0 ) 70 | // Polynomial Multiplied 71 | // Polynomial Equation is 72 | // ( 24x^3 ) + ( 42x^2 ) + ( 12x^2 ) + ( 54x^1 ) + ( 21x^1 ) + ( 27x^0 ) 73 | -------------------------------------------------------------------------------- /Linked List/Python Programs 🐍/Singly/polynomial_SL.py: -------------------------------------------------------------------------------- 1 | # Write a C program to store polynomial using 2 | # linked list. Store the term in descending order. 3 | 4 | class Node: 5 | def __init__(self, coeff, expo): 6 | self.coeff = coeff 7 | self.expo = expo 8 | self.next = None 9 | 10 | 11 | class Polynomial: 12 | def __init__(self): 13 | self.head = None 14 | 15 | def createList(self): 16 | n = int(input("Enter number of terms: ")) 17 | for i in range(n): 18 | coeff = int(input("Enter the Coefficient of term "+str(i+1)+": ")) 19 | expo = int(input("Enter the Exponent of term " + str(i+1) + ": ")) 20 | self.insert(coeff, expo) 21 | 22 | def insert(self, co, ex): 23 | newNode = Node(co, ex) 24 | temp = self.head 25 | if self.head is None or ex > temp.expo: 26 | newNode.next = self.head 27 | self.head = newNode 28 | else: 29 | while temp.next is not None and ex < temp.next.expo: 30 | temp = temp.next 31 | newNode.next = temp.next 32 | temp.next = newNode 33 | 34 | def display(self): 35 | if self.head is None: 36 | print("No Polynomial") 37 | return 38 | print("Your Polynomial Equation is:") 39 | temp = self.head 40 | while temp: 41 | print("( "+str(temp.coeff)+"x^"+str(temp.expo)+" )", end='') 42 | temp = temp.next 43 | if temp is not None: 44 | print(" + ", end='') 45 | 46 | 47 | poly = Polynomial() 48 | print("Enter the Polynomial...") 49 | poly.createList() 50 | poly.display() 51 | 52 | # OUTPUT 53 | # Enter the Polynomial... 54 | # Enter number of terms: 4 55 | # Enter the Coefficient of term 1: 10 56 | # Enter the Exponent of term 1: 2 57 | # Enter the Coefficient of term 2: 20 58 | # Enter the Exponent of term 2: 8 59 | # Enter the Coefficient of term 3: 30 60 | # Enter the Exponent of term 3: 4 61 | # Enter the Coefficient of term 4: 40 62 | # Enter the Exponent of term 4: 6 63 | # Your Polynomial Equation is: 64 | # ( 20x^8 ) + ( 40x^6 ) + ( 30x^4 ) + ( 10x^2 ) 65 | -------------------------------------------------------------------------------- /Questions/README3.md: -------------------------------------------------------------------------------- 1 |

Assignment 3

2 | 3 | > 💠 Singly Linked List
👉🏼 [Question](/Questions/Assignment-3%40DSALAB.txt) 4 | 5 | --- 6 | 7 | | Question | C Programs ☠️ | Python Programs 🐍 | 8 | | -------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------- | 9 | | 1 | [Insert SLL](/Linked%20List/C%20Programs%20%E2%98%A0%EF%B8%8F/Singly/insert_LL.c) & [Insert Algo](/Linked%20List/Algorithms%20%F0%9F%93%9D/InsertAlgo.txt) | [Insert SLL](/Linked%20List/Python%20Programs%20%F0%9F%90%8D/Singly/insert_SL.py) | 10 | | 2 | [Delete SLL](/Linked%20List/C%20Programs%20%E2%98%A0%EF%B8%8F/Singly/delete_LL.c) & [Delete Algo](/Linked%20List/Algorithms%20%F0%9F%93%9D/DeleteAlgo.txt) | [Delete SLL](/Linked%20List/Python%20Programs%20%F0%9F%90%8D/Singly/delete_SL.py) | 11 | | 3 | [Singly LL](/Linked%20List/C%20Programs%20%E2%98%A0%EF%B8%8F/Singly/singly_LL.c) | [Singly SLL](/Linked%20List/Python%20Programs%20%F0%9F%90%8D/Singly/singly_LL.py) | 12 | | 4 | [Polynomial SLL](/Linked%20List/C%20Programs%20%E2%98%A0%EF%B8%8F/Singly/polynomial_LL.c) | [Polynomial SLL](/Linked%20List/Python%20Programs%20%F0%9F%90%8D/Singly/polynomial_SL.py) | 13 | | 5 | [Count Nodes](/Linked%20List/C%20Programs%20%E2%98%A0%EF%B8%8F/Singly/countNodes.c) | [Count Nodes](/Linked%20List/Python%20Programs%20%F0%9F%90%8D/Singly/countNodes.py) | 14 | | 6 | [Odd Even Node SLL](/Linked%20List/C%20Programs%20%E2%98%A0%EF%B8%8F/Singly/oddEvenNode_LL.c) | [Odd Even Node](/Linked%20List/Python%20Programs%20%F0%9F%90%8D/Singly/oddEvenNode.py) | 15 | -------------------------------------------------------------------------------- /Linked List/Python Programs 🐍/Singly/swapNodes_SLL.py: -------------------------------------------------------------------------------- 1 | # Given a singly linked list of size N. The task is 2 | # to swap elements in the linked list pairwise. 3 | 4 | class Node: 5 | def __init__(self, data): 6 | self.data = data 7 | self.next = None 8 | 9 | 10 | class LinkedList: 11 | def __init__(self): 12 | self.head = None 13 | 14 | def createList(self): 15 | print("Creating Linked List...") 16 | print("Enter -1 to end") 17 | num = int(input("Enter the data: ")) 18 | while(num != -1): 19 | newNode = Node(num) 20 | if self.head is None: 21 | self.head = newNode 22 | else: 23 | last = self.head 24 | while last.next: 25 | last = last.next 26 | last.next = newNode 27 | num = int(input("Enter the data: ")) 28 | 29 | def displayList(self): 30 | if self.head is None: 31 | print("List is empty") 32 | return 33 | print("HEAD", end='') 34 | temp = self.head 35 | while temp: 36 | print(" -> " + str(temp.data), end='') 37 | temp = temp.next 38 | print(" -> NULL\n") 39 | 40 | def swapNodes(self): 41 | dummy = Node(0) 42 | curr = self.head 43 | if curr is None or curr.next is None: 44 | return 45 | prev = dummy 46 | while curr and curr.next: 47 | prev.next = curr.next 48 | curr.next = curr.next.next 49 | prev.next.next = curr 50 | curr = curr.next 51 | prev = prev.next.next 52 | self.head = dummy.next 53 | 54 | 55 | myList = LinkedList() 56 | myList.createList() 57 | myList.displayList() 58 | print("List After Swapping") 59 | myList.swapNodes() 60 | myList.displayList() 61 | 62 | 63 | # OUTPUT 64 | # Creating Linked List... 65 | # Enter -1 to end 66 | # Enter the data: 10 67 | # Enter the data: 20 68 | # Enter the data: 30 69 | # Enter the data: 40 70 | # Enter the data: 50 71 | # Enter the data: -1 72 | # HEAD -> 10 -> 20 -> 30 -> 40 -> 50 -> NULL 73 | 74 | # List After Swapping 75 | # HEAD -> 20 -> 10 -> 40 -> 30 -> 50 -> NULL 76 | -------------------------------------------------------------------------------- /Queue/C Programs/queue_SLL.c: -------------------------------------------------------------------------------- 1 | // Write a C program to implement the concept 2 | // of queue using single linked list. 3 | 4 | #include 5 | #include 6 | 7 | typedef struct Node 8 | { 9 | int data; 10 | struct Node *next; 11 | } NODE; 12 | 13 | typedef struct Queue 14 | { 15 | NODE *front; 16 | NODE *rear; 17 | } QUEUE; 18 | 19 | QUEUE *createQueue(QUEUE *); 20 | QUEUE *enqueue(QUEUE *, int); 21 | QUEUE *dequeue(QUEUE *); 22 | void display(QUEUE *); 23 | 24 | int main() 25 | { 26 | QUEUE *q; 27 | q = createQueue(q); 28 | display(q); 29 | q = dequeue(q); 30 | 31 | q = enqueue(q, 11); 32 | q = enqueue(q, 12); 33 | q = enqueue(q, 13); 34 | q = enqueue(q, 14); 35 | display(q); 36 | 37 | q = dequeue(q); 38 | q = dequeue(q); 39 | q = enqueue(q, 15); 40 | display(q); 41 | 42 | return 0; 43 | } 44 | 45 | QUEUE *createQueue(QUEUE *q) 46 | { 47 | q->front = NULL; 48 | q->rear = NULL; 49 | return q; 50 | } 51 | 52 | QUEUE *enqueue(QUEUE *q, int val) 53 | { 54 | NODE *newNode = (NODE *)malloc(sizeof(NODE)); 55 | newNode->data = val; 56 | if (q->front == NULL) 57 | { 58 | q->front = q->rear = newNode; 59 | q->front->next = q->rear->next = NULL; 60 | } 61 | else 62 | { 63 | q->rear->next = newNode; 64 | q->rear = newNode; 65 | q->rear->next = NULL; 66 | } 67 | return q; 68 | } 69 | 70 | QUEUE *dequeue(QUEUE *q) 71 | { 72 | if (q->front == NULL) 73 | printf("UNDERFLOW!\n"); 74 | else 75 | { 76 | NODE *ptr = (NODE *)malloc(sizeof(NODE)); 77 | ptr = q->front; 78 | q->front = q->front->next; 79 | free(ptr); 80 | } 81 | return q; 82 | } 83 | 84 | void display(QUEUE *q) 85 | { 86 | NODE *ptr; 87 | ptr = q->front; 88 | if (ptr == NULL) 89 | { 90 | printf("Queue is Empty\n"); 91 | return; 92 | } 93 | printf("FRONT"); 94 | while (ptr != NULL) 95 | { 96 | printf(" -> %d", ptr->data); 97 | ptr = ptr->next; 98 | } 99 | printf(" <- REAR\n"); 100 | } 101 | 102 | // OUTPUT 103 | // Queue is Empty 104 | // UNDERFLOW! 105 | // FRONT -> 11 -> 12 -> 13 -> 14 <- REAR 106 | // FRONT -> 13 -> 14 -> 15 <- REAR -------------------------------------------------------------------------------- /Linked List/Python Programs 🐍/Singly/oddEvenNode.py: -------------------------------------------------------------------------------- 1 | # Write a program to print the odd number and even 2 | # number nodes separately from a single linked list 3 | 4 | class Node: 5 | def __init__(self, data): 6 | self.data = data 7 | self.next = None 8 | 9 | 10 | class LinkedList: 11 | 12 | def __init__(self): 13 | self.head = None 14 | 15 | def create(self): 16 | print("Creating Linked List...") 17 | print("Enter -1 to end") 18 | num = int(input("Enter the data: ")) 19 | while (num != -1): 20 | self.insert(num) 21 | num = int(input("Enter the data: ")) 22 | 23 | def insert(self, num): 24 | newNode = Node(num) 25 | if self.head is None: 26 | self.head = newNode 27 | return 28 | last = self.head 29 | while last.next: 30 | last = last.next 31 | last.next = newNode 32 | 33 | def display(self): 34 | if self.head is None: 35 | print("List is empty") 36 | return 37 | print("HEAD", end='') 38 | temp = self.head 39 | while temp: 40 | print(" -> " + str(temp.data), end='') 41 | temp = temp.next 42 | print(" -> NULL\n") 43 | 44 | def seperateOddEven(self): 45 | if not self.head: 46 | return 47 | odd, even = LinkedList(), LinkedList() 48 | count = 1 49 | temp = self.head 50 | while temp: 51 | if count % 2 != 0: 52 | odd.insert(temp.data) 53 | else: 54 | even.insert(temp.data) 55 | temp = temp.next 56 | count += 1 57 | return odd, even 58 | 59 | 60 | myList = LinkedList() 61 | myList.create() 62 | print("Original List: ") 63 | myList.display() 64 | odd, even = myList.seperateOddEven() 65 | print("Odd Nodes: ") 66 | odd.display() 67 | print("Even Nodes: ") 68 | even.display() 69 | 70 | 71 | # OUTPUT 72 | # Creating Linked List... 73 | # Enter - 1 to end 74 | # Enter the data: 10 75 | # Enter the data: 20 76 | # Enter the data: 30 77 | # Enter the data: 40 78 | # Enter the data: 50 79 | # Enter the data: -1 80 | # Original List: 81 | # HEAD -> 10 -> 20 -> 30 -> 40 -> 50 -> NULL 82 | 83 | # Odd Nodes: 84 | # HEAD -> 10 -> 30 -> 50 -> NULL 85 | 86 | # Even Nodes: 87 | # HEAD -> 20 -> 40 -> NULL 88 | -------------------------------------------------------------------------------- /Queue/C Programs/queue_Arr.c: -------------------------------------------------------------------------------- 1 | // Write a C program to implement queue using dynamic array. 2 | // Execute all the necessary functions of queue. 3 | 4 | #include 5 | #include 6 | 7 | int *queue, front = -1, rear = -1, size; 8 | 9 | void insert(int); 10 | int del(void); 11 | int peek(void); 12 | void display(void); 13 | 14 | int main() 15 | { 16 | int n; 17 | printf("ENTER SIZE OF QUEUE: "); 18 | scanf("%d", &size); 19 | queue = (int *)malloc(sizeof(int) * size); 20 | 21 | insert(10); 22 | insert(20); 23 | insert(30); 24 | display(); 25 | n = del(); 26 | printf("\n%d is deleted", n); 27 | n = peek(); 28 | printf("\nFirst value in queue is %d", n); 29 | 30 | free(queue); 31 | return 0; 32 | } 33 | 34 | void insert(int val) 35 | { 36 | if (rear == size - 1) 37 | { 38 | printf("\nTHE QUEUE IS FULL"); 39 | } 40 | else 41 | { 42 | if (front == -1) 43 | { 44 | front++; 45 | } 46 | rear++; 47 | *(queue + rear) = val; 48 | printf("\n%d has been inserted", val); 49 | } 50 | } 51 | 52 | int del() 53 | { 54 | int val; 55 | if (front == -1 || front > rear) 56 | { 57 | printf("THE QUEUE IS EMPTY"); 58 | return -1; 59 | } 60 | else 61 | { 62 | val = *(queue + front); 63 | front++; 64 | if (front > rear) 65 | { 66 | front = rear = -1; 67 | } 68 | return val; 69 | } 70 | } 71 | 72 | int peek(void) 73 | { 74 | if (front == -1 || front > rear) 75 | { 76 | printf("THE QUEUE IS EMPTY"); 77 | return -1; 78 | } 79 | else 80 | { 81 | return *(queue + front); 82 | } 83 | } 84 | 85 | void display(void) 86 | { 87 | int i; 88 | if (front == -1 || front > rear) 89 | { 90 | printf("\nQUEUE IS EMPTY"); 91 | } 92 | else 93 | { 94 | printf("\nFRONT --> "); 95 | for (i = front; i <= rear; i++) 96 | { 97 | printf(" %d ", *(queue + i)); 98 | } 99 | printf("<-- REAR"); 100 | } 101 | } 102 | 103 | // OUTPUT 104 | // ENTER SIZE OF QUEUE: 4 105 | // 10 has been inserted 106 | // 20 has been inserted 107 | // 30 has been inserted 108 | // FRONT --> 10 20 30 <-- REAR 109 | // 10 is deleted 110 | // First value in queue is 20 -------------------------------------------------------------------------------- /Dynamic-Memory-Allocation/C programs/mergeUnsorted.c: -------------------------------------------------------------------------------- 1 | // 8. Write a C program to merge two unsorted 2 | // dynamic array in sorted order 3 | 4 | #include 5 | #include 6 | 7 | void bubbleSort(int *arr, int n); 8 | void sortedMerge(int *arr1, int *arr2, int *res, int m, int n); 9 | 10 | int main() 11 | { 12 | int *arr1, *arr2, *res; 13 | int n, m, i; 14 | printf("Enter Size of first Array: "); 15 | scanf("%d", &m); 16 | printf("Enter Size of second Array: "); 17 | scanf("%d", &n); 18 | arr1 = (int *)malloc(m * sizeof(int)); 19 | arr2 = (int *)malloc(n * sizeof(int)); 20 | res = (int *)malloc((m + n) * sizeof(int)); 21 | 22 | // Getting array elements 23 | for (i = 0; i < m; i++) 24 | { 25 | printf("Enter first Array[%d]: ", i); 26 | scanf("%d", arr1 + i); 27 | } 28 | printf("\n"); 29 | for (i = 0; i < n; i++) 30 | { 31 | printf("Enter Second Array[%d]: ", i); 32 | scanf("%d", arr2 + i); 33 | } 34 | 35 | sortedMerge(arr1, arr2, res, m, n); 36 | 37 | printf("\nThe merged and sorted array is\n"); 38 | for (i = 0; i < m + n; i++) 39 | { 40 | printf("%d ", *(res + i)); 41 | } 42 | 43 | free(arr1); 44 | free(arr2); 45 | free(res); 46 | return 0; 47 | } 48 | 49 | void bubbleSort(int *arr, int n) 50 | { 51 | int i, j, temp; 52 | for (i = 0; i < n; i++) 53 | { 54 | for (j = 0; j < n - i - 1; j++) 55 | { 56 | if (*(arr + j) > *(arr + (j + 1))) 57 | { 58 | temp = *(arr + j); 59 | *(arr + j) = *(arr + (j + 1)); 60 | *(arr + (j + 1)) = temp; 61 | } 62 | } 63 | } 64 | } 65 | 66 | void sortedMerge(int *arr1, int *arr2, int *res, int m, int n) 67 | { 68 | int i, j = 0; 69 | for (i = 0; i < m; i++) 70 | { 71 | *(res + j) = *(arr1 + i); 72 | j++; 73 | } 74 | for (i = 0; i < n; i++) 75 | { 76 | *(res + j) = *(arr2 + i); 77 | j++; 78 | } 79 | bubbleSort(res, m + n); 80 | } 81 | 82 | // OUTPUT 83 | // Enter Size of first Array: 3 84 | // Enter Size of second Array: 4 85 | // Enter first Array[0]: 7 86 | // Enter first Array[1]: 2 87 | // Enter first Array[2]: 9 88 | 89 | // Enter Second Array[0]: 4 90 | // Enter Second Array[1]: 6 91 | // Enter Second Array[2]: 2 92 | // Enter Second Array[3]: 8 93 | 94 | // The merged and sorted array is 95 | // 2 2 4 6 7 8 9 -------------------------------------------------------------------------------- /Questions/Assignment-8@DSALAB.txt: -------------------------------------------------------------------------------- 1 | University of Engineering & Management, Kolkata 2 | Course: B.Tech (CSE / CSE(AIML) / CSETOT-CYS-BCT) 3 | Semester: 3rd 4 | Paper Name: Data Structure & Algorithm Laboratory 5 | Paper Code: PCC - CS391 6 | Date of Assignment - 13.10.2022 7 | Date of Submission - 20.10.2022 8 | Section: CSE(AIML) A,B 9 | 10 | 11 | 12 | Assignment — VIII 13 | (All programs to be implemented in C) 14 | 15 | Topic: Binary Tree 16 | 17 | 1. Write a program to implement Binary Tree using array along with the following functions: 18 | a) To create a binary tree. 19 | b) To display tree using inorder. 20 | c) To display tree using preorder. 21 | d) To display tree using postorder. 22 | e) To count number of node present in the tree. 23 | f) To find the height of the tree. 24 | g) To find the number of leaf node. 25 | h) To find the number of internal node. 26 | i) To search a data present in the tree. 27 | 28 | 2. Write a program to implement Binary Tree using linked list along with the following functions: 29 | a) To create a binary tree. 30 | b) To display tree using inorder. 31 | c) To display tree using preorder. 32 | d) To display tree using postorder. 33 | e) To count number of node present in the tree. 34 | f) To find the height of the tree. 35 | g) To find the number of leaf node. 36 | h) To find the number of internal node. 37 | i) To search a data present in the tree. 38 | 39 | 40 | 41 | Assignment — VIII 42 | (All programs to be implemented in Python) 43 | 44 | Topic: Binary Tree 45 | 46 | 1. Write a program to implement Binary Tree using linked list along with the following functions: 47 | a) To create a binary tree. 48 | b) To display tree using inorder. 49 | c) To display tree using preorder. 50 | d) To display tree using postorder. 51 | e) To count number of node present in the tree. 52 | f) To find the height of the tree. 53 | g) To find the number of leaf node. 54 | h) To find the number of internal node. 55 | i) To search a data present in the tree. 56 | 57 | 58 | Instruction: 59 | 1. Mention the following thing using A4 pages: 60 | a. Program Objective 61 | b. Algorithm (if it is asked) 62 | c. Program Code. 63 | d. Output 64 | 2. Handwritten assignment solutions are required. No printout is allowed. 65 | 3. Submit all the assignments using the channel file. 66 | 4. One Index page must be attached there. 67 | 5. Late submission will not be entertained. Maintain the deadline. -------------------------------------------------------------------------------- /Searching & Sorting/C Programs/radixSort.c: -------------------------------------------------------------------------------- 1 | // Write a program to implement Radix sort. 2 | 3 | #include 4 | 5 | int getMax(int[], int n); 6 | void countingSort(int[], int, int); 7 | void radixsort(int[], int); 8 | void printArray(int[], int); 9 | 10 | int main() 11 | { 12 | int array[] = {121, 432, 564, 23, 1, 45, 788}; 13 | int n = sizeof(array) / sizeof(array[0]); 14 | radixsort(array, n); 15 | printArray(array, n); 16 | } 17 | 18 | void printArray(int array[], int size) 19 | { 20 | for (int i = 0; i < size; ++i) 21 | { 22 | printf("%d ", array[i]); 23 | } 24 | printf("\n"); 25 | } 26 | 27 | // Function to get the largest element from an array 28 | int getMax(int array[], int n) 29 | { 30 | int max = array[0]; 31 | for (int i = 1; i < n; i++) 32 | if (array[i] > max) 33 | max = array[i]; 34 | return max; 35 | } 36 | 37 | // Using counting sort to sort the elements in the basis of significant places 38 | void countingSort(int array[], int size, int place) 39 | { 40 | int output[size + 1]; 41 | int max = (array[0] / place) % 10; 42 | 43 | for (int i = 1; i < size; i++) 44 | { 45 | if (((array[i] / place) % 10) > max) 46 | max = array[i]; 47 | } 48 | int count[max + 1]; 49 | 50 | for (int i = 0; i < max; ++i) 51 | count[i] = 0; 52 | 53 | // Calculate count of elements 54 | for (int i = 0; i < size; i++) 55 | count[(array[i] / place) % 10]++; 56 | 57 | // Calculate cumulative count 58 | for (int i = 1; i < 10; i++) 59 | count[i] += count[i - 1]; 60 | 61 | // Place the elements in sorted order 62 | for (int i = size - 1; i >= 0; i--) 63 | { 64 | output[count[(array[i] / place) % 10] - 1] = array[i]; 65 | count[(array[i] / place) % 10]--; 66 | } 67 | 68 | for (int i = 0; i < size; i++) 69 | array[i] = output[i]; 70 | } 71 | 72 | // Main function to implement radix sort 73 | void radixsort(int array[], int size) 74 | { 75 | // Get maximum element 76 | int max = getMax(array, size); 77 | 78 | // Apply counting sort to sort elements based on place value. 79 | for (int place = 1; max / place > 0; place *= 10) 80 | countingSort(array, size, place); 81 | } 82 | 83 | // OUTPUT 84 | // 1 23 45 121 432 564 788 85 | 86 | // Class: Sorting algorithm 87 | // Data structure: Array 88 | // Worst Time: O(w*n) 89 | // Worst Space: O(w+n) 90 | // where n is the number of keys, and w is the key length. -------------------------------------------------------------------------------- /Graph/C Programs/prim.c: -------------------------------------------------------------------------------- 1 | // Prim's Algorithm in C 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #define INF 9999999 8 | 9 | // number of vertices in graph 10 | #define V 5 11 | 12 | // create a 2d array of size 5x5 13 | // for adjacency matrix to represent graph 14 | int G[V][V] = { 15 | {0, 9, 75, 0, 0}, 16 | {9, 0, 95, 19, 42}, 17 | {75, 95, 0, 51, 66}, 18 | {0, 19, 51, 0, 31}, 19 | {0, 42, 66, 31, 0}}; 20 | 21 | int main() 22 | { 23 | int no_edge; // number of edge 24 | 25 | // create a array to track selected vertex 26 | // selected will become true otherwise false 27 | int selected[V]; 28 | 29 | // set selected false initially 30 | memset(selected, false, sizeof(selected)); 31 | 32 | // set number of edge to 0 33 | no_edge = 0; 34 | 35 | // the number of egde in minimum spanning tree will be 36 | // always less than (V -1), where V is number of vertices in 37 | // graph 38 | 39 | // choose 0th vertex and make it true 40 | selected[0] = true; 41 | 42 | int x; // row number 43 | int y; // col number 44 | 45 | // print for edge and weight 46 | printf("Edge : Weight\n"); 47 | 48 | while (no_edge < V - 1) 49 | { 50 | // For every vertex in the set S, find the all adjacent vertices 51 | // , calculate the distance from the vertex selected at step 1. 52 | // if the vertex is already in the set S, discard it otherwise 53 | // choose another vertex nearest to selected vertex at step 1. 54 | 55 | int min = INF; 56 | x = 0; 57 | y = 0; 58 | 59 | for (int i = 0; i < V; i++) 60 | { 61 | if (selected[i]) 62 | { 63 | for (int j = 0; j < V; j++) 64 | { 65 | if (!selected[j] && G[i][j]) 66 | { // not in selected and there is an edge 67 | if (min > G[i][j]) 68 | { 69 | min = G[i][j]; 70 | x = i; 71 | y = j; 72 | } 73 | } 74 | } 75 | } 76 | } 77 | printf("%d - %d : %d\n", x, y, G[x][y]); 78 | selected[y] = true; 79 | no_edge++; 80 | } 81 | 82 | return 0; 83 | } 84 | 85 | // OUTPUT 86 | // Edge : Weight 87 | // 0 - 1 : 9 88 | // 1 - 3 : 19 89 | // 3 - 4 : 31 90 | // 3 - 2 : 51 91 | 92 | // The time complexity of Prim's algorithm is O(E log V) -------------------------------------------------------------------------------- /Questions/Assignment-1@DSALAB.txt: -------------------------------------------------------------------------------- 1 | UEM New Logo 2 | 3 | University of Engineering & Management, Kolkata 4 | Course: B.Tech (CSE / CSE(AIML) / CSE(IOT-CYS-BCT) 5 | Semester: 3rd 6 | Paper Name: Data Structure & Algorithm Laboratory 7 | Paper Code: PCC - CS391 8 | 9 | 10 | 11 | Assignment – I 12 | (All programs to be implemented in C and Python Programming Language) 13 | 14 | Topic : Dynamic Array 15 | 1. Write a C program to search an element in an Array using dynamic memory allocation. 16 | 2. Write a C program to find the 3rd/user defined position based maximum element in an array using dynamic memory allocation. 17 | 3. Write a C program to find the minimum element in an array using dynamic memory allocation. 18 | 4. Write a C program to search an element in a 2D-Array using dynamic memory allocation. 19 | 5. Write a C program to find the maximum element in a 2D-array using dynamic memory allocation. 20 | 6. Write a C program to find the minimum element in a 2D-array using dynamic memory allocation. 21 | 7. Write a C program to merge two sorted dynamic array. 22 | 8. Write a C program to merge two unsorted dynamic array in sorted order. 23 | 9. Write a C program to delete a range of data from a dynamic array. 24 | 10.Write a C program to modify the size of an array and utilize that during run time. 25 | 26 | 27 | 28 | 29 | Assignments for Python 30 | (List, Tuples, Set, Dictionary) 31 | (All programs to be implemented in Python Programming Language) 32 | 33 | 34 | List: 35 | 1. Reverse a list in Python 36 | 2. Concatenate two lists index-wise. 37 | 3. Turn every item of a list into its square. 38 | 4. Add new item to list after a specified item. 39 | 5. Remove all occurrences of a specific item from a list. 40 | 41 | 42 | Tuples: 43 | 44 | 1. Access value 20 from the tuple. 45 | 2. Unpack the tuple into 4 variables. 46 | 3. Copy specific elements from one tuple to a new tuple. 47 | 4. Counts the number of occurrences of item ‘x’ from a tuple. 48 | 5. Check if all items in the tuple are the same. 49 | 50 | 51 | Set: 52 | 1. Add a list of elements to a set. 53 | 2. Get Only unique items from two sets. 54 | 3. Remove items from the set at once. 55 | 4. Return a set of elements present in Set A or B, but not both. 56 | 5. Check if two sets have any elements in common. If yes, display the common elements. 57 | 58 | 59 | 60 | Dictionary: 61 | 1. Convert two lists into a dictionary. 62 | 2. Merge two Python dictionaries into one. 63 | 3. Create a dictionary by extracting the keys from a given dictionary. 64 | 4. Delete a list of keys from a dictionary. 65 | 5. Check if a value exists in a dictionary. -------------------------------------------------------------------------------- /Stack/C programs/HTMLtag.c: -------------------------------------------------------------------------------- 1 | // Write a C program to validate an html tag. 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #define MAX 5 8 | 9 | char st[MAX][MAX]; 10 | int top = -1; 11 | 12 | bool isValid(char str[]); 13 | void push(char c[]); 14 | char *pop(void); 15 | bool isEmpty(void); 16 | 17 | int main() 18 | { 19 | char str[40]; 20 | printf("Enter HTML tag:\n"); 21 | gets(str); 22 | if (isValid(str)) 23 | printf("Valid Tag"); 24 | else 25 | printf("Invalid Tag"); 26 | return 0; 27 | } 28 | 29 | bool isValid(char str[]) 30 | { 31 | int len = strlen(str); 32 | int i, t; 33 | char ch[5], *ch1; 34 | if (str[0] != '<' || str[len - 1] != '>') 35 | return false; 36 | 37 | for (i = 0; i < len; i++) 38 | { 39 | // Push Operation 40 | if (str[i] == '<' && str[i + 1] != '/') 41 | { 42 | i++; 43 | t = 0; 44 | while (str[i] != '>') 45 | { 46 | ch[t++] = str[i]; 47 | i++; 48 | } 49 | ch[t] = '\0'; 50 | push(ch); 51 | } 52 | 53 | // Pop Operation 54 | if (str[i] == '<' && str[i + 1] == '/') 55 | { 56 | i += 2; 57 | t = 0; 58 | ch1 = pop(); 59 | while (str[i] != '>') 60 | { 61 | if (*(ch1 + t) != (char)str[i]) 62 | return false; 63 | t++; 64 | i++; 65 | } 66 | } 67 | } 68 | if (isEmpty) 69 | return true; 70 | return false; 71 | } 72 | 73 | void push(char c[]) 74 | { 75 | if (top == MAX - 1) 76 | printf("Stack Overflow"); 77 | else 78 | { 79 | top++; 80 | strcpy(st[top], c); 81 | } 82 | } 83 | 84 | char *pop() 85 | { 86 | char *ch = (char *)malloc(sizeof(char) * MAX); 87 | if (top == -1) 88 | { 89 | printf("Stack Underflow!"); 90 | ch = '\0'; 91 | } 92 | else 93 | { 94 | int i = 0; 95 | while (st[top][i] != '\0') 96 | { 97 | *(ch + i) = st[top][i]; 98 | i++; 99 | } 100 | top--; 101 | } 102 | return ch; 103 | } 104 | 105 | bool isEmpty() 106 | { 107 | if (top >= 0) 108 | return false; 109 | return true; 110 | } 111 | 112 | // TEST CASES 113 | //

TEXT

-> Valid 114 | // TEXT -> Valid 115 | //

TEXT

-> Invalid 116 | // TEXT -> Invalid 117 | // TEXT -> Invalid 118 | -------------------------------------------------------------------------------- /Questions/Assignment-4@DSALAB.txt: -------------------------------------------------------------------------------- 1 | UEM New Logo 2 | 3 | University of Engineering & Management, Kolkata 4 | Course: B.Tech (CSE / CSE(AIML) / CSE(IOT-CYS-BCT) 5 | Semester: 3rd 6 | Paper Name: Data Structure & Algorithm Laboratory 7 | Paper Code: PCC - CS391 8 | Date of Assignment- .08.2022 9 | Date of Submission - .08.2022 10 | Section: 11 | 12 | 13 | 14 | Assignment – IV 15 | (All programs to be implemented in C) 16 | 17 | Topic : Single linked list 18 | 1. Write a C program to reverse an already created single linked list. 19 | Mention the necessary algorithm. 20 | 2. Write a C program to merge two already sorted list. 21 | Mention the necessary algorithm. 22 | 3. Given a singly linked list of size N. The task is to left-shift the linked list by k nodes, where k is a given positive integer smaller than or equal to length of the linked list. 23 | 4. Write a C program to perform addition of two polynomials using linked list. 24 | 5. Given a singly linked list of size N. The task is to swap elements in the linked list pairwise. For example, if the input list is 1 2 3 4, the resulting list after swaps will be 2 1 4 3. 25 | 6. Write a C program to detect a loop in a single linked list. 26 | 7. Write a C program to perform multiplication of two polynomials using linked list. 27 | 8. Write a C program to implement the concept of stack using single linked list. 28 | 9. Write a C program to implement the concept of queue using single linked list. 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | Assignment – IV 37 | (All programs to be implemented using Python Language) 38 | 39 | Topic : Single linked list 40 | 1. Write a program to reverse an already created single linked list. 41 | 2. Write a program to merge two already sorted list. 42 | 3. Write a program to perform addition of two polynomials using linked list. 43 | 4. Given a singly linked list of size N. The task is to swap elements in the linked list pairwise. For example, if the input list is 1 2 3 4, the resulting list after swaps will be 2 1 4 3. 44 | 5. Write a program to detect a loop in a single linked list. 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | Instruction: 58 | 1. Mention the following thing using A4 pages: 59 | a. Program Objective 60 | b. Algorithm (if it is asked) 61 | c. Program Code. 62 | d. Output 63 | 64 | 65 | 66 | 67 | 2. Handwritten assignment solutions are required. No printout is allowed. 68 | 3. Submit all the assignments using the channel file. 69 | 4. One Index page must be attached there. 70 | 5. Late submission will not be entertained. Maintain the deadline. -------------------------------------------------------------------------------- /Linked List/Python Programs 🐍/Singly/sortedMerge_SLL.py: -------------------------------------------------------------------------------- 1 | # Write a python program to merge two already sorted list. 2 | 3 | class Node: 4 | def __init__(self, data): 5 | self.data = data 6 | self.next = None 7 | 8 | 9 | class LinkedList: 10 | def __init__(self): 11 | self.head = None 12 | 13 | def createList(self): 14 | print("Creating Linked List...") 15 | print("Enter -1 to end") 16 | num = int(input("Enter the data: ")) 17 | while(num != -1): 18 | newNode = Node(num) 19 | if self.head is None: 20 | self.head = newNode 21 | else: 22 | last = self.head 23 | while last.next: 24 | last = last.next 25 | last.next = newNode 26 | num = int(input("Enter the data: ")) 27 | 28 | def displayList(self): 29 | if self.head is None: 30 | print("List is empty") 31 | return 32 | print("HEAD", end='') 33 | temp = self.head 34 | while temp: 35 | print(" -> " + str(temp.data), end='') 36 | temp = temp.next 37 | print(" -> NULL\n") 38 | 39 | 40 | def mergeLists(headA, headB): 41 | dummyNode = Node(0) 42 | tail = dummyNode 43 | while True: 44 | if headA is None: 45 | tail.next = headB 46 | break 47 | if headB is None: 48 | tail.next = headA 49 | break 50 | if headA.data <= headB.data: 51 | tail.next = headA 52 | headA = headA.next 53 | else: 54 | tail.next = headB 55 | headB = headB.next 56 | tail = tail.next 57 | return dummyNode.next 58 | 59 | 60 | listA = LinkedList() 61 | print("Enter First List") 62 | listA.createList() 63 | listA.displayList() 64 | 65 | listB = LinkedList() 66 | print("Enter Second List") 67 | listB.createList() 68 | listB.displayList() 69 | 70 | listA.head = mergeLists(listA.head, listB.head) 71 | print("Merged Linked List is:") 72 | listA.displayList() 73 | 74 | # OUTPUT 75 | # Enter First List 76 | # Creating Linked List... 77 | # Enter - 1 to end 78 | # Enter the data: 5 79 | # Enter the data: 7 80 | # Enter the data: 9 81 | # Enter the data: -1 82 | # HEAD -> 5 -> 7 -> 9 -> NULL 83 | 84 | # Enter Second List 85 | # Creating Linked List... 86 | # Enter - 1 to end 87 | # Enter the data: 3 88 | # Enter the data: 4 89 | # Enter the data: 8 90 | # Enter the data: 10 91 | # Enter the data: -1 92 | # HEAD -> 3 -> 4 -> 8 -> 10 -> NULL 93 | 94 | # Merged Linked List is : 95 | # HEAD -> 3 -> 4 -> 5 -> 7 -> 8 -> 9 -> 10 -> NULL 96 | -------------------------------------------------------------------------------- /Linked List/C Programs ☠️/Singly/polynomial_LL.c: -------------------------------------------------------------------------------- 1 | // Write a C program to store polynomial using 2 | // linked list. Store the term in ascending order. 3 | 4 | #include 5 | #include 6 | 7 | typedef struct node 8 | { 9 | int coeff; 10 | int expo; 11 | struct node *next; 12 | } NODE; 13 | 14 | NODE *create(NODE *); 15 | NODE *insert(NODE *, int, int); 16 | void display(NODE *); 17 | 18 | int main() 19 | { 20 | NODE *head = NULL; 21 | printf("Enter the Polynomial...\n"); 22 | head = create(head); 23 | display(head); 24 | return 0; 25 | } 26 | 27 | NODE *create(NODE *head) 28 | { 29 | int n, i, coeff, expo; 30 | printf("Enter the Number of Terms: "); 31 | scanf("%d", &n); 32 | 33 | for (i = 0; i < n; i++) 34 | { 35 | printf("Enter the coefficient of term %d: ", i + 1); 36 | scanf("%d", &coeff); 37 | 38 | printf("Enter the Exponent of term %d: ", i + 1); 39 | scanf("%d", &expo); 40 | 41 | head = insert(head, coeff, expo); 42 | } 43 | return head; 44 | } 45 | 46 | NODE *insert(NODE *head, int co, int ex) 47 | { 48 | NODE *newNode = (NODE *)malloc(sizeof(NODE)); 49 | NODE *ptr; 50 | newNode->coeff = co; 51 | newNode->expo = ex; 52 | 53 | if (head == NULL || ex < head->expo) 54 | { 55 | newNode->next = head; 56 | head = newNode; 57 | } 58 | else 59 | { 60 | ptr = head; 61 | while (ptr->next != NULL && ex > ptr->next->expo) 62 | { 63 | ptr = ptr->next; 64 | } 65 | newNode->next = ptr->next; 66 | ptr->next = newNode; 67 | } 68 | return head; 69 | } 70 | 71 | void display(NODE *head) 72 | { 73 | if (head == NULL) 74 | { 75 | printf("No Polynomial"); 76 | exit(1); 77 | } 78 | printf("Polynomial Equation is\n"); 79 | NODE *ptr = head; 80 | while (ptr != NULL) 81 | { 82 | printf("( %dx^%d )", ptr->coeff, ptr->expo); 83 | ptr = ptr->next; 84 | if (ptr != NULL) 85 | { 86 | printf(" + "); 87 | } 88 | } 89 | } 90 | 91 | // OUTPUT 92 | // Enter the Polynomial... 93 | // Enter the Number of Terms: 4 94 | // Enter the coefficient of term 1: 11 95 | // Enter the Exponent of term 1: 9 96 | // Enter the coefficient of term 2: 6 97 | // Enter the Exponent of term 2: 2 98 | // Enter the coefficient of term 3: 9 99 | // Enter the Exponent of term 3: 7 100 | // Enter the coefficient of term 4: 4 101 | // Enter the Exponent of term 4: 5 102 | // Polynomial Equation is 103 | // ( 6x^2 ) + ( 4x^5 ) + ( 9x^7 ) + ( 11x^9 ) -------------------------------------------------------------------------------- /Linked List/C Programs ☠️/Singly/polyAdd_SLL.c: -------------------------------------------------------------------------------- 1 | // Write a C program to perform addition 2 | // of two polynomials using linked list. 3 | 4 | #include "polynomialLL.h" 5 | 6 | NODE *polyAdd(NODE *, NODE *); 7 | 8 | int main() 9 | { 10 | NODE *poly1 = NULL, *poly2 = NULL; 11 | 12 | printf("Enter First Polynomial\n"); 13 | poly1 = create(poly1); 14 | display(poly1); 15 | 16 | printf("\nEnter Second Polynomial\n"); 17 | poly2 = create(poly2); 18 | display(poly2); 19 | 20 | NODE *sum = polyAdd(poly1, poly2); 21 | printf("\nPolynomial Added\n"); 22 | display(sum); 23 | return 0; 24 | } 25 | 26 | NODE *polyAdd(NODE *p1, NODE *p2) 27 | { 28 | NODE *poly1 = p1, *poly2 = p2; 29 | NODE *sum = NULL; 30 | if (poly1 == NULL && poly2 == NULL) 31 | return sum; 32 | if (poly1 != NULL && poly2 == NULL) 33 | return poly1; 34 | if (poly1 == NULL && poly2 != NULL) 35 | return poly2; 36 | while (poly1 != NULL && poly2 != NULL) 37 | { 38 | if (poly1->expo > poly2->expo) 39 | { 40 | sum = insert(sum, poly1->coeff, poly1->expo); 41 | poly1 = poly1->next; 42 | } 43 | else if (poly1->expo < poly2->expo) 44 | { 45 | sum = insert(sum, poly2->coeff, poly2->expo); 46 | poly2 = poly2->next; 47 | } 48 | else 49 | { 50 | sum = insert(sum, poly1->coeff + poly2->coeff, poly1->expo); 51 | poly1 = poly1->next; 52 | poly2 = poly2->next; 53 | } 54 | } 55 | while (poly1 != NULL) 56 | { 57 | sum = insert(sum, poly1->coeff, poly1->expo); 58 | poly1 = poly1->next; 59 | } 60 | while (poly2 != NULL) 61 | { 62 | sum = insert(sum, poly2->coeff, poly2->expo); 63 | poly2 = poly2->next; 64 | } 65 | return sum; 66 | } 67 | 68 | // Enter First Polynomial 69 | // Enter the Number of Terms: 3 70 | // Enter the coefficient of term 1: 4 71 | // Enter the Exponent of term 1: 3 72 | // Enter the coefficient of term 2: 5 73 | // Enter the Exponent of term 2: 2 74 | // Enter the coefficient of term 3: 7 75 | // Enter the Exponent of term 3: 0 76 | // Polynomial Equation is 77 | // ( 4x^3 ) + ( 5x^2 ) + ( 7x^0 ) 78 | // Enter Second Polynomial 79 | // Enter the Number of Terms: 3 80 | // Enter the coefficient of term 1: 2 81 | // Enter the Exponent of term 1: 3 82 | // Enter the coefficient of term 2: 7 83 | // Enter the Exponent of term 2: 1 84 | // Enter the coefficient of term 3: 9 85 | // Enter the Exponent of term 3: 0 86 | // Polynomial Equation is 87 | // ( 2x^3 ) + ( 7x^1 ) + ( 9x^0 ) 88 | // Polynomial Added 89 | // Polynomial Equation is 90 | // ( 6x^3 ) + ( 5x^2 ) + ( 7x^1 ) + ( 16x^0 ) 91 | -------------------------------------------------------------------------------- /Linked List/Python Programs 🐍/Singly/detectCycle_SLL.py: -------------------------------------------------------------------------------- 1 | # Write a python program to detect 2 | # a loop in a single linked list. 3 | 4 | class Node: 5 | def __init__(self, data): 6 | self.data = data 7 | self.next = None 8 | 9 | 10 | class LinkedList: 11 | def __init__(self): 12 | self.head = None 13 | 14 | def createList(self): 15 | print("Creating Linked List...") 16 | print("Enter -1 to end") 17 | num = int(input("Enter the data: ")) 18 | while(num != -1): 19 | newNode = Node(num) 20 | if self.head is None: 21 | self.head = newNode 22 | else: 23 | last = self.head 24 | while last.next: 25 | last = last.next 26 | last.next = newNode 27 | num = int(input("Enter the data: ")) 28 | 29 | def displayList(self): 30 | if self.head is None: 31 | print("List is empty") 32 | return 33 | print("HEAD", end='') 34 | temp = self.head 35 | while temp: 36 | print(" -> " + str(temp.data), end='') 37 | temp = temp.next 38 | print(" -> NULL\n") 39 | 40 | def detectCycle(self): 41 | slow = self.head 42 | fast = self.head 43 | entry = self.head 44 | if fast is None and fast.next is None: 45 | print("No Cycle in Linked List") 46 | return 47 | while fast and fast.next: 48 | slow = slow.next 49 | fast = fast.next.next 50 | if slow == fast: 51 | while slow != entry: 52 | slow = slow.next 53 | entry = entry.next 54 | print("Cycle Detected at Node Data: "+str(entry.data)) 55 | return 56 | print("No Cycle in Linked List") 57 | 58 | 59 | if __name__ == "__main__": 60 | myList = LinkedList() 61 | print("Enter More Than 2 Data") 62 | myList.createList() 63 | myList.displayList() 64 | myList.detectCycle() 65 | 66 | size = 1 67 | tail = myList.head 68 | while tail.next: 69 | tail = tail.next 70 | size += 1 71 | 72 | if size >= 2: 73 | tail.next = myList.head.next 74 | print("Linked List Successfully created with Cycle") 75 | myList.detectCycle() 76 | 77 | 78 | # OUTPUT 79 | # Enter More Than 2 Data 80 | # Creating Linked List... 81 | # Enter - 1 to end 82 | # Enter the data: 51 83 | # Enter the data: 97 84 | # Enter the data: 3 85 | # Enter the data: 6 86 | # Enter the data: -1 87 | # HEAD -> 51 -> 97 -> 3 -> 6 -> NULL 88 | 89 | # No Cycle in Linked List 90 | # Linked List Successfully created with Cycle 91 | # Cycle Detected at Node Data: 97 92 | -------------------------------------------------------------------------------- /Queue/C Programs/circularQueue.c: -------------------------------------------------------------------------------- 1 | // Write a C program to implement circular queue. 2 | 3 | #include 4 | #define MAX 3 5 | 6 | int queue[MAX]; 7 | int front = -1, rear = -1; 8 | void insert(int); 9 | int delete_element(void); 10 | int peek(void); 11 | void display(void); 12 | 13 | int main() 14 | { 15 | insert(10); 16 | insert(20); 17 | insert(30); 18 | delete_element(); 19 | display(); 20 | peek(); 21 | insert(40); 22 | display(); 23 | delete_element(); 24 | return 0; 25 | } 26 | 27 | void insert(int num) 28 | { 29 | if (front == 0 && rear == MAX - 1) 30 | printf("\nOVERFLOW"); 31 | else 32 | { 33 | if (front == -1 && rear == -1) 34 | { 35 | front = rear = 0; 36 | queue[rear] = num; 37 | } 38 | else if (rear == MAX - 1 && front != 0) 39 | { 40 | rear = 0; 41 | queue[rear] = num; 42 | } 43 | else 44 | { 45 | rear++; 46 | queue[rear] = num; 47 | } 48 | printf("\n%d is inserted", num); 49 | } 50 | } 51 | 52 | int delete_element() 53 | { 54 | int val; 55 | if (front == -1 && rear == -1) 56 | { 57 | printf("\nUNDERFLOW"); 58 | return -1; 59 | } 60 | val = queue[front]; 61 | if (front == rear) 62 | { 63 | front = rear = -1; 64 | } 65 | else 66 | { 67 | if (front == MAX - 1) 68 | front = 0; 69 | else 70 | front++; 71 | } 72 | printf("\n%d is deleted from queue", val); 73 | return val; 74 | } 75 | 76 | int peek() 77 | { 78 | if (front == -1 && rear == -1) 79 | { 80 | printf("\nQUEUE IS EMPTY"); 81 | return -1; 82 | } 83 | else 84 | { 85 | printf("\nTop element is %d: ", queue[front]); 86 | return queue[front]; 87 | } 88 | } 89 | 90 | void display() 91 | { 92 | int i; 93 | if (front == -1 && rear == -1) 94 | printf("\nQUEUE IS EMPTY"); 95 | else 96 | { 97 | printf("\nFRONT --> "); 98 | if (front < rear) 99 | { 100 | for (i = front; i <= rear; i++) 101 | { 102 | printf("%d ", queue[i]); 103 | } 104 | } 105 | else 106 | { 107 | for (i = front; i < MAX; i++) 108 | printf("%d ", queue[i]); 109 | for (i = 0; i <= rear; i++) 110 | printf("%d ", queue[i]); 111 | } 112 | printf("<-- REAR"); 113 | } 114 | } 115 | 116 | // OUTPUT 117 | // 10 is inserted 118 | // 20 is inserted 119 | // 30 is inserted 120 | // 10 is deleted from queue 121 | // FRONT --> 20 30 <-- REAR 122 | // Top element is 20: 123 | // 40 is inserted 124 | // FRONT --> 20 30 40 <-- REAR 125 | // 20 is deleted from queue -------------------------------------------------------------------------------- /Stack/Python Programs/towerOfHanoi.py: -------------------------------------------------------------------------------- 1 | # Write a program to solve the tower of Hanoi using stack 2 | 3 | import sys 4 | 5 | 6 | class Stack: 7 | def __init__(self, capacity): 8 | self.capacity = capacity 9 | self.top = -1 10 | self.array = [0]*capacity 11 | 12 | 13 | def createStack(capacity): 14 | stack = Stack(capacity) 15 | return stack 16 | 17 | 18 | def isFull(stack): 19 | return (stack.top == (stack.capacity - 1)) 20 | 21 | 22 | def isEmpty(stack): 23 | return (stack.top == -1) 24 | 25 | 26 | def push(stack, item): 27 | if(isFull(stack)): 28 | return 29 | stack.top += 1 30 | stack.array[stack.top] = item 31 | 32 | 33 | def Pop(stack): 34 | if(isEmpty(stack)): 35 | return -sys.maxsize 36 | Top = stack.top 37 | stack.top -= 1 38 | return stack.array[Top] 39 | 40 | 41 | def moveDisksBetweenTwoPoles(src, dest, s, d): 42 | pole1TopDisk = Pop(src) 43 | pole2TopDisk = Pop(dest) 44 | 45 | if (pole1TopDisk == -sys.maxsize): 46 | push(src, pole2TopDisk) 47 | moveDisk(d, s, pole2TopDisk) 48 | 49 | elif (pole2TopDisk == -sys.maxsize): 50 | push(dest, pole1TopDisk) 51 | moveDisk(s, d, pole1TopDisk) 52 | 53 | elif (pole1TopDisk > pole2TopDisk): 54 | push(src, pole1TopDisk) 55 | push(src, pole2TopDisk) 56 | moveDisk(d, s, pole2TopDisk) 57 | 58 | else: 59 | push(dest, pole2TopDisk) 60 | push(dest, pole1TopDisk) 61 | moveDisk(s, d, pole1TopDisk) 62 | 63 | 64 | def moveDisk(fromPeg, toPeg, disk): 65 | print("Move the disk", disk, "from '", fromPeg, "' to '", toPeg, "'") 66 | 67 | 68 | def tohIterative(num_of_disks, src, aux, dest): 69 | s, d, a = 'A', 'C', 'B' 70 | 71 | if (num_of_disks % 2 == 0): 72 | temp = d 73 | d = a 74 | a = temp 75 | total_num_of_moves = int(pow(2, num_of_disks) - 1) 76 | 77 | for i in range(num_of_disks, 0, -1): 78 | push(src, i) 79 | 80 | for i in range(1, total_num_of_moves + 1): 81 | if (i % 3 == 1): 82 | moveDisksBetweenTwoPoles(src, dest, s, d) 83 | 84 | elif (i % 3 == 2): 85 | moveDisksBetweenTwoPoles(src, aux, s, a) 86 | 87 | elif (i % 3 == 0): 88 | moveDisksBetweenTwoPoles(aux, dest, a, d) 89 | 90 | 91 | num_of_disks = int(input("Enter Number of Disks: ")) 92 | src = createStack(num_of_disks) 93 | dest = createStack(num_of_disks) 94 | aux = createStack(num_of_disks) 95 | 96 | tohIterative(num_of_disks, src, aux, dest) 97 | 98 | # OUTPUT 99 | # Enter Number of Disks: 3 100 | # Move the disk 1 from ' A ' to ' C ' 101 | # Move the disk 2 from ' A ' to ' B ' 102 | # Move the disk 1 from ' C ' to ' B ' 103 | # Move the disk 3 from ' A ' to ' C ' 104 | # Move the disk 1 from ' B ' to ' A ' 105 | # Move the disk 2 from ' B ' to ' C ' 106 | # Move the disk 1 from ' A ' to ' C 107 | -------------------------------------------------------------------------------- /Questions/Assignment-3@DSALAB.txt: -------------------------------------------------------------------------------- 1 | UEM New Logo 2 | 3 | University of Engineering & Management, Kolkata 4 | Course: B.Tech (CSE / CSE(AIML) / CSE(IOT-CYS-BCT) 5 | Semester: 3rd 6 | Paper Name: Data Structure & Algorithm Laboratory 7 | Paper Code: PCC - CS391 8 | Date of Assignment- 17.08.2022 9 | Date of Submission - 24.08.2022 10 | Section: cse A, B 11 | 12 | 13 | 14 | Assignment – III 15 | (All programs to be implemented in C) 16 | 17 | Topic : Single linked list 18 | 1. Write a C program to implement the following functions for single linked list. 19 | * createList 20 | • insertAtFirst 21 | * insertAtLast 22 | * insertAtAny 23 | * displayList . 24 | Mention the necessary algorithm. 25 | 2. Write a C program to implement the following functions for single linked list. 26 | * createList 27 | • deleteFromFirst 28 | * deleteFromLast 29 | * deleteFromAny 30 | * displayList . 31 | Mention the necessary algorithm. 32 | 3. Write a C program to implement all the insert and delete functions and display function along with an appropriate menu for a single linked list. 33 | 4. Write a C program to store polynomial using linked list. Store the term in ascending order. 34 | 5. Write a C function to count the number of node present in a linked list. 35 | 6. Write a C program to print the odd number and even number nodes separately from a single linked list. 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | Assignment – III 49 | (All programs to be implemented in Python Language) 50 | 51 | Topic : Single linked list 52 | 1. Write a program to implement the following functions for single linked list. 53 | * createList 54 | • insertAtFirst 55 | * insertAtLast 56 | * insertAtAny 57 | * displayList . 58 | 2. Write a program to implement the following functions for single linked list. 59 | * createList 60 | • deleteFromFirst 61 | * deleteFromLast 62 | * deleteFromAny 63 | * displayList . 64 | 3. Write a program to implement all the insert and delete functions and display function along with an appropriate menu for a single linked list. 65 | 4. Write a program to store polynomial using linked list. Store the term in descending order. 66 | 5. Write a function to count the number of node present in a linked list. 67 | 6. Write a program to print the odd number and even number nodes separately from a single linked list. 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | Instruction: 77 | 1. Mention the following thing using A4 pages: 78 | a. Program Objective 79 | b. Algorithm (if it is asked) 80 | c. Program Code. 81 | d. Output 82 | 83 | 84 | 85 | 86 | 2. Handwritten assignment solutions are required. No printout is allowed. 87 | 3. Submit all the assignments using the channel file. 88 | 4. One Index page must be attached there. 89 | 5. Late submission will not be entertained. Maintain the deadline. -------------------------------------------------------------------------------- /Stack/C programs/evaluatePostfix.c: -------------------------------------------------------------------------------- 1 | // Write a C program to evaluate a postfix expression. 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #define MAX 10 8 | 9 | float st[MAX]; 10 | int top = -1; 11 | enum sType 12 | { 13 | operator, operand }; 14 | void push(float st[], float); 15 | float pop(float st[]); 16 | float evaluatePostfixExp(char exp[]); 17 | enum sType SymType(char); 18 | 19 | int main() 20 | { 21 | float val; 22 | char exp[20]; 23 | printf("Enter any Postfix Expression: "); 24 | gets(exp); 25 | val = evaluatePostfixExp(exp); 26 | printf("Value of Postfix Expression: %2.f", val); 27 | return 0; 28 | } 29 | 30 | float evaluatePostfixExp(char exp[]) 31 | { 32 | int i = 0; 33 | char ch; 34 | float op1, op2, val; 35 | while (exp[i] != '\0') 36 | { 37 | ch = exp[i]; 38 | if (ch == ' ') 39 | continue; 40 | switch (SymType(ch)) 41 | { 42 | case operand: 43 | fflush(stdin); 44 | printf("Enter the value of %c: ", ch); 45 | scanf("%f", &val); 46 | push(st, val); 47 | break; 48 | 49 | case operator: 50 | op1 = pop(st); 51 | op2 = pop(st); 52 | switch (ch) 53 | { 54 | case '+': 55 | val = op2 + op1; 56 | break; 57 | case '-': 58 | val = op2 - op1; 59 | break; 60 | case '*': 61 | val = op2 * op1; 62 | break; 63 | case '/': 64 | val = op2 / op1; 65 | break; 66 | case '%': 67 | val = (int)op2 % (int)op1; 68 | break; 69 | case '^': 70 | val = (float)pow(op1, op2); 71 | break; 72 | default: 73 | break; 74 | } 75 | push(st, val); 76 | break; 77 | } 78 | i++; 79 | } 80 | return (pop(st)); 81 | } 82 | 83 | void push(float st[], float val) 84 | { 85 | if (top == MAX - 1) 86 | printf("Stack Overflow!"); 87 | else 88 | st[++top] = val; 89 | } 90 | 91 | float pop(float st[]) 92 | { 93 | float val = -1; 94 | if (top == -1) 95 | printf("Stack Underflow!"); 96 | else 97 | val = st[top--]; 98 | return val; 99 | } 100 | 101 | enum sType SymType(char c) 102 | { 103 | enum sType sym; 104 | if (isalpha(c)) 105 | sym = operand; 106 | else 107 | sym = operator; 108 | return sym; 109 | } 110 | 111 | // OUTPUT 112 | // Enter any Postfix Expression: ABC/+D* 113 | // Enter the value of A: 10 114 | // Enter the value of B: 18 115 | // Enter the value of C: 9 116 | // Enter the value of D: 2 117 | // Value of Postfix Expression: 24 118 | 119 | /* 120 | Do not write this part 121 | (A+B/C)*D = ABC/+D* 122 | (10+18/9)*2 = 24 123 | */ -------------------------------------------------------------------------------- /Stack/C programs/pallindrome.c: -------------------------------------------------------------------------------- 1 | // Write a C program to find the palindrome using appropriate ADT to implement a more 2 | // powerful version of the is_palindrome() function. As a reminder, this function 3 | // implements simple palindome verification. Here is the signature and documentation for 4 | // the function: 5 | // • bool is_palindrome(char *text) 6 | 7 | // Return true if text is a palindrome, false otherwise. A palindrome is a string that is 8 | // identical to itself when reversed. For example, "madam", "dad", and "abba" are 9 | // palindromes. Note: the empty string is a palindrome, as is every string of length one. 10 | 11 | // Your solution should ignore whitespace and punctuation, and all comparisons should be 12 | // case-insensitive. Include some tests in your main function. Examples of valid 13 | // palindromes: 14 | 15 | // Example- "", "a", "aa", "aaa", "aba", "abba", "Taco cat", "Madam, I'm Adam", "A man, a plan, 16 | // a canal: Panama", "Doc, note: I dissent. A fast never prevents a fatness. I diet on cod." 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | char *stack; 25 | int top = -1; 26 | 27 | void push(char ele); 28 | char pop(void); 29 | bool is_pallindrome(char *text); 30 | void printOutput(bool); 31 | 32 | int main() 33 | { 34 | char str1[] = ""; 35 | printOutput(is_pallindrome(str1)); 36 | char str2[] = "A"; 37 | printOutput(is_pallindrome(str2)); 38 | char str3[] = "Bb"; 39 | printOutput(is_pallindrome(str3)); 40 | char str4[] = "Madam, I'm Adam"; 41 | printOutput(is_pallindrome(str4)); 42 | char str5[] = "A man, a plan, a canal: Panama"; 43 | printOutput(is_pallindrome(str5)); 44 | char str6[] = "Data & Structure"; 45 | printOutput(is_pallindrome(str6)); 46 | return 0; 47 | } 48 | 49 | void push(char ele) 50 | { 51 | top++; 52 | *(stack + top) = ele; 53 | } 54 | 55 | char pop(void) 56 | { 57 | char c = *(stack + top); 58 | top--; 59 | return c; 60 | } 61 | 62 | bool is_pallindrome(char *text) 63 | { 64 | int len = 0, i = 0; 65 | char ele, temp[strlen(text)]; 66 | 67 | while (*(text + i) != '\0') 68 | { 69 | if (isalpha(*(text + i))) 70 | { 71 | temp[len] = *(text + i); 72 | len++; 73 | } 74 | i++; 75 | } 76 | temp[len] = '\0'; 77 | 78 | stack = (char *)malloc(len * sizeof(char)); 79 | 80 | for (i = 0; i < len / 2; i++) 81 | { 82 | push(temp[i]); 83 | } 84 | 85 | if (len % 2 != 0) 86 | { 87 | i++; 88 | } 89 | 90 | while (*(temp + i) != '\0') 91 | { 92 | ele = tolower(pop()); 93 | if (ele != tolower(temp[i])) 94 | { 95 | return false; 96 | } 97 | i++; 98 | } 99 | return true; 100 | } 101 | 102 | void printOutput(bool b) 103 | { 104 | if (b) 105 | printf("\nTRUE"); 106 | else 107 | printf("\nFALSE"); 108 | } 109 | 110 | // OUTPUT 111 | // TRUE 112 | // TRUE 113 | // TRUE 114 | // TRUE 115 | // TRUE 116 | // FALSE -------------------------------------------------------------------------------- /Graph/C Programs/DFS.c: -------------------------------------------------------------------------------- 1 | // DFS algorithm in C 2 | 3 | #include 4 | #include 5 | 6 | struct node 7 | { 8 | int vertex; 9 | struct node *next; 10 | }; 11 | 12 | struct node *createNode(int v); 13 | 14 | struct Graph 15 | { 16 | int numVertices; 17 | int *visited; 18 | 19 | // We need int** to store a two dimensional array. 20 | // Similary, we need struct node** to store an array of Linked lists 21 | struct node **adjLists; 22 | }; 23 | 24 | // DFS algo 25 | void DFS(struct Graph *graph, int vertex) 26 | { 27 | struct node *adjList = graph->adjLists[vertex]; 28 | struct node *temp = adjList; 29 | 30 | graph->visited[vertex] = 1; 31 | printf("Visited %d \n", vertex); 32 | 33 | while (temp != NULL) 34 | { 35 | int connectedVertex = temp->vertex; 36 | 37 | if (graph->visited[connectedVertex] == 0) 38 | { 39 | DFS(graph, connectedVertex); 40 | } 41 | temp = temp->next; 42 | } 43 | } 44 | 45 | // Create a node 46 | struct node *createNode(int v) 47 | { 48 | struct node *newNode = malloc(sizeof(struct node)); 49 | newNode->vertex = v; 50 | newNode->next = NULL; 51 | return newNode; 52 | } 53 | 54 | // Create graph 55 | struct Graph *createGraph(int vertices) 56 | { 57 | struct Graph *graph = malloc(sizeof(struct Graph)); 58 | graph->numVertices = vertices; 59 | 60 | graph->adjLists = malloc(vertices * sizeof(struct node *)); 61 | 62 | graph->visited = malloc(vertices * sizeof(int)); 63 | 64 | int i; 65 | for (i = 0; i < vertices; i++) 66 | { 67 | graph->adjLists[i] = NULL; 68 | graph->visited[i] = 0; 69 | } 70 | return graph; 71 | } 72 | 73 | // Add edge 74 | void addEdge(struct Graph *graph, int src, int dest) 75 | { 76 | // Add edge from src to dest 77 | struct node *newNode = createNode(dest); 78 | newNode->next = graph->adjLists[src]; 79 | graph->adjLists[src] = newNode; 80 | 81 | // Add edge from dest to src 82 | newNode = createNode(src); 83 | newNode->next = graph->adjLists[dest]; 84 | graph->adjLists[dest] = newNode; 85 | } 86 | 87 | // Print the graph 88 | void printGraph(struct Graph *graph) 89 | { 90 | int v; 91 | for (v = 0; v < graph->numVertices; v++) 92 | { 93 | struct node *temp = graph->adjLists[v]; 94 | printf("Adjacency list of vertex %d ", v); 95 | while (temp) 96 | { 97 | printf("-> %d ", temp->vertex); 98 | temp = temp->next; 99 | } 100 | printf("\n"); 101 | } 102 | } 103 | 104 | int main() 105 | { 106 | struct Graph *graph = createGraph(4); 107 | addEdge(graph, 0, 1); 108 | addEdge(graph, 0, 2); 109 | addEdge(graph, 1, 2); 110 | addEdge(graph, 2, 3); 111 | 112 | printGraph(graph); 113 | 114 | DFS(graph, 2); 115 | 116 | return 0; 117 | } 118 | 119 | // OUTPUT 120 | // Adjacency list of vertex 0 -> 2 -> 1 121 | // Adjacency list of vertex 1 -> 2 -> 0 122 | // Adjacency list of vertex 2 -> 3 -> 1 -> 0 123 | // Adjacency list of vertex 3 -> 2 124 | // Visited 2 125 | // Visited 3 126 | // Visited 1 127 | // Visited 0 128 | 129 | // Time: O(E+V) 130 | // Space: O(V) -------------------------------------------------------------------------------- /Linked List/Python Programs 🐍/Singly/delete_SL.py: -------------------------------------------------------------------------------- 1 | # Write a Python program to implement the following functions for single linked list. 2 | # • createList • deleteFromFirst • deleteFromLast • deleteFromAny • displayList 3 | 4 | class Node: 5 | def __init__(self, data): 6 | self.data = data 7 | self.next = None 8 | 9 | 10 | class LinkedList: 11 | def __init__(self): 12 | self.head = None 13 | 14 | def createList(self): 15 | print("Creating Linked List...") 16 | print("Enter -1 to end") 17 | num = int(input("Enter the data: ")) 18 | while(num != -1): 19 | newNode = Node(num) 20 | if self.head is None: 21 | self.head = newNode 22 | else: 23 | last = self.head 24 | while last.next: 25 | last = last.next 26 | last.next = newNode 27 | num = int(input("Enter the data: ")) 28 | 29 | def deleteFromFirst(self): 30 | if self.head is None: 31 | print("List is empty") 32 | return 33 | self.head = self.head.next 34 | 35 | def deleteFromLast(self): 36 | if self.head is None: 37 | print("List is empty") 38 | return 39 | temp = self.head 40 | while temp.next.next: 41 | temp = temp.next 42 | temp.next = None 43 | 44 | def deleteFromAny(self): 45 | if self.head is None: 46 | print("List is empty") 47 | return 48 | pos = int(input("Enter Position of Deletation: ")) 49 | temp = self.head 50 | i = 1 51 | while i < pos-1 and temp is not None: 52 | temp = temp.next 53 | i += 1 54 | if temp is None: 55 | print("Index out of bound") 56 | else: 57 | temp.next = temp.next.next 58 | 59 | def displayList(self): 60 | if self.head is None: 61 | print("List is empty") 62 | return 63 | print("HEAD", end='') 64 | temp = self.head 65 | while temp: 66 | print(" -> " + str(temp.data), end='') 67 | temp = temp.next 68 | print(" -> NULL\n") 69 | 70 | 71 | myList = LinkedList() 72 | myList.createList() 73 | print("Initial List: ") 74 | myList.displayList() 75 | 76 | print("Deleting from First") 77 | myList.deleteFromFirst() 78 | myList.displayList() 79 | 80 | print("Deleting from Last") 81 | myList.deleteFromLast() 82 | myList.displayList() 83 | 84 | print("Deleting from Any") 85 | myList.deleteFromAny() 86 | myList.displayList() 87 | 88 | # OUTPUT 89 | # Creating Linked List... 90 | # Enter - 1 to end 91 | # Enter the data: 2 92 | # Enter the data: 9 93 | # Enter the data: 6 94 | # Enter the data: 8 95 | # Enter the data: 4 96 | # Enter the data: 5 97 | # Enter the data: -1 98 | # Initial List: 99 | # HEAD -> 2 -> 9 -> 6 -> 8 -> 4 -> 5 -> NULL 100 | 101 | # Deleting from First 102 | # HEAD -> 9 -> 6 -> 8 -> 4 -> 5 -> NULL 103 | 104 | # Deleting from Last 105 | # HEAD -> 9 -> 6 -> 8 -> 4 -> NULL 106 | 107 | # Deleting from Any 108 | # Enter Position of Deletation: 3 109 | # HEAD -> 9 -> 6 -> 4 -> NULL 110 | -------------------------------------------------------------------------------- /Linked List/Python Programs 🐍/Singly/insert_SL.py: -------------------------------------------------------------------------------- 1 | # Write a Python program to implement the following functions for single linked list. 2 | # • createList • insertAtFirst • insertAtLast • insertAtAny • displayList 3 | 4 | class Node: 5 | def __init__(self, data): 6 | self.data = data 7 | self.next = None 8 | 9 | 10 | class LinkedList: 11 | def __init__(self): 12 | self.head = None 13 | 14 | def createList(self): 15 | print("Creating Linked List...") 16 | print("Enter -1 to end") 17 | num = int(input("Enter the data: ")) 18 | while(num != -1): 19 | newNode = Node(num) 20 | if self.head is None: 21 | self.head = newNode 22 | else: 23 | last = self.head 24 | while last.next: 25 | last = last.next 26 | last.next = newNode 27 | num = int(input("Enter the data: ")) 28 | 29 | def insertAtFirst(self): 30 | num = int(input("Enter the data: ")) 31 | newNode = Node(num) 32 | newNode.next = self.head 33 | self.head = newNode 34 | 35 | def insertAtLast(self): 36 | num = int(input("Enter the data: ")) 37 | newNode = Node(num) 38 | if self.head is None: 39 | self.head = newNode 40 | return 41 | last = self.head 42 | while last.next: 43 | last = last.next 44 | last.next = newNode 45 | 46 | def insertAtAny(self): 47 | pos = int(input("Enter Position of insertion: ")) 48 | num = int(input("Enter the data: ")) 49 | newNode = Node(num) 50 | temp = self.head 51 | i = 1 52 | while i < pos-1 and temp is not None: 53 | temp = temp.next 54 | i += 1 55 | if temp is None: 56 | print("Index out of bound") 57 | else: 58 | newNode.next = temp.next 59 | temp.next = newNode 60 | 61 | def displayList(self): 62 | if self.head is None: 63 | print("List is empty") 64 | return 65 | print("HEAD", end='') 66 | temp = self.head 67 | while temp: 68 | print(" -> " + str(temp.data), end='') 69 | temp = temp.next 70 | print(" -> NULL\n") 71 | 72 | 73 | myList = LinkedList() 74 | myList.createList() 75 | print("Initial List: ") 76 | myList.displayList() 77 | 78 | print("Inserting at First") 79 | myList.insertAtFirst() 80 | myList.displayList() 81 | 82 | print("Inserting at Last") 83 | myList.insertAtLast() 84 | myList.displayList() 85 | 86 | print("Inserting at Any") 87 | myList.insertAtAny() 88 | myList.displayList() 89 | 90 | # OUTPUT 91 | # Creating Linked List... 92 | # Enter - 1 to end 93 | # Enter the data: 5 94 | # Enter the data: 2 95 | # Enter the data: -1 96 | # Initial List: 97 | # HEAD -> 5 -> 2 -> NULL 98 | 99 | # Inserting at First 100 | # Enter the data: 11 101 | # HEAD -> 11 -> 5 -> 2 -> NULL 102 | 103 | # Inserting at Last 104 | # Enter the data: 72 105 | # HEAD -> 11 -> 5 -> 2 -> 72 -> NULL 106 | 107 | # Inserting at Any 108 | # Enter Position of insertion: 3 109 | # Enter the data: 50 110 | # HEAD -> 11 -> 5 -> 50 -> 2 -> 72 -> NULL 111 | -------------------------------------------------------------------------------- /Questions/Assignment-2@DSALAB.txt: -------------------------------------------------------------------------------- 1 | UEM New Logo 2 | 3 | University of Engineering & Management, Kolkata 4 | Course: B.Tech (CSE / CSE(AIML) / CSE(IOT-CYS-BCT) 5 | Semester: 3rd 6 | Paper Name: Data Structure & Algorithm Laboratory 7 | Paper Code: PCC - CS391 8 | Date of Assignment- 10.08.2022 9 | Date of Submission - 17.08.2022 10 | Section: CSE A, B 11 | 12 | 13 | 14 | Assignment – II 15 | (All programs to be implemented in C Programming Language) 16 | 17 | Topic : Stack & Queue 18 | 1. Write a C program to implement stack using dynamic array and perform the following operation • PUSH() • POP() • PEEK() • DISPLAY(). Mention the necessary algorithm. 19 | 2. Write a C program to implement queue using dynamic array. Execute all the necessary functions of queue. 20 | 3. Write a C program to reverse a string using appropriate ADT. 21 | 4. Write a C program to implement the concept of double stack, where both end can be used for operations. 22 | 5. Write a C program to find the palindrome using appropriate ADT to implement a more powerful version of the is_palindrome() function. As a reminder, this function implements simple palindome verification. Here is the signature and documentation for the function: 23 | * bool is_palindrome(char *text) 24 | Return true if text is a palindrome, false otherwise. A palindrome is a string that is identical to itself when reversed. For example, "madam", "dad", and "abba" are palindromes. Note: the empty string is a palindrome, as is every string of length one. 25 | Your solution should ignore whitespace and punctuation, and all comparisons should be case-insensitive. Include some tests in your main function. Examples of valid palindromes: 26 | Example- "", "a", "aa", "aaa", "aba", "abba", "Taco cat", "Madam, I'm Adam", "A man, a plan, a canal: Panama", "Doc, note: I dissent. A fast never prevents a fatness. I diet on cod." 27 | 6. Write a C program to implement queue using stack. 28 | 7. Write a C program to implement circular queue. 29 | 30 | 31 | ASSIGMENT II (Python) 32 | (All programs to be implemented in Python Programming Language) 33 | 34 | 35 | 1. Write a Python program to implement a stack. 36 | 2. Write a Python program to implement a queue. 37 | 3. Write a Python program to reverse a string using stack (using array). 38 | 4. Write a Python program to implement circular queue using array. 39 | 5. Given a stack of boxes in different colors. Write a python function 40 | that accepts the stack of boxes and removes those boxes having 41 | color other than the primary colors (Red, Green and Blue) from the stack. 42 | The removed boxes should be en-queued into a new queue and returned. 43 | The original stack should have only the boxes having primary colors 44 | and the order must be maintained. 45 | Perform case sensitive string comparison wherever necessary. 46 | Note: Consider the queue to be of the same size as that of the original stack. 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | Instruction: 56 | 1. Mention the following thing using A4 pages: 57 | a. Program Objective 58 | b. Algorithm (if it is asked) 59 | c. Program Code. 60 | d. Output 61 | 62 | 63 | 64 | 65 | 2. Handwritten assignment solutions are required. No printout is allowed. 66 | 3. Submit all the assignments using the channel file. 67 | 4. One Index page must be attached there. 68 | 5. Late submission will not be entertained. Maintain the deadline. -------------------------------------------------------------------------------- /Linked List/C Programs ☠️/Doubly/sortedMerge_DLL.c: -------------------------------------------------------------------------------- 1 | // Write a C program to merge two already sorted double list. 2 | 3 | #include 4 | #include 5 | 6 | typedef struct node 7 | { 8 | struct node *prev; 9 | int data; 10 | struct node *next; 11 | } NODE; 12 | 13 | NODE *createNode(int); 14 | NODE *createList(NODE *); 15 | void displayList(NODE *); 16 | NODE *sortedMerge(NODE *, NODE *); 17 | 18 | int main() 19 | { 20 | NODE *head1 = NULL, *head2 = NULL; 21 | printf("Enter First List\n"); 22 | head1 = createList(head1); 23 | displayList(head1); 24 | 25 | printf("\nEnter Second List\n"); 26 | head2 = createList(head2); 27 | displayList(head2); 28 | 29 | NODE *res = sortedMerge(head1, head2); 30 | printf("\nSorted Merge list is\n"); 31 | displayList(res); 32 | 33 | return 0; 34 | } 35 | 36 | NODE *createNode(int val) 37 | { 38 | NODE *temp = (NODE *)malloc(sizeof(NODE)); 39 | temp->data = val; 40 | temp->prev = NULL; 41 | temp->next = NULL; 42 | return temp; 43 | } 44 | 45 | NODE *createList(NODE *head) 46 | { 47 | int num; 48 | printf("Enter -1 to end\n"); 49 | printf("Enter the data: "); 50 | scanf("%d", &num); 51 | while (num != -1) 52 | { 53 | NODE *newNode = createNode(num); 54 | if (head == NULL) 55 | { 56 | head = newNode; 57 | } 58 | else 59 | { 60 | NODE *ptr = head; 61 | while (ptr->next != NULL) 62 | { 63 | ptr = ptr->next; 64 | } 65 | ptr->next = newNode; 66 | newNode->prev = ptr; 67 | } 68 | printf("Enter the data: "); 69 | scanf("%d", &num); 70 | } 71 | return head; 72 | } 73 | 74 | void displayList(NODE *head) 75 | { 76 | NODE *ptr; 77 | ptr = head; 78 | printf("HEAD"); 79 | while (ptr != NULL) 80 | { 81 | printf(" <-> %d", ptr->data); 82 | ptr = ptr->next; 83 | } 84 | printf(" -> NULL\n"); 85 | } 86 | 87 | NODE *sortedMerge(NODE *list1, NODE *list2) 88 | { 89 | if (list1 == NULL) 90 | return list2; 91 | if (list2 == NULL) 92 | return list1; 93 | if (list1->data > list2->data) 94 | { 95 | NODE *temp = list1; 96 | list1 = list2; 97 | list2 = temp; 98 | } 99 | 100 | NODE *res = list1; 101 | while (list1 != NULL && list2 != NULL) 102 | { 103 | NODE *ptr1 = NULL; 104 | while (list1 != NULL && list1->data <= list2->data) 105 | { 106 | ptr1 = list1; 107 | list1 = list1->next; 108 | } 109 | ptr1->next = list2; 110 | list2->prev = ptr1; 111 | 112 | // swap 113 | NODE *temp = list1; 114 | list1 = list2; 115 | list2 = temp; 116 | } 117 | return res; 118 | } 119 | 120 | // OUTPUT 121 | // Enter First List 122 | // Enter -1 to end 123 | // Enter the data: 5 124 | // Enter the data: 7 125 | // Enter the data: 9 126 | // Enter the data: -1 127 | // HEAD <-> 5 <-> 7 <-> 9 -> NULL 128 | 129 | // Enter Second List 130 | // Enter -1 to end 131 | // Enter the data: 3 132 | // Enter the data: 4 133 | // Enter the data: 8 134 | // Enter the data: 10 135 | // Enter the data: -1 136 | // HEAD <-> 3 <-> 4 <-> 8 <-> 10 -> NULL 137 | 138 | // Sorted Merge list is 139 | // HEAD <-> 3 <-> 4 <-> 5 <-> 7 <-> 8 <-> 9 <-> 10 -> NULL -------------------------------------------------------------------------------- /Linked List/C Programs ☠️/Singly/oddEvenNode_LL.c: -------------------------------------------------------------------------------- 1 | // Write a C program to print the odd number and even 2 | // number nodes separately from a single linked list. 3 | 4 | #include 5 | #include 6 | 7 | typedef struct node 8 | { 9 | int data; 10 | struct node *next; 11 | } NODE; 12 | 13 | NODE *createList(NODE *); 14 | void displayList(NODE *); 15 | void seperateOddEven(NODE *, NODE **, NODE **); 16 | 17 | int main() 18 | { 19 | NODE *head = NULL, *odd = NULL, *even = NULL; 20 | head = createList(head); 21 | printf("Initial Linked List is...\n"); 22 | displayList(head); 23 | 24 | seperateOddEven(head, &odd, &even); 25 | printf("Odd Nodes Are: "); 26 | displayList(odd); 27 | printf("Even Nodes Are: "); 28 | displayList(even); 29 | 30 | return 0; 31 | } 32 | 33 | NODE *createList(NODE *head) 34 | { 35 | NODE *newNode, *ptr; 36 | int num; 37 | printf("Creating Linked List...\n"); 38 | printf("Enter -1 to end\n"); 39 | printf("Enter the data: "); 40 | scanf("%d", &num); 41 | while (num != -1) 42 | { 43 | newNode = (NODE *)malloc(sizeof(NODE)); 44 | newNode->data = num; 45 | if (head == NULL) 46 | { 47 | newNode->next = NULL; 48 | head = newNode; 49 | } 50 | else 51 | { 52 | ptr = head; 53 | while (ptr->next != NULL) 54 | { 55 | ptr = ptr->next; 56 | } 57 | ptr->next = newNode; 58 | newNode->next = NULL; 59 | } 60 | printf("Enter the data: "); 61 | scanf("%d", &num); 62 | } 63 | return head; 64 | } 65 | 66 | void displayList(NODE *head) 67 | { 68 | NODE *ptr; 69 | ptr = head; 70 | printf("HEAD"); 71 | while (ptr != NULL) 72 | { 73 | printf(" -> %d", ptr->data); 74 | ptr = ptr->next; 75 | } 76 | printf(" -> NULL\n"); 77 | } 78 | 79 | void seperateOddEven(NODE *head, NODE **odd, NODE **even) 80 | { 81 | if (head == NULL) 82 | { 83 | return; 84 | } 85 | int count = 1; 86 | NODE *ptr1 = NULL; 87 | NODE *ptr2 = NULL; 88 | 89 | while (head != NULL) 90 | { 91 | if (count % 2 != 0) 92 | { 93 | if (*odd == NULL) 94 | { 95 | *odd = head; 96 | ptr1 = head; 97 | } 98 | else 99 | { 100 | ptr1->next = head; 101 | ptr1 = ptr1->next; 102 | } 103 | } 104 | else 105 | { 106 | if (*even == NULL) 107 | { 108 | *even = head; 109 | ptr2 = head; 110 | } 111 | else 112 | { 113 | ptr2->next = head; 114 | ptr2 = ptr2->next; 115 | } 116 | } 117 | head = head->next; 118 | count++; 119 | } 120 | ptr1->next = NULL; 121 | ptr2->next = NULL; 122 | } 123 | 124 | // OUTPUT 125 | // Creating Linked List... 126 | // Enter -1 to end 127 | // Enter the data: 10 128 | // Enter the data: 20 129 | // Enter the data: 30 130 | // Enter the data: 40 131 | // Enter the data: 50 132 | // Enter the data: -1 133 | // Initial Linked List is... 134 | // HEAD -> 10 -> 20 -> 30 -> 40 -> 50 -> NULL 135 | // Odd Nodes Are: HEAD -> 10 -> 30 -> 50 -> NULL 136 | // Even Nodes Are: HEAD -> 20 -> 40 -> NULL -------------------------------------------------------------------------------- /Stack/C programs/infixToPostfix.c: -------------------------------------------------------------------------------- 1 | // Write a C program to convert an 2 | // infix expression to postfix expression. 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #define MAX 20 10 | 11 | char st[MAX]; 12 | int top = -1; 13 | 14 | void push(char st[], char); 15 | char pop(char st[]); 16 | int precedence(char); 17 | void infixToPostfix(char infix[], char postfix[]); 18 | bool isOpening(char); 19 | bool isClosing(char); 20 | bool isOperator(char); 21 | bool hasPriority(char, char); 22 | 23 | int main() 24 | { 25 | char infix[MAX], postfix[MAX]; 26 | printf("Enter a Infix Expression: "); 27 | gets(infix); 28 | strcpy(postfix, ""); 29 | infixToPostfix(infix, postfix); 30 | printf("Corresponding Postfix Expression is :\n%s", postfix); 31 | return 0; 32 | } 33 | 34 | void infixToPostfix(char infix[], char postfix[]) 35 | { 36 | int i = 0, j = 0; 37 | while (infix[i] != '\0') 38 | { 39 | if (isOpening(infix[i])) 40 | push(st, infix[i]); 41 | else if (isClosing(infix[i])) 42 | { 43 | while (top != -1 && !isOpening(st[top])) 44 | postfix[j++] = pop(st); 45 | if (top == -1) 46 | { 47 | printf("\nIncorrect Expression\n"); 48 | exit(1); 49 | } 50 | pop(st); 51 | } 52 | else if (isdigit(infix[i]) || isalpha(infix[i])) 53 | postfix[j++] = infix[i]; 54 | else if (isOperator(infix[i])) 55 | { 56 | while ((top != -1) && !isOpening(st[top]) && hasPriority(st[top], infix[i])) 57 | postfix[j++] = pop(st); 58 | push(st, infix[i]); 59 | } 60 | else 61 | { 62 | printf("\nINCORRECT ELEMENT IN EXPRESSION"); 63 | exit(1); 64 | } 65 | i++; 66 | } 67 | while ((top != -1) && !isOpening(st[top])) 68 | { 69 | postfix[j] = pop(st); 70 | j++; 71 | } 72 | postfix[j] = '\0'; 73 | } 74 | 75 | void push(char st[], char val) 76 | { 77 | if (top == MAX - 1) 78 | printf("\nSTACK OVERFLOW!"); 79 | else 80 | st[++top] = val; 81 | } 82 | 83 | char pop(char st[]) 84 | { 85 | char val = ' '; 86 | if (top == -1) 87 | printf("\nSTACK UNDERFLOW!"); 88 | else 89 | val = st[top--]; 90 | return val; 91 | } 92 | 93 | bool isOpening(char c) 94 | { 95 | if (c == '(' || c == '{' || c == '[') 96 | return true; 97 | return false; 98 | } 99 | 100 | bool isClosing(char c) 101 | { 102 | if (c == ')' || c == '}' || c == ']') 103 | return true; 104 | return false; 105 | } 106 | 107 | bool isOperator(char c) 108 | { 109 | if (c == '+' || c == '-' || c == '*' || c == '/' || c == '%' || c == '^') 110 | return true; 111 | return false; 112 | } 113 | 114 | int precedence(char operator) 115 | { 116 | if (operator== '+' || operator== '-') 117 | return 2; 118 | else if (operator== '/' || operator== '*' || operator== '%') 119 | return 3; 120 | else if (operator== '^') 121 | return 4; 122 | return -1; 123 | } 124 | 125 | bool hasPriority(char op1, char op2) 126 | { 127 | int op1Weight = precedence(op1); 128 | int op2Weight = precedence(op2); 129 | if (op1Weight == op2Weight) 130 | return op1Weight == 4 ? false : true; 131 | return op1Weight > op2Weight ? true : false; 132 | } 133 | 134 | // OUTPUT 135 | // Enter a Infix Expression: [A+B]*C/D^E^F%(G-H) 136 | // Corresponding Postfix Expression is : 137 | // AB+C*DEF^^/GH-% -------------------------------------------------------------------------------- /Stack/C programs/towerOfHanoi.c: -------------------------------------------------------------------------------- 1 | // Write a C program to solve the tower of Hanoi using stack. 2 | // geeksforgeeks.org/iterative-tower-of-hanoi/#:~:text=The%20Tower%20of%20Hanoi%20is,thus%20making%20a%20conical%20shape. 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | struct Stack 10 | { 11 | unsigned capacity; 12 | int top; 13 | int *array; 14 | }; 15 | 16 | struct Stack *createStack(unsigned capacity) 17 | { 18 | struct Stack *stack = (struct Stack *)malloc(sizeof(struct Stack)); 19 | stack->capacity = capacity; 20 | stack->top = -1; 21 | stack->array = (int *)malloc(stack->capacity * sizeof(int)); 22 | return stack; 23 | } 24 | 25 | int isFull(struct Stack *stack) 26 | { 27 | return (stack->top == stack->capacity - 1); 28 | } 29 | 30 | int isEmpty(struct Stack *stack) 31 | { 32 | return (stack->top == -1); 33 | } 34 | 35 | void push(struct Stack *stack, int item) 36 | { 37 | if (isFull(stack)) 38 | return; 39 | stack->array[++stack->top] = item; 40 | } 41 | 42 | int pop(struct Stack *stack) 43 | { 44 | if (isEmpty(stack)) 45 | return INT_MIN; 46 | return stack->array[stack->top--]; 47 | } 48 | 49 | void moveDisk(char fromPeg, char toPeg, int disk) 50 | { 51 | printf("Move the disk %d from \'%c\' to \'%c\'\n", 52 | disk, fromPeg, toPeg); 53 | } 54 | 55 | void moveDisksBetweenTwoPoles(struct Stack *src, struct Stack *dest, char s, char d) 56 | { 57 | int pole1TopDisk = pop(src); 58 | int pole2TopDisk = pop(dest); 59 | 60 | if (pole1TopDisk == INT_MIN) 61 | { 62 | push(src, pole2TopDisk); 63 | moveDisk(d, s, pole2TopDisk); 64 | } 65 | 66 | else if (pole2TopDisk == INT_MIN) 67 | { 68 | push(dest, pole1TopDisk); 69 | moveDisk(s, d, pole1TopDisk); 70 | } 71 | 72 | else if (pole1TopDisk > pole2TopDisk) 73 | { 74 | push(src, pole1TopDisk); 75 | push(src, pole2TopDisk); 76 | moveDisk(d, s, pole2TopDisk); 77 | } 78 | 79 | else 80 | { 81 | push(dest, pole2TopDisk); 82 | push(dest, pole1TopDisk); 83 | moveDisk(s, d, pole1TopDisk); 84 | } 85 | } 86 | 87 | void tohIterative(int num_of_disks, struct Stack *src, struct Stack *aux, struct Stack *dest) 88 | { 89 | int i, total_num_of_moves; 90 | char s = 'A', d = 'C', a = 'B'; 91 | 92 | if (num_of_disks % 2 == 0) 93 | { 94 | char temp = d; 95 | d = a; 96 | a = temp; 97 | } 98 | total_num_of_moves = pow(2, num_of_disks) - 1; 99 | 100 | for (i = num_of_disks; i >= 1; i--) 101 | push(src, i); 102 | 103 | for (i = 1; i <= total_num_of_moves; i++) 104 | { 105 | if (i % 3 == 1) 106 | moveDisksBetweenTwoPoles(src, dest, s, d); 107 | 108 | else if (i % 3 == 2) 109 | moveDisksBetweenTwoPoles(src, aux, s, a); 110 | 111 | else if (i % 3 == 0) 112 | moveDisksBetweenTwoPoles(aux, dest, a, d); 113 | } 114 | } 115 | 116 | int main() 117 | { 118 | unsigned num_of_disks; 119 | printf("Enter Number of Disks: "); 120 | scanf("%u", &num_of_disks); 121 | struct Stack *src, *dest, *aux; 122 | 123 | src = createStack(num_of_disks); 124 | aux = createStack(num_of_disks); 125 | dest = createStack(num_of_disks); 126 | 127 | tohIterative(num_of_disks, src, aux, dest); 128 | return 0; 129 | } 130 | 131 | // OUTPUT 132 | // Enter Number of Disks: 3 133 | // Move the disk 1 from 'A' to 'C' 134 | // Move the disk 2 from 'A' to 'B' 135 | // Move the disk 1 from 'C' to 'B' 136 | // Move the disk 3 from 'A' to 'C' 137 | // Move the disk 1 from 'B' to 'A' 138 | // Move the disk 2 from 'B' to 'C' 139 | // Move the disk 1 from 'A' to 'C' -------------------------------------------------------------------------------- /Linked List/Python Programs 🐍/Singly/polyAdd_SLL.py: -------------------------------------------------------------------------------- 1 | # Write a python program to perform addition 2 | # of two polynomials using linked list 3 | 4 | class Node: 5 | def __init__(self, coeff, expo): 6 | self.coeff = coeff 7 | self.expo = expo 8 | self.next = None 9 | 10 | 11 | class Polynomial: 12 | def __init__(self): 13 | self.head = None 14 | 15 | def create(self): 16 | n = int(input("Enter number of terms: ")) 17 | for i in range(n): 18 | coeff = int(input("Enter the Coefficient of term "+str(i+1)+": ")) 19 | expo = int(input("Enter the Exponent of term " + str(i+1) + ": ")) 20 | self.insert(coeff, expo) 21 | 22 | def insert(self, co, ex): 23 | newNode = Node(co, ex) 24 | temp = self.head 25 | if self.head is None or ex > temp.expo: 26 | newNode.next = self.head 27 | self.head = newNode 28 | else: 29 | while temp.next is not None and ex < temp.next.expo: 30 | temp = temp.next 31 | newNode.next = temp.next 32 | temp.next = newNode 33 | 34 | def display(self): 35 | if self.head is None: 36 | print("No Polynomial") 37 | return 38 | print("Your Polynomial Equation is:") 39 | temp = self.head 40 | while temp: 41 | print("( "+str(temp.coeff)+"x^"+str(temp.expo)+" )", end='') 42 | temp = temp.next 43 | if temp is not None: 44 | print(" + ", end='') 45 | 46 | def polyAdd(self, p1, p2): 47 | poly1 = p1 48 | poly2 = p2 49 | if poly1 is None and poly2 is None: 50 | print("Can't Multiply") 51 | return 52 | if poly1 is None and poly2 is not None: 53 | self.head = p2 54 | return 55 | if poly1 is not None and poly2 is None: 56 | self.head = p1 57 | return 58 | while poly1 and poly2: 59 | if poly1.expo > poly2.expo: 60 | self.insert(poly1.coeff, poly1.expo) 61 | poly1 = poly1.next 62 | elif poly1.expo < poly2.expo: 63 | self.insert(poly2.coeff, poly2.expo) 64 | poly2 = poly2.next 65 | else: 66 | self.insert(poly1.coeff+poly2.coeff, poly2.expo) 67 | poly1 = poly1.next 68 | poly2 = poly2.next 69 | while poly1: 70 | self.insert(poly1.coeff, poly1.expo) 71 | poly1 = poly1.next 72 | while poly2: 73 | self.insert(poly2.coeff, poly2.expo) 74 | poly2 = poly2.next 75 | 76 | 77 | poly1 = Polynomial() 78 | print("Enter First Polynomial") 79 | poly1.create() 80 | poly1.display() 81 | 82 | poly2 = Polynomial() 83 | print("\nEnter Second Polynomial") 84 | poly2.create() 85 | poly2.display() 86 | 87 | sum = Polynomial() 88 | sum.polyAdd(poly1.head, poly2.head) 89 | print("\nPolynomial Added") 90 | sum.display() 91 | 92 | 93 | # OUTPUT 94 | 95 | # Enter First Polynomial 96 | # Enter number of terms: 3 97 | # Enter the Coefficient of term 1: 4 98 | # Enter the Exponent of term 1: 3 99 | # Enter the Coefficient of term 2: 9 100 | # Enter the Exponent of term 2: 1 101 | # Enter the Coefficient of term 3: 7 102 | # Enter the Exponent of term 3: 0 103 | # Your Polynomial Equation is: 104 | # ( 4x^3 ) + ( 9x^1 ) + ( 7x^0 ) 105 | 106 | # Enter Second Polynomial 107 | # Enter number of terms: 2 108 | # Enter the Coefficient of term 1: 8 109 | # Enter the Exponent of term 1: 2 110 | # Enter the Coefficient of term 2: -3 111 | # Enter the Exponent of term 2: 1 112 | # Your Polynomial Equation is: 113 | # ( 8x^2 ) + ( -3x^1 ) 114 | 115 | # Polynomial Added 116 | # Your Polynomial Equation is: 117 | # ( 4x^3 ) + ( 8x^2 ) + ( 6x^1 ) + ( 7x^0 ) 118 | -------------------------------------------------------------------------------- /Questions/README1.md: -------------------------------------------------------------------------------- 1 |

Assignment 1

2 | 3 | > 💠 Dynamic Memory Allocation
👉🏼 [Question](/Questions/Assignment-1%40DSALAB.txt) 4 | 5 | | No. | C Programs | Python | 6 | | --- | ------------------------------------------------------------------------ | ------------------------------------------------------------------------------ | 7 | | 1. | [searchEle](/Dynamic-Memory-Allocation/C%20programs/searchEle.c) | [searchEle](/Dynamic-Memory-Allocation/Python%20programs/searchEle.py) | 8 | | 2. | [findPos](/Dynamic-Memory-Allocation/C%20programs/findPos.c) | [findPos](/Dynamic-Memory-Allocation/Python%20programs/findPos.py) | 9 | | 3. | [findMin](/Dynamic-Memory-Allocation/C%20programs/findMin.c) | [findMin](/Dynamic-Memory-Allocation/Python%20programs/findMin.py) | 10 | | 4. | [search2DArr](/Dynamic-Memory-Allocation/C%20programs/search2DArr.c) | [search2DArr](/Dynamic-Memory-Allocation/Python%20programs/search2DArr.py) | 11 | | 5. | [max2DArr](/Dynamic-Memory-Allocation/C%20programs/max2DArr.c) | [max2DArr](/Dynamic-Memory-Allocation/Python%20programs/max2DArr.py) | 12 | | 6. | [min2DArr](/Dynamic-Memory-Allocation/C%20programs/min2DArr.c) | [min2DArr](/Dynamic-Memory-Allocation/Python%20programs/min2DArr.py) | 13 | | 7. | [sortedMerge](/Dynamic-Memory-Allocation/C%20programs/sortedMerge.c) | [sortedMerge](/Dynamic-Memory-Allocation/Python%20programs/sortedMerge.py) | 14 | | 8. | [mergeUnsorted](/Dynamic-Memory-Allocation/C%20programs/mergeUnsorted.c) | [mergeUnsorted](/Dynamic-Memory-Allocation/Python%20programs/mergeUnsorted.py) | 15 | | 9. | [delDataArr](/Dynamic-Memory-Allocation/C%20programs/delDataArr.c) | [delDataArr](/Dynamic-Memory-Allocation/Python%20programs/delDataArr.py) | 16 | | 10. | [modifySize](/Dynamic-Memory-Allocation/C%20programs/modifySize.c) | [modifySize](/Dynamic-Memory-Allocation/Python%20programs/modifySize.py) | 17 | 18 | | List | Tuple | Set | Dictionary | 19 | | -------------------------------------------------------------------- | ------------------------------------------------------------- | ------------------------------------------------------- | ---------------------------------------------------------------------------- | 20 | | [list1](/Dynamic-Memory-Allocation/List%20Python/reverseList.py) | [tuple1](/Dynamic-Memory-Allocation/Tuple%20Python/tuple1.py) | [set1](/Dynamic-Memory-Allocation/Set%20Python/set1.py) | [dictionary1](/Dynamic-Memory-Allocation/Dictionary%20Python/dictionary1.py) | 21 | | [list2](/Dynamic-Memory-Allocation/List%20Python/concatenateList.py) | [tuple2](/Dynamic-Memory-Allocation/Tuple%20Python/tuple2.py) | [set2](/Dynamic-Memory-Allocation/Set%20Python/set2.py) | [dictionary2](/Dynamic-Memory-Allocation/Dictionary%20Python/dictionary2.py) | 22 | | [list3](/Dynamic-Memory-Allocation/List%20Python/sqList.py) | [tuple3](/Dynamic-Memory-Allocation/Tuple%20Python/tuple3.py) | [set3](/Dynamic-Memory-Allocation/Set%20Python/set3.py) | [dictionary3](/Dynamic-Memory-Allocation/Dictionary%20Python/dictionary3.py) | 23 | | [list4](/Dynamic-Memory-Allocation/List%20Python/addItem.py) | [tuple4](/Dynamic-Memory-Allocation/Tuple%20Python/tuple4.py) | [set4](/Dynamic-Memory-Allocation/Set%20Python/set4.py) | [dictionary4](/Dynamic-Memory-Allocation/Dictionary%20Python/dictionary4.py) | 24 | | [list5](/Dynamic-Memory-Allocation/List%20Python/removeItem.py) | [tuple5](/Dynamic-Memory-Allocation/Tuple%20Python/tuple5.py) | [set5](/Dynamic-Memory-Allocation/Set%20Python/set5.py) | [dictionary5](/Dynamic-Memory-Allocation/Dictionary%20Python/dictionary5.py) | 25 | -------------------------------------------------------------------------------- /Stack/C programs/doubleStack.c: -------------------------------------------------------------------------------- 1 | // Write a C program to implement the concept of double 2 | // stack, where both end can be used for operations. 3 | 4 | #include 5 | #include 6 | #define MAX 10 7 | 8 | int stack[MAX], topA = -1, topB = MAX; 9 | 10 | void pushA(int val) 11 | { 12 | if (topA == topB - 1) 13 | printf("\nStack A Overflow"); 14 | else 15 | { 16 | topA += 1; 17 | stack[topA] = val; 18 | } 19 | } 20 | 21 | int popA(void) 22 | { 23 | int val; 24 | if (topA == -1) 25 | { 26 | printf("\nStack A Underflow"); 27 | val = -1; 28 | } 29 | else 30 | { 31 | val = stack[topA]; 32 | topA--; 33 | } 34 | return val; 35 | } 36 | 37 | void display_stackA(void) 38 | { 39 | int i; 40 | if (topA == -1) 41 | printf("\nStack A is Empty"); 42 | else 43 | { 44 | printf("\nContents of Stack A are:"); 45 | for (i = topA; i >= 0; i--) 46 | printf("\nStack[%d]: %d", i, stack[i]); 47 | } 48 | } 49 | 50 | void pushB(int val) 51 | { 52 | if (topB - 1 == topA) 53 | printf("\nStack B Overflow"); 54 | else 55 | { 56 | topB -= 1; 57 | stack[topB] = val; 58 | } 59 | } 60 | 61 | int popB(void) 62 | { 63 | int val; 64 | if (topB == MAX) 65 | { 66 | printf("\nStack B Underflow"); 67 | val = -1; 68 | } 69 | else 70 | { 71 | val = stack[topB]; 72 | topB++; 73 | } 74 | return val; 75 | } 76 | 77 | void display_stackB(void) 78 | { 79 | int i; 80 | if (topB == MAX) 81 | printf("\nStack B is Empty"); 82 | else 83 | { 84 | printf("\nContents of Stack B are:"); 85 | for (i = topB; i < MAX; i++) 86 | printf("\nStack[%d]: %d", i, stack[i]); 87 | } 88 | } 89 | 90 | int main() 91 | { 92 | int option, val; 93 | printf("\n*** MAIN MENU***"); 94 | printf("\n1. PUSH IN STACK A"); 95 | printf("\n2. PUSH IN STACK B"); 96 | printf("\n3. POP FROM STACK A"); 97 | printf("\n4. POP FROM STACK B"); 98 | printf("\n5. DISPLAY STACK A"); 99 | printf("\n6. DISPLAY STACK B"); 100 | printf("\n7. EXIT"); 101 | 102 | do 103 | { 104 | printf("\nEnter your choice: "); 105 | scanf("%d", &option); 106 | switch (option) 107 | { 108 | case 1: 109 | printf("\nEnter value to push in Stack A: "); 110 | scanf("%d", &val); 111 | pushA(val); 112 | break; 113 | case 2: 114 | printf("\nEnter value to push in Stack B: "); 115 | scanf("%d", &val); 116 | pushB(val); 117 | break; 118 | case 3: 119 | val = popA(); 120 | if (val != -1) 121 | printf("%d poped from Stack A", val); 122 | break; 123 | case 4: 124 | val = popB(); 125 | if (val != -1) 126 | printf("%d poped from Stack B", val); 127 | break; 128 | case 5: 129 | display_stackA(); 130 | break; 131 | case 6: 132 | display_stackB(); 133 | break; 134 | case 7: 135 | exit(1); 136 | default: 137 | printf("\nInvalid Choice"); 138 | break; 139 | } 140 | 141 | } while (option != 7); 142 | return 0; 143 | } 144 | 145 | // OUTPUT 146 | // *** MAIN MENU*** 147 | // 1. PUSH IN STACK A 148 | // 2. PUSH IN STACK B 149 | // 3. POP FROM STACK A 150 | // 4. POP FROM STACK B 151 | // 5. DISPLAY STACK A 152 | // 6. DISPLAY STACK B 153 | // 7. EXIT 154 | // Enter your choice: 1 155 | 156 | // Enter value to push in Stack A: 15 157 | 158 | // Enter your choice: 1 159 | 160 | // Enter value to push in Stack A: 25 161 | 162 | // Enter your choice: 5 163 | 164 | // Contents of Stack A are: 165 | // Stack[1]: 25 166 | // Stack[0]: 15 167 | // Enter your choice: 4 168 | 169 | // Stack B Underflow 170 | // Enter your choice: 7 -------------------------------------------------------------------------------- /Linked List/Algorithms 📝/DLL Algo.txt: -------------------------------------------------------------------------------- 1 | *** ADD AT BEGINNING *** 2 | Step 1: IF ptr = NULL 3 | Write OVERFLOW 4 | Go to Step 9 5 | [END OF IF] 6 | Step 2: SET NEW_NODE = ptr 7 | Step 3: SET ptr = ptr -> NEXT 8 | Step 4: SET NEW_NODE -> DATA = VAL 9 | Step 5: SET NEW_NODE -> PREV = NULL 10 | Step 6: SET NEW_NODE -> NEXT = START 11 | Step 7: SET head -> PREV = NEW_NODE 12 | Step 8: SET head = NEW_NODE 13 | Step 9: EXIT 14 | 15 | -------------------------------------------------- 16 | 17 | *** ADD AT THE END *** 18 | Step 1: IF PTR = NULL 19 | Write OVERFLOW 20 | Go to Step 11 21 | [END OF IF] 22 | Step 2: SET NEW_NODE = PTR 23 | Step 3: SET PTR = PTR -> NEXT 24 | Step 4: SET NEW_NODE -> DATA = VAL 25 | Step 5: SET NEW_NODE -> NEXT = NULL 26 | Step 6: SET TEMP = START 27 | Step 7: Repeat Step 8 while TEMP -> NEXT != NULL 28 | Step 8: SET TEMP = TEMP -> NEXT 29 | [END OF LOOP] 30 | Step 9: SET TEMP -> NEXT = NEW_NODE 31 | Step 10: SET NEW_NODE -> PREV = TEMP 32 | Step 11: EXIT 33 | 34 | ------------------------------------------------------ 35 | 36 | *** ADD AT ANY POSITION *** 37 | Step 1: IF PTR = NULL 38 | Write OVERFLOW 39 | Go to Step 15 40 | [END OF IF] 41 | Step 2: SET NEW_NODE = PTR 42 | Step 3: SET PTR = PTR -> NEXT 43 | Step 4: SET NEW_NODE -> DATA = VAL 44 | Step 5: SET TEMP = START 45 | Step 6: REPEAT 7 to 10 until POS > 2 46 | Step 7: SET TEMP = TEMP -> NEXT 47 | Step 8: SET POS = POS - 1 48 | STEP 9: IF TEMP = NULL 49 | STEP 10: WRITE "LESS THAN DESIRED NO. OF ELEMENTS" 50 | GOTO STEP 15 51 | [END OF IF] 52 | [END OF LOOP] 53 | Step 11: SET NEW_NODE -> NEXT = TEMP -> NEXT 54 | Step 12: SET NEW_NODE -> PREV = TEMP 55 | Step 13: SET TEMP -> NEXT = NEW_NODE 56 | Step 14: SET TEMP -> NEXT -> PREV = NEW_NODE 57 | Step 15: EXIT 58 | 59 | ---------------------------------------------------- 60 | 61 | *** SEARCHING IN DOUBLE LINKED LIST *** 62 | Step 1: IF HEAD == NULL 63 | WRITE "UNDERFLOW" 64 | GOTO STEP 9 65 | [END OF IF] 66 | Step 2: Set PTR = HEAD 67 | Step 3: Set COUNT = 0 68 | Step 4: Repeat step 5 to 7 while PTR != NULL 69 | Step 5: SET COUNT = COUNT + 1 70 | Step 6: IF PTR → DATA = ITEM 71 | return COUNT 72 | [END OF IF] 73 | Step 7: PTR = PTR → NEXT 74 | [END OF LOOP] 75 | Step 8: WRITE "ITEM NOT FOUND" 76 | Step 9: EXIT 77 | 78 | ------------------------------------------------------ 79 | 80 | *** COUNT NODES *** 81 | Step 1: Set PTR = HEAD 82 | Step 2: SET COUNT = 0 83 | Step 3: Repeat step 4 and 5 while PTR != NULL 84 | Step 4: PTR = PTR → NEXT 85 | Step 5: SET COUNT = COUNT + 1 86 | [END OF LOOP] 87 | Step 6: return COUNT 88 | 89 | ------------------------------------------------------ 90 | 91 | *** DELETE FROM BEGINING *** 92 | STEP 1: IF HEAD = NULL 93 | WRITE UNDERFLOW 94 | GOTO STEP 6 95 | STEP 2: SET PTR = HEAD 96 | STEP 3: SET HEAD = HEAD → NEXT 97 | STEP 4: SET HEAD → PREV = NULL 98 | STEP 5: FREE PTR 99 | STEP 6: EXIT 100 | 101 | ------------------------------------------------------ 102 | 103 | *** DELETE FROM THE END *** 104 | Step 1: IF HEAD = NULL 105 | Write UNDERFLOW 106 | Go to Step 7 107 | [END OF IF] 108 | Step 2: SET TEMP = HEAD 109 | Step 3: REPEAT STEP 4 WHILE TEMP->NEXT != NULL 110 | Step 4: SET TEMP = TEMP->NEXT 111 | [END OF LOOP] 112 | Step 5: SET TEMP ->PREV-> NEXT = NULL 113 | Step 6: FREE TEMP 114 | Step 7: EXIT 115 | 116 | ------------------------------------------------------ 117 | 118 | *** DELETE FROM ANY *** 119 | Step 1: IF HEAD = NULL 120 | Write UNDERFLOW 121 | Go to Step 9 122 | [END OF IF] 123 | Step 2: SET PTR = HEAD 124 | Step 3: Repeat Step 4 and 5 while POS > 1 125 | Step 4: SET PTR = PTR -> NEXT 126 | Step 5: SET POS = POS - 1 127 | [END OF LOOP] 128 | Step 6: SET PREPTR = PTR -> NEXT 129 | Step 7: SET PREPTR -> NEXT = PTR -> NEXT 130 | Step 8: SET PTR -> NEXT -> PREV = PREPTR 131 | Step 9: FREE PTR 132 | Step 10: EXIT 133 | -------------------------------------------------------------------------------- /Graph/C Programs/kruskal.c: -------------------------------------------------------------------------------- 1 | // Kruskal's algorithm in C 2 | 3 | #include 4 | 5 | #define MAX 30 6 | 7 | typedef struct edge 8 | { 9 | int u, v, w; 10 | } edge; 11 | 12 | typedef struct edge_list 13 | { 14 | edge data[MAX]; 15 | int n; 16 | } edge_list; 17 | 18 | edge_list elist; 19 | 20 | int Graph[MAX][MAX], n; 21 | edge_list spanlist; 22 | 23 | void kruskalAlgo(); 24 | int find(int belongs[], int vertexno); 25 | void applyUnion(int belongs[], int c1, int c2); 26 | void sort(); 27 | void print(); 28 | 29 | // Applying Krushkal Algo 30 | void kruskalAlgo() 31 | { 32 | int belongs[MAX], i, j, cno1, cno2; 33 | elist.n = 0; 34 | 35 | for (i = 1; i < n; i++) 36 | for (j = 0; j < i; j++) 37 | { 38 | if (Graph[i][j] != 0) 39 | { 40 | elist.data[elist.n].u = i; 41 | elist.data[elist.n].v = j; 42 | elist.data[elist.n].w = Graph[i][j]; 43 | elist.n++; 44 | } 45 | } 46 | 47 | sort(); 48 | 49 | for (i = 0; i < n; i++) 50 | belongs[i] = i; 51 | 52 | spanlist.n = 0; 53 | 54 | for (i = 0; i < elist.n; i++) 55 | { 56 | cno1 = find(belongs, elist.data[i].u); 57 | cno2 = find(belongs, elist.data[i].v); 58 | 59 | if (cno1 != cno2) 60 | { 61 | spanlist.data[spanlist.n] = elist.data[i]; 62 | spanlist.n = spanlist.n + 1; 63 | applyUnion(belongs, cno1, cno2); 64 | } 65 | } 66 | } 67 | 68 | int find(int belongs[], int vertexno) 69 | { 70 | return (belongs[vertexno]); 71 | } 72 | 73 | void applyUnion(int belongs[], int c1, int c2) 74 | { 75 | int i; 76 | 77 | for (i = 0; i < n; i++) 78 | if (belongs[i] == c2) 79 | belongs[i] = c1; 80 | } 81 | 82 | // Sorting algo 83 | void sort() 84 | { 85 | int i, j; 86 | edge temp; 87 | 88 | for (i = 1; i < elist.n; i++) 89 | for (j = 0; j < elist.n - 1; j++) 90 | if (elist.data[j].w > elist.data[j + 1].w) 91 | { 92 | temp = elist.data[j]; 93 | elist.data[j] = elist.data[j + 1]; 94 | elist.data[j + 1] = temp; 95 | } 96 | } 97 | 98 | // Printing the result 99 | void print() 100 | { 101 | int i, cost = 0; 102 | 103 | for (i = 0; i < spanlist.n; i++) 104 | { 105 | printf("\n%d - %d : %d", spanlist.data[i].u, spanlist.data[i].v, spanlist.data[i].w); 106 | cost = cost + spanlist.data[i].w; 107 | } 108 | 109 | printf("\nSpanning tree cost: %d", cost); 110 | } 111 | 112 | int main() 113 | { 114 | int i, j, total_cost; 115 | 116 | n = 6; 117 | 118 | Graph[0][0] = 0; 119 | Graph[0][1] = 4; 120 | Graph[0][2] = 4; 121 | Graph[0][3] = 0; 122 | Graph[0][4] = 0; 123 | Graph[0][5] = 0; 124 | Graph[0][6] = 0; 125 | 126 | Graph[1][0] = 4; 127 | Graph[1][1] = 0; 128 | Graph[1][2] = 2; 129 | Graph[1][3] = 0; 130 | Graph[1][4] = 0; 131 | Graph[1][5] = 0; 132 | Graph[1][6] = 0; 133 | 134 | Graph[2][0] = 4; 135 | Graph[2][1] = 2; 136 | Graph[2][2] = 0; 137 | Graph[2][3] = 3; 138 | Graph[2][4] = 4; 139 | Graph[2][5] = 0; 140 | Graph[2][6] = 0; 141 | 142 | Graph[3][0] = 0; 143 | Graph[3][1] = 0; 144 | Graph[3][2] = 3; 145 | Graph[3][3] = 0; 146 | Graph[3][4] = 3; 147 | Graph[3][5] = 0; 148 | Graph[3][6] = 0; 149 | 150 | Graph[4][0] = 0; 151 | Graph[4][1] = 0; 152 | Graph[4][2] = 4; 153 | Graph[4][3] = 3; 154 | Graph[4][4] = 0; 155 | Graph[4][5] = 0; 156 | Graph[4][6] = 0; 157 | 158 | Graph[5][0] = 0; 159 | Graph[5][1] = 0; 160 | Graph[5][2] = 2; 161 | Graph[5][3] = 0; 162 | Graph[5][4] = 3; 163 | Graph[5][5] = 0; 164 | Graph[5][6] = 0; 165 | 166 | kruskalAlgo(); 167 | print(); 168 | } 169 | 170 | // OUTPUT 171 | // 2 - 1 : 2 172 | // 5 - 2 : 2 173 | // 3 - 2 : 3 174 | // 4 - 3 : 3 175 | // 1 - 0 : 4 176 | // Spanning tree cost: 14 177 | 178 | // The time complexity Of Kruskal's Algorithm is: O(E log E) -------------------------------------------------------------------------------- /Searching & Sorting/C Programs/hashing.c: -------------------------------------------------------------------------------- 1 | // Write a program to implement the concept of Hashing. 2 | 3 | #include 4 | 5 | int ht[10], i, found = 0, key; 6 | 7 | void insert_val(); 8 | void search_val(); 9 | void delete_val(); 10 | void display(); 11 | 12 | int main() 13 | { 14 | int option; 15 | for (i = 0; i < 10; i++) // to initialize every element as ‘-1’ 16 | ht[i] = -1; 17 | printf("*** MENU *** \n1.Insert \n2.Search \n3.Delete \n4.Display \n5.Exit\n"); 18 | do 19 | { 20 | printf("Enter Choice: "); 21 | scanf("%d", &option); 22 | switch (option) 23 | { 24 | case 1: 25 | insert_val(); 26 | break; 27 | case 2: 28 | search_val(); 29 | break; 30 | case 3: 31 | delete_val(); 32 | break; 33 | case 4: 34 | display(); 35 | break; 36 | default: 37 | printf("Invalid choice entry!!!\n"); 38 | break; 39 | } 40 | } while (option != 5); 41 | return 0; 42 | } 43 | 44 | void insert_val() 45 | { 46 | int val, f = 0; 47 | printf("Enter the element to be inserted : "); 48 | scanf("%d", &val); 49 | key = (val % 10) - 1; 50 | if (ht[key] == -1) 51 | { 52 | ht[key] = val; 53 | } 54 | else 55 | { 56 | if (key < 9) 57 | { 58 | for (i = key + 1; i < 10; i++) 59 | { 60 | if (ht[i] == -1) 61 | { 62 | ht[i] = val; 63 | break; 64 | } 65 | } 66 | } 67 | for (i = 0; i < key; i++) 68 | { 69 | if (ht[i] == -1) 70 | { 71 | ht[i] = val; 72 | break; 73 | } 74 | } 75 | } 76 | } 77 | 78 | void display() 79 | { 80 | for (i = 0; i < 10; i++) 81 | printf("%d\t", ht[i]); 82 | printf("\n"); 83 | } 84 | 85 | void search_val() 86 | { 87 | int val, flag = 0; 88 | printf("Enter the element to be searched: "); 89 | scanf("%d", &val); 90 | key = (val % 10) - 1; 91 | if (ht[key] == val) 92 | flag = 1; 93 | else 94 | { 95 | for (i = key + 1; i < 10; i++) 96 | { 97 | if (ht[i] == val) 98 | { 99 | flag = 1; 100 | key = i; 101 | break; 102 | } 103 | } 104 | } 105 | if (flag == 0) 106 | { 107 | for (i = 0; i < key; i++) 108 | { 109 | if (ht[i] == val) 110 | { 111 | flag = 1; 112 | key = i; 113 | break; 114 | } 115 | } 116 | } 117 | if (flag == 1) 118 | { 119 | found = 1; 120 | printf("The item searched was found at position %d!\n", key + 1); 121 | } 122 | else 123 | { 124 | key = -1; 125 | printf("The item searched was not found in the hash table\n"); 126 | } 127 | } 128 | 129 | void delete_val() 130 | { 131 | search_val(); 132 | if (found == 1) 133 | { 134 | if (key != -1) 135 | { 136 | printf("The element deleted is %d \n", ht[key]); 137 | ht[key] = -1; 138 | } 139 | } 140 | } 141 | 142 | // OUTPUT 143 | // *** MENU *** 144 | // 1.Insert 145 | // 2.Search 146 | // 3.Delete 147 | // 4.Display 148 | // 5.Exit 149 | // Enter Choice: 1 150 | // Enter the element to be inserted : 45 151 | // Enter Choice: 1 152 | // Enter the element to be inserted : 73 153 | // Enter Choice: 1 154 | // Enter the element to be inserted : 81 155 | // Enter Choice: 4 156 | // 81 -1 73 -1 45 -1 -1 -1 -1 -1 157 | // Enter Choice: 2 158 | // Enter the element to be searched: 73 159 | // The item searched was found at position 3! 160 | // Enter Choice: 3 161 | // Enter the element to be searched: 45 162 | // The item searched was found at position 5! 163 | // The element deleted is 45 164 | // Enter Choice: 4 165 | // 81 -1 73 -1 -1 -1 -1 -1 -1 -1 166 | // Enter Choice: 5 167 | --------------------------------------------------------------------------------