├── Image.png ├── readme.md ├── Queue With Static Array.c ├── LICENSE ├── Stack With Single Linked List.c ├── Stack With Static Array.c ├── Double Linked List.c └── Single Linked Lists.c /Image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Vreij-Lal/DSA-IN-C/HEAD/Image.png -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # DSA IN C 2 | banner image 3 | This repository contains a variety of data structures and algorithms implemented in the C programming language. It serves as a resource for students, educators, and anyone interested in learning more about the implementation of fundamental algorithms and data structures in C. 4 | 5 | ## Stay Tuned! 6 | 🎉 Exciting Updates Ahead! 🎉 7 | 8 | I'm actively working on adding new data structures and algorithms every day to this repository. If you find this project helpful or interesting, make sure to star this repo ⭐ to stay updated with the latest additions and improvements. Your support keeps me motivated and helps the community grow! Let's learn and build together! 💪 9 | 10 | ## Author 11 | [@Vreij-Lal](https://github.com/Vreij-Lal) 12 | 13 | ## License 14 | [MIT License](LICENSE) 15 | -------------------------------------------------------------------------------- /Queue With Static Array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define CAPACITY = 5 5 | 6 | int queue[CAPACITY]; 7 | 8 | int front = 0; 9 | int rear = 0; 10 | 11 | void insert(); 12 | void pop(); 13 | void traverse(); 14 | 15 | void main(){ 16 | 17 | } 18 | 19 | void insert(){ 20 | if(CAPACITY == rear){ 21 | printf("Queue is full"); 22 | } 23 | else{ 24 | int ele, 25 | printf("insert element:"); 26 | scanf("%d", &ele); 27 | queue[rear] = ele; 28 | rear++; 29 | } 30 | } 31 | 32 | void traverse(){ 33 | if(front == rear){ 34 | printf("no items to print"); 35 | } 36 | else{ 37 | for(int i = front; i < rear; i++){ 38 | printf("%d", queue[i]); 39 | } 40 | } 41 | } 42 | 43 | void pop(){ 44 | if(front == rear){ 45 | printf("no elements to print"); 46 | } 47 | else{ 48 | for(int i = 0; i < CAPACITY; i++){ 49 | queue[i] = queue[i + 1]; 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Vreij Lal 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Stack With Single Linked List.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | //function prototypes 5 | void push(); 6 | void pop(); 7 | void traverse(); 8 | 9 | //linked list definition 10 | struct node { 11 | int data; 12 | struct node* link; 13 | }; 14 | 15 | //global linked list root variable 16 | struct node* top = NULL; 17 | 18 | void main(){ 19 | 20 | } 21 | 22 | void push(){ 23 | struct node* temp; 24 | temp = (struct node*)malloc(sizeof(struct node)); 25 | printf("Enter value:"); 26 | scanf("%d", &temp -> data); 27 | temp -> link = top; 28 | top = temp; 29 | } 30 | 31 | void pop(){ 32 | struct node* temp; 33 | if(top == NULL){ 34 | printf("no elements in the stack\n"); 35 | } 36 | else{ 37 | temp = top; 38 | top = top -> link; 39 | temp -> link = NULL; 40 | free(temp); 41 | } 42 | } 43 | 44 | void traverse(){ 45 | struct node *temp; 46 | if(top == NULL){ 47 | printf("no elements to traverse"); 48 | } 49 | else{ 50 | temp = top; 51 | while (temp != NULL){ 52 | printf("%d", temp -> data); 53 | temp = temp -> link; 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /Stack With Static Array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define CAPACITY 5 5 | int stack[CAPACITY]; 6 | int top=-1; 7 | 8 | //function Prototypes 9 | void push(int); 10 | int isFull(); 11 | void traverse(); 12 | int isEmpty(); 13 | void peek(); 14 | void pop(); 15 | 16 | void main() 17 | { 18 | int choice; 19 | while(1){ 20 | 21 | printf("1_Push\n"); 22 | printf("2_Traverse\n"); 23 | printf("3_Pop\n"); 24 | printf("4_Peek\n"); 25 | scanf("%d", &choice); 26 | system("cls"); 27 | if(choice == 1){ 28 | system("cls"); 29 | int value; 30 | printf("enter Value: "); 31 | scanf("%d", &value); 32 | push(value); 33 | } 34 | if(choice == 2){ 35 | system("cls"); 36 | traverse(); 37 | } 38 | if(choice == 3){ 39 | system("cls"); 40 | pop(); 41 | } 42 | if(choice == 4){ 43 | system("cls"); 44 | peek(); 45 | } 46 | if(choice != 1 && choice != 2 && choice != 3 && choice != 4){ 47 | printf("invalid choice\n"); 48 | } 49 | system("cls"); 50 | } 51 | 52 | } 53 | 54 | //to push a value into the stack. 55 | void push(int value){ 56 | if (isFull()){ 57 | printf("stack overflow\n"); 58 | } 59 | else{ 60 | top++; 61 | stack[top] = value; 62 | } 63 | } 64 | 65 | //to check if stack is full or not. 66 | int isFull(){ 67 | if(top == (CAPACITY - 1)){ 68 | return 1; 69 | } 70 | else{ 71 | return 0; 72 | } 73 | } 74 | 75 | //to check if stack is empty or not. 76 | int isEmpty(){ 77 | if(top == -1){ 78 | return 1; 79 | } 80 | else{ 81 | return 0; 82 | } 83 | } 84 | 85 | //to display all the values in the stack 86 | void traverse(){ 87 | if(isEmpty()){ 88 | printf("stack is empty\n"); 89 | } 90 | else{ 91 | for(int i = 0; i <= top; i++){ 92 | printf("%d\n", stack[i]); 93 | } 94 | } 95 | } 96 | 97 | //to display the top element 98 | void peek(){ 99 | if(isEmpty()){ 100 | printf("stack is empty\n"); 101 | } 102 | else{ 103 | printf("%d\n", stack[top]); 104 | } 105 | } 106 | 107 | //to remove the top element 108 | void pop(){ 109 | if(isEmpty()){ 110 | printf("stack underflow\n"); 111 | } 112 | else{ 113 | top--; 114 | printf("element removed\n"); 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /Double Linked List.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 *root = NULL; 11 | 12 | void append(); 13 | int length(); 14 | 15 | void main(){ 16 | 17 | } 18 | 19 | void append(){ 20 | struct node *temp; 21 | temp = (struct node *)malloc(sizeof(struct node)); 22 | printf("Enter value"); 23 | scanf("%d", &temp -> data); 24 | temp->left = NULL; 25 | temp->right = NULL; 26 | if(root == NULL){ 27 | root = temp; 28 | } 29 | else{ 30 | struct node *p; 31 | p = root; 32 | while(p->right != NULL){ 33 | p = p -> right; 34 | } 35 | p -> right = temp; 36 | temp -> left = p; 37 | } 38 | } 39 | 40 | int length(){ 41 | int count = 0; 42 | struct node *temp; 43 | temp = root; 44 | while(temp->right != NULL){ 45 | temp = temp->right; 46 | count++; 47 | } 48 | return count; 49 | } 50 | 51 | void display(){ 52 | struct node *temp; 53 | temp = root; 54 | if(temp == NULL){ 55 | printf("Stack is empty"); 56 | } 57 | else{ 58 | while(temp -> right != NULL){ 59 | printf("%d", temp -> data); 60 | temp = temp -> right; 61 | } 62 | } 63 | } 64 | 65 | void appendAtStart(){ 66 | struct node *temp; 67 | temp = (struct node *)malloc(sizeof(struct node)); 68 | printf("enter value:"); 69 | scanf("%d", &temp -> data); 70 | temp -> right = NULL; 71 | temp -> left == NULL; 72 | if(root == NULL){ 73 | root = temp; 74 | } 75 | else{ 76 | temp -> right = root; 77 | root -> left = temp; 78 | root = temp; 79 | } 80 | } 81 | 82 | void appendAfter(){ 83 | int len, position, i = 1; 84 | struct node *temp, *p; 85 | len = length(); 86 | printf("enter number of postition to add after:"); 87 | scanf("%d", &position); 88 | if(position > len){ 89 | printf("invalid position"); 90 | } 91 | else{ 92 | temp = (struct node *)malloc(sizeof(struct node)); 93 | printf("Enter value"); 94 | scanf("%d", &temp->data); 95 | temp -> left = NULL; 96 | temp -> right = NULL; 97 | p = root; 98 | while(i < position){ 99 | p = p -> right; 100 | i++; 101 | } 102 | temp -> right = p -> right; 103 | p -> right -> left = temp; 104 | temp -> left = p; 105 | p -> right = temp; 106 | } 107 | } -------------------------------------------------------------------------------- /Single Linked Lists.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void append(); 5 | int length(); 6 | void addAtStart(); 7 | void addAfter(); 8 | void del(); 9 | void reverse(); 10 | 11 | //linked list definition 12 | struct node { 13 | int data; 14 | struct node* link; 15 | }; 16 | 17 | //global linked list root variable 18 | struct node* root = NULL; 19 | 20 | 21 | //main function 22 | void main(){ 23 | 24 | int choice; 25 | 26 | while (1){ 27 | 28 | printf("single linked list Operations\n"); 29 | printf("1.Append\n"); 30 | printf("2.AddAtStart\n"); 31 | printf("3.AddAfter\n"); 32 | printf("4.length\n"); 33 | printf("5.Display\n"); 34 | printf("6.Delete\n"); 35 | printf("7.Reverse"); 36 | printf("8.Quit\n"); 37 | scanf("%d", &choice); 38 | 39 | switch(choice){ 40 | case 1: 41 | system("cls"); 42 | append(); 43 | break; 44 | case 2: 45 | system("cls"); 46 | addAtStart(); 47 | break; 48 | case 3: 49 | system("cls"); 50 | addAfter(); 51 | break; 52 | case 4: 53 | system("cls"); 54 | printf("%d\n", length()); 55 | break; 56 | case 5: 57 | system("cls"); 58 | display(); 59 | break; 60 | case 6: 61 | system("cls"); 62 | del(); 63 | break; 64 | case 7: 65 | system("cls"); 66 | reverse(); 67 | break; 68 | case 8: 69 | exit(1); 70 | default: 71 | system("cls"); 72 | printf("invalid choice"); 73 | break; 74 | } 75 | 76 | } 77 | } 78 | 79 | void append(){ 80 | struct node *temp, *p; 81 | temp = (struct node*)malloc(sizeof(struct node)); 82 | printf("enter element number: "); 83 | scanf("%d", &temp -> data); 84 | temp -> link = NULL; 85 | if(root == NULL){ 86 | root = temp; 87 | } 88 | else{ 89 | p = root; 90 | while(p -> link != NULL){ 91 | p = p -> link; 92 | } 93 | p -> link = temp; 94 | } 95 | 96 | } 97 | 98 | int length(){ 99 | int i = 0; 100 | struct node *p; 101 | p = root; 102 | while(p != NULL){ 103 | p = p -> link; 104 | i++; 105 | } 106 | return i; 107 | } 108 | 109 | void addAtStart(){ 110 | struct node * temp; 111 | temp = (struct node*)malloc(sizeof(struct node)); 112 | printf("enter value:"); 113 | scanf("%d", &temp -> data); 114 | temp -> link = NULL; 115 | if(root == NULL){ 116 | root = temp; 117 | } 118 | else{ 119 | temp -> link = root; 120 | root = temp; 121 | } 122 | } 123 | 124 | void addAfter(){ 125 | struct node *temp, *p; 126 | int loc, i = 1; 127 | printf("enter location:"); 128 | scanf("%d", &loc); 129 | if(loc > length()){ 130 | printf("invalid location\n"); 131 | } 132 | else{ 133 | p = root; 134 | while(i < loc){ 135 | p = p -> link; 136 | i++; 137 | } 138 | temp = (struct node*)malloc(sizeof(struct node)); 139 | printf("enter value: "); 140 | scanf("%d", &temp -> data); 141 | temp -> link = NULL; 142 | temp -> link = p -> link; 143 | p -> link = temp; 144 | } 145 | 146 | } 147 | 148 | void display(){ 149 | if(root == NULL){ 150 | printf("list is empty\n"); 151 | } 152 | else{ 153 | struct node *p; 154 | p = root; 155 | while(p != NULL){ 156 | printf("%d\n", p -> data); 157 | p = p -> link; 158 | } 159 | } 160 | 161 | } 162 | 163 | void del(){ 164 | int loc; 165 | printf("enter location: "); 166 | scanf("%d", &loc); 167 | 168 | if(loc > length()){ 169 | printf("invalid location\n"); 170 | } 171 | 172 | if(loc == 1){ 173 | struct node *p; 174 | p = root; 175 | root = p -> link; 176 | p -> link = NULL; 177 | free(p); 178 | } 179 | else{ 180 | struct node *temp, *q; 181 | int i = 1; 182 | temp = root; 183 | while(i < loc - 1){ 184 | temp = temp -> link; 185 | i++; 186 | } 187 | q = temp -> link; 188 | temp -> link = q -> link; 189 | q -> link = NULL; 190 | free(q); 191 | 192 | } 193 | } 194 | 195 | void reverse(){ 196 | struct node *p, *q; 197 | int i, j, len, temp; 198 | len = length(); 199 | i = 0; 200 | j - len - 1; 201 | while(i < j){ 202 | int k = 0; 203 | while(k < j){ 204 | q = q -> link; 205 | k++; 206 | } 207 | temp = p -> data; 208 | p -> data = q -> data; 209 | q -> data = temp; 210 | i++; 211 | j--; 212 | p = p -> link; 213 | q = root; 214 | } 215 | } 216 | --------------------------------------------------------------------------------