├── 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 | //