Sunday, 19 November 2017

Selection Sort in C

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,i,j,t,min;
    printf("Enter the size of array\n");
    scanf("%d",&n);
    int A[n];
    printf("Enter the elements of array\n");
    for(i=0;i<n;i++)
        scanf("%d",&A[i]);
    printf("elements of array is\n");
    for(i=0;i<n;i++)
        printf("%d  ",A[i]);
    for(i=0;i<n-1;i++)
    {
        min=i;
        for(j=i+1;j<n;j++)
        {
            if(A[j]<A[min])
                min=j;
        }
        t=A[i];
        A[i]=A[min];
        A[min]=t;
    }
     printf("elements of sorted array is\n");
    for(i=0;i<n;i++)
        printf("%d  ",A[i]);
    return 0;
}

Insertion sort in C

#include <stdio.h>
#include <stdlib.h>

int main()
{
     int *A;
    int i,n,j;
    int val;
    printf("Enter the size of array\n");
    scanf("%d",&n);
    A = (int*) malloc(sizeof(int)*n);
    printf("Enter the elements in the array\n");
    for(i=0;i<n;i++)
        scanf("%d",&A[i]);
    printf("your elements are\n");
     for(i=0;i<n;i++)
        printf("%d ",A[i]);
    for(i=1;i<n;i++)
    {
        val=A[i];
        j=i;
        while(j>0&&val<A[j-1])
        {
            A[j]=A[j-1];
            j=j-1;
        }
        A[j]=val;
    }
     printf("your sorted elements are\n");
     for(i=0;i<n;i++)
        printf("%d ",A[i]);

}

Heap sort in C

#include <stdio.h>
#include <stdlib.h>
int heapsize;
void Heapify(int *A,int i)
{
    int l,r;
    l=2*i+1;
    r=2*i+2;
    int max;
    if(l<=heapsize&&A[l]>A[i])
        max = l;
    else
        max=i;
    if(r<=heapsize&&A[r]>A[max])
        max = r;
    if(max!=i)
    {
        int temp;
        temp=A[i];
        A[i]=A[max];
        A[max]=temp;
        Heapify(A,max);
    }
}
void Buildheap(int *A)
{
    int i;
    heapsize = 10-1;
    for(i=(10-1)/2;i>=0;i--)
    {
        Heapify(A,i);
    }
}
void Heapsort(int *A)
{
    Buildheap(A);
    int i;
    for(i=9;i>0;i--)
    {
        int swap;
        swap = A[heapsize];
        A[heapsize]=A[0];
        A[0]=swap;
        heapsize--;
        Heapify(A,0);
    }

}
int main()
{
   int A[10] = {1,4,2,6,7,5,8,2,555,3};
   Heapsort(A);
   int i;
   for(i = 0; i < 10; i++)
   printf("%d ",A[i]);
   return 0;
}

Saturday, 18 November 2017

Program to Prime numbers between a given range of numbers in JAVA using square root function

import java.util.Scanner;

public class test {

    public static void main(String args[]){
        Scanner s = new Scanner(System.in);
        System.out.println("Enter the lower range");
        int m = s.nextInt();
        System.out.println("Enter the higher range");
        int n= s.nextInt();
        int t=0,i;
        for (i=m;i<=n;i++)
        {
            for(int j=2;j<=Math.sqrt(i);j++)
            {
                if(i%j==0) {
                    t=0;   
                    break;
                }
                else                    
                t=1;
            }
            if(t==1)
                System.out.println(i);

        }
        
    }

}

Friday, 17 November 2017

Traversal in Binary search tree using C

#include <stdio.h>
#include <stdlib.h>
typedef struct bst
{
    int data;
    struct abc *left;
    struct abc *right;
}node;
node *tree=NULL;
node *create(node *tree,int val)
{
    node *ptr,*parentptr,*nodeptr;
    ptr=(node *)malloc(sizeof(node));
    ptr->data=val;
    ptr->left=NULL;
    ptr->right=NULL;
    if(tree==NULL)
    {
        tree=ptr;
        tree->left=NULL;
        tree->right=NULL;
    }
    else
    {
        parentptr=NULL;
        nodeptr=tree;
        while(nodeptr!=NULL)
        {
            parentptr=nodeptr;
            if(val<nodeptr->data)
                nodeptr=nodeptr->left;
            else
                nodeptr=nodeptr->right;
        }
        if(val<parentptr->data)
            parentptr->left=ptr;
        else
            parentptr->right=ptr;
    }
    return tree;
}


node *in_order(node *tree)
{
    if(tree!=NULL)
    {
        in_order(tree->left);
        printf("%d ",tree->data);
        in_order(tree->right);
    }
    return tree;
}
node *pre_order(node *tree)
{
    if(tree!=NULL)
    {
        printf("%d ",tree->data);
        pre_order(tree->left);
        pre_order(tree->right);
    }
    return tree;
}
node *post_order(node *tree)
{
    if(tree!=NULL)
    {
        post_order(tree->left);
        post_order(tree->right);
        printf("%d ",tree->data);
    }
    return tree;
}

int main()
{
    int val,opt;
    do
    {
        printf("\nMenu\n1--Insert\n2--In Order\n3--Pre Order\n4--Post Order\nEnter your choice\n");
        scanf("%d",&opt);
        switch(opt)
        {
            case 1:printf("Enter the value to be inserted\t ");
                    scanf("%d",&val);
                    tree=create(tree,val);
                    break;
            case 2:tree=in_order(tree);
                    break;
            case 3:tree=pre_order(tree);
                    break;
            case 4:tree=post_order(tree);
                    break;
        }
    }while(opt!=5);
    return 0;
}

Binary search in C

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int *A;
    int i,n,search;
    printf("Enter the size of array\n");
    scanf("%d",&n);
    A = (int*) malloc(sizeof(int)*n);
    printf("Enter the elements in the array\n");
    for(i=0;i<n;i++)
        scanf("%d",&A[i]);
    printf("your elements are\n");
     for(i=0;i<n;i++)
        printf("%d ",A[i]);
    printf("\nEnter the value to search\n");
    scanf("%d",&search);
    binsearch(A,n,search);
    free(A);
}
 void binsearch(int *A,int n,int search)
{
    int i=0,j=n-1,num;
    while(i<=j)
    {
        num =(i+j)/2;
        if(A[num]==search)
        {
            printf("Number is present at position %d\n",num+1);
            return;
        }
        else if(A[num]<search)
            i = num+1;
        else
            j = num-1;
    }
    printf("Search is unsuccessful because number is not in the list\n ");
}

Thursday, 16 November 2017

Double ended queue using linklist

#include<stdio.h>
#include<stdlib.h>
struct node
{
 int info;
 struct node *next;
}*front,*temp,*rear,*ptr;
void insFront()
{
 temp=(struct node*)malloc(sizeof(struct node));
 printf("\nEnter element: ");
 scanf("%d",&temp->info);
 if(front==NULL && rear==NULL)
 {
  front=rear=temp;
  temp->next=NULL;
  return;
 }
 temp->next=front;
 front=temp;
}
void insRear()
{
 temp=(struct node*)malloc(sizeof(struct node));
 printf("\nEnter element: ");
 scanf("%d",&temp->info);
 temp->next=NULL;
 if(front==NULL && rear==NULL)
 {
  front=rear=temp;
  return;
 }
 rear->next=temp;
 rear=rear->next;
}
void delFront()
{
 if(front==NULL)
 {
  printf("\nEmpty Dequeue\n");
  return;
 }
 if(front==rear && front!=NULL)
 {
  ptr=front;
  front=rear=NULL;
  free(ptr);
 }
 else
 {
  ptr=front;
  front=front->next;
  free(ptr);
 }
}

void delRear()
{
 if(rear==front && rear==NULL)
 {
  printf("Empty DEQUE");
  return;
 }
 else if(rear==front)
 {
  rear=front=NULL;
  free(rear);
  free(front);
 }
 else
 {
  ptr=front;
  while(ptr->next!=NULL)
  {
   temp=ptr;
   ptr=ptr->next;
  }
  temp->next=NULL;
  free(ptr);
  rear=temp;
 }
}
void display()
{
 if(front==NULL && rear==NULL)
 {
  printf("\nEmpty Dequeue\n");
  return;
 }
 ptr=front;
 while(ptr!=NULL)
 {
  printf("%d  ",ptr->info);
  ptr=ptr->next;
 }
}
int  main()
{
 int ch;

 while(1)
 {

  printf("\n1. Insert at rear");
  printf("\n2. Insert at Front");
  printf("\n3. Delete from front");
  printf("\n4. Delete from rear");
  printf("\n5. Display");
  printf("\n6. Exit");
  printf("\nEnter your choice:\n");
  scanf("%d",&ch);
  switch (ch)
  {
   case 1: insRear(); break;
   case 2: insFront(); break;
   case 3: delFront(); break;
   case 4: delRear(); break;
   case 5: display(); break;
   case 6: exit(0); break;
   default: printf("Invalid option");
  }
 }
}

Double ended queue using array in C

#include<stdio.h>
#include<stdlib.h>
#define SIZE 5

int a[10],front=-1,rear=-1;
void insEnd(int item)
{

    if(rear>=SIZE-1)
    {
        printf("overflow\n");
    }
    else
    {
        if(front==-1)
        {
            front++;
            rear++;
        }
        else
        {
            rear=rear+1;
        }
    a[rear]=item;
    printf("Inserted item is %d\n",a[rear]);
    }
}
void insBeg(int item)
{

        if(front==-1)
        {
            front=0;
            a[++rear]=item;
            printf("inserted element is %d\n",item);
        }
        else if(front!=0)
        {
            a[--front]=item;
            printf("inserted element is %d\n",item);

        }


}
void display()
{
    int i;
    if(front==-1)
    {
        printf("Dequeue is empty\n");
    }
    else
    {
        for(i=front;i<=rear;i++)
        {
            printf("%d  ",a[i]);
        }
    }
}
void deleteBeg()
{
        if(front==-1)
        {
            printf("Dequeue is empty\n");
            return;
        }
        else
        {
            printf("the deleted element is %d",a[front]);
            if(front==rear)
            {
                front=rear=-1;
                return;
            }
            else
                front=front+1;
        }
}
void deleteEnd()
{
        if(front==-1)
        {
            printf("deletion is not possible dequeue is empty\n");
            return;
        }
        else
        {
            printf("the deleted element is %d",a[rear]);
            if(front==rear)
            {
                front=rear=-1;
            }
            else
                rear=rear-1;
        }
}
int main()
{
    int ch,item;
    do
    {

        printf("\n1_insert at beginning\n");
        printf("2_insert at end");
        printf("\n 3_display");
        printf("\n 4_deletion from front");
        printf("\n 5_deletion from rear");
        printf("\n 6_exit");
        printf("\n enter your choice\n");
        scanf("%d",&ch);
        switch(ch)
        {
            case 1:printf("enter the element to be inserted\n");
                          scanf("%d",&item);
                          insBeg(item);
                          break;
            case 2:printf("enter the element to be inserted\n");
                         scanf("%d",&item);
                         insEnd(item);
                         break;
            case 3:display();
                        break;
            case 4:deleteBeg();
                        break;
            case 5:deleteEnd();
                      break;
            case 6:exit(0);
                        break;
            default:printf("invalid choice\n");
                    break;
        }
    }
    while(ch!=6);
}

Conversion from Infix to Postfix in C

#include<stdio.h>
char stack[20];
int top = -1;
void push(char x)
{
    stack[++top] = x;
}

char pop()
{
    if(top == -1)
        return -1;
    else
        return stack[top--];
}

int priority(char x)
{
    if(x == '(')
        return 0;
    if(x == '+' || x == '-')
        return 1;
    if(x == '*' || x == '/')
        return 2;
}

int main()
{
    char exp[20];
    char *e, x;
    printf("Enter the expression : ");
    scanf("%s",exp);
    e = exp;
    while(*e != '\0')
    {
        if(isalnum(*e))
            printf("%c",*e);
        else if(*e == '(')
            push(*e);
        else if(*e == ')')
        {
            while((x = pop()) != '(')
                printf("%c", x);
        }
        else
        {
            while(priority(stack[top]) >= priority(*e))
                printf("%c",pop());
            push(*e);
        }
        e++;
    }
    while(top != -1)
    {
        printf("%c",pop());
    }
}

Queue using linklist in C

#include <stdio.h>
#include <stdlib.h>
typedef struct queue
{
    int data;
    struct abc *next;
}node;
node *rear=NULL;
node *front=NULL;
void ins(int val)
{
    node *q;
    q=(node *)malloc(sizeof(node));
    q->data=val;
    if(rear==NULL)
    {
        rear=front=q;
    }
    else
    {
        rear->next=q;
        rear=q;
    }
}
void del()
{
    node *temp;
    temp=front;
    if(front==NULL)
    {
        printf("Queue Underflow");
    }
    else{
    front=front->next;
    free(temp);}
}
void display()
{
    node *temp;
    temp=front;
    if(front==NULL)
        printf("Queue is Empty");
    else{
    while(temp!=rear)
    {
        printf("%d ",temp->data);
        temp=temp->next;
    }
    printf("%d ",temp->data);
        }
}
int main()
{

    int val,ch;
    do
    {
        printf("\nEnter  1 to Insert\n2 to Delete\n3--Display\n4 to Exit\n Enter your choice\n");
        scanf("%d",&ch);
        switch(ch)
        {
            case 1:printf("Enter the value to be inserted\t");
                    scanf("%d",&val);
                    ins(val);
                    break;
            case 2:del();
                    break;
            case 3:display();
                    break;
        }
    }while(ch!=4);
    return 0;
}

Double link list using C

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

struct node *create(struct node *head)
{
    struct node *newnode,*ptr;
    int val;
    printf("Enter -1 to stop\n");
    printf("Enter the value of node:\n");
    scanf("%d", &val);
    while(val!=-1)
    {
        newnode=(struct node*)malloc(sizeof(struct node));
        newnode -> data= val;
        if(head==NULL)
        {
            head=newnode;
            newnode->prev=NULL;
            newnode->next=NULL;
        }
        else
        {
            ptr=head;
            while(ptr->next != NULL)
            {
                ptr=ptr->next;
            }
            ptr->next =newnode;
        }
        newnode->prev=NULL;
        newnode->next=NULL;
        printf("Enter the value of node\n");
        scanf("%d",&val);
   }
   return head;
}
struct node *display(struct node *head)
{
    if(head==NULL)
    {
        printf("Linked list is not created\n");
    }
    else
    {
        struct node *ptr;
        ptr=head;
        while(ptr!= NULL)
        {
            printf("%d ",ptr->data);
            ptr=ptr->next;
        }
    }
    return head;
}

struct node *insBeg(struct node *head)
{
    struct node *newnode;
    newnode=(struct node*)malloc(sizeof(struct node));
    if(head==NULL)
    {
        printf("Linked list is not created\n");
    }
    else
    {
        int val;
        printf("Enter the value of node\n");
        scanf("%d",&val);
        newnode->data=val;
        newnode->prev=NULL;
        newnode->next=head;
        head=newnode;
    }
    return head;
}
struct node *insEnd(struct node *head)
{
    if(head==NULL)
    {
        printf("Linked list is not created\n");
    }
    else
    {
        struct node *newnode,*ptr;
        newnode=(struct node *)malloc(sizeof(struct node));
        int val;
        printf("4. Enter the value of node\n");
        scanf("%d",&val);
        newnode->data = val;
        ptr=head;
        while(ptr->next !=NULL)
        {
            ptr=ptr->next;
        }
        ptr->next =newnode;
        newnode->prev=ptr;
        newnode->next =NULL;
    }
    return head;
}
struct node *insBef(struct node *head)
{
    if(head==NULL)
    {
        printf("Linked list is not created\n");
    }
    else
    {
        struct node *ptr,*pptr;
        ptr=head;
        pptr=ptr->next;
        int n,val;
        printf("Enter the value before which you want to insert\n");
        scanf("%d",&n);
        if(ptr->data==n)
        {
            head=insBeg(head);
        }
        else
        {
            while(pptr->data!=n)
            {
                ptr=pptr;
                pptr=pptr->next;
                if(pptr==NULL)
                {
                    printf("Value not in the list\n");
                    return head;
                }
            }
            struct node *newnode;
            newnode=(struct node*)malloc(sizeof(struct node));
            printf("Enter the value of newnode\n");
            scanf("%d",&val);
            newnode->next=pptr;
            ptr->next=newnode;
            newnode->prev=ptr;
            newnode->data=val;
        }

    }
    return head;
}

struct node *insAft(struct node *head)
{
    if(head==NULL)
    {
        printf("Linked list is not created\n");
    }
    else
    {
            struct node *ptr,*pptr;
            ptr=head;
            pptr=ptr->next;
            int n;
            printf("Enter the value of node after which you want to insert\n");
            scanf("%d",&n);
            while(ptr->data!=n)
            {
                ptr=pptr;
                if(ptr==NULL)
                {
                    printf("Node doesn't exist in the list\n");
                    return head;
                }
                pptr=pptr->next;
            }
            struct node *newnode;
            newnode=(struct node*)malloc(sizeof(struct node));
            int val;
            printf("Enter the value of node\n");
            scanf("%d",&val);
            newnode->next=pptr;
            ptr->next=newnode;
            newnode->prev=ptr;
            newnode->data=val;

    }
    return head;
}
struct node *posIns(struct node *head)
{
    int n=0,k=2;

    printf("\nEnter the position at which you want to insert\n");
    scanf("%d",&n);

    if(head==NULL)
    {
        printf("Linked list is not created\n");
    }
    else
    {
        int c;
        struct node *ptr;
        ptr=head;
        while(ptr!=NULL)
        {
            c++;
            ptr=ptr->next;
        }
        if(n<=0 || n>(c+1))
        {
            printf("Insert a valid position\n");
        }
        else if(n==1)
        {
            head=insBeg(head);
        }
        else
        {
            int val;
            struct node *newnode;
            newnode=(struct node*)malloc(sizeof(struct node));
            printf("Enter the value of node\n");
            scanf("%d",&val);
            newnode->data=val;
            ptr=head;
            if(k==n)
            {
                newnode->next=ptr->next;
                ptr->next=newnode;
                newnode->prev=ptr;
            }
            else
            {
                while(k!=n)
                {
                    ptr=ptr->next;
                    k++;
                }
                if(ptr==NULL)
                    printf("\nPlease enter a valid option\n");
                else
                {
                    newnode->next=ptr->next;
                    ptr->next=newnode;
                    newnode->prev=ptr;
                }

            }
        }
    }
    return head;
}
struct node *delBeg(struct node *head)
{
    if(head==NULL)
    {
        printf("Linked list is not created\n");
    }
    else
    {
        struct node *ptr;
        ptr=head;
        head=head->next;
        head->prev=NULL;
        free(ptr);
    }
    return head;
}
struct node *delEnd(struct node *head)
{
    if(head==NULL)
    {
        printf("Linked list is not created\n");
    }
    else
    {
        struct node *ptr,*preptr;
        if(head->next==NULL)
        {
            head=NULL;
            free(head);
        }
        else
        {
            ptr=head;
            while(ptr->next!=NULL)
            {
                preptr=ptr;
                ptr=ptr->next;
            }
            preptr->next=NULL;
            free(ptr);
        }
    }
    return head;
}
struct node *delBef(struct node *head)
{
    if(head==NULL)
    {
        printf("Linked list is not created\n");
    }
    else
    {
        struct node *ptr,*pptr;
        ptr=head;
        pptr=ptr->next;
        int n;
        printf("Enter the value of node before which you want to delete\n");
        scanf("%d",&n);
        if(ptr->data==n)
        {
            printf("Deletion is not possible\n");
        }
        else if(pptr->data==n)
        {
            head=delBeg(head);
        }
        else
        {
            while(pptr->next->data!=n)
            {
                ptr=pptr;
                pptr=pptr->next;
                if(pptr->next==NULL)
                {
                    printf("Invalid choice\n");
                    return head;
                }
            }
            ptr->next=pptr->next;
            free(pptr);
        }

    }
    return head;
}
struct node *delAft(struct node *head)
{
    if(head==NULL)
    {
        printf("Linked list is not created\n");
    }
    else
    {
        struct node *ptr,*pptr;
        ptr=head;
        pptr=ptr->next;
        int n;
        printf("Enter the value of node after which you want to delete\n");
        scanf("%d",&n);
        while(ptr->data!=n)
        {
            ptr=pptr;
            if(ptr->next==NULL)
            {
                printf("\nPlease enter a valid value\n");
                return head;
            }
            pptr=pptr->next;
        }
        ptr->next=pptr->next;
        free(pptr);
    }
    return head;
}

struct node *posDel(struct node *head)
{
     int n=0,k=2;

    printf("\nEnter the position at which you want to delete\n");
    scanf("%d",&n);

    if(head==NULL)
    {
        printf("Linked list is not created\n");
    }
    else
    {
        int c=0;;
        struct node *ptr,*pptr;
        ptr=head;
        while(ptr!=NULL)
        {
            c++;
            ptr=ptr->next;
        }
        if(n<=0 || n>c)
        {
            printf("insert a valid position\n");
        }
        else if(n==1)
        {
            head=delBeg(head);
        }
        else
        {
            ptr=head;
            pptr=ptr->next;
            while(k!=n)
            {
                ptr=ptr->next;
                pptr=ptr->next;
                k++;
            }
            if(ptr==NULL)
                printf("\nPlease enter a valid option\n");
            else
            {
                ptr->next=pptr->next;
                free(pptr);
            }
        }
    }
    return head;
}

int main()
{
    int num;
    struct node *head=NULL;
    do
    {
        printf("Enter your option\n");
        printf("1. Creation or insertion of doubly linked list\n");
        printf("2. Deletion\n");
        printf("Enter -1 to exit\n");
        scanf("%d",&num);
        switch(num)
        {
            case 1:
                {
                     do
                     {

                           printf("\n1. Create a doubly linked list \n");
                           printf("2. Display the linked list \n");
                           printf("3. Insert a node at the beginning \n");
                           printf("4. Insert a node at the end \n");
                           printf("5. Insert a node before a given node \n");
                           printf("6. Insert a node after a given node \n");
                           printf("7. Insert a node at a position\n");
                           printf("Enter your option \n");
                           printf("Enter -1 to exit\n");
                           scanf("%d",&num);
                           switch(num)
                           {
                               case 1:
                                   head=create(head);
                                   break;
                               case 2:
                                    head=display(head);
                                    break;
                               case 3:
                                    head=insBeg(head);
                                    break;
                               case 4:
                                    head=insEnd(head);
                                    break;
                               case 5:
                                    head=insBef(head);
                                    break;
                               case 6:
                                    head=insAft(head);
                                    break;
                               case 7:
                                    head=posIns(head);
                           }
                       }while(num!=-1);
                       break;
                }
                case 2:
                        {
                            do
                            {
                                printf("\n\n1. Display\n");
                                printf("2. Delete a node at the beginning\n");
                                printf("3. Delete a node at the end\n");
                                printf("4. Delete a node before a given node\n");
                                printf("5. Delete a node after a given node\n");
                                printf("6. Delete a node at a position\n");
                                printf("Enter -1 to exit\n");
                                printf("Enter your option\n");
                                scanf("%d",&num);
                                switch(num)
                                {
                                case 1:
                                    {
                                        head=display(head);
                                        break;
                                    }
                                case 2:
                                    {
                                        head=delBeg(head);
                                        break;
                                    }
                                case 3:
                                    {
                                        head=delEnd(head);
                                        break;
                                    }
                                case 4:
                                    {
                                        head=delBef(head);
                                        break;
                                    }
                                case 5:
                                    {
                                        head=delAft(head);
                                        break;
                                    }
                                case 6:
                                    {
                                        head=posDel(head);
                                        break;
                                    }
                                }
                            }while(num!=-1);
                            break;
                        }
        }
        printf("Enter your option\n");
        printf("Enter any no. to continue Insertion or Deletion\n");
        printf("Enter -1 to exit\n");
        scanf("%d",&num);
    }while(num!=-1);

   return 0;
}


 

Copyright @ 2015