Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Few DSA problems #441

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 162 additions & 0 deletions DSA/DSA-C/Queue-Implementing.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
#include <stdio.h>
#include <stdlib.h>

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");
}
79 changes: 79 additions & 0 deletions DSA/DSA-C/linked-list-code.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

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;
}
26 changes: 26 additions & 0 deletions DSA/DSA-C/tower-of-hanoi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <stdio.h>

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);
}
68 changes: 68 additions & 0 deletions DSA/DSA-C/tree.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include<stdio.h>
#include<stdlib.h>

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<numtabs; i++)
printf("\t");
}
//to display the tree
void printtree_rec(node *root, int level){
if (root == NULL){
printtabs(level);
printf("---<empty>---\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;
}