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

Monday, 26 January 2015

A C program to perform 3 binary Set Operations i.e Union , Intersection and Set Differance.

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

int  insert(int *A, int a,int size){       
    int flag = 0;
    int i;

    for(i = 0; i <= size ; i++){
        if(A[i] == a){
            flag = 1;
            printf("Dulicate not allowed in Set\n");
        }   
    }
   
    if(flag == 0){
        A[size] = a;
        return 1;
    }
    else
        return 0;
}

void  intersection(int *A,int *B,int sizeA,int sizeB){   
    int ni,i,j;
    int sizei=0;
    if(sizeA < sizeB)
        ni = sizeA;
    else   
        ni = sizeB;
    int itr = 0;
    int *intsec = malloc(ni * sizeof(int));
    for(i = 0;i < sizeA;i++){
        for(j = 0;j < sizeB;j++){
            if(A[i] == B[j]){
                intsec[sizei] = A[i];
                sizei++;   
                    continue;           
            }
            itr++;
        }
    }

    printf("\nIntersection Result is\n");
    for(i = 0; i < sizei ; i++)   
        printf("%d \n",intsec[i]);
   
}

void setdifferance(int *A,int *B,int sizeA,int sizeB,int ch){
    int i ,j,ni;
    int flag =0;
    int k = 0;

    if(sizeA > sizeB)
                ni = sizeA;
        else
                ni = sizeB;

    int *setdiff = malloc(ni * sizeof(int));   

    if(ch == 1) {
      for(i = 0 ; i < sizeA ; i++){
        for(j = 0 ; j < sizeB; j++){
            if(A[i] == B[j]){
                flag = 1;
            }   
        }           
        if(flag == 0){
            setdiff[k] = A[i];
            k++;
        }
        flag = 0;   
       }
    }
    if(ch == 2){
      for(i = 0 ; i < sizeB ; i++){
                for(j = 0 ; j < sizeA ; j++){
                        if(B[i] == A[j]){
                                flag = 1;
                        }
        }
               if(flag == 0){
                      setdiff[k] = B[i];
                      k++;
               }
               flag = 0;
      }
    }

    printf("\nSet Differance Result is\n");
   
    if(k == 0)
        printf("NULL\n");
       
        for(i = 0; i < k ; i++)
                printf("%d \n",setdiff[i]);
}

void unionOp(int *A,int *B,int sizeA, int sizeB){
    int size = sizeA + sizeB;
    int *uni = malloc(size * sizeof(int));
    int i,j;
    int k=0;
    int flag =0;
    for(i = 0 ; i < sizeA; i++){
        uni[i] = A[i];   
    }
        for(j = 0 ; j < sizeB ; j++){
        for(i = 0 ; i < sizeA ; i++){
            if(B[j] == A[i]){
                flag =1;
            }
        }
        if(flag == 0){
            k++;
            uni[sizeA+k-1] = B[j];
        }
        flag = 0;
    }

   
    printf("\nUnion Result is\n");
       
    for(i = 0; i < sizeA+k ; i++)
                printf("%d \n",uni[i]);
          
}

void main(){
    int na,nb,a,b,sizeA,sizeB,flag,diffch;
    int ch=1;
    //struct set *s, *B;
    sizeA = 0;
    sizeB = 0;
    printf("Enter number of elements of set 1\n");
    scanf("%d",&na);
    int *A = malloc(na * sizeof(int));
    int *B = malloc(nb * sizeof(int));
    while(sizeA < na){
        printf("\nInsert elements in set 1\n");
        scanf("%d",&a);
        sizeA +=  insert(A,a,sizeA);
    }
   
    printf("\nEnter number of elements of set 2\n");
        scanf("%d",&nb);

    while(sizeB < nb){
        printf("Insert elements in set 2\n");   
        scanf("%d",&b);
        sizeB += insert(B,b,sizeB);
    }

    while(ch){
    printf("\nPress 1 for intersection\n"
        "Press 2 for union\n"
        "Press 3 for set differance\n"
        "Press 0 for exit \n");

    scanf("%d",&ch);

    switch(ch){
        case 1:
            intersection(A,B,sizeA,sizeB);
            break;
        case 2:
            unionOp(A,B,sizeA,sizeB);
            break;       
        case 3:
            printf("\nFor (Set 1 - set2) press 1 "
                "Else press 2\n");
            scanf("%d",&diffch);
            setdifferance(A,B,sizeA,sizeB,diffch);
            break;
        case 0:
            printf("Good-Bye\n");
            break;   
        default:
            printf("Invalid Input\n");   
        }
    }
}

Sunday, 5 October 2014

C Program To Implement Bank Token Generation Machine(Using threads and Semaphores)

/*
Problem Description
A bank has three entrances and eight counters. Each entrance has a token issuing machine next to it. A customer enters or exits the bank through any of the entrances. He/she then collects a token and waits for the token number to be 'called' by any counter. The calling of a token is taken to mean that the customer finishes his/her job and exits from the bank.
It must be ensured that:
  • Tokens must be issued in strict sequence.
  • No token number must be skipped or issued twice.
  • Counters should call the tokens in a strict ascending order.
  • No token should be skipped or called twice.
  • No counter can call a token number that has not been issued at that time.
Write a program that uses multiple threads for the token issuing machines and counters with appropriate synchronisation primitives*/

//Solution

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

#define MAXCUSTOMERS        2000000
#define TOKEN_MACHINES      3
#define SEVICE_COUNTERS     8

int nitems;
int buff[MAXCUSTOMERS];
struct {
  sem_t mutexpro, mutexcon , nstored;  
  int nput;
  int nval;
  int nvalcon;
} shared;

void *prod(void *), *cons(void *);

int main (int argc, char *argv[])
{
     int i, nthreads, countpro[3],countcon[8], totalsum = 0;
     pthread_t tid_prod[TOKEN_MACHINES], tid_cons[SEVICE_COUNTERS];

     if (argc != 2) {
 fprintf(stderr, "Usage: mutexes #maxnumberOfCUSTOMER\n");
 exit(1);
     }

     if ((nitems = atoi (argv[1])) > MAXCUSTOMERS)
 nitems = MAXCUSTOMERS;

     sem_init (&shared.mutexpro, 0, 1);
     sem_init (&shared.mutexcon, 0, 1);
     sem_init (&shared.nstored, 0, 0);

     for (i=0; i<3; i++) {
 countpro[i] = 0;
 pthread_create(&tid_prod[i], NULL, prod, &countpro[i]);
     }
     
     for (i=0; i<8; i++) {      
 countcon[i]=0;
 pthread_create(&tid_cons[i], NULL, cons, &countcon[i]);
     }

     for (i=0; i<3; i++) {
 pthread_join(tid_prod[i], NULL);
  totalsum += countpro[i];
     }
     
     for (i=0; i<8; i++) {
pthread_join(tid_cons[i], NULL);
      printf("\nCustomers served by %d counter are %d\n", i, countcon[i]);
     }

     sem_destroy (&shared.mutexpro);
     sem_destroy (&shared.mutexcon);
     sem_destroy (&shared.nstored);
     exit(0);
}

void *prod(void *arg)
{
     for ( ; ; ) {
 sem_wait (&shared.mutexpro);
 if (shared.nput >= nitems) {
      sem_post (&shared.mutexpro);
      return (NULL);
 }

 buff[shared.nput] = shared.nval;
 printf("The token issued at entrance %d \n",buff[shared.nput]);
 shared.nput++;
 shared.nval++;
 sem_post (&shared.mutexpro); 
 sem_post (&shared.nstored);
 *((int *) arg) += 1;
     }
}

void *cons(void *arg)
{
     int i;
     
     for ( ; ;) {
 sem_wait (&shared.mutexcon);
 if (shared.nvalcon >= nitems) {
      sem_post (&shared.mutexcon);
      return (NULL);
 }
          sem_wait (&shared.nstored);    
          printf("The customer served is %d \n",buff[shared.nvalcon]);
 shared.nvalcon = shared.nvalcon + 1;
 sem_post (&shared.mutexcon);
       *((int *) arg) += 1;
     }     
     return(NULL);

}

Sunday, 21 September 2014

Binary Search Tree(Insertion,traversal,deletion)- Input the names of cities and insert them in to tree.Traverse the tree and also delete elements from the tree.

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

struct node{
char city[20];
struct node *left;
struct node *right;
struct node *parent;
};


void display(struct node *);
struct node * delete(struct node *root,char del[20]);

struct node * insert(char city[20],struct node *root){
struct node *temp = (struct node *)malloc(sizeof(struct node));
struct node *prev = (struct node *)malloc(sizeof(struct node));
struct node *new = (struct node *)malloc(sizeof(struct node));
strcpy(new->city,city);
new->left = NULL;
new->right = NULL;
if(root->parent == NULL){
                        //printf("inside root if\n");
                        root = new;
                        new->parent = root;
        }
else{
int right=0;
int left =0;
temp = root;
while(temp != NULL ){
prev = temp;
left = 0;
right = 0;
if(strcmp(temp->city,new->city) < 0){
//printf("insertion in right\n");
temp = temp->right;
right = 1;
}
else{
//printf("insertion in left\n");
temp = temp->left;
left = 1;
}
}
if(right == 1){
//printf("inside right\n");
//printf("parent %s \n",prev->city);
prev->right = new;
new->parent = prev;
right =0;
left = 0;
}
if(left == 1){
//printf("inside left\n");
//printf("parent %s \n",prev->city);
prev->left = new;
new->parent = prev;
right =0;
left =0;
}
}
return root;
}

void display(struct node *current){
if(current !=NULL){
display(current->left);
printf("%s \n",current->city);
display(current->right);
}
}

void main(){
char city[20];
struct node *root = (struct node *)malloc(sizeof(struct node));
root->left = NULL;
root->right = NULL;
root->parent = NULL;
int i;
int num;
char del[20];
int ch=0;
while(1){
printf("press 1 to insert,2 to delete and 3 to exit\n");
scanf("%d",&ch);
switch(ch){
case 1 :
printf("Enter the number of elements you wana insert in tree \n");
scanf("%d",&num);
for(i=0;i<num;i++){
printf("Enter the name of the city\n");
scanf("%s",city);
root = insert( city , root);
//printf("root city is %s\n",root->city);
}
printf("the list is as follows\n");
display(root);
break;
case 2 :
printf("enter the number of elements you wana delete\n");
scanf("%d",&num);
for(i=0;i<num;i++){
printf("Enter the city to be deleated \n");
scanf("%s",del);
root = delete( root , del);
printf("the list is as follows\n");
display(root);
}
break;
case 3 :
exit(0);
break;
default :
printf("invalid input\n");
break;
}
}
}


struct node * delete(struct node *root,char del[20]){
int flag;
int right = 0;
int left = 0 ;
int rt =0;
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp = root;
struct node *prev =(struct node *)malloc(sizeof(struct node));
struct node *temp2 =(struct node *)malloc(sizeof(struct node));
while(temp != NULL){
//printf("current city to be compared is %s\n",temp->city);
//printf("left child is left %s and right child is %s and parent is %s\n",temp->left->city,temp->right->city,temp->parent->city);
if(strcmp(temp->city,del) == 0){
//printf("the city to be deleated is %s right value= %d left value = %d\n",temp->city,right,left);
if(temp == root){
rt = 1;
}
if(temp->right == NULL && temp->left == NULL && (right ==1 || rt == 1)){
if(rt == 1){
printf("tree is empty \n");
rt = 0;
temp->parent = NULL;
free(temp);
}
else{
//printf("the node has no child and is the right child\n");
temp->parent->right = NULL;
temp->parent = NULL;
right = 0;
left  = 0;
free(temp);
}
}
else if(temp->right == NULL && temp->left == NULL && (left == 1 || rt==1)){
if(rt == 1){
printf("TREE IS EMPTY\n");
rt = 0;
temp->parent = NULL;
free(temp);
}
else{
//printf("the node has no child and is the left child\n");
temp->parent->left = NULL;
temp->parent = NULL;
right = 0;
left = 0;
free(temp);
}
}
else if(temp->right == NULL && (left == 1 || rt == 1)){
//printf("the node has left child and is left child to its parent\n");
if(rt == 1){
root =root->left;
root->parent = root;
temp->left = NULL;
temp->right = NULL;
rt = 0;
free(temp);  
}
else{
//printf("the parent of node to be deleted is %s\n",temp->parent->city);
temp->parent->left = temp->left;
//printf("new left child of parent is %s\n",temp->parent->left->city);
//printf("original parent of left child is %s\n",temp->left->parent->city);
temp->left->parent = temp->parent;
//printf("the new parent is %s\n",temp->left->parent->city);
temp->left = NULL;
temp->parent = NULL;
left = 0;
right = 0;
free(temp);
}
}
else if(temp->right == NULL && (right == 1 || rt == 1)){
printf("the node has left child and is right child to its parent\n");
if(rt == 1){
root = root->left;
root->parent = root;
temp->left = NULL;
temp->parent = NULL;
rt =0;
free(temp);
}
else{
//printf("the parent of node to be deleted is %s\n",temp->parent->city);
temp->parent->right = temp->left;
//printf("new right child of parent is %s\n",temp->parent->right->city);
temp->left->parent = temp->parent;
temp->left = NULL;
temp->parent = NULL;
left =0;
right =0;
free(temp);
}
}
else if(temp->left == NULL && (right ==1 || rt == 1)){
//printf("the node has right child and itself is a right child\n");
if(rt == 1){
root = root->right;
root->parent = root;
temp->left = NULL;
temp->parent = NULL;
rt = 0;
free(temp);
}
else{
temp->parent->right = temp->right;
temp->right->parent = temp->parent;
temp->right = NULL;
temp->parent =NULL;
left = 0;
right = 0;
free(temp);
}
}
else if(temp->left == NULL && (left == 1 || rt == 1)){
//printf("the node has right child and itself is left child\n");
if(rt == 1){
root = root->right;
root->parent = root;
temp->left = NULL;
temp->parent = NULL;
rt = 0;
free(temp);
}
else{
temp->parent->left = temp->right;
temp->right->parent = temp->parent;
temp->right = NULL;
temp->parent = NULL;
left = 0;
right = 0;
free(temp);
}
}
else{
//printf("the city to be delated has both left and right child\n");
prev = temp;
temp = temp->right;
printf("the right child of city to be deleated is %s\n",temp->city);
if(temp->left == NULL && (left == 1 || rt == 1)){
if(rt == 1 ){
root = root->right;
                                        root->parent = root;
                                        root->left  = prev->left;
prev->left->parent = root;
prev->left = NULL;
prev->right = NULL;
                                        prev->parent = NULL;
                                        rt = 0;
                                      free(prev);
}
else{
prev->parent->left =prev->right;
prev->right->parent = prev->parent;
prev->left->parent = prev->right;
prev->right->left = prev->left;
prev->parent =NULL;
prev->left = NULL;
prev->right =  NULL;
right = 0;
left = 0;
free(prev);
}
break;
}
if(temp->left == NULL && right == 1){
//printf("the child of city to be deleated has no left child and itself is a right child\n");
                                        prev->parent->right =prev->right;
                                        prev->right->parent = prev->parent;
                                        prev->left->parent = prev->right;
                                        prev->right->left = prev->left;
                                        prev->parent =NULL;
                                        prev->left = NULL;
                                        prev->right =  NULL;
                                        right = 0;
                                        left = 0;
                                        free(prev);
                                        break;
                                }                                                          
else{
temp = temp->left;
}
//printf("before while left child been pionted is %s  and its left child is %s\n",temp->city,temp->left->city);
while(temp->left != NULL){
temp = temp->left;
}
//printf("the child to be replaced with the child to be deleated is %s\n",temp->city);
if(temp->right == NULL && (left == 1 ||rt == 1)){
//printf("the child has no further child and is in the left subtree of del child\n");
if(rt == 1){
root = temp;
rt = 0 ;
                                                temp->parent->left = NULL;
                                                temp->left = prev->left;
                                                temp->right = prev->right;
                                                temp->parent = root;
                                                prev->left->parent = root;
                                                prev->right->parent = root;
                                                prev->left = NULL;
                                                prev->right = NULL;
                                                prev->parent = NULL;
                                                left = 0;
                                                right = 0;
                                                free(prev);

}
else{
temp->parent = prev->parent;
temp->parent->left = NULL;
temp->left = prev->left;
temp->right = prev->right;
temp->parent = prev->parent;
prev->left->parent = temp;
prev->right->parent = temp;
prev->parent->left = temp;
prev->left = NULL;
prev->right = NULL;
prev->parent = NULL;
left = 0;
right = 0;
free(prev);
}
}
else if(temp->right == NULL && right == 1){
//printf("no further children of the child and it is in left right\n ");
temp->parent->left = NULL;
temp->left = prev->left;
temp->right = prev->right;
temp->parent = prev->parent;
prev->left->parent = temp;
prev->right->parent = temp;
prev->parent->right = temp;
prev->left = NULL;
prev->right = NULL;
prev->parent = NULL;
left = 0;
right = 0;
free(prev);
}
else if(temp->right != NULL && (right == 1 || rt == 1)){
//printf("the child has further a right child and is in right subtree of del\n");
if(rt == 1){
root  = temp;
rt = 0;
temp->parent->left = temp->right;
                                        temp->right->parent = temp->parent;
                                        temp->left = prev->left;
                                        temp->right = prev->right;
                                        temp->parent = root;
                                        prev->left->parent = root;
                                        prev->right->parent = root;
                                        prev->left = NULL;
                                        prev->right = NULL;
                                        prev->parent = NULL;
                                        left = 0;
                                        right = 0;
                                        free(prev);
}
else{
temp->parent->left = temp->right;
temp->right->parent = temp->parent;
temp->left = prev->left;
                                        temp->right = prev->right;
                                        temp->parent = prev->parent;
                                        prev->left->parent = temp;
                                        prev->right->parent = temp;
                                        prev->parent->right = temp;
                                        prev->left = NULL;
                                        prev->right = NULL;
                                        prev->parent = NULL;
                                        left = 0;
                                        right = 0;
                                        free(prev);
}
}
else if(temp->right != NULL && left == 1){
                                        //printf("the child has further children and is in left subtree of del\n");
temp->parent->left = temp->right;
                                        temp->right->parent = temp->parent;
                                        temp->left = prev->left;
                                        temp->right = prev->right;
                                        temp->parent = prev->parent;
                                        prev->left->parent = temp;
                                        prev->right->parent = temp;
                                        prev->parent->left = temp;
                                        prev->left = NULL;
                                        prev->right = NULL;
                                        prev->parent = NULL;
                                        left = 0;
                                        right = 0;
                                        free(prev);
                                }
}
return root;
}
else{
if(strcmp(temp->city,del)>0){
temp = temp->left;
left = 1;
right = 0;
rt  = 0;
}
else if(strcmp(temp->city,del)<0){
temp = temp->right;
right =1;
left = 0;
rt = 0;
}
}

}
return root;
}

Sunday, 14 September 2014

Job Scheduler using heap for priotiry que.

/*
I want to design a job scheduler which work in a Shortest Job First Strategy. The input to the scheduler is a Priority Queue that can accept the request for a new job dynamically. Usingthe algorithms discussed in the class, design thisPriority Queue with all possible operationson it.
*/

#include<stdio.h>
#include<stdlib.h>
void max_heapify(int *ar,int *bst,int i,int *size);
void swap(int *a,int *b){
 int temp= *a;
 *a = *b;
 *b = temp;
}
void builtheap(int *ar,int *bst, int *no)
{
 int i; 
 for(i=(*no)/2;i>=1;i--){
//  printf("inside for val of i = %d and no =%d \n",i, *no);  
  max_heapify(ar,bst,i,no);
 }
}
void max_heapify(int *ar,int *bst,int i,int *size){ 
int p1,temp,temp2; 
if(i!=0 && ((2*i+1==(*size) && i == (*size)/2) || (i != (*size)/2) && (*size)!=0)){
// printf("2i+1 ele %d  size %d\n",ar[(2*i)+1],*size); 
 if(ar[2*i+1]<ar[2*i]){
  if(ar[2*i+1]<ar[i]){
   swap(&ar[i],&ar[2*i+1]);
   swap(&bst[i],&bst[2*i+1]); 
   max_heapify(ar,bst,i/2,size);  
  }
  else if(ar[2*i+1]==ar[i]){
   if(bst[2*i+1]<bst[i]){
    swap(&ar[2*i+1],&ar[i]);
    swap(&bst[2*i+1],&bst[i]);
    max_heapify(ar,bst,i/2,size);
   }    
  } 
 }
 else if(ar[2*i+1]>ar[2*i]){
  if(ar[2*i]<ar[i]){
   
   swap(&ar[2*i],&ar[i]);
   swap(&bst[2*i],&bst[i]);
   max_heapify(ar,bst,i/2,size);
  }
  else if(ar[2*i]==ar[i]){
   if(bst[2*i]<bst[i]){
    swap(&ar[2*i],&ar[i]);
    swap(&bst[2*i],&bst[i]);   
    max_heapify(ar,bst,i/2,size);
   }
  }
 }
 else{
  if(bst[2*i+1]<bst[2*i]){
   if(ar[2*i+1]<ar[i]){
    swap(&ar[2*i+1],&ar[i]);
    swap(&bst[2*i+1],&bst[i]);
    max_heapify(ar,bst,i/2,size);
   }
   else if(ar[2*i+1]==ar[i]){
                         if(bst[2*i+1]<bst[i]){
                                 swap(&ar[2*i+1],&ar[i]);
                                 swap(&bst[2*i+1],&bst[i]);
                          max_heapify(ar,bst,i/2,size);
    }   
   }
  }
  else if(bst[2*i+1]>bst[2*i]){
   if(ar[2*i]<ar[i]){
                         swap(&ar[2*i],&ar[i]);
    swap(&bst[2*i],&bst[i]);
    max_heapify(ar,bst,i/2,size);
   }
                 else if(ar[2*i]==ar[i]){
                         if(bst[2*i]<bst[i]){
                                 swap(&ar[2*i],&ar[i]);
                                 swap(&bst[2*i],&bst[i]);
     max_heapify(ar,bst,i/2,size);
                         }
   } 
  }
  else
  {
   if(ar[i]>ar[2*i]){
    swap(&ar[i],&ar[2*i]);
    swap(&bst[i],&bst[2*i]);
    max_heapify(ar,bst,i/2,size);
   }
   else if(ar[i]==ar[2*i]){
    if(bst[2*i]<bst[i]){
     swap(&ar[i],&ar[2*i]);
                                 swap(&bst[i],&bst[2*i]);
     max_heapify(ar,bst,i/2,size);
    }
   }
  }
 } 
}
if(i!=0 &&(2*i==(*size) && i == (*size)/2 || i != (*size)/2 && (*size)!=0)){
// printf("element 2i = %d size = %d \n",ar[2*i],*size);
 if(ar[i]>ar[2*i]){
         swap(&ar[i],&ar[2*i]);
                swap(&bst[i],&bst[2*i]);
                max_heapify(ar,bst,i/2,size);
         }
        else if(ar[i]==ar[2*i]){
         if(bst[2*i]<bst[i]){
                swap(&ar[i],&ar[2*i]);
                  swap(&bst[i],&bst[2*i]);
                    max_heapify(ar,bst,i/2,size);
                 }
      }

} 
}
void deletion(int *ar,int *bst ,int *size,int *delno){
 if(*size>=1){
  delno[0] = ar[1];
  delno[1] = bst[1];
 // printf("inside del job is with arvl time = %d and bst time = %d\n",delno[0],delno[1]);
 // printf("inside del value of *size is %d\n",*size);  
  swap(&ar[*size],&ar[1]);
  swap(&bst[*size],&bst[1]);
  (*size)--; 
 // printf("size in del = %d \n",*size);
  ar = realloc(ar,((*size)+1) * sizeof(int));
  bst = realloc(bst,((*size)+1)*sizeof(int)); 
  builtheap(ar,bst,size); 
 // int j;  
 // for(j=0;j<=*size;j++){
 //  printf("%d  %d \n",ar[j],bst[j]); 
 // }
 }
}
void main()
{
 int no;
 printf("Enter number of jobs\n");
 scanf("%d",&no);
 int arsize = no+1;
 int *ar = malloc(arsize * sizeof(int));
 int *bst = malloc(arsize * sizeof(int));
 int i=1;
 ar[0]=0;
 bst[0]=0;
 while(no--){
  printf("enter arival time and bust time for %d job\n",i);
  scanf("%d",&ar[i]);
  scanf("%d",&bst[i]);
  i++;
 }
 int size = arsize-1; 
 builtheap(ar,bst,&size);
 int j;
 //for(j=0;j<=size;j++){
 // printf("%d  %d \n",ar[j],bst[j]); 
 //}
 //printf("after builheap\n");
 int delno[2];
 for(j=1 ; j<= arsize-1; j++){
  deletion(ar,bst,&size,delno);  
  printf("%d job is with arvl time = %d and bst time = %d\n",j,delno[0],delno[1]);
 }
} 

Wednesday, 3 September 2014

Cicular linked list with pointer used to traverse the link list (as an attempt to reduce complexity at insertion time)

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

struct linklist{
    int pos;
    int max_pos;
    struct node *nodadd;
};
void createnode(struct linklist *linklist1,struct linklist *first);

void delete(struct linklist *linklist1,struct linklist *first){
    int pos;
    int i;
    if(linklist1->nodadd->prev != NULL){
       linklist1->nodadd = linklist1->nodadd->prev;//inorder to prevent changes in showlist made for insert function
    }
    showlist(linklist1,first);
    printf("enter the element position to be deleted \n");
    scanf("%d",&pos);
    if(pos < linklist1->max_pos){
    //printf("the element being pointed is %d and pos is %d and max pos is %d\n", linklist1->nodadd->val,linklist1->pos,linklist1->max_pos);
    if(linklist1->max_pos == 2 && pos ==1){
        struct node *temp;
        temp = linklist1->nodadd;
        linklist1->nodadd = NULL;
        linklist1->pos = 0;
        linklist1->max_pos = 1;
        first->nodadd = NULL;
        //printf("first node is deleted %p %p\n",first,linklist1);
        free(temp);
        //printf("first node is deleted after temp\n");

    }
    else if(linklist1->max_pos == 3 && pos == 2){
        first->nodadd->next->next =NULL;
first->nodadd->next->prev =NULL;
        struct node *temp;
        temp = first->nodadd->next;
        first->nodadd->next = NULL;
        first->nodadd->prev =NULL;
        free(temp);
        linklist1->nodadd = first->nodadd;
        linklist1->pos = 1;
        linklist1->max_pos = 2;
    }
    else if(linklist1->max_pos == 3 && pos == 1){
        first->nodadd = first->nodadd->next;
        first->nodadd->next->next =NULL;
        first->nodadd->next->prev =NULL;
        struct node *temp;
        temp = first->nodadd->next;
        first->nodadd->next = NULL;
        first->nodadd->prev =NULL;
        free(temp);
        linklist1->nodadd = first->nodadd;
        linklist1->pos = 1;
        linklist1->max_pos = 2;
    }
    else if(linklist1->pos == pos){
        linklist1->nodadd =linklist1->nodadd->prev;
        if(linklist1->pos != 1){
                linklist1->pos = linklist1->pos -1;
        }
        else if(pos == 1) {
                    linklist1->pos = linklist1->max_pos-2;
        }
        linklist1->max_pos = linklist1->max_pos-1;
        //printf("the element to be before deleted is %d and prev pos is %d\n", linklist1->nodadd->val,linklist1->pos);
        struct node *temp,*temp2;
        struct linklist *temp1 =(struct linklist *)malloc(sizeof(struct linklist));
        if(pos == 1){
                temp1->nodadd = linklist1->nodadd;
                first->nodadd = first->nodadd->next;
        }
        else {
                temp1->nodadd = first->nodadd;
                for(i = 1 ; i < pos-1; i++){
                        temp1->nodadd = temp1->nodadd->next;
                }
        }
        //printf("the element to be delated is %d and pos is %d\n",temp1->nodadd->next->val,pos);
        temp = temp1->nodadd->next;
        temp2 = temp1->nodadd;
        temp1->nodadd->next = temp1->nodadd->next->next;
        temp1->nodadd->next->prev = temp2;
        temp->prev = NULL;
        temp->next = NULL;
        free(temp);
        //printf("the value of temp1 in delete %d and temp next is %d\n",temp1->nodadd->val,temp1->nodadd->next->val);
    }
    else{
        //struct linklist *temp1;
        //printf("first node is deleted in last else\n");
        struct node *temp,*temp2;
        struct linklist *temp1 =(struct linklist *)malloc(sizeof(struct linklist));
        if(pos == 1){
                //linklist1->nodadd = linklist1->nodadd->prev;
                temp1->nodadd = first->nodadd->prev;
                first->nodadd =first->nodadd->next;
                //printf("the value of last node in delete %d and first node is %d\n",first->nodadd->prev->val,first->nodadd->val);
                //linklist1->pos = linklist1->max_pos-2;
        }
        else {
                temp1->nodadd = first->nodadd;
                for(i = 1 ; i < pos-1; i++){
                    temp1->nodadd = temp1->nodadd->next;
                }
        }
        //printf("the element to be delated is %d and pos is %d\n",temp1->nodadd->next->val,pos);
        temp  = temp1->nodadd->next;
        temp2 = temp1->nodadd;
        temp1->nodadd->next = temp1->nodadd->next->next;
        temp1->nodadd->next->prev = temp2;
        temp->prev = NULL;
        temp->next = NULL;
        free(temp);
        //printf("the value of temp1 in delete %d and temp next is %d\n",temp1->nodadd->val,temp1->nodadd->next->val);
        if(linklist1->pos == 1) {
                linklist1->pos = linklist1->max_pos -2;
        }
        if(linklist1->pos > pos){
                linklist1->pos = linklist1->pos-1;
        }

        linklist1->max_pos = linklist1->max_pos-1;
        //free(temp->nodadd->next);
    }
    if(first->nodadd !=NULL){
      if(linklist1->nodadd->prev!=NULL){
        linklist1->nodadd = linklist1->nodadd->prev;//function showlist balance this move
        //printf("First value is %d\n",first->nodadd->val);
       }
      //printf("first node is deleted this is before last few lines\n");
      showlist(linklist1,first);
    }
    else if(first->nodadd == NULL){
        printf("\nThe List is empty\n");
    }
        createnode(linklist1,first);
    }
    else{
printf("\n Invalid positon plz reenter the position\n");
if(linklist1->nodadd->next != NULL){
linklist1->nodadd = linklist1->nodadd->next;
}
delete(linklist1,first);
    }
}
void createnode(struct linklist *linklist1,struct linklist *first){
    int num;
    int pos;
    int ch;
    printf("\npress 1 to delete ,2 to insert and 3 to exit \n");
    scanf("%d",&ch);
    switch(ch){
    case 1 :
        if(linklist1->nodadd == NULL){
            printf("Sorry no element in list \n");
            createnode(linklist1,first);
        }
 else
            delete(linklist1,first);
        break;
    case 2 :
        /*printf("%d\n",linklist1->pos);
        if(linklist1->nodadd != NULL)
            printf("%d\n",linklist1->nodadd->val);
        printf("%d\n",linklist1->max_pos);
        */
        printf("enter the number and position of num\n");
        scanf("%d %d",&num,&pos);
        if(pos > (linklist1->max_pos) || pos <=0){
            printf("invalid pos \n");
        }
        else if(pos == 1 && linklist1->nodadd == NULL){
            struct node *node1;
            node1 = (struct node*)malloc(sizeof(struct node));
            node1->val = num;
            node1->prev = NULL;
            node1->next = NULL;
            linklist1->pos =1;
            linklist1->max_pos = 2;
            linklist1->nodadd = node1;
            first->nodadd = node1;
        }
        else if(pos == 1 && linklist1->nodadd->next == NULL){
            struct node *node1;
            node1 = (struct node*)malloc(sizeof(struct node));
            node1->val = num;
            node1->next = linklist1->nodadd;
            node1->prev = linklist1->nodadd;
            linklist1->nodadd->next = node1;
            linklist1->nodadd->prev = node1;
            first->nodadd = node1;
            linklist1->pos = 2;
            linklist1->nodadd = node1;
            linklist1->max_pos = linklist1->max_pos+1;
        }
        else if(pos == 2 && linklist1->nodadd->next == NULL){
            struct node  *node1 = (struct node *)malloc(sizeof(struct node));
            node1->prev = linklist1->nodadd;
            node1->next = linklist1->nodadd;
            node1->val  = num;
            printf("the value in node is %d %d",node1->val,num);
            linklist1->nodadd->next = node1;
            linklist1->nodadd->prev = node1;
            linklist1->pos = 1;
            linklist1->max_pos = linklist1->max_pos +1;
            linklist1->nodadd = node1;
        }
        else{
            int traverse;
            int temp = linklist1->pos;
            //printf("\ntemp in create node causing warning is %d\n", temp);
            if(pos >= linklist1->pos){
                if(pos == linklist1->pos){
                    linklist1->nodadd = linklist1->nodadd->prev;
                    if(linklist1->pos !=1);
                        linklist1->pos = (linklist1->pos) - 1;
                }
                int traverse = pos - (linklist1->pos);
                while(traverse > 1){
                  linklist1->nodadd =linklist1->nodadd->next;
                  traverse = traverse -1;
                  linklist1->pos = (linklist1->pos)+1;
                }
                struct node  *node1 = (struct node *)malloc(sizeof(struct node));
                struct node  *temp  = linklist1->nodadd->next;
                linklist1->nodadd->next->prev = node1;//new line added test it once
                linklist1->nodadd->next = node1;
                node1->prev = linklist1->nodadd;
                node1->next = temp;
                node1->val  = num;
                if(pos == 1)
                  first->nodadd = node1;
                if(pos != temp->val)
                  linklist1->pos = (linklist1->pos)+1;
                linklist1->max_pos = linklist1->max_pos +1;
            }
   else{
                 int traverse = linklist1->pos - pos;
                 while(traverse > -1){
                     linklist1->nodadd = linklist1->nodadd->prev;
                     traverse = traverse -1;
                     if(linklist1->pos ==1)
                       linklist1->pos = linklist1->max_pos-1;
                     else
                       linklist1->pos = (linklist1->pos) - 1;
                 }
                struct node  *node1 = (struct node *)malloc(sizeof(struct node));
                struct node  *temp  = linklist1->nodadd->next;
                temp->prev = node1;
                linklist1->nodadd->next = node1;
                node1->prev = linklist1->nodadd;
                node1->next = temp;
                node1->val  = num;
                if(linklist1->pos == linklist1->max_pos -1){
                  linklist1->pos = 1;
                  first->nodadd = node1;
                }
                else{
                  linklist1->pos = (linklist1->pos)+1;
                }
                linklist1->max_pos = linklist1->max_pos +1;
           }
       }
       showlist(linklist1,first);
       createnode(linklist1,first);
       break;
    case 3 :
       exit(0);
    default :
        printf("invaid input\n");
        createnode(linklist1,first);
    }
}

void showlist(struct linklist *linklist1,struct linklist *first){
    if(first->nodadd != NULL){
    int i;
    struct linklist *temp=(struct linklist *)malloc(sizeof(struct linklist)) ;
    temp->nodadd = first->nodadd;
    printf("The list is as follows \n");
    //printf("element at pos first is%d\n",first->nodadd->val);
    //printf("temp val in showlist is %d and linkist1 val is: %d\n",temp->nodadd->val , linklist1->nodadd->val);
    for(i =1; i <=(linklist1->max_pos);i++){
        if(i < linklist1->max_pos){
            printf(" the val is :%d and position is : %d \n", temp->nodadd->val,i);
            if(temp->nodadd->next != NULL)
                temp->nodadd = temp->nodadd->next;
        }
        if(linklist1->nodadd->next!=NULL)  //after insertion of new element to make t point at same element
            linklist1->nodadd = linklist1->nodadd->next;
    }
    //printf("\nin showlist last the element being pointed is %d and pos is %d and first is %d\n",linklist1->nodadd->val , linklist1->pos,first->nodadd->val);
}
}
void main(){
    int val;
    int num;
    int i;
    struct linklist *linklist1;
    linklist1 = (struct linklist *)malloc(sizeof(struct linklist));
    struct linklist *first = (struct linklist *)malloc(sizeof(struct linklist));
    linklist1->pos = 0;
    linklist1->max_pos =1;
    linklist1->nodadd = NULL;
//    printf("enter 1 to insert\n");
//    scanf("%d",&val);
    printf("%d\n",linklist1->pos);
            //showlist(linklist1);
            createnode(linklist1,first);
/*          case 2:
            showlist();
            printf("enter the num pos to be deleated");
            scanf("%d",&pos);
            deletenode(pos);
        */
}

Wednesday, 27 August 2014

Read a text file containing several occurrences of student and replace the word student with graduate.

Read a text file containing several occurrences of student and replace the word student with graduate.

#include<stdio.h>
#include<string.h>
int main(){
    FILE *fp;
    char ch;
    char str[1000];
    int i=0;
    FILE *fp1;
    fp = fopen("test.txt","r");
    fp1= fopen("test1.txt","w+");
    ch=fgetc(fp);
    while(ch!=EOF){   
        if(ch == '\n'){           
            i++;           
            str[i] = '\0';
            i = 0;
            char *a = NULL;
            char b[9]="graduate";
            a = strtok(str," \n");
            while(a!=NULL){
                if(strncmp(a,"student",8) == 0){
                    fprintf(fp1,"%s ",b);       
                }
                else{
                    fprintf(fp1,"%s ",a);   
                }
            a = strtok(NULL," \n");
            }
            fprintf(fp1,"%c",'\n');
        }
        else{
            str[i] = ch;
            i++;
        }           
    ch =fgetc(fp);
    }
    fclose(fp);
    fclose(fp1);
    return 0;
}