├── .gitignore ├── Array ├── 1-Traversing-Array.c ├── 2-Inserting-Array 2.c ├── 2-Inserting-Array.c ├── 3-deleting-item-form-array.c ├── 4-sort-int-bubble-sort.c ├── 4-sort-string-bubble-sort.c ├── 5-searching-int-linear-binary.c ├── 5-searching-string-linear-binary.c ├── 6-Merging-two-char-arrays.c ├── 6-Merging-two-int-arrays.c ├── 7-2D-array-into-linear-array.c ├── 8-Matrix-add-mul-sub.c ├── 9-sparse-matrix-into-1D-Array.c └── create-add-delete-sort-search-in-array.c ├── Graph ├── 1-graph.c ├── 2-strongly-connected-or-not.c ├── 3-warshalls-path-matrix.c ├── 4-sortest-path-with-warshall-algorithm.c ├── 5-linked-representation-of-graph.c ├── 6-BFS-in-C.c ├── 6-BFS.c └── 7-DFS.c ├── LinkedList ├── 1-create-linkedlist-store-value.c ├── 2-Traversing-maximum-minimum-total-average-sin-value-linkedlist.c ├── 3-inserting-String-byPosition-and-according-to-sorting.c ├── 3-inserting-byPosition-and-according-to-sorting.c ├── 4-deleting-item-from-list.c ├── 5-Sorting-in-linkedlist.c ├── 6-Searching-in-linkedlist.c ├── create-add-delete-sort-search-in-linked-list.c └── linkedlist.c ├── Question (2020).pdf ├── README.md ├── Stack-Queue-Recursion ├── 1-push-item-into-stack.c ├── 10-insert-element-into-queue.c ├── 11-delete-element-from-queue.c ├── 2-delete-top-element-into-stack.c ├── 3-evaluate-postfix-notation.c ├── 4-infix-to-postfix.c ├── 5-evaluate-infix-notation.c ├── 6-Quick-Sort.c ├── 7-factorial-using-recursive.c ├── 8-fibonacci-sequence-using-recursion.c └── 9-tower-of-hanoi-for-n-disks.c └── Tree ├── 1-binary-search-tree.c ├── 2-insertion-into-heap.c ├── 3-delete-from-heap.c ├── 4-preorder-tree-traversal.c ├── 5-inorder-tree-traversal.c └── 6-postorder-tree-traversal.c /.gitignore: -------------------------------------------------------------------------------- 1 | *.exe 2 | *.o 3 | -------------------------------------------------------------------------------- /Array/1-Traversing-Array.c: -------------------------------------------------------------------------------- 1 | /* Traversing: carry out the following operations on an Array 2 | a) Maximum Value b) Minimum Value c) Average Value d) Total Value e) Sin Value 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | void printArray(int arr[], int n); 10 | int maximumValue(int arr[], int n); 11 | int minimumValue(int arr[], int n); 12 | float avgValue(int arr[], int n); 13 | int totalValue(int arr[], int n); 14 | void sinValue(int arr[], int n); 15 | 16 | int main() 17 | { 18 | int array[100], n, i; 19 | printf("Enter the total number of elements: "); 20 | scanf("%d", &n); 21 | 22 | printf("\nEnter %d number of element: ", n); 23 | for(i = 0; i max){ 40 | max = arr[i]; 41 | } 42 | } 43 | return max; 44 | } 45 | 46 | int minimumValue(int arr[], int n) 47 | { 48 | int min= arr[0], i; 49 | for(i=0; i 5 | #include 6 | #include 7 | 8 | char array[20][100]; 9 | int n; 10 | void sortElement(char arr[][100], int n); 11 | int value(char a[][100], int n); 12 | int index(char a[][100], int n); 13 | 14 | 15 | int main(){ 16 | 17 | printf("Enter Size of array: "); 18 | scanf("%d",&n); 19 | 20 | printf("Enter Strings of the array: \n"); 21 | 22 | for(int i=0;i=position; j--){ 97 | strcpy(arr[j], arr[j-1]); 98 | } 99 | strcpy(arr[position], item); 100 | n=n+1; 101 | 102 | printf("\nAfter inserted: "); 103 | for(int k=0;k=indexValue; j--){ 121 | strcpy(arr[j],arr[j-1]); 122 | } 123 | strcpy(arr[indexValue-1], item); 124 | n=n+1; 125 | printf("\nAfter inserted: "); 126 | for(int i=0; i 6 | 7 | void printArray(int arr[], int n); 8 | void insertElementToSortedArray(int arr[], int n); 9 | int insertElementByPosition(int arr[], int n, int pos, int value); 10 | 11 | int main() 12 | { 13 | int n, array[100], pos, value, x = 1; 14 | printf("Enter total number of element: "); 15 | scanf("%d", &n); 16 | 17 | printf("Enter %d number of elements: ", n); 18 | for(int i=0; i= pos; i--){ 46 | arr[i] = arr[i-1]; 47 | } 48 | arr[pos] = value; 49 | 50 | return arr; 51 | } 52 | 53 | 54 | void insertElementToSortedArray(int arr[], int n) 55 | { 56 | int value, i, j, temp, pos; 57 | printf("Enter the value that you want to insert: "); 58 | scanf("%d", &value); 59 | 60 | for(i = 0; i 6 | #include 7 | #include 8 | 9 | char array[20][100]; 10 | int n; 11 | void printArray(char arr[][100], int n); 12 | int deleteElementByPosition(char arr[][100], int n, int pos); 13 | int deleleElement(char arr[][100], int n); 14 | 15 | int main() 16 | { 17 | int pos; 18 | printf("Enter the total number of element: "); 19 | scanf("%d", &n); 20 | 21 | printf("Enter %d number of string: ", n); 22 | 23 | for(int i=0; i 6 | 7 | void printArray(int arr[], int n) 8 | { 9 | for(int i=0; i arr[j+1]){ 21 | temp = arr[j]; 22 | arr[j] = arr[j+1]; 23 | arr[j+1] = temp; 24 | } 25 | } 26 | } 27 | printArray(arr, n); 28 | } 29 | 30 | int main() 31 | { 32 | int array[100], n; 33 | printf("Enter total number of element: "); 34 | scanf("%d", &n); 35 | 36 | printf("\nEnter %d number of elements: ", n); 37 | for(int i=0; i 6 | #include 7 | 8 | void sortElement(char arr[][50], int n); 9 | 10 | int main() 11 | { 12 | char array[10][50]; 13 | int n; 14 | printf("Enter total number of element: "); 15 | scanf("%d", &n); 16 | 17 | printf("Enter elements: "); 18 | for(int i=0; i0){ 37 | strcpy(temp, arr[j]); 38 | strcpy(arr[j], arr[j+1]); 39 | strcpy(arr[j+1], temp); 40 | } 41 | } 42 | } 43 | printf("\n\n Sorted Elements: "); 44 | for(i =0; i 6 | #include 7 | 8 | void linearSearch(int arr[], int n, int search); 9 | void binarySearch(int arr[], int n, int search); 10 | 11 | int main() 12 | { 13 | int array[100], i, n, searchNum, select; 14 | 15 | printf(" Enter the total number of element: "); 16 | scanf("%d", &n); 17 | 18 | printf("\n Enter %d number of elements: ", n); 19 | for(i =0; i search){ 71 | end = mid - 1; 72 | } 73 | else if(arr[mid] == search){ 74 | printf("\n %d is FOUND at index array[%d]\n", search, mid); 75 | break; 76 | } 77 | else{ 78 | start = mid + 1; 79 | } 80 | 81 | mid = (start + end) / 2; 82 | } 83 | if(start > end){ 84 | printf("\n %d in NOT FOUND\n", search); 85 | } 86 | } 87 | 88 | -------------------------------------------------------------------------------- /Array/5-searching-string-linear-binary.c: -------------------------------------------------------------------------------- 1 | /* Searching: 2 | Search for Karim using Linear/Binary Search 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | void linearSearch(char arr[][50], int n, char search[]); 10 | void binarySearch(char arr[][50], int n, char search[]); 11 | 12 | int main() 13 | { 14 | char array[10][50], searchStr[10]; 15 | int n, i, select; 16 | printf(" Enter total number of element: "); 17 | scanf("%d", &n); 18 | 19 | printf("Enter %d number of String: ", n); 20 | for(i = 0; i 0){ 76 | start = mid + 1; 77 | } 78 | else if(strcmp(search, arr[mid]) == 0){ 79 | printf("\n \"%s\" is FOUND at array[%d].\n", search, mid); 80 | break; 81 | } 82 | else{ 83 | end = mid - 1; 84 | } 85 | mid = (start + end)/2; 86 | } 87 | if(start > end){ 88 | printf("\n \"%s\" is NOT FOUND.\n", search); 89 | } 90 | } 91 | 92 | -------------------------------------------------------------------------------- /Array/6-Merging-two-char-arrays.c: -------------------------------------------------------------------------------- 1 | /* Merging: 2 | Add two character type arrays. 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | void mergeStr(char *str1, char *str2) 9 | { 10 | int len = strlen(str1), i=0; 11 | while(str2[i] != '\0'){ 12 | str1[len] = str2[i]; 13 | len++; 14 | i++; 15 | } 16 | str1[len] = '\0'; 17 | } 18 | 19 | int main() 20 | { 21 | int x, m, n, i, k; 22 | char str1[50], str2[50], merge[100]; 23 | 24 | printf(" Enter your 1st String: "); 25 | gets(str1); 26 | 27 | printf("\n Enter your 2nd String: "); 28 | gets(str2); 29 | 30 | printf("\n\n 1st String: "); 31 | puts(str1); 32 | 33 | printf("\n\n 2nd String: "); 34 | puts(str2); 35 | 36 | printf("\n\n Merged String: "); 37 | mergeStr(str1, str2); 38 | puts(str1); 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Array/6-Merging-two-int-arrays.c: -------------------------------------------------------------------------------- 1 | /*Merging: 2 | Add two integer type arrays. 3 | */ 4 | 5 | #include 6 | 7 | int main() 8 | { 9 | int x, m, n, i, k; 10 | printf(" Enter 1st array size: "); 11 | scanf("%d", &m); 12 | printf("\n Enter 2nd array size: "); 13 | scanf("%d", &n); 14 | 15 | x = m+n; 16 | int array1[m], array2[n], merge[x]; 17 | 18 | printf("\n Enter 1st array elements : "); 19 | for(i = 0; i 4 | #define n 3 5 | 6 | void print1DArray(int arr[]) 7 | { 8 | int x = n*n, i; 9 | for(i=0; i 4 | #include 5 | 6 | int mat1[20][20], mat2[20][20], row1, row2, col1, col2; 7 | 8 | // input matrix function 9 | void inputMatrix(int row, int col, int mat[20][20]) 10 | { 11 | int i, j; 12 | for(i=0; i 4 | 5 | int main() 6 | { 7 | int A[10][10], B[100], row, col, i, j, k=0, searchRow, searchCol; 8 | printf("Enter the number of row of sparse matrix: "); 9 | scanf("%d", &row); 10 | 11 | printf("\nEnter the number of column of sparse matrix: "); 12 | scanf("%d", &col); 13 | 14 | printf("\nEnter your sparse matrix: \n"); 15 | for(i=0; i 2 | 3 | void printArray(int arr[], int length) 4 | { 5 | printf("\n---------------------------------------\n Elements: "); 6 | for(int i=0; i arr[j+1]){ 32 | temp = arr[j]; 33 | arr[j] = arr[j+1]; 34 | arr[j+1] = temp; 35 | } 36 | } 37 | } 38 | printArray(arr, length); 39 | } 40 | 41 | int deleteElement(int arr[], int length) 42 | { 43 | int deleteNum, pos; 44 | printf("\n Enter a number that you want delete: "); 45 | scanf("%d", &deleteNum); 46 | 47 | pos = searchElement(arr, length, deleteNum); 48 | 49 | if(pos < length){ 50 | for(int i = pos; i 11 | #include 12 | 13 | int adj[20][20], n, mul[20][20]; 14 | 15 | void adjacentFind(); 16 | void multiplication(); 17 | void pathLength(int length); 18 | 19 | int main() 20 | { 21 | int i, j, k; 22 | printf("Enter the total number of vertex: "); 23 | scanf("%d", &n); 24 | 25 | printf("Enter your adjacent matrix: \n"); 26 | for(i=0; i V%d \n", i+1, j+1); 66 | } 67 | } 68 | } 69 | } 70 | 71 | void multiplication() 72 | { 73 | int i, j, k, res[20][20]; 74 | for(i=0; i V%d\n", j+1, k+1); 98 | } 99 | } 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /Graph/2-strongly-connected-or-not.c: -------------------------------------------------------------------------------- 1 | /*Take adjacency matrix with m nodes as input and calculate Br and from that calculate Path Matrix and tell whether the matrix is strongly connected or not. 2 | */ 3 | 4 | #include 5 | #include 6 | 7 | int adj[10][10], A[10][10], sum[10][10], n; 8 | 9 | void mul(); 10 | void printMat(int mat[10][10]); 11 | void checkStrong(); 12 | 13 | int main() 14 | { 15 | int i, j, k; 16 | printf("Enter order of matrix: "); 17 | scanf("%d", &n); 18 | 19 | printf("\n Enter %d x %d matrix elements: \n", n, n); 20 | for(i=0; i 0){ 98 | printf(" The graph is not strongly connected.\n"); 99 | } 100 | else{ 101 | printf(" The graph is strongly connected.\n"); 102 | } 103 | } 104 | 105 | -------------------------------------------------------------------------------- /Graph/3-warshalls-path-matrix.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fahimahammed/CSE2122-Data-Structure-Lab/b1ad8efdb04f8295d3777efc4b05f3ef0d749a0a/Graph/3-warshalls-path-matrix.c -------------------------------------------------------------------------------- /Graph/4-sortest-path-with-warshall-algorithm.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int W[10][10], A[10][10], n; 4 | 5 | void printMat(int mat[10][10]); 6 | void sortestPath(); 7 | 8 | int main() 9 | { 10 | int i, j; 11 | printf("Enter the order of the matrix: "); 12 | scanf("%d", &n); 13 | 14 | printf("Enter matrix: \n"); 15 | for(i=0; i 5 | #include 6 | 7 | #define N 6 8 | 9 | struct Graph 10 | { 11 | struct Node *head[N]; 12 | }; 13 | 14 | struct Node 15 | { 16 | int dest; 17 | struct Node* next; 18 | }; 19 | 20 | struct Edge 21 | { 22 | int src, dest; 23 | }; 24 | 25 | 26 | 27 | struct Graph* createGraph(struct Edge edges[], int n) 28 | { 29 | struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph)); 30 | 31 | for(int i=0; ihead[i] = NULL; 33 | } 34 | 35 | for(int i=0; idest = dest; 42 | 43 | newNode->next = graph->head[src]; 44 | graph->head[src] = newNode; 45 | } 46 | 47 | return graph; 48 | }; 49 | 50 | void printGraph(struct Graph* graph) 51 | { 52 | for(int i =0; ihead[i]; 54 | while(ptr != NULL){ 55 | printf("(%d => %d)\t", i, ptr->dest); 56 | ptr = ptr->next; 57 | } 58 | printf("\n"); 59 | } 60 | } 61 | 62 | int main(void) 63 | { 64 | struct Edge edges[] = 65 | { 66 | {0, 1}, {1, 2}, {2, 0}, {2, 1}, {3, 2}, {4, 5}, {5, 4} 67 | }; 68 | 69 | int n = sizeof(edges)/sizeof(edges[0]); 70 | struct Graph *graph = createGraph(edges, n); 71 | 72 | printGraph(graph); 73 | 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /Graph/6-BFS-in-C.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void BFS(int); 5 | int graph[10][10], visited[10], total; 6 | 7 | int main() 8 | { 9 | int i, j; 10 | printf("\nEnter the total number of vertices in graph: "); 11 | scanf("%d", &total); 12 | 13 | printf("\nEnter the value for adj matrix: "); 14 | for(i=0; i 4 | #include 5 | #define SIZE 40 6 | 7 | struct queue { 8 | int items[SIZE]; 9 | int front; 10 | int rear; 11 | }; 12 | 13 | struct queue* createQueue(); 14 | void enqueue(struct queue* q, int); 15 | int dequeue(struct queue* q); 16 | void display(struct queue* q); 17 | int isEmpty(struct queue* q); 18 | void printQueue(struct queue* q); 19 | 20 | struct node { 21 | int vertex; 22 | struct node* next; 23 | }; 24 | 25 | struct node* createNode(int); 26 | 27 | struct Graph { 28 | int numVertices; 29 | struct node** adjLists; 30 | int* visited; 31 | }; 32 | 33 | // BFS algorithm 34 | void bfs(struct Graph* graph, int startVertex) { 35 | struct queue* q = createQueue(); 36 | 37 | graph->visited[startVertex] = 1; 38 | enqueue(q, startVertex); 39 | 40 | while (!isEmpty(q)) { 41 | printQueue(q); 42 | int currentVertex = dequeue(q); 43 | printf("Visited %d\n", currentVertex); 44 | 45 | struct node* temp = graph->adjLists[currentVertex]; 46 | 47 | while (temp) { 48 | int adjVertex = temp->vertex; 49 | 50 | if (graph->visited[adjVertex] == 0) { 51 | graph->visited[adjVertex] = 1; 52 | enqueue(q, adjVertex); 53 | } 54 | temp = temp->next; 55 | } 56 | } 57 | } 58 | 59 | // Creating a node 60 | struct node* createNode(int v) { 61 | struct node* newNode = malloc(sizeof(struct node)); 62 | newNode->vertex = v; 63 | newNode->next = NULL; 64 | return newNode; 65 | } 66 | 67 | // Creating a graph 68 | struct Graph* createGraph(int vertices) { 69 | struct Graph* graph = malloc(sizeof(struct Graph)); 70 | graph->numVertices = vertices; 71 | 72 | graph->adjLists = malloc(vertices * sizeof(struct node*)); 73 | graph->visited = malloc(vertices * sizeof(int)); 74 | 75 | int i; 76 | for (i = 0; i < vertices; i++) { 77 | graph->adjLists[i] = NULL; 78 | graph->visited[i] = 0; 79 | } 80 | 81 | return graph; 82 | } 83 | 84 | // Add edge 85 | void addEdge(struct Graph* graph, int src, int dest) { 86 | // Add edge from src to dest 87 | struct node* newNode = createNode(dest); 88 | newNode->next = graph->adjLists[src]; 89 | graph->adjLists[src] = newNode; 90 | 91 | // Add edge from dest to src 92 | newNode = createNode(src); 93 | newNode->next = graph->adjLists[dest]; 94 | graph->adjLists[dest] = newNode; 95 | } 96 | 97 | // Create a queue 98 | struct queue* createQueue() { 99 | struct queue* q = malloc(sizeof(struct queue)); 100 | q->front = -1; 101 | q->rear = -1; 102 | return q; 103 | } 104 | 105 | // Check if the queue is empty 106 | int isEmpty(struct queue* q) { 107 | if (q->rear == -1) 108 | return 1; 109 | else 110 | return 0; 111 | } 112 | 113 | // Adding elements into queue 114 | void enqueue(struct queue* q, int value) { 115 | if (q->rear == SIZE - 1) 116 | printf("\nQueue is Full!!"); 117 | else { 118 | if (q->front == -1) 119 | q->front = 0; 120 | q->rear++; 121 | q->items[q->rear] = value; 122 | } 123 | } 124 | 125 | // Removing elements from queue 126 | int dequeue(struct queue* q) { 127 | int item; 128 | if (isEmpty(q)) { 129 | printf("Queue is empty"); 130 | item = -1; 131 | } else { 132 | item = q->items[q->front]; 133 | q->front++; 134 | if (q->front > q->rear) { 135 | printf("Resetting queue "); 136 | q->front = q->rear = -1; 137 | } 138 | } 139 | return item; 140 | } 141 | 142 | // Print the queue 143 | void printQueue(struct queue* q) { 144 | int i = q->front; 145 | 146 | if (isEmpty(q)) { 147 | printf("Queue is empty"); 148 | } else { 149 | printf("\nQueue contains \n"); 150 | for (i = q->front; i < q->rear + 1; i++) { 151 | printf("%d ", q->items[i]); 152 | } 153 | } 154 | } 155 | 156 | int main() { 157 | struct Graph* graph = createGraph(6); 158 | addEdge(graph, 0, 1); 159 | addEdge(graph, 0, 2); 160 | addEdge(graph, 1, 2); 161 | addEdge(graph, 1, 4); 162 | addEdge(graph, 1, 3); 163 | addEdge(graph, 2, 4); 164 | addEdge(graph, 3, 4); 165 | 166 | bfs(graph, 0); 167 | 168 | return 0; 169 | } 170 | -------------------------------------------------------------------------------- /Graph/7-DFS.c: -------------------------------------------------------------------------------- 1 | // DFS algorithm in C 2 | 3 | #include 4 | #include 5 | 6 | struct node { 7 | int vertex; 8 | struct node* next; 9 | }; 10 | 11 | struct node* createNode(int v); 12 | 13 | struct Graph { 14 | int numVertices; 15 | int* visited; 16 | 17 | // We need int** to store a two dimensional array. 18 | // Similary, we need struct node** to store an array of Linked lists 19 | struct node** adjLists; 20 | }; 21 | 22 | // DFS algo 23 | void DFS(struct Graph* graph, int vertex) { 24 | struct node* adjList = graph->adjLists[vertex]; 25 | struct node* temp = adjList; 26 | 27 | graph->visited[vertex] = 1; 28 | printf(" %d ", vertex); 29 | 30 | while (temp != NULL) { 31 | int connectedVertex = temp->vertex; 32 | 33 | if (graph->visited[connectedVertex] == 0) { 34 | DFS(graph, connectedVertex); 35 | } 36 | temp = temp->next; 37 | } 38 | } 39 | 40 | // Create a node 41 | struct node* createNode(int v) { 42 | struct node* newNode = malloc(sizeof(struct node)); 43 | newNode->vertex = v; 44 | newNode->next = NULL; 45 | return newNode; 46 | } 47 | 48 | // Create graph 49 | struct Graph* createGraph(int vertices) { 50 | struct Graph* graph = malloc(sizeof(struct Graph)); 51 | graph->numVertices = vertices; 52 | 53 | graph->adjLists = malloc(vertices * sizeof(struct node*)); 54 | 55 | graph->visited = malloc(vertices * sizeof(int)); 56 | 57 | int i; 58 | for (i = 0; i < vertices; i++) { 59 | graph->adjLists[i] = NULL; 60 | graph->visited[i] = 0; 61 | } 62 | return graph; 63 | } 64 | 65 | // Add edge 66 | void addEdge(struct Graph* graph, int src, int dest) { 67 | // Add edge from src to dest 68 | struct node* newNode = createNode(dest); 69 | newNode->next = graph->adjLists[src]; 70 | graph->adjLists[src] = newNode; 71 | 72 | // Add edge from dest to src 73 | newNode = createNode(src); 74 | newNode->next = graph->adjLists[dest]; 75 | graph->adjLists[dest] = newNode; 76 | } 77 | 78 | // Print the graph 79 | void printGraph(struct Graph* graph) { 80 | int v; 81 | for (v = 0; v < graph->numVertices; v++) { 82 | struct node* temp = graph->adjLists[v]; 83 | printf("\n Adjacency list of vertex %d\n ", v); 84 | while (temp) { 85 | printf("%d ", temp->vertex); 86 | temp = temp->next; 87 | } 88 | printf("\n"); 89 | } 90 | } 91 | 92 | int main() { 93 | struct Graph* graph = createGraph(4); 94 | addEdge(graph, 0, 1); 95 | addEdge(graph, 0, 2); 96 | addEdge(graph, 1, 2); 97 | addEdge(graph, 2, 3); 98 | 99 | printGraph(graph); 100 | 101 | printf("\n Visited: "); 102 | DFS(graph, 3); 103 | 104 | return 0; 105 | } 106 | -------------------------------------------------------------------------------- /LinkedList/1-create-linkedlist-store-value.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct node{ 5 | int data; 6 | struct node *next; 7 | }*head; 8 | 9 | void createList(int n); 10 | void printList(); 11 | 12 | int main() 13 | { 14 | int n; 15 | printf("Enter total number of nodes: "); 16 | scanf("%d", &n); 17 | 18 | createList(n); 19 | printf("\n\n LInked List\n"); 20 | printList(); 21 | return 0; 22 | } 23 | 24 | void createList(int n) 25 | { 26 | struct node *newNode, *temp; 27 | int data, i; 28 | 29 | head = (struct node *)malloc(sizeof(struct node)); 30 | if(head == NULL){ 31 | printf("Unable to access memory.\n"); 32 | exit(0); 33 | } 34 | 35 | printf("Enter the data of node 1: "); 36 | scanf("%d", &data); 37 | 38 | head->data = data; 39 | head->next = NULL; 40 | 41 | temp = head; 42 | for(i=2; i<=n; i++){ 43 | newNode = (struct node *)malloc(sizeof(struct node)); 44 | 45 | if(newNode == NULL){ 46 | printf("Unable to access memory.\n"); 47 | exit(0); 48 | } 49 | 50 | printf("Enter the data of node %d: ", i); 51 | scanf("%d", &data); 52 | 53 | newNode->data = data; 54 | newNode->next = NULL; 55 | 56 | temp->next = newNode; 57 | temp = temp->next; 58 | } 59 | } 60 | 61 | void printList() 62 | { 63 | struct node *temp; 64 | if(head == NULL){ 65 | printf("List is empty!\n"); 66 | exit(0); 67 | } 68 | 69 | temp = head; 70 | while(temp != NULL){ 71 | printf(" %d ->", temp->data); 72 | temp = temp->next; 73 | } 74 | printf(" NULL\n"); 75 | } 76 | -------------------------------------------------------------------------------- /LinkedList/2-Traversing-maximum-minimum-total-average-sin-value-linkedlist.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct node{ 6 | int data; 7 | struct node *next; 8 | } * head; 9 | 10 | void createList(int n); 11 | void printList(); 12 | void maxValue(); 13 | void minValue(); 14 | float total(); 15 | void average(float sum, int n); 16 | void sinValue(); 17 | 18 | int main() 19 | { 20 | int n; 21 | float sum; 22 | printf("Enter total number of nodes: "); 23 | scanf("%d", &n); 24 | 25 | createList(n); 26 | printList(); 27 | maxValue(); 28 | minValue(); 29 | sum = total(); 30 | average(sum, n); 31 | sinValue(); 32 | return 0; 33 | } 34 | 35 | void createList(int n) 36 | { 37 | struct node *newNode, *temp; 38 | int data, i; 39 | 40 | head = (struct node*)malloc(sizeof(struct node)); 41 | 42 | if (head == NULL){ 43 | printf("Unable to access memory.\n"); 44 | exit(0); 45 | } 46 | 47 | printf("\n Enter the data of node 1: "); 48 | scanf("%d", &data); 49 | head->data = data; 50 | head->next = NULL; 51 | 52 | temp = head; 53 | 54 | for(i=2; i<=n; i++){ 55 | newNode = (struct node *)malloc(sizeof(struct node)); 56 | 57 | if (newNode == NULL){ 58 | printf("Unable to access memory\n"); 59 | exit(0); 60 | } 61 | 62 | printf(" Enter the data of node %d: ", i); 63 | scanf("%d", &data); 64 | newNode->data = data; 65 | newNode->next = NULL; 66 | 67 | temp->next = newNode; 68 | temp = temp->next; 69 | } 70 | } 71 | 72 | void printList() 73 | { 74 | struct node *temp; 75 | if(head == NULL){ 76 | printf("List is Empty!\n"); 77 | exit(0); 78 | } 79 | temp = head; 80 | printf("\n List: "); 81 | while(temp != NULL){ 82 | printf(" %d ", temp->data); 83 | temp = temp->next; 84 | } 85 | printf("\n"); 86 | } 87 | 88 | void maxValue() 89 | { 90 | struct node *temp; 91 | int max; 92 | if(head == NULL){ 93 | printf("List is Empty!\n"); 94 | exit(0); 95 | } 96 | 97 | temp = head; 98 | max = temp->data; 99 | while(temp != NULL){ 100 | if(max < temp->data){ 101 | max = temp->data; 102 | } 103 | temp = temp->next; 104 | } 105 | 106 | printf("\n Maximum Value : %d\n", max); 107 | } 108 | 109 | void minValue() 110 | { 111 | struct node *temp; 112 | int min; 113 | if(head == NULL){ 114 | printf("List is empty!\n"); 115 | exit(0); 116 | } 117 | 118 | temp = head; 119 | min = temp->data; 120 | while(temp != NULL){ 121 | if(min > temp->data){ 122 | min = temp->data; 123 | } 124 | temp = temp->next; 125 | } 126 | printf("\n Minimum Value : %d\n", min); 127 | } 128 | 129 | float total() 130 | { 131 | struct node *temp; 132 | float sum = 0.00; 133 | if(head == NULL){ 134 | printf("List is empty!\n"); 135 | exit(0); 136 | } 137 | 138 | temp = head; 139 | while(temp != NULL){ 140 | sum = sum + temp->data; 141 | //printf(" node: %d ", temp->data); 142 | temp = temp->next; 143 | } 144 | printf("\n Total Value : %.0f\n", sum); 145 | 146 | return sum; 147 | } 148 | 149 | void average(float sum, int n) 150 | { 151 | printf("\n Average Value : %0.2f\n", (sum/n)); 152 | } 153 | 154 | void sinValue() 155 | { 156 | struct node *temp; 157 | if(head == NULL){ 158 | printf("List is empty!\n"); 159 | exit(0); 160 | } 161 | 162 | temp = head; 163 | printf("\n Sin Values: \n"); 164 | while (temp != NULL){ 165 | printf("\tsin(%d) = %0.2f\n", temp->data, sin(temp->data)); 166 | temp = temp->next; 167 | } 168 | 169 | } 170 | -------------------------------------------------------------------------------- /LinkedList/3-inserting-String-byPosition-and-according-to-sorting.c: -------------------------------------------------------------------------------- 1 | /*3. Inserting: insert a string in sorted list and by position*/ 2 | // INCOMPLETE 3 | 4 | #include "stdafx.h" 5 | #include 6 | #include 7 | 8 | typedef struct stringData { 9 | char *s; 10 | struct stringData *next; 11 | } Node; 12 | 13 | Node *createNode(char *s) { 14 | Node *newNode = (Node *)malloc(sizeof(Node)); 15 | newNode->s = s; 16 | newNode->next = NULL; 17 | return newNode; 18 | } 19 | 20 | 21 | void insert(Node *head, Node *newNode) { 22 | if (head == NULL) { 23 | head->s = newNode->s; 24 | head = newNode; 25 | } 26 | } 27 | 28 | void printList(Node *head) { 29 | while (head != NULL) { 30 | printf("%s\n", head->s); 31 | head = head->next; 32 | } 33 | } 34 | 35 | 36 | 37 | int main() 38 | { 39 | Node *head = createNode(NULL); 40 | 41 | Node *a = createNode("A"); 42 | insert(head, a); 43 | 44 | printList(head); 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /LinkedList/3-inserting-byPosition-and-according-to-sorting.c: -------------------------------------------------------------------------------- 1 | /*3. Inserting: a) inset a node in nth position. b) insert a node in sorted list*/ 2 | 3 | #include 4 | #include 5 | 6 | struct node{ 7 | int data; 8 | struct node *next; 9 | }*head; 10 | 11 | void createList(int n); 12 | void printList(); 13 | void insertAccSort(int n); 14 | void insertByPos(int value, int loc, struct node *head); 15 | 16 | int main() 17 | { 18 | int n, select, value, pos; 19 | printf("Enter total number of nodes: "); 20 | scanf("%d", &n); 21 | 22 | createList(n); 23 | printList(); 24 | 25 | printf("\n MENU \n\t1. Insert by postion.\n\t2. Insert according to ascending oreder.\n\t0. Exit\n Enter your choice: "); 26 | scanf("%d", &select); 27 | switch(select) 28 | { 29 | case 1: 30 | printf("\n Enter the position: "); 31 | scanf("%d", &pos); 32 | printf("\n Enter the data: "); 33 | scanf("%d", &value); 34 | insertByPos(value, pos, head); 35 | break; 36 | 37 | case 2: 38 | insertAccSort(n); 39 | break; 40 | 41 | case 0: 42 | exit(0); 43 | break; 44 | 45 | default: 46 | printf("\n Please enter valid input.\n"); 47 | } 48 | 49 | return 0; 50 | } 51 | 52 | void createList(int n) 53 | { 54 | struct node *temp, *newNode; 55 | int data, i; 56 | 57 | head = (struct node *)malloc(sizeof(struct node)); 58 | 59 | if (head == NULL){ 60 | printf("Unable to access memory.\n"); 61 | exit(0); 62 | } 63 | 64 | printf("\n Enter the data of node 1 : "); 65 | scanf("%d", &data); 66 | head->data = data; 67 | head->next = NULL; 68 | 69 | temp = head; 70 | for(i=2; i<=n; i++){ 71 | newNode = (struct node *)malloc(sizeof(struct node)); 72 | 73 | if(newNode == NULL){ 74 | printf("Unable to access memory.\n"); 75 | exit(0); 76 | } 77 | printf(" Enter the data of node %d : ", i); 78 | scanf("%d", &data); 79 | newNode->data = data; 80 | newNode->next = NULL; 81 | 82 | temp->next = newNode; 83 | temp = temp->next; 84 | } 85 | } 86 | 87 | void printList() 88 | { 89 | struct node *temp; 90 | if(head == NULL){ 91 | printf("List is Empty!\n"); 92 | exit(0); 93 | } 94 | temp = head; 95 | printf("\n List: "); 96 | while(temp != NULL){ 97 | printf(" %d ", temp->data); 98 | temp = temp->next; 99 | } 100 | printf("\n"); 101 | } 102 | 103 | void insertAccSort(int n) 104 | { 105 | struct node *temp, *newNode; 106 | int value, i, tmp, loc=1; 107 | printf("\n Enter a number that you want to insert at sorted list: "); 108 | scanf("%d", &value); 109 | 110 | temp = head; 111 | for(i=1; i<=n; i++){ 112 | tmp = temp->data; 113 | if(value > tmp){ 114 | loc++; 115 | } 116 | temp = temp->next; 117 | 118 | } 119 | //printf("loc: %d\n", loc); 120 | insertByPos(value, loc, head); 121 | } 122 | 123 | void insertByPos(int value, int loc, struct node *head1) 124 | { 125 | struct node *temp, *newNode; 126 | int i; 127 | newNode = (struct node *)malloc(sizeof(struct node)); 128 | 129 | if(newNode == NULL){ 130 | printf("Unable to allocate memory.\n"); 131 | exit(0); 132 | } 133 | 134 | newNode->data = value; 135 | newNode->next = NULL; 136 | 137 | temp = head1; 138 | for(i=2; inext; 140 | //temp->next = newNode; 141 | if(temp == NULL){ 142 | break; 143 | } 144 | } 145 | if(temp != NULL){ 146 | newNode->next = temp->next; 147 | temp->next = newNode; 148 | } 149 | printList(); 150 | } 151 | 152 | 153 | -------------------------------------------------------------------------------- /LinkedList/4-deleting-item-from-list.c: -------------------------------------------------------------------------------- 1 | /*deleting item from list*/ 2 | 3 | #include 4 | #include 5 | 6 | struct node{ 7 | int data; 8 | struct node *next; 9 | } *head; 10 | 11 | void createList(int n); 12 | void printList(); 13 | void deleteItemByPos(int pos); 14 | void deleteItem(); 15 | 16 | int main() 17 | { 18 | int n, item, pos; 19 | printf("Enter the total number of nodes: "); 20 | scanf("%d", &n); 21 | 22 | createList(n); 23 | printList(); 24 | 25 | printf("\n Enter the position: "); 26 | scanf("%d", &pos); 27 | deleteItemByPos(pos); 28 | printList(); 29 | 30 | printf("\n Enter the item: "); 31 | scanf("%d", &item); 32 | deleteItem(item); 33 | printList(); 34 | 35 | return 0; 36 | } 37 | 38 | void createList(int n) 39 | { 40 | struct node *newNode, *temp; 41 | int data, i; 42 | 43 | head = (struct node *)malloc(sizeof(struct node)); 44 | if(head == NULL){ 45 | printf("Unable to allocate memory.\n"); 46 | exit(0); 47 | } 48 | 49 | printf("\n Enter the data of node 1: "); 50 | scanf("%d", &data); 51 | head->data = data; 52 | head->next = NULL; 53 | 54 | temp = head; 55 | for(i = 2; i<=n; i++){ 56 | newNode = (struct node *)malloc(sizeof(struct node)); 57 | if(newNode == NULL){ 58 | printf("Unable to allocate memory\n"); 59 | exit(0); 60 | } 61 | 62 | printf(" Enter the data of node %d: ", i); 63 | scanf("%d", &data); 64 | 65 | newNode->data = data; 66 | newNode->next = NULL; 67 | 68 | temp->next = newNode; 69 | temp = temp->next; 70 | } 71 | } 72 | 73 | void printList() 74 | { 75 | struct node *temp; 76 | if(head == NULL){ 77 | printf("\n List is Empty!\n"); 78 | exit(0); 79 | } 80 | 81 | temp = head; 82 | while(temp != NULL){ 83 | printf(" %d ", temp->data); 84 | temp = temp->next; 85 | } 86 | } 87 | 88 | void deleteItemByPos(int pos) 89 | { 90 | struct node *temp, *prvs; 91 | if(head == NULL){ 92 | printf("\n List is Empty\n"); 93 | exit(0); 94 | } 95 | 96 | temp = head; 97 | if(pos == 1){ 98 | printf("\n\n %d is removed from list.\n", head->data); 99 | head = head->next; 100 | }else{ 101 | temp = temp->next; 102 | } 103 | 104 | for(int i=2; i<=pos; i++){ 105 | if(i == pos-1){ 106 | prvs = temp; 107 | } 108 | if(i==pos){ 109 | printf("\n\n %d is removed from list.\n", temp->data); 110 | prvs->next = temp->next; 111 | } 112 | temp = temp->next; 113 | } 114 | } 115 | 116 | void deleteItem(int item) 117 | { 118 | struct node *temp; 119 | int pos=1, data; 120 | if(head == NULL){ 121 | printf("\nList is Empty!\n"); 122 | exit(0); 123 | } 124 | 125 | temp = head; 126 | while(temp != NULL){ 127 | data = temp->data; 128 | if(data != item){ 129 | pos++; 130 | //printf(" %d ", temp->data); 131 | } 132 | if(data == item){ 133 | break; 134 | } 135 | temp = temp->next; 136 | } 137 | deleteItemByPos(pos); 138 | } 139 | -------------------------------------------------------------------------------- /LinkedList/5-Sorting-in-linkedlist.c: -------------------------------------------------------------------------------- 1 | /*Sorting a list in linked list*/ 2 | 3 | #include 4 | #include 5 | 6 | struct node{ 7 | int data; 8 | struct node *next; 9 | } *head; 10 | 11 | void createList(int n); 12 | void printList(); 13 | void sortList(); 14 | 15 | int main() 16 | { 17 | int n; 18 | printf("Enter the total number of nodes: "); 19 | scanf("%d", &n); 20 | 21 | createList(n); 22 | printf("\n Given List: "); 23 | printList(); 24 | 25 | sortList(); 26 | printf("\n Sorted list: "); 27 | printList(); 28 | printf("\n"); 29 | return 0; 30 | } 31 | 32 | void createList(int n) 33 | { 34 | struct node *temp, *newNode; 35 | int data, i; 36 | 37 | head = (struct node *)malloc(sizeof(struct node)); 38 | 39 | if(head == NULL){ 40 | printf("Unable to allocate memory.\n"); 41 | exit(0); 42 | } 43 | 44 | printf("\n Enter the data of node 1: "); 45 | scanf("%d", &data); 46 | head->data = data; 47 | head->next = NULL; 48 | 49 | temp = head; 50 | for(i=2; i<=n; i++){ 51 | newNode = (struct node *)malloc(sizeof(struct node)); 52 | if(newNode == NULL){ 53 | printf("Unable to allocate memory\n"); 54 | exit(0); 55 | } 56 | 57 | printf(" Enter the data of node %d: ", i); 58 | scanf("%d", &data); 59 | newNode->data = data; 60 | newNode->next = NULL; 61 | temp->next = newNode; 62 | temp = temp->next; 63 | } 64 | } 65 | 66 | void printList() 67 | { 68 | struct node *temp; 69 | if(head == NULL){ 70 | printf("\nEmpty List!\n"); 71 | exit(0); 72 | } 73 | temp = head; 74 | while(temp != NULL){ 75 | printf(" %d ", temp->data); 76 | temp = temp->next; 77 | } 78 | 79 | } 80 | 81 | void sortList() 82 | { 83 | struct node *current = head, *index = NULL; 84 | int temp; 85 | 86 | if(head == NULL){ 87 | printf("\nList is empty.\n"); 88 | exit(0); 89 | } 90 | 91 | while(current != NULL){ 92 | index = current->next; 93 | while(index != NULL){ 94 | if(current->data > index->data){ 95 | temp = current->data; 96 | current->data = index->data; 97 | index->data = temp; 98 | } 99 | index = index->next; 100 | } 101 | current = current->next; 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /LinkedList/6-Searching-in-linkedlist.c: -------------------------------------------------------------------------------- 1 | /*Searching in linked list*/ 2 | 3 | #include 4 | #include 5 | 6 | struct node{ 7 | int data; 8 | struct node *next; 9 | } *head; 10 | 11 | void createList(int n); 12 | void printList(); 13 | void searchItem(int item); 14 | 15 | int main() 16 | { 17 | int n, item; 18 | printf("Enter the total number of nodes: "); 19 | scanf("%d", &n); 20 | 21 | createList(n); 22 | printf("\n Given List: "); 23 | printList(); 24 | 25 | printf("\n\n Enter a number that you searching: "); 26 | scanf("%d", &item); 27 | searchItem(item); 28 | printf("\n"); 29 | return 0; 30 | } 31 | 32 | void createList(int n) 33 | { 34 | struct node *temp, *newNode; 35 | int data, i; 36 | 37 | head = (struct node *)malloc(sizeof(struct node)); 38 | 39 | if(head == NULL){ 40 | printf("Unable to allocate memory.\n"); 41 | exit(0); 42 | } 43 | 44 | printf("\n Enter the data of node 1: "); 45 | scanf("%d", &data); 46 | head->data = data; 47 | head->next = NULL; 48 | 49 | temp = head; 50 | for(i=2; i<=n; i++){ 51 | newNode = (struct node *)malloc(sizeof(struct node)); 52 | if(newNode == NULL){ 53 | printf("Unable to allocate memory\n"); 54 | exit(0); 55 | } 56 | 57 | printf(" Enter the data of node %d: ", i); 58 | scanf("%d", &data); 59 | newNode->data = data; 60 | newNode->next = NULL; 61 | temp->next = newNode; 62 | temp = temp->next; 63 | } 64 | } 65 | 66 | void printList() 67 | { 68 | struct node *temp; 69 | if(head == NULL){ 70 | printf("\nEmpty List!\n"); 71 | exit(0); 72 | } 73 | temp = head; 74 | while(temp != NULL){ 75 | printf(" %d ", temp->data); 76 | temp = temp->next; 77 | } 78 | } 79 | 80 | void searchItem(int item) 81 | { 82 | struct node *temp; 83 | int data, c=0; 84 | if(head == NULL){ 85 | printf("\n List is empty\n"); 86 | exit(0); 87 | } 88 | 89 | temp = head; 90 | while(temp != NULL){ 91 | data = temp->data; 92 | if(item == data){ 93 | printf("\n %d is FOUND.\n", item); 94 | c = 1; 95 | break; 96 | } 97 | temp = temp->next; 98 | } 99 | if(c == 0){ 100 | printf("\n %d is NOT FOUND.\n", item); 101 | } 102 | } 103 | 104 | -------------------------------------------------------------------------------- /LinkedList/create-add-delete-sort-search-in-linked-list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct node * createLinkedList(int arr[], int n); 5 | int searchInList(struct node *head, int value); 6 | 7 | 8 | struct node{ 9 | int data; 10 | struct node *next; 11 | }; 12 | 13 | 14 | int main() 15 | { 16 | int a[] = {2, 13, 4, 16}; 17 | struct node *head; 18 | head = createLinkedList(a, 4); 19 | while(head != NULL){ 20 | printf("%d ", head->data); 21 | head = head->next; 22 | } 23 | printf("index; %d\n", searchInList(head, 2)); 24 | return 0; 25 | } 26 | 27 | int searchInList(struct node *head, int x){ 28 | int index = 1; 29 | while(head != NULL){ 30 | if (head->data == x){ 31 | return index; 32 | } 33 | index++; 34 | head = head->next; 35 | } 36 | return -1; 37 | 38 | } 39 | 40 | struct node * createLinkedList(int arr[], int n) 41 | { 42 | int i; 43 | struct node *head = NULL, *temp = NULL, *current=NULL; 44 | 45 | for(i=0; idata = arr[i]; 48 | temp->next = NULL; 49 | 50 | if(head == NULL){ 51 | head = temp; 52 | current = temp; 53 | } 54 | else{ 55 | current->next = temp; 56 | current = current->next; 57 | } 58 | 59 | } 60 | return head; 61 | } 62 | -------------------------------------------------------------------------------- /LinkedList/linkedlist.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct node{ 5 | int data; 6 | struct node *next; 7 | }*head; 8 | 9 | void createList(int n); 10 | void printList(); 11 | int searchList(); 12 | 13 | int main() 14 | { 15 | int n, x; 16 | printf("Enter the total number of nodes: "); 17 | scanf("%d", &n); 18 | 19 | createList(n); 20 | printList(); 21 | x = searchList(); 22 | if(x == -1){ 23 | printf("\nNot Found\n"); 24 | } 25 | else{ 26 | printf("\nFound\n"); 27 | } 28 | //printf("%d", x); 29 | return 0; 30 | } 31 | 32 | void createList(int n) 33 | { 34 | struct node *newNode, *temp; 35 | int data, i; 36 | 37 | head = (struct node *)malloc(sizeof(struct node)); 38 | 39 | printf("Enter the value of node 1: "); 40 | scanf("%d", &data); 41 | 42 | head->data = data; 43 | head->next = NULL; 44 | 45 | temp = head; 46 | for(i= 2; i<=n; i++){ 47 | newNode = (struct node *)malloc(sizeof(struct node)); 48 | if(newNode == NULL){ 49 | printf("unable to allocate memory."); 50 | break; 51 | } 52 | printf("Enter the value of node %d: ", i); 53 | scanf("%d", &data); 54 | newNode->data = data; 55 | newNode->next = NULL; 56 | 57 | temp->next = newNode; 58 | temp = temp->next; 59 | } 60 | } 61 | 62 | void printList() 63 | { 64 | struct node *temp; 65 | if(head == NULL){ 66 | printf("Empty\n"); 67 | return; 68 | } 69 | temp = head; 70 | while(temp != NULL){ 71 | printf("%d -> ", temp->data); 72 | temp = temp->next; 73 | } 74 | printf("NULL\n"); 75 | } 76 | 77 | int searchList() 78 | { 79 | printf("Enter a number to search: "); 80 | int searchNum, index=1; 81 | scanf("%d", &searchNum); 82 | 83 | while(head != NULL){ 84 | if(head->data == searchNum){ 85 | return index; 86 | } 87 | index++; 88 | head = head->next; 89 | } 90 | return -1; 91 | 92 | //printList(); 93 | } 94 | -------------------------------------------------------------------------------- /Question (2020).pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fahimahammed/CSE2122-Data-Structure-Lab/b1ad8efdb04f8295d3777efc4b05f3ef0d749a0a/Question (2020).pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CSE2122: Data Structure Lab 2 | Welcome to the CSE2122 Data Structure Lab repository! This collection of C programs is designed to help you understand and implement fundamental data structures such as arrays and linked lists. The repository is organized into sections, each focusing on specific operations and functionalities. 3 | 4 | 5 |

Array

6 |
    7 |
  1. Traversing
  2. 8 |
  3. Inserting (int)
  4. 9 |
  5. Inserting (string)
  6. 10 |
  7. Deleting
  8. 11 |
  9. Sorting (Bubble sort: int type data)
  10. 12 |
  11. Sorting (Bubble sort: string type data)
  12. 13 |
  13. Searching (int)
  14. 14 |
  15. Searching (string)
  16. 15 |
  17. Merging (int)
  18. 16 |
  19. Merging (string)
  20. 17 |
  21. 2D array into Linear Array
  22. 18 |
  23. Matrix (Addition/Subtraction/Multiplication)
  24. 19 |
  25. Sparse Matrix into 1D array
  26. 20 |
21 | 22 |

Linked List

23 | 24 |
    25 |
  1. Create Linked List
  2. 26 |
  3. Traversing
  4. 27 |
  5. Inserting
  6. 28 |
  7. Deleting
  8. 29 |
  9. Sorting
  10. 30 |
  11. Searching
  12. 31 |
32 | 33 |

Stack, Queue, Recursion

34 |

Tree

35 |

Graph

36 | 37 | 38 |
Contributor(s)
39 | 40 |

FAHIM AHAMMED FIROZ

41 | -------------------------------------------------------------------------------- /Stack-Queue-Recursion/1-push-item-into-stack.c: -------------------------------------------------------------------------------- 1 | /*Push item into stack*/ 2 | 3 | #include 4 | #include 5 | 6 | #define MAX 5 7 | int stack[MAX], top = -1; 8 | 9 | void push(int item) 10 | { 11 | if(top == MAX-1){ 12 | printf("\n OVERFLOW.\n"); 13 | } 14 | else{ 15 | top = top + 1; 16 | stack[top] = item; 17 | printf("\n %d is pushed in stack.\n", item); 18 | } 19 | } 20 | 21 | void show() 22 | { 23 | printf("\n STACK: \n"); 24 | for(int i=top; i>=0; i--){ 25 | printf("\t%d\n", stack[i]); 26 | } 27 | } 28 | 29 | int main() 30 | { 31 | push(5); 32 | push(4); 33 | push(3); 34 | push(2); 35 | push(7); 36 | push(2); 37 | push(7); 38 | push(2); 39 | show(); 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Stack-Queue-Recursion/10-insert-element-into-queue.c: -------------------------------------------------------------------------------- 1 | /*Insert an element into a queue.*/ 2 | 3 | #include 4 | #define MAX 5 5 | 6 | int queueArray[MAX], front=-1, rear = -1; 7 | 8 | void enqueue() 9 | { 10 | if(rear == MAX-1){ 11 | printf("\n OVERFLOW\n"); 12 | } 13 | else{ 14 | if(front == -1){ 15 | front = 0; 16 | } 17 | printf("\n Enter a number: "); 18 | scanf("%d", &queueArray[++rear]); 19 | } 20 | 21 | } 22 | void display() 23 | { 24 | int i; 25 | if (front == - 1) 26 | printf(" Queue is empty \n"); 27 | else 28 | { 29 | printf(" Queue is : \n"); 30 | for (i = front; i <= rear; i++){ 31 | printf("%d ", queueArray[i]); 32 | } 33 | printf("\n"); 34 | } 35 | } 36 | int main() 37 | { 38 | enqueue(); 39 | enqueue(); 40 | enqueue(); 41 | display(); 42 | enqueue(); 43 | display(); 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /Stack-Queue-Recursion/11-delete-element-from-queue.c: -------------------------------------------------------------------------------- 1 | /*Delete an element from a queue. 2 | */ 3 | 4 | #include 5 | #define n 5 6 | int queue[n], front=-1, rear=-1; 7 | 8 | ///insert element into queue. 9 | void enqueue(int x){ 10 | if((rear+1)%n == front){ 11 | printf("Overflow\n"); 12 | } 13 | 14 | else if(front == -1 && rear == -1){ 15 | front = rear = 0; 16 | queue[rear]=x; 17 | } 18 | else{ 19 | rear = (rear+1)%n; 20 | queue[rear]=x; 21 | } 22 | } 23 | 24 | ///delete element into queue. 25 | void dequeue(){ 26 | if(front == -1 && rear == -1){ 27 | printf("Queue is Empty\n"); 28 | } 29 | else if(front == rear){ 30 | printf("Deleted Element: %d\n",queue[front]); 31 | front = rear = -1; 32 | } 33 | else{ 34 | printf("Deleted Element: %d\n",queue[front]); 35 | front = (front+1)%n; 36 | } 37 | } 38 | 39 | ///display element of queue. 40 | void display(){ 41 | int i = front; 42 | if(front == -1 && rear == -1){ 43 | printf("Queue is Empty\n"); 44 | } 45 | else if(front == rear){ 46 | printf("Queue is- %d \n",queue[front]); 47 | } 48 | else{ 49 | printf("Queue is- "); 50 | while(i != rear){ 51 | printf("%d ",queue[i]); 52 | i = (i+1)%n; 53 | } 54 | printf("%d \n",queue[rear]); 55 | } 56 | } 57 | int main(){ 58 | display(); 59 | printf("\n"); 60 | enqueue(1); 61 | enqueue(2); 62 | display(); 63 | printf("\n"); 64 | enqueue(3); 65 | enqueue(4); 66 | enqueue(5); 67 | display(); 68 | printf("\n"); 69 | enqueue(6); 70 | dequeue(); 71 | display(); 72 | printf("\n"); 73 | dequeue(); 74 | display(); 75 | printf("\n"); 76 | enqueue(31); 77 | enqueue(41); 78 | enqueue(51); 79 | display(); 80 | printf("\n"); 81 | return 0; 82 | } 83 | -------------------------------------------------------------------------------- /Stack-Queue-Recursion/2-delete-top-element-into-stack.c: -------------------------------------------------------------------------------- 1 | /*Delete top element into stack*/ 2 | 3 | #include 4 | #include 5 | 6 | #define MAX 5 7 | int stack[MAX], top = -1; 8 | 9 | void push(int item) 10 | { 11 | if(top == MAX-1){ 12 | printf("\n OVERFLOW"); 13 | } 14 | else{ 15 | top = top + 1; 16 | stack[top] = item; 17 | printf("\n %d is pushed in stack.\n", item); 18 | } 19 | } 20 | 21 | void show() 22 | { 23 | if(top == -1){ 24 | printf("\n Stack is Empty!\n"); 25 | } 26 | else{ 27 | printf("\nStack:\n"); 28 | for(int i=top; i>=0; i--){ 29 | printf(" %d\n", stack[i]); 30 | } 31 | } 32 | } 33 | 34 | void pop() 35 | { 36 | if(top == -1){ 37 | printf("\n UNDERFLOW.\n"); 38 | } 39 | else{ 40 | printf("\n Pop item: %d\n", stack[top]); 41 | top--; 42 | } 43 | } 44 | 45 | int main() 46 | { 47 | push(5); 48 | show(); 49 | push(3); 50 | push(4); 51 | show(); 52 | pop(); 53 | show(); 54 | pop(); 55 | show(); 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /Stack-Queue-Recursion/3-evaluate-postfix-notation.c: -------------------------------------------------------------------------------- 1 | /*Find the value of a Arithmetic expression P written in Postfix notation. 2 | */ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #define MAX 100 9 | int stack[MAX], top = -1; 10 | 11 | 12 | void push(int n) 13 | { 14 | stack[++top] = n; 15 | } 16 | 17 | 18 | int pop() 19 | { 20 | return stack[top--]; 21 | } 22 | 23 | 24 | void evaluatePostfix(char exp[]) 25 | { 26 | char *e; 27 | int A, B, value, num; 28 | 29 | e=exp; 30 | while(*e != NULL){ 31 | if(isdigit(*e)){ 32 | num = *e - 48; 33 | push(num); 34 | } 35 | else{ 36 | A = pop(); 37 | B = pop(); 38 | 39 | switch(*e){ 40 | case '+': 41 | value = A + B; 42 | break; 43 | case '-': 44 | value = B - A; 45 | break; 46 | case '*': 47 | value = A * B; 48 | break; 49 | case '/': 50 | value = B / A; 51 | break; 52 | case '^': 53 | value = pow(B, A); 54 | break; 55 | } 56 | push(value); 57 | } 58 | e++; 59 | } 60 | printf("\n Result: %d\n", pop()); 61 | } 62 | 63 | 64 | int main() 65 | { 66 | char exp[MAX]; 67 | printf("Enter your postfix expression: "); 68 | scanf("%s", &exp); 69 | 70 | evaluatePostfix(exp); 71 | return 0; 72 | } 73 | -------------------------------------------------------------------------------- /Stack-Queue-Recursion/4-infix-to-postfix.c: -------------------------------------------------------------------------------- 1 | /*Transform an Infix expression into Postfix expression.*/ 2 | 3 | #include 4 | #include 5 | #define MAX 100 6 | char stack[MAX]; 7 | int top = -1; 8 | 9 | void push(char x) 10 | { 11 | stack[++top] = x; 12 | } 13 | 14 | char pop() 15 | { 16 | if(top == -1){ 17 | return -1; 18 | } 19 | else{ 20 | return stack[top--]; 21 | } 22 | } 23 | 24 | int priotity(char x) 25 | { 26 | if(x == '('){ 27 | return 0; 28 | } 29 | if(x == '+' || x == '-'){ 30 | return 1; 31 | } 32 | if(x == '*' || x == '/'){ 33 | return 2; 34 | } 35 | if(x == '^'){ 36 | return 3; 37 | } 38 | return 0; 39 | } 40 | 41 | int main() 42 | { 43 | char exp[MAX]; 44 | char *e, x; 45 | printf("Enter the expression: "); 46 | scanf("%s", &exp); 47 | printf("\n"); 48 | 49 | e = exp; 50 | 51 | while(*e != '\0'){ 52 | if(isalnum(*e)){ 53 | printf("%c ", *e); 54 | } 55 | else if(*e == '(' ){ 56 | push(*e); 57 | } 58 | else if(*e == ')' ){ 59 | while((x = pop()) != '('){ 60 | printf("%c ", x); 61 | } 62 | } 63 | else{ 64 | while(priotity(stack[top]) >= priotity(*e)){ 65 | printf("%c ", pop()); 66 | } 67 | push(*e); 68 | } 69 | e++; 70 | } 71 | while(top != -1){ 72 | printf("%c ", pop()); 73 | } 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /Stack-Queue-Recursion/5-evaluate-infix-notation.c: -------------------------------------------------------------------------------- 1 | /*Find the value of an Arithmetic expression I written in Infix notation.*/ 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #define MAX 50 8 | int stackNum[MAX], topNum = -1, topOp = -1; 9 | char stackOp[MAX]; 10 | 11 | void pushNum(int x) 12 | { 13 | stackNum[++topNum] = x; 14 | } 15 | 16 | void pushOp(char ch) 17 | { 18 | stackOp[++topOp] = ch; 19 | } 20 | 21 | int popNum() 22 | { 23 | return stackNum[topNum--]; 24 | } 25 | 26 | char popOp() 27 | { 28 | return stackOp[topOp--]; 29 | } 30 | 31 | int eval(int num[50], char op[50]) 32 | { 33 | int x, y; 34 | char ope; 35 | x = popNum(); 36 | y = popNum(); 37 | 38 | ope = popOp(); 39 | 40 | switch(ope) 41 | { 42 | case '+': 43 | return x+y; 44 | case '-': 45 | return y-x; 46 | case '*': 47 | return x*y; 48 | case '/': 49 | if(x == 0){ 50 | printf("\n Can not divided by 0.\n"); 51 | exit(0); 52 | } 53 | else{ 54 | return y/x; 55 | } 56 | } 57 | return 0; 58 | } 59 | 60 | int is_operator(char ch) 61 | { 62 | return (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '^'); 63 | } 64 | 65 | int precedence(char c) 66 | { 67 | switch(c) 68 | { 69 | case '+': 70 | return 1; 71 | case '-': 72 | return 1; 73 | case '*': 74 | return 2; 75 | case '/': 76 | return 2; 77 | case '^': 78 | return 3; 79 | } 80 | return -1; 81 | } 82 | 83 | 84 | int evaluateInfix(char exp[]) 85 | { 86 | int i, num, output, r; 87 | char c; 88 | for(i=0; exp[i] != NULL; i++){ 89 | c = exp[i]; 90 | if(isdigit(c) != 0){ 91 | num = 0; 92 | while(isdigit(c)){ 93 | num = num*10+(c-'0'); 94 | i++; 95 | if(i 5 | 6 | void decode(int *a, int n) 7 | { 8 | for(int i=0; i p) j--; 20 | 21 | if(i<=j){ 22 | temp = a[i]; 23 | a[i] = a[j]; 24 | a[j] = temp; 25 | i++; 26 | j--; 27 | } 28 | } 29 | if(fi){ 33 | quicksort(a, i, l); 34 | } 35 | } 36 | 37 | int main() 38 | { 39 | int a[] = {2, 5, 1, 6, 3, 4}; 40 | quicksort(a, 0, 5); 41 | decode(a, 6); 42 | } 43 | -------------------------------------------------------------------------------- /Stack-Queue-Recursion/7-factorial-using-recursive.c: -------------------------------------------------------------------------------- 1 | /*Calculate the factorial of a given number using recursive technique. 2 | */ 3 | 4 | #include 5 | 6 | int factorial(int n) 7 | { 8 | if(n == 0 || n == 1){ 9 | return 1; 10 | } 11 | else{ 12 | return factorial(n-1)*n; 13 | } 14 | } 15 | 16 | int main() 17 | { 18 | int num; 19 | printf("Enter a num: "); 20 | scanf("%d", &num); 21 | 22 | printf("\nFactorial of %d is: %d \n", num, factorial(num)); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Stack-Queue-Recursion/8-fibonacci-sequence-using-recursion.c: -------------------------------------------------------------------------------- 1 | /*Calculate the FN of a Fibonacci sequence using recursive technique.*/ 2 | 3 | #include 4 | 5 | int fibo(int n) 6 | { 7 | if(n==0){ 8 | return 0; 9 | } 10 | else if(n==1){ 11 | return 1; 12 | } 13 | else{ 14 | return fibo(n-1)+fibo(n-2); 15 | } 16 | } 17 | 18 | int main() 19 | { 20 | int num, i; 21 | printf("Enter a num: "); 22 | scanf("%d", &num); 23 | 24 | printf("\nFibonacci sequence:\n"); 25 | for(i=0; i 4 | 5 | void towers(int num, char from, char to, char aux) 6 | { 7 | if(num == 1){ 8 | printf("\n Move disk 1 from %c to %c.", from, to); 9 | return; 10 | } 11 | towers(num - 1, from, aux, to); 12 | printf("\n Move disk %d from %c to %c.", num, from, to); 13 | towers(num -1, aux, to, from); 14 | } 15 | 16 | int main() 17 | { 18 | int num; 19 | printf("Enter the number of disks: "); 20 | scanf("%d", &num); 21 | 22 | printf("\nThe sequence of moves involved in the tower of Hanoi are- \n"); 23 | towers(num, 'A', 'C', 'B'); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Tree/1-binary-search-tree.c: -------------------------------------------------------------------------------- 1 | /*Write a program to insert an element in a Binary search tree; if the element already inserted before then display the location.*/ 2 | 3 | #include 4 | 5 | int tree[100] = {0}; 6 | 7 | void insert(int data); 8 | void display(); 9 | 10 | int main() 11 | { 12 | insert(3); 13 | insert(4); 14 | insert(5); 15 | insert(6); 16 | insert(4); 17 | display(); 18 | return 0; 19 | } 20 | 21 | void insert(int data) 22 | { 23 | int i = 0, root = 0; 24 | while(1){ 25 | if(tree[i] == data){ 26 | printf("Already in position %d\n", i); 27 | break; 28 | } 29 | else if(tree[i] == 0){ 30 | tree[i] = data; 31 | break; 32 | } 33 | else if(data < tree[i]){ 34 | i = 2*i + 1; 35 | } 36 | else if(data > tree[i]){ 37 | i = 2*i + 2; 38 | } 39 | } 40 | } 41 | 42 | void display() 43 | { 44 | printf("Index -> Value\n"); 45 | for(int i=0; i<15; i++){ 46 | printf("%d -> %d\n", i, tree[i]); 47 | 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Tree/2-insertion-into-heap.c: -------------------------------------------------------------------------------- 1 | /*Write a program to insert an element into the heap*/ 2 | 3 | #include 4 | 5 | int heap[100], n; 6 | 7 | void insert(int num, int loc); 8 | void display(); 9 | 10 | int main() 11 | { 12 | int select, num; 13 | n = 0; 14 | while(1){ 15 | printf("\n Select a option\n"); 16 | printf("1.Insert the element \n"); 17 | printf("2.Display all elements \n"); 18 | printf("0.Quit \n"); 19 | printf("Enter your choice : "); 20 | scanf("%d", &select); 21 | 22 | switch(select){ 23 | case 1: 24 | printf("\nEnter the element to be inserted to the heap: "); 25 | scanf("%d", &num); 26 | insert(num, n); 27 | n++; 28 | break; 29 | 30 | case 2: 31 | display(); 32 | break; 33 | 34 | case 0: 35 | exit(0); 36 | break; 37 | 38 | default: 39 | printf("\nInvalid choice.\n"); 40 | } 41 | } 42 | 43 | } 44 | 45 | void insert(int num, int loc) 46 | { 47 | int parentNode; 48 | while(loc > 0){ 49 | parentNode = (loc - 1)/ 2; 50 | if(num <= heap[parentNode]){ 51 | heap[loc] = num; 52 | return; 53 | } 54 | heap[loc] = heap[parentNode]; 55 | loc = parentNode; 56 | } 57 | heap[0] = num; 58 | } 59 | 60 | void display() 61 | { 62 | int i; 63 | if(n == 0){ 64 | printf("\nHEAP IS EMPTY!\N"); 65 | return; 66 | } 67 | for(i=0; i 5 | 6 | int heap[100], n; 7 | 8 | void insertNum(int num, int loc); 9 | void deleteNum(int num); 10 | void display(); 11 | 12 | int main() 13 | { 14 | int select, num; 15 | n = 0; 16 | while(1){ 17 | printf("\n Select a option\n"); 18 | printf("1.Insert the element \n"); 19 | printf("2.Delete the element\n"); 20 | printf("3.Display all elements \n"); 21 | printf("0.Quit \n"); 22 | printf("Enter your choice : "); 23 | scanf("%d", &select); 24 | 25 | switch(select){ 26 | case 1: 27 | printf("\nEnter the element to be inserted to the heap: "); 28 | scanf("%d", &num); 29 | insertNum(num, n); 30 | n++; 31 | break; 32 | 33 | case 2: 34 | printf("Enter a number that your want to delete: "); 35 | scanf("%d", &num); 36 | deleteNum(num); 37 | break; 38 | 39 | case 3: 40 | display(); 41 | break; 42 | 43 | case 0: 44 | exit(0); 45 | break; 46 | 47 | default: 48 | printf("\nInvalid choice.\n"); 49 | } 50 | } 51 | 52 | } 53 | 54 | void insertNum(int num, int loc) 55 | { 56 | int parentNode; 57 | while(loc > 0){ 58 | parentNode = (loc - 1)/2; 59 | if(num <= heap[parentNode]){ 60 | heap[loc] = num; 61 | return; 62 | } 63 | heap[loc] = heap[parentNode]; 64 | loc = parentNode; 65 | } 66 | heap[0] = num; 67 | } 68 | 69 | void display() 70 | { 71 | int i; 72 | if(n == 0){ 73 | printf("\nHeap is empty\n"); 74 | return; 75 | } 76 | for(i=0; i heap[parentNode]){ 102 | insertNum(heap[i], i); 103 | return; 104 | } 105 | 106 | left = 2*i + 1; 107 | right = 2*i - 2; 108 | 109 | while(right < n){ 110 | if(heap[i] >= heap[left] && heap[i] >= heap[right]){ 111 | return; 112 | } 113 | if(heap[right] <= heap[left]){ 114 | temp = heap[i]; 115 | heap[i] = heap[left]; 116 | heap[left] = temp; 117 | i = left; 118 | } 119 | else{ 120 | temp = heap[i]; 121 | heap[i] = heap[right]; 122 | heap[right] = temp; 123 | i = right; 124 | } 125 | 126 | } 127 | if(left == n-1 && heap[i]){ 128 | temp = heap[i]; 129 | heap[i] = heap[left]; 130 | heap[left] = temp; 131 | } 132 | } 133 | -------------------------------------------------------------------------------- /Tree/4-preorder-tree-traversal.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct node{ 5 | int data; 6 | struct node* left; 7 | struct node* right; 8 | }; 9 | 10 | struct node* createNode(int value) 11 | { 12 | struct node* newNode = malloc(sizeof(struct node)); 13 | newNode->data = value; 14 | newNode->left = NULL; 15 | newNode->right = NULL; 16 | 17 | return newNode; 18 | }; 19 | 20 | struct node* insertLeft(struct node* root, int value) 21 | { 22 | root->left = createNode(value); 23 | return root->left; 24 | }; 25 | 26 | struct node* insertRight(struct node* root, int value) 27 | { 28 | root->right = createNode(value); 29 | return root->left; 30 | }; 31 | 32 | void preorderTraversal(struct node* root) 33 | { 34 | if(root == NULL){ 35 | return; 36 | } 37 | printf(" %d ->", root->data); 38 | preorderTraversal(root->left); 39 | preorderTraversal(root->right); 40 | } 41 | 42 | int main() 43 | { 44 | struct node* root = createNode(1); 45 | insertLeft(root, 2); 46 | insertRight(root, 3); 47 | 48 | insertLeft(root->left, 4); 49 | insertRight(root->left, 5); 50 | 51 | insertLeft(root->right, 6); 52 | insertRight(root->right, 7); 53 | 54 | printf("\nInorder Traversal: "); 55 | preorderTraversal(root); 56 | 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /Tree/5-inorder-tree-traversal.c: -------------------------------------------------------------------------------- 1 | /*Traverse the tree in inorder*/ 2 | 3 | #include 4 | #include 5 | 6 | struct node { 7 | int data; 8 | struct node *left; 9 | struct node *right; 10 | }; 11 | 12 | void inorderTraversal(struct node* root){ 13 | if(root == NULL){ 14 | return; 15 | } 16 | inorderTraversal(root->left); 17 | printf(" %d ->", root->data); 18 | inorderTraversal(root->right); 19 | } 20 | 21 | struct node* createNode(value) 22 | { 23 | struct node* newNode = malloc(sizeof(struct node)); 24 | newNode->data = value; 25 | newNode->left = NULL; 26 | newNode->right = NULL; 27 | 28 | return newNode; 29 | }; 30 | 31 | struct node* insertRight(struct node* root, int value){ 32 | root->right = createNode(value); 33 | return root->right; 34 | }; 35 | 36 | struct node* insertLeft(struct node* root, int value){ 37 | root->left = createNode(value); 38 | return root->left; 39 | }; 40 | 41 | 42 | int main() 43 | { 44 | struct node* root = createNode(1); 45 | insertLeft(root, 2); 46 | insertRight(root, 3); 47 | 48 | insertLeft(root->left, 4); 49 | insertRight(root->left, 5); 50 | 51 | insertLeft(root->right, 6); 52 | insertRight(root->right, 7); 53 | 54 | printf("\nInorder Traversal: "); 55 | inorderTraversal(root); 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /Tree/6-postorder-tree-traversal.c: -------------------------------------------------------------------------------- 1 | /*Traverse the tree in postorder.*/ 2 | 3 | #include 4 | #include 5 | 6 | struct node{ 7 | int data; 8 | struct node* left; 9 | struct node* right; 10 | }; 11 | 12 | void postorderTraversal(struct node* root) 13 | { 14 | if(root == NULL){ 15 | return; 16 | } 17 | postorderTraversal(root->left); 18 | postorderTraversal(root->right); 19 | printf(" %d ->", root->data); 20 | } 21 | 22 | struct node* createNode(int value) 23 | { 24 | struct node* newNode = malloc(sizeof(struct node)); 25 | newNode->data = value; 26 | newNode->left = NULL; 27 | newNode->right = NULL; 28 | 29 | return newNode; 30 | }; 31 | 32 | struct node* insertLeft(struct node* root, int value) 33 | { 34 | root->left = createNode(value); 35 | return root->left; 36 | }; 37 | 38 | struct node* insertRight(struct node* root, int value) 39 | { 40 | root->right = createNode(value); 41 | return root->right; 42 | }; 43 | 44 | 45 | int main() 46 | { 47 | struct node* root = createNode(1); 48 | insertLeft(root, 2); 49 | insertRight(root, 3); 50 | 51 | insertLeft(root->left, 4); 52 | insertRight(root->left, 5); 53 | 54 | insertLeft(root->right, 6); 55 | insertRight(root->right, 7); 56 | 57 | printf("\nInorder Traversal: "); 58 | postorderTraversal(root); 59 | 60 | return 0; 61 | } 62 | --------------------------------------------------------------------------------