Search Program on this blog

Showing posts with label C program. Show all posts
Showing posts with label C program. Show all posts

Thursday, 27 August 2015

Linklist: Create node , Traverse List, Delete Node

/*   C Program to create linklist, Traversal and deletion

*/

#include<stdio.h>
#include<stdlib.h>
struct node{
  int data;
  struct node *next;
};

struct node * head=NULL;
void addNode(int x){
  struct node *newNode,*p;
  p=head;
  newNode=(struct node *)malloc(sizeof(struct node));
  newNode->data=x;
  newNode->next=NULL;
  if(head==NULL){    // if list is empty
    head=newNode;
    return;
  }
  else{
    while(p->next!=NULL){ //find the last node
      p=p->next;
    }
    p->next=newNode;   //add node at the end
  }
  return;
}

void deleteNode(int x){
  if(head==NULL){
     printf("The list is empty\n");
     return;
  }
  else{
    struct node *p=head,*q;
    q=p;
    while(p!=NULL){
      //if element is first
      if(head->data==x){
        if(head->next!=NULL){  //if there are more nodes
          q=p;
          p=p->next;
          free(q);
          head=p;
          return;
        }
        else if(head->next==NULL){  //if head is the only node
          free(p);
          head=NULL;
          return;
        }
      }
      else if(p->data==x){
        if(p->next!=NULL){    //if node is not the last node
          q->next=p->next;
          free(p);
          return;
        }
        else if(p->next==NULL){  //if node is the last node
          free(p);
          q->next=NULL;
          return;
        }
      }
      q=p;
      p=p->next;
    }
    printf("Element not found\n");
    return;   
  }
}
void traverse(){
  if(head==NULL){
    printf("The list is empty\n");
  }
  else{
    struct node *p=head;
    do{
      printf("%d ",p->data);
      p=p->next;
    }while(p!=NULL);
    printf("\n");
  }
}
    
int main(){
  int choice;
  int x;
  while(1){
    printf("1)Add node\n2)Delete node\n3)Traverse list\n4)exit\n");
    scanf("%d",&choice);
    switch(choice){
      case 1:printf("Enter element to add\n");
             scanf("%d",&x);
      addNode(x);
        break;
      case 2:printf("Enter the node to be deleted\n");
             scanf("%d",&x);
             deleteNode(x);
        break;
      case 3:traverse();
        break;
      case 4:exit(1);
      default:printf("Enter valid choice\n");
    }
  }
  return 0;
}

Wednesday, 26 August 2015

C program to implement josephus problem using circular linklist

/*
   C- Program to implement josephus problem
*/
#include<stdio.h>
#include<stdlib.h>
struct node{
        int data;
        struct node *next;
};
typedef struct node node;
int josephus(int, node *);
node *head=NULL,*h=NULL;
void main()
{
        int count=1,n,m;
        node *new;
        printf("enter the value of n:\n");   //total number of elements in list
        scanf("%d",&n);
        printf("enter the value of m:\n");  //m-th element will be removed every time
        scanf("%d",&m);
        while(count<=n)
        {
                new=(node*)malloc(sizeof(node));
                new->data=count++;
                if(head==NULL)
                {
                        head=new;
                        new->next=head;
                }
                else
                {
                        h=head;
                        while(h->next!=head)
                                h=h->next;
                        h->next=new;
                        new->next=head;
                }
        }
        printf("circular linklist is:\n");
        h=head;
        while(h->next!=head)
        {
         printf("%d->",h->data);
                h=h->next;
        }
        printf("%d",h->data);
        printf("\n");
        josephus(m,head);
}
int josephus(int m,node *front)
{
        node *f;
        int c=1;
        while(front->next!=front)
        {
                c=1;
                while(c!=m)
                {
                        f=front;
                        front=front->next;
                        c++;
                }
                f->next=front->next;
                printf("%d->",front->data); //sequence in which nodes getting deleted
                front=f->next;
        }
        printf("\n");
        printf("Winner is:%d\n",front->data);
        return;
}

Friday, 21 August 2015

C- Program for insertion sort

//C-Program for insertion sort


#include<stdio.h>
#include<time.h>
#define MAX_SIZE 10
void swap(int *a,int *b){
  int temp;
  temp=*a;
  *a=*b;
  *b=temp;
}
void sort(int *arr)
{
  int i,j;
  for(i=1;i<MAX_SIZE;i++){
    for(j=i;j>0;j--){
      if(arr[j]<arr[j-1]){
        swap(&arr[j],&arr[j-1]);
      }
    }
  }
}

int main()
{
  int i;
  int arr[MAX_SIZE]={10,20,30,40,50,60,65,70,76,80};
  for(i=0;i<MAX_SIZE;i++) //Array before sorting
    printf("%d ",arr[i]);
  sort(arr);
  printf("\n\n");
  for(i=0;i<MAX_SIZE;i++) //Array after sorting
    printf("%d ",arr[i]);
  return 0;
}

Summation of primes: program to print sum of all prime numbers upto 2000000

/*Program to print sum of all prime numbers upto 2000000*/

/*To check whether a number is prime or not it takes n^2 time by
  brute force but this program is sqrt(n) according to
  mathematical rules to check prime number efficiently.
*/

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define MAX 2000000
int main()
{
  int i,j;
  unsigned long int prod=0;
  int prime;
  for(i=2;i<=MAX;i++){
    prime=1;
    for(j=2;j<(int)(sqrt(i)+1);j++){
      if(i%j==0){
        prime=0;
        break;
      }
    }
    if(prime==1){
      prod=prod+i;
    }
  }
  printf("%ld",prod);
  return 0;
}

Create and Traverse Binary Search Tree without using recursion

/*
 Program to create and display a binary search tree
*/

#include<stdio.h>
#include<stdlib.h>
#define MAX_NODES 100

struct node{
  struct node *lChild;
  int data;
  struct node *rChild;
};
struct node *root=NULL;

/*Adding Element to a Binary Search Tree*/
void addElement(struct node *parent,int num)
{
  struct node *newNode;
  newNode=(struct node *)malloc(sizeof(struct node));
  newNode->lChild=NULL;
  newNode->rChild=NULL;
  newNode->data=num;
  if(root==NULL){
    root=newNode;
    return;
  }
  else{
    if(parent->data>num){  // go left
      if(parent->lChild==NULL){
        parent->lChild=newNode;
      }
      else{
        addElement(parent->lChild,num);
      }
    }
    else{                //go right
      if(parent->rChild==NULL){
        parent->rChild=newNode;
      }
      else{
        addElement(parent->rChild,num);
      }
    }
  }
  return;
}
/*Inorder Traversal with recursion*/
void inorder(struct node *parent)
{
  if(root==NULL){
    printf("No Element in Tree\n");
  }
  else{
    
    if(parent->lChild!=NULL){
      inorder(parent->lChild);
    }
    printf("%d ",parent->data);
    if(parent->rChild!=NULL){
      inorder(parent->rChild);
    }
  }
  return;
}

/*Binary Search Operation*/
struct node * search(struct node *parent,int num)
{
  if(root==NULL){
    printf("Empty Tree\n");
    return NULL;
  }
  else{
    if(parent->data==num){
      return parent;
    }
    else if(parent->data>num){
      if(parent->lChild!=NULL){
        search(parent->lChild,num);
      }
      else{
        printf("Element not found\n");
        return NULL;
      }
    }
    else{
      if(parent->rChild!=NULL){
        search(parent->rChild,num);
      }
      else{
        printf("Element not found\n");
        return NULL;
      }
    }
  }
}

/*Push pop for stack*/
void push(struct node **stack,int *top,struct node *child){
  if(*top<MAX_NODES-1){
    stack[++(*top)]=child;
  }
}

struct node *pop(struct node **stack,int *top){
  if(*top>-1){
    return stack[(*top)--];
  }
  else{
    printf("stack empty");
    return NULL;
  }
}


/*Pre order Traversal without using recursion*/
void preorder(){
  struct node * stack[MAX_NODES];
  struct node *p;
  int top=-1;
  if(root==NULL){
    printf("Tree is empty");
    return;
  }
  push(stack,&top,root);
  while(top!=-1){
    p=pop(stack,&top);
    printf("%d ",p->data);
    if(p->rChild!=NULL)
      push(stack,&top,p->rChild);
    if(p->lChild!=NULL)
      push(stack,&top,p->lChild);
  }
}

/*Post Order Traversal without using recursion*/
void postOrder(){
  struct node *stack[MAX_NODES];
  struct node *stack2[MAX_NODES];
  struct node*p;
  int top1=-1,top2=-1;
  if(root==NULL){
    printf("Tree is empty");
    return;
  }
  push(stack,&top1,root);
  while(top1!=-1){
    p=pop(stack,&top1);
    push(stack2,&top2,p);
    if(p->lChild!=NULL)
      push(stack,&top1,p->lChild);
    
    if(p->rChild!=NULL)
      push(stack,&top1,p->rChild);
  }
  while(top2!=-1){
    p=pop(stack2,&top2);
    printf("%d ",p->data);
  }
}


/*In order Traversal Without using Recursion*/
void inorderWithoutRecursion(){
  int arrIndex=-1;
  struct node *arr[MAX_NODES];
  struct node *stack[MAX_NODES];
  struct node *p;
  int top=-1,i;
  int flag=0;
  if(root==NULL){
    printf("Tree is empty\n");
    return;
  }
  push(stack,&top,root);
  while(top!=-1){
    p=pop(stack,&top);
    flag=0;
    for(i=0;i<=arrIndex;i++){
      if(arr[i]==p){
        printf("%d\n",p->data);
        flag=1;
        break;
      }
    }
    if(flag==0){
      if(p->rChild!=NULL)
        push(stack,&top,p->rChild);
      push(stack,&top,p);
      arr[++arrIndex]=p;
      if(p->lChild!=NULL)
        push(stack,&top,p->lChild);
    }
  }
}


/*Test Program to run the code*/
int main()
{
  struct node *p;
  int num,choice;
  while(1)
  {
    printf("\nEnter the choice\n");
    printf("1)Add element\n");
    printf("2)Inorder\n");
    printf("3)Search\n");
    printf("4)Preorder Traverse\n");
    printf("5)Post Order Traverse\n");
    printf("6)Inorder Traversal Without Recursion\n");
    printf("7)Exit\n");
    scanf("%d",&choice);
    switch(choice)
    {
      case 1:printf("Enter the number\n");
             scanf("%d",&num);
             addElement(root,num);
        break;
      case 2:inorder(root);
        break;
      case 3:printf("Enter element to search\n");
             scanf("%d",&num);
             p=search(root,num);
        break;
      case 4:preorder();
        break;
      
      case 5:postOrder();
        break;
      case 6:inorderWithoutRecursion();
        break;
      case 7:exit(0);
        break;
      default:printf("Enter valid choice\n");
    }
  }
  return 0;
}

Tuesday, 18 August 2015

Program to create and display a binary search tree

/*
 Program to create and display a binary search tree
*/

#include<stdio.h>
#include<stdlib.h>
struct node{
  struct node *lChild;
  int data;
  struct node *rChild;
};
struct node *root=NULL;

void addElement(struct node *parent,int num)
{
  struct node *newNode;
  newNode=(struct node *)malloc(sizeof(struct node));
  newNode->lChild=NULL;
  newNode->rChild=NULL;
  newNode->data=num;
  if(root==NULL){
    root=newNode;
    return;
  }
  else{
    if(parent->data>num){  // go left
      if(parent->lChild==NULL){
        parent->lChild=newNode;
      }
      else{
        addElement(parent->lChild,num);
      }
    }
    else{                //go right
      if(parent->rChild==NULL){
        parent->rChild=newNode;
      }
      else{
        addElement(parent->rChild,num);
      }
    }
  }
  return;
}

void display(struct node *parent)
{
  if(root==NULL){
    printf("No Element in Tree\n");
  }
  else{
    printf("%d ",parent->data);
    if(parent->lChild!=NULL){
      display(parent->lChild);
    }
    if(parent->rChild!=NULL){
      display(parent->rChild);
    }
  }
  return;
}
    

int main()
{
  int num,choice;
  while(1)
  {
    printf("\nEnter the choice\n1)Add element\n2)Display Tree\n3)Exit\n");
    scanf("%d",&choice);
    switch(choice)
    {
      case 1:printf("Enter the number\n");
             scanf("%d",&num);
             addElement(root,num);
        break;
      case 2:display(root);
        break;
      case 3:exit(0);
        break;
      default:printf("Enter valid choice\n");
    }
  }
  return 0;
}

Monday, 17 August 2015

C program for factorial of a number with recursion

#include<stdio.h>
int factorial(int num){
  if(num==1){  //Termination condition for recursion
    return 1;
  }
  else{ 
    return num*factorial(num-1);  //Recursive call to evaluate recursively factorial of n-1
  }
}
int main(){
  int number,result;
  printf("Enter the number to find factorial\n");
  scanf("%d",&number);  //user input for number
  result=factorial(number);  //call to factorial
  printf("Factorial of %d is %d\n",number,result);
  return 0;
}
ipput:5
output:120

C Program of Hello

#include<stdio.h>
int main()
{
     printf("Hello World");
     return 0;
}