From e49494ef230a5f035ccf4dcebbe17adbc2d1bddf Mon Sep 17 00:00:00 2001 From: Tushar Sublaik <99117094+tushar-sublaik@users.noreply.github.com> Date: Sat, 22 Oct 2022 05:46:53 +0530 Subject: [PATCH 1/4] Create tower-of-hanoi.c most common problem in dsa . --- DSA/DSA-C/tower-of-hanoi.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 DSA/DSA-C/tower-of-hanoi.c diff --git a/DSA/DSA-C/tower-of-hanoi.c b/DSA/DSA-C/tower-of-hanoi.c new file mode 100644 index 00000000..83f2c4ca --- /dev/null +++ b/DSA/DSA-C/tower-of-hanoi.c @@ -0,0 +1,26 @@ +#include + +void towers(int num, char fromtower, char totower, char auxtower); + +int main() +{ + int num; + + printf("\nEnter the number of disks : "); + scanf("%d", &num); + printf("\nThe sequence of moves involved in the Tower of Hanoi are :\n"); + towers(num, 'A', 'C', 'B'); + printf("\n\n*** The disks are successfully shifted from tower A to tower C ***\n\n"); + return 0; +} +void towers(int num, char fromtower, char totower, char auxtower) +{ + if (num == 1) + { + printf("\n Move disk 1 from tower %c to tower %c", fromtower, totower); + return; + } + towers(num - 1, fromtower, auxtower, totower); + printf("\n Move disk %d from tower %c to tower %c", num, fromtower, totower); + towers(num - 1, auxtower, totower, fromtower); +} From 2006fce2aed206f3bb92e66ef8beb46f07af5609 Mon Sep 17 00:00:00 2001 From: Tushar Sublaik <99117094+tushar-sublaik@users.noreply.github.com> Date: Sat, 22 Oct 2022 05:49:59 +0530 Subject: [PATCH 2/4] Create Queue-Implementing.c All the operations applicable on Queue are done in single code. --- DSA/DSA-C/Queue-Implementing.c | 162 +++++++++++++++++++++++++++++++++ 1 file changed, 162 insertions(+) create mode 100644 DSA/DSA-C/Queue-Implementing.c diff --git a/DSA/DSA-C/Queue-Implementing.c b/DSA/DSA-C/Queue-Implementing.c new file mode 100644 index 00000000..7432e3d1 --- /dev/null +++ b/DSA/DSA-C/Queue-Implementing.c @@ -0,0 +1,162 @@ +#include +#include + +struct node +{ + int info; + struct node *ptr; +}*front,*rear,*temp,*front1; + +int frontelement(); +void enq(int data); +void deq(); +void empty(); +void display(); +void create(); +void queuesize(); + +int count = 0; + +void main() +{ + int no, ch, e; + printf("\n*** Implementing queue using Linked List ***\n"); + printf("\n 1: Insert element to queue "); + printf("\n 2: Delete element from queue "); + printf("\n 3: To check front element "); + printf("\n 4: To check empty queue "); + printf("\n 5: To check queue size "); + printf("\n 6: Display all elements of queue "); + printf("\n 7: Quit "); + create(); + while (1) + { + printf("\n Enter choice : "); + scanf("%d", &ch); + switch (ch) + { + case 1: + printf("Enter data : "); + scanf("%d", &no); + enq(no); + break; + case 2: + deq(); + break; + case 3: + e = frontelement(); + if (e != 0) + printf("Front element : %d", e); + else + printf("\n No front element in Queue as queue is empty"); + break; + case 4: + empty(); + break; + case 5: + queuesize(); + break; + case 6: + display(); + break; + case 7: + exit(0); + default: + printf("Wrong choice, Please enter correct choice "); + break; + } + } +} + +void create() +{ + front = rear = NULL; +} + +void queuesize() +{ + printf("\n Queue size : %d", count); +} + +void enq(int data) +{ + if (rear == NULL) + { + rear = (struct node *)malloc(1*sizeof(struct node)); + rear->ptr = NULL; + rear->info = data; + front = rear; + } + else + { + temp=(struct node *)malloc(1*sizeof(struct node)); + rear->ptr = temp; + temp->info = data; + temp->ptr = NULL; + + rear = temp; + } + count++; +} + +void display() +{ + front1 = front; + printf("The queue is : "); + if ((front1 == NULL) && (rear == NULL)) + { + printf("Queue is empty"); + return; + } + while (front1 != rear) + { + printf("%d ", front1->info); + front1 = front1->ptr; + } + if (front1 == rear) + printf("%d", front1->info); + printf("\n"); +} + +void deq() +{ + front1 = front; + + if (front1 == NULL) + { + printf("\n Error: Trying to display elements from empty queue"); + return; + } + else + if (front1->ptr != NULL) + { + front1 = front1->ptr; + printf("\n Dequed value : %d", front->info); + free(front); + front = front1; + } + else + { + printf("\n Dequed value : %d", front->info); + free(front); + front = NULL; + rear = NULL; + } + count--; +} + +int frontelement() +{ + if ((front != NULL) && (rear != NULL)) + return(front->info); + else + return 0; +} + +void empty() +{ + if ((front == NULL) && (rear == NULL)) + printf("\n Queue empty"); + else + printf("Queue not empty"); +} From 18be2de8da5bd3c98a2bd0919dee9142b67faa22 Mon Sep 17 00:00:00 2001 From: Tushar Sublaik <99117094+tushar-sublaik@users.noreply.github.com> Date: Sat, 22 Oct 2022 05:52:27 +0530 Subject: [PATCH 3/4] Create tree.c Code for tree problem with input of value in the code itself. --- DSA/DSA-C/tree.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 DSA/DSA-C/tree.c diff --git a/DSA/DSA-C/tree.c b/DSA/DSA-C/tree.c new file mode 100644 index 00000000..54639f11 --- /dev/null +++ b/DSA/DSA-C/tree.c @@ -0,0 +1,68 @@ +#include +#include + +typedef struct node{ + int data; + struct node* left; + struct node* right; +}node; + +node* createNode(int data){ + node *n = (node *)malloc(sizeof(node)); + if(n != NULL){ + n->left = NULL; + n->right = NULL; + n->data = data; + } + return n; +} +//to create difference in levels of tree +void printtabs(int numtabs){ + for(int i=0; i---\n"); + return; + } + printtabs(level); + printf("value = %d\n", root->data); + printtabs(level); + printf("left\n"); + + printtree_rec(root->left, level+1); + printtabs(level); + printf("right\n"); + + printtree_rec(root->right, level+1); + printtabs(level); + printf("\n"); +} + +void printtree(node *root){ + printtree_rec(root, 0); +} + +int main(){ + node *n1 = createNode(1); + node *n2 = createNode(2); + node *n3 = createNode(3); + node *n4 = createNode(4); + node *n5 = createNode(5); + node *n6 = createNode(6); + node *n7 = createNode(7); + + n1->left = n2; + n1->right = n3; + n2->left = n4; + n2->right = n5; + n3->left = n6; + n3->right = n7; + + printf("\n*** Program to display the tree ***\n\n"); + printtree(n1); + return 0; +} From e718958f915694a8cfef88d02efc00bd727d5e67 Mon Sep 17 00:00:00 2001 From: Tushar Sublaik <99117094+tushar-sublaik@users.noreply.github.com> Date: Sat, 22 Oct 2022 05:57:58 +0530 Subject: [PATCH 4/4] Create linked-list-code.c special subtraction code for 2 no. in linked list which was quite difficult to execute in the beginning . --- DSA/DSA-C/linked-list-code.c | 79 ++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 DSA/DSA-C/linked-list-code.c diff --git a/DSA/DSA-C/linked-list-code.c b/DSA/DSA-C/linked-list-code.c new file mode 100644 index 00000000..0be00d30 --- /dev/null +++ b/DSA/DSA-C/linked-list-code.c @@ -0,0 +1,79 @@ +#include +#include +#include + +struct node +{ + int data; + struct node* link; +}; +struct node* list1=NULL; +struct node* list2=NULL; +void display(struct node* root,int n) +{ + struct node* temp; + temp=(struct node*)malloc(sizeof(struct node)); + temp->data=n; + temp->link=NULL; + if(root==NULL) + { + root=temp; + } + else + { + struct node* p; + p=root; + while(p->link!=NULL) + { + p=p->link; + } + p->link=temp; + } +} +int main() +{ + int x,y,value, s1=0,s2=0,i,j,l,d; + + printf("\nSize of 1st linked list: "); + scanf("%d",&x); + printf("Enter data of 1st linked list: "); + for(i=x-1;i>=0;i--) + { + scanf("%d",&value); + s1+=value*pow(10,i); + display(list1,value); + } + printf("\n"); + printf("Size of 2nd linked list: "); + scanf("%d",&y); + printf("Enter data of 2nd linked list: "); + for(i=y-1;i>=0;i--) + { + scanf("%d",&value); + s2+=value*pow(10,i); + display(list2,value); + } + printf("\n"); + if(s1 > s2) + { + d=s1-s2; + } + else + { + d=s2-s1; + } + l=(d==0)?1:log10(d)+1; + int a[l]; + for(j=l;j>0;j--) + { + a[j]=d%10; + d=d/10; + } + printf("Difference : "); + for(j=1;j<=l;j++) + { + printf("%d ",a[j]); + } + printf("\n\n"); + return 0; +}