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

add some usefull c DS and Algorithm #365

Open
wants to merge 1 commit into
base: master
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
79 changes: 79 additions & 0 deletions c-programs-main/FullTreeCheck.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

typedef struct Node{
int data;
struct Node *left,*right;
}Node;

Node* createNode(int data){
Node* temp = (Node*)malloc(sizeof(Node));
temp->data = data;
temp->left = NULL;
temp->right = NULL;
return temp;
}

void linker(Node* left,Node* parent,Node* right){
parent->left = left;
parent->right = right;
}

bool preOrder(Node* root){
if(!root)
return false;
printf("%d\n",root->data);
preOrder(root->left);
preOrder(root->right);
return true;
}

bool inOrder(Node* root){
if(!root)
return false;
printf("%d\n",root->data);
inOrder(root->left);
inOrder(root->right);
return true;
}

bool postOrder(Node* root){
if(!root)
return false;
printf("%d\n",root->data);
postOrder(root->left);
postOrder(root->right);
return true;
}

bool checkFullTree(Node* root){
if(root==NULL)
return true;
if(root->left==NULL && root->right==NULL)
return true;
if(root->left && root->right)
return checkFullTree(root->left) && checkFullTree(root->right);
return false;
}

int main(){
Node* n1 = createNode(12);
Node* n2 = createNode(11);
Node* n3 = createNode(14);
Node* n4 = createNode(18);
Node* n5 = createNode(120);
Node* n6 = createNode(121);
Node* n7 = createNode(124);
Node* n8 = createNode(22);
Node* n9 = createNode(28);
linker(n2,n1,n3);
linker(n4,n2,n5);
linker(n6,n3,n7);
linker(n9,n6,n8);
if(checkFullTree(n1))
printf("It is a full tree\n");
else
printf("Not a full tree\n");
return(0);
}
8 changes: 8 additions & 0 deletions c-programs-main/c-examples/example-1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// C "Hello, World!" Program

#include <stdio.h>

int main(){
puts("Hello World!!");
return(0);
}
33 changes: 33 additions & 0 deletions c-programs-main/c-examples/example-10.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// complex number

#include <stdio.h>

typedef struct {
double real;
double imag;
}complex;

complex add(complex a,complex b){
complex c;
c.real = a.real+b.real;
c.imag = a.imag+b.imag;
return c;
}

void print(complex a){
printf("%.2lf + i%.2lf\n",a.real,a.imag);
}

void input(complex *ptr){
printf("Enter the number(use ',') : ");
scanf("%lf,%lf",&ptr->real,&ptr->imag);
}

int main(){
complex num1,num2;
input(&num1);
input(&num2);
complex sum = add(num1,num2);
print(sum);
return(0);
}
11 changes: 11 additions & 0 deletions c-programs-main/c-examples/example-2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// C Program to Print an Integer (Entered by the User)

#include <stdio.h>

int main(){
int integer;
printf("Enter the number : ");
scanf("%d",&integer);
printf("You entered : %d\n",integer);
return(0);
}
12 changes: 12 additions & 0 deletions c-programs-main/c-examples/example-3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// C Program to Add Two Integers

#include <stdio.h>

int main(){
int num1,num2,sum;
printf("Enter the numbers : ");
scanf("%d,%d",&num1,&num2);
sum = num1+num2;
printf("%d + %d = %d\n",num1,num2,sum);
return(0);
}
12 changes: 12 additions & 0 deletions c-programs-main/c-examples/example-4.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// C Program to Multiply Two Floating-Point Numbers

#include <stdio.h>

int main(){
float num1,num2,product;
printf("Enter the numbers(use ',') : ");
scanf("%f,%f",&num1,&num2);
product = num1*num2;
printf("%.2f x %.2f = %.2f\n",num1,num2,product);
return(0);
}
11 changes: 11 additions & 0 deletions c-programs-main/c-examples/example-5.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// C Program to Find ASCII Value of a Character

#include <stdio.h>

int main(){
char symbol;
printf("Enter the symbol : ");
scanf("%c",&symbol);
printf("ASCII value of %c is %d\n",symbol,symbol);
return(0);
}
19 changes: 19 additions & 0 deletions c-programs-main/c-examples/example-6.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// C Program to Compute Quotient and Remainder

#include <stdio.h>
int main() {
int dividend, divisor, quotient, remainder;
printf("Enter dividend: ");
scanf("%d", &dividend);
printf("Enter divisor: ");
scanf("%d", &divisor);

quotient = dividend / divisor;

remainder = dividend % divisor;

printf("Quotient = %d\n", quotient);
printf("Remainder = %d\n", remainder);
return (0);
}

11 changes: 11 additions & 0 deletions c-programs-main/c-examples/example-7.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// C Program to Find the Size of int, float, double and char

#include <stdio.h>

int main(){
printf("Size of Int is %d bytes\n",sizeof(int));
printf("Size of float is %d bytes\n",sizeof(float));
printf("Size of double is %d bytes\n",sizeof(double));
printf("Size of char is %d byte\n",sizeof(char));
return(0);
}
40 changes: 40 additions & 0 deletions c-programs-main/c-examples/example-8.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// various function/ways to to Swap Two Numbers

#include <stdio.h>

void swap1(){
int a,b,c;
printf("Enter the numbers(use ',') : ");
scanf("%d,%d",&a,&b);
c = a;
a = b;
b = c;
printf("a = %d\nb = %d\n",a,b);
}

void swap2(){
int a,b;
printf("Enter the numbers(use ',') : ");
scanf("%d,%d",&a,&b);
a = a+b;
b = a-b;
a = a-b;
printf("a = %d\nb = %d\n",a,b);
}

void swap3(){
int a,b;
printf("Enter the numbers(use ',') : ");
scanf("%d,%d",&a,&b);
a = a^b;
b = a^b;
a = a^b;
printf("a = %d\nb = %d\n",a,b);
}

int main(){
swap1();
swap2();
swap3();
return(0);
}
14 changes: 14 additions & 0 deletions c-programs-main/c-examples/example-9.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// C Program to Check Whether a Number is Even or Odd

#include <stdio.h>

int main(){
int number;
printf("Enter the number : ");
scanf("%d",&number);
if(number%2==0)
printf("%d is a even number\n",number);
else
printf("%d is not a even number\n",number);
return(0);
}
32 changes: 32 additions & 0 deletions c-programs-main/games/number_guess.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <stdio.h>

int main(){
int a = (int)__TIME__[7]-45;
int b = (int)__TIME__[6]-45;
for(int i = 1;i<b;++i){
a = 3*a+1;
}
printf("%d\n",a);
while(786){
int guess;
printf("Enter your guess : ");
scanf("%d",&guess);
if(guess==a){
printf(" _________________ \n");
printf("< WOW you find it >\n");
printf(" ----------------- \n");
printf(" \\ ^__^ \n");
printf(" \\ (oo)\\______\n");
printf(" (__)\\ )\\/\n");
printf(" ||----w |\n");
printf(" || ||\n");

break;
}
else if(guess<a)
printf("your guess is small.\n");
else
printf("your guess is large.\n");
}
return(0);
}
33 changes: 33 additions & 0 deletions c-programs-main/leap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <stdio.h>
#include <stdbool.h>

#define MIN 1

bool checkLeap(int year){
if(year%400==0)
return true;
else if(year%100==0)
return false;
else if(year%4==0)
return true;
else
return false;
}

int main(){
int year;
bool flag;
printf("Enter the year to check : ");
scanf("%d",&year);
if(year<MIN)
printf("%d is not a valid year\n");
else{
flag = checkLeap(year);
if(flag)
printf("%d is a leap year\n",year);
else
printf("%d is not a leap year\n",year);
}

return(0);
}
37 changes: 37 additions & 0 deletions c-programs-main/link_list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <stdio.h>

struct node{
int data;
struct node* next;
};

typedef struct node node;

int print(node*);



int main(){
node n1,n2,n3,n4;
node* head;
n1.data = 12;
n2.data = 13;
n3.data = 15;
n4.data = 19;
head = &n1;
n1.next = &n2;
n2.next = &n3;
n3.next = &n4;
n4.next = NULL;
print(head);
return(0);
}

int print(node* head){
while(head!=NULL){
printf("%d\n",head->data);
head = (head->next);
}

return(0);
}
Loading