My Profile

Ahmednagar, Maharashtra, India
I, Das ShrikKrishna J. MCA III IMSCD&R, Ahmednagar.

Saturday, 30 July 2011

Program of sparse matrix for 3-tuple method using array

/* Program of sparse matrix for 3-tuple method using array*/
#include<stdio.h>
#define srow 50
#define mrow 20
#define mcolumn 20
main()
{
int mat[mrow][mcolumn],sparse[srow][3];
int i,j,nzero=0,mr,mc,sr,s;
printf("Enter number of rows : ");
scanf("%d",&mr);
printf("Enter number of columns : ");
scanf("%d",&mc);
for(i=0;i<mr;i++)
for(j=0;j<mc;j++)
{
printf("Enter element for row %d,column %d : ",i+1,j+1);
scanf("%d",&mat[i][j]);
}
printf("Entered matrix is : \n");
for(i=0;i<mr;i++)
{
for(j=0;j<mc;j++)
{
printf("%6d",mat[i][j]);
if(mat[i][j]!=0)
nzero++;
}
printf("\n");
}
sr=nzero+1;
sparse[0][0]=mr;
sparse[0][1]=mc;
sparse[0][2]=nzero;
s=1;
for(i=0;i<mr;i++)
for(j=0;j<mc;j++)
{
if(mat[i][j]!=0)
{
sparse[s][0]=i+1;
sparse[s][1]=j+1;
sparse[s][2]=mat [i][j];
s++;
}
}
printf("Sparse matrix is :\n");
for(i=0;i<sr;i++)
{
for(j=0;j<3;j++)
printf("%5d",sparse[i][j]);
printf("\n");
}
}/*End of                main()*/

Program of reversing a string using stack

/* Program of reversing a string using stack */
#include<stdio.h>
#define MAX 20
#include<string.h>
int top = -1;
char stack[MAX];
char pop();
push(char);
main()
{
char str[20];
int i;
printf("Enter the string : " );
gets(str);
/*Push characters of the string str on the stack */
for(i=0;i<strlen(str);i++)
push(str[i]);
/*Pop characters from the stack and store in string str */
for(i=0;i<strlen(str);i++)
str[i]=pop();
printf("Reversed string is : ");
puts(str);
}/*End of main()*/
push(char item)
{
if(top == (MAX-1))
printf("Stack Overflow\n");
else
stack[++top] =item;
}/*End of push()*/
char pop()
{
if(top == -1)
printf("Stack Underflow\n");
else
return stack[top--];
}/*End of           pop()*/

Program of queue using linked list

/* Program of queue using linked list*/
# include<stdio.h>
# include<malloc.h>
struct node
{
int info;
struct node *link;
}*front=NULL,*rear=NULL;
main()
{
int choice;
while(1)
{ printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
exit(1);
default :
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of main()*/
insert()
{
struct node *tmp;
int added_item;
tmp = (struct node *)malloc(sizeof(struct node));
printf("Input the element for adding in queue : ");
scanf("%d",&added_item);
tmp->info = added_item;
tmp->link=NULL;
if(front==NULL) /*If Queue is empty*/
front=tmp;
else
rear->link=tmp;
rear=tmp;
}/*End of insert()*/
del()
{
struct node *tmp;
if(front == NULL)
printf("Queue Underflow\n");
else
{
tmp=front;
printf("Deleted element is %d\n",tmp->info);
front=front->link;
free(tmp);
}
}/*End of del()*/
display()
{
struct node *ptr;
ptr = front;
if(front == NULL)
printf("Queue is empty\n");
else
{
printf("Queue elements :\n");
while(ptr != NULL)
{
printf("%d ",ptr->info);
ptr = ptr->link;
}
printf("\n");
}/*End of else*/
}/*End of           display()*/

Program of queue using array

/*Program of queue using array*/
# include<stdio.h>
# define MAX 5
int queue_arr[MAX];
int rear = -1;
int front = -1;
main()
{
int choice;
while(1)
{
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 :
insert();
break;
case 2 :
del();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of main()*/
insert()
{
int added_item;
if (rear==MAX-1)
printf("Queue Overflow\n");
else
{
if (front==-1) /*If queue is initially empty */
front=0;
printf("Input the element for adding in queue : ");
scanf("%d", &added_item);
rear=rear+1;
queue_arr[rear] = added_item ;
}
}/*End of insert()*/
del()
{
if (front == -1 || front > rear)
{
printf("Queue Underflow\n");
return ;
}
else
{
printf("Element      deleted from queue is : %d\n", queue_arr[front]);
front=front+1;
}
}/*End of del() */
display()
{
int i;
if (front == -1)
printf("Queue is empty\n");
else
{
printf("Queue is :\n");
for(i=front;i<= rear;i++)
printf("%d ",queue_arr[i]);
printf("\n");
}
}/*End of  display() */

Program of queue using circular linked list

/* Program of queue using circular linked list*/
# include <stdio.h>
# include <malloc.h>
struct node
{
int info;
struct node *link;
}*rear=NULL;
main()
{
int choice;
while(1)
{
printf("1.Insert \n");
printf("2.Delete \n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
exit();
default:
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of main()*/
insert()
{
int num;
struct node *q,*tmp;
printf("Enter the element for insertion : ");
scanf("%d",&num);
tmp= malloc(sizeof(struct node));
tmp->info = num;
if(rear == NULL) /*If queue is empty */
{
rear = tmp;
tmp->link = rear;
}
else
{
tmp->link = rear->link;
rear->link = tmp;
rear = tmp;
}
}/*End of insert()*/
del()
{
struct node *tmp,*q;
if(rear==NULL)
{
printf("Queue underflow\n");
return;
}
if( rear->link == rear ) /*If only one element*/
{
tmp = rear;
rear = NULL;
free(tmp);
return;
}
q=rear->link;
tmp=q;
rear->link = q->link;
printf("Deleted element is %d\n",tmp->info);
free(tmp);
}/*End of del()*/
display()
{
struct node *q;
if(rear == NULL)
{
printf("Queue is empty\n");
return;
}
q = rear->link;
printf("Queue is :\n");
while(q != rear)
{
printf("%d ", q->info);
q = q->link;
}
printf("%d\n",rear->info);
}

Program of priority queue using linked list

/* Program of priority queue using linked list*/
# include<stdio.h>
# include<malloc.h>
struct node
{
int priority;
int info;
struct node *link;
}*front = NULL;
main()
{
int choice;
while(1)
{
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
exit(1);
default :
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of main()*/
insert()
{
struct node *tmp,*q;
int added_item,item_priority;
tmp = (struct node *)malloc(sizeof(struct node));
printf("Input the item value to be added in the queue : ");
scanf("%d",&added_item);
printf("Enter its priority : ");
scanf("%d",&item_priority);
tmp->info = added_item;
tmp->priority = item_priority;
/*Queue is empty or item to be added has priority more than first item*/
if( front == NULL || item_priority < front->priority )
{
tmp->link = front;
front = tmp;
}
else
{
q = front;
while( q->link != NULL && q->link->priority <= item_priority )
q=q->link;
tmp->link = q->link;
q->link = tmp;
}/*End of else*/
}/*End of insert()*/
del()
{
struct node *tmp;
if(front == NULL)
printf("Queue Underflow\n");
else
{
tmp = front;
printf("Deleted item is %d\n",tmp->info);
front = front->link;
free(tmp);
}
}/*End of del()*/
display()
{
struct node *ptr;
ptr = front;
if(front == NULL)
printf("Queue is empty\n");
else
{ printf("Queue is :\n");
printf("Priority Item\n");
while(ptr != NULL)
{
printf("%5d %5d\n",ptr->priority,ptr->info);
ptr = ptr->link;
}
}
}

Program for conversion of infix to postfix and evaluation of postfix

/* Program for conversion of infix to postfix and evaluation of postfix.
It will take only single digit in expression */
#include<stdio.h>
#include<string.h>
#include<math.h>
#define Blank ' '
#define Tab '\t'
#define MAX 50
long int pop ();
long int eval_post();
char infix[MAX], postfix[MAX];
long int stack[MAX];
int top;
main()
{
long int value;
char choice='y';
while(choice == 'y')
{
top = 0;
printf("Enter infix : ");
fflush(stdin);
gets(infix);
infix_to_postfix();
printf("Postfix : %s\n",postfix);
value=eval_post();
printf("Value of expression : %ld\n",value);
printf("Want to continue(y/n) : ");
scanf("%c",&choice);
}
}/*End of main()*/
infix_to_postfix()
{
int i,p=0,type,precedence,len;
char next ;
stack[top]='#';
len=strlen(infix);
infix[len]='#';
for(i=0; infix[i]!='#';i++)
{
if( !white_space(infix[i]))
{
switch(infix[i])
{
case '(':
push(infix[i]);
break;
case ')':
while((next = pop()) != '(')
postfix[p++] = next;
break;
case '+':
case '-':
case '*':
case '/':
case '%':
case '^':
precedence = prec(infix[i]);
while(stack[top]!='#' && precedence<= prec(stack[top]))
postfix[p++] = pop();
push(infix[i]);
break;
default: /*if an operand comes */
postfix[p++] = infix[i];
}/*End of switch */
}/*End of if */
}/*End of for */
while(stack[top]!='#')
postfix[p++] = pop();
postfix[p] = '\0' ; /*End postfix with'\0' to make it a string*/
}/*End of infix_to_postfix()*/
/* This function returns the precedence of the operator */
prec(char symbol )
{
switch(symbol)
{
case '(':
return 0;
case '+':
case '-':
return 1;
case '*':
case '/':
case '%':
return 2;
case '^':
return 3;
}/*End of switch*/
}/*End of prec()*/
push(long int symbol)
{
if(top > MAX)
{
printf("Stack overflow\n");
exit(1);
}
else
{
top=top+1;
stack[top] = symbol;
}
}/*End of push()*/
long int pop()
{
if (top == -1 )
{
printf("Stack underflow \n");
exit(2);
}
else
return (stack[top--]);
}/*End of pop()*/
white_space(char symbol)
{
if( symbol == Blank || symbol == Tab || symbol == '\0')
return 1;
else
return 0;
}/*End of white_space()*/
long int eval_post()
{
long int a,b,temp,result,len;
int i;
len=strlen(postfix);
postfix[len]='#';
for(i=0;postfix[i]!='#';i++)
{
if(postfix[i]<='9' && postfix[i]>='0')
push( postfix[i]-48 );
else
{
a=pop();
b=pop();
switch(postfix[i])
{
case '+':
temp=b+a; break;
case '-':
temp=b-a;break;
case '*':
temp=b*a;break;
case '/':
temp=b/a;break;
case '%':
temp=b%a;break;
case '^':
temp=pow(b,a);
}/*End of switch */
push(temp);
}/*End of else*/
}/*End of for */
result=pop();
return result;
}/*End of eval_post */

Program to check nesting of parentheses using stack

/* Program to check nesting of parentheses using stack */
#include<stdio.h>
#define MAX 20
#define true 1
#define false 0
int top = -1;
int stack[MAX];
push(char);
char pop();
main()
{
char exp[MAX],temp;
int i,valid=true;
printf("Enter an algebraic expression : ");
gets(exp);
for(i=0;i<strlen(exp);i++)
{
if(exp[i]=='(' || exp[i]=='{' || exp[i]=='[')
push( exp[i] );
if(exp[i]==')' || exp[i]=='}' || exp[i]==']')
if(top == -1) /* stack empty */
valid=false;
else
{
temp=pop();
if( exp[i]==')' && (temp=='{' || temp=='[') )
valid=false;
if( exp[i]=='}' && (temp=='(' || temp=='[') )
valid=false;
if( exp[i]==']' && (temp=='(' || temp=='{') )
valid=false;
}/*End of else */
}/*End of for*/
if(top>=0) /*stack not empty*/
valid=false;
if( valid==true )
printf("Valid expression\n");
else
printf("Invalid expression\n");
}/*End of main()*/
push(char item)
{
if(top == (MAX-1))
printf("Stack Overflow\n");
else
{
top=top+1;
stack[top] = item;
}
}/*End of push()*/
char pop()
{
if(top == -1)
printf("Stack Underflow\n");
else
return(stack[top--]);
}

Program of input and output restricted dequeue using array

/* Program of input and output restricted dequeue using array*/
# include<stdio.h>
# define MAX 5
int deque_arr[MAX];
int left = -1;
int right = -1;
main()
{
int choice;
printf("1.Input restricted dequeue\n");
printf("2.Output restricted dequeue\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 :
input_que();
break;
case 2:
output_que();
break;
default:
printf("Wrong choice\n");
}/*End of switch*/
}/*End of main()*/
input_que()
{
int choice;
while(1)
{
printf("1.Insert at right\n");
printf("2.Delete from left\n");
printf("3.Delete from right\n");
printf("4.Display\n");
printf("5.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert_right();
break;
case 2:
delete_left();
break;
case 3:
delete_right();
break;
case 4:
display_queue();
break;
case 5:
exit();
default:
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of input_que() */
output_que()
{
int choice;
while(1)
{
printf("1.Insert at right\n");
printf("2.Insert at left\n");
printf("3.Delete from left\n");
printf("4.Display\n");
printf("5.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert_right();
break;
case 2:
insert_left();
break;
case 3:
delete_left();
break;
case 4:
display_queue();
break;
case 5:
exit();
default:
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of output_que() */
insert_right()
{
int added_item;
if((left == 0 && right == MAX-1) || (left == right+1))
{
printf("Queue Overflow\n");
return;
}
if (left == -1) /* if queue is initially empty */
{
left = 0;
right = 0;
}
else
if(right == MAX-1) /*right is at last position of queue */
right = 0;
else
right = right+1;
printf("Input the element for adding in queue : ");
scanf("%d", &added_item);
deque_arr[right] = added_item ;
}/*End of insert_right()*/
insert_left()
{
int added_item;
if((left == 0 && right == MAX-1) || (left == right+1))
{
printf("Queue Overflow \n");
return;
}
if (left == -1)/*If queue is initially empty*/
{
left = 0;
right = 0;
}
else
if(left== 0)
left=MAX-1;
else
left=left-1;
printf("Input the element for adding in queue : ");
scanf("%d", &added_item);
deque_arr[left] = added_item ;
}/*End of insert_left()*/
delete_left()
{
if (left == -1)
{
printf("Queue Underflow\n");
return ;
}
printf("Element deleted from queue is : %d\n",deque_arr[left]);
if(left == right) /*Queue has only one element */
{
left = -1;
right=-1;
}
else
if(left == MAX-1)
left = 0;
else
left = left+1;
}/*End of delete_left()*/
delete_right()
{
if (left == -1)
{
printf("Queue Underflow\n");
return ;
}
printf("Element deleted from queue is : %d\n",deque_arr[right]);
if(left == right) /*queue has only one element*/
{
left = -1;
right=-1;
}
else
if(right == 0)
right=MAX-1;
else
right=right-1;
}/*End of delete_right() */
display_queue()
{
int front_pos = left,rear_pos = right;
if(left == -1)
{
printf("Queue is empty\n");
return;
}
printf("Queue elements :\n");
if( front_pos <= rear_pos )
{
while(front_pos <= rear_pos)
{
printf("%d ",deque_arr[front_pos]);
front_pos++;
}
}
else
{
while(front_pos <= MAX-1)
{
printf("%d ",deque_arr[front_pos]);
front_pos++;
}
front_pos = 0;
while(front_pos <= rear_pos)
{
printf("%d ",deque_arr[front_pos]);
front_pos++;
}
}/*End of else */
printf("\n");
}

Program of circular queue using array

/* Program of circular queue using array*/
# include<stdio.h>
# define MAX 5
int cqueue_arr[MAX];
int front = -1;
int rear = -1;
main()
{
int choice;
while(1)
{
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 :
insert();
break;
case 2 :
del();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while */
}/*End of main()*/
insert()
{
int added_item;
if((front == 0 && rear == MAX-1) || (front == rear+1))
{
printf("Queue Overflow \n");
return;
}
if (front == -1) /*If queue is empty */
{
front = 0;
rear = 0;
}
else
if(rear == MAX-1)/*rear is at last position of queue */
rear = 0;
else
rear = rear+1;
printf("Input the element for insertion in queue : ");
scanf("%d", &added_item);
cqueue_arr[rear] = added_item ;
}/*End of insert()*/
del()
{
if (front == -1)
{
printf("Queue Underflow\n");
return ;
}
printf("Element deleted from queue is : %d\n",cqueue_arr[front]);
if(front == rear) /* queue has only one element */
{
front = -1;
rear=-1;
}
else
if(front == MAX-1)
front = 0;
else
front = front+1;
}/*End of del() */
display()
{
int front_pos = front,rear_pos = rear;
if(front == -1)
{
printf("Queue is empty\n");
return;
}
printf("Queue elements :\n");
if( front_pos <= rear_pos )
while(front_pos <= rear_pos)
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
else
{
while(front_pos <= MAX-1)
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
front_pos = 0;
while(front_pos <= rear_pos)
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
}/*End of else */
printf("\n");
}

Program of sorting using shell sort

/* Program of sorting using shell sort */
#include <stdio.h>
#define MAX 20
main()
{
int arr[MAX], i,j,k,n,incr;
printf("Enter the number of elements : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter element %d : ",i+1);
scanf("%d",&arr[i]);
}
printf("Unsorted list is :\n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\nEnter maximum increment (odd value) : ");
scanf("%d",&incr);
/*Shell sort*/
while(incr>=1)
{
for(j=incr;j<n;j++)
{
k=arr[j];
for(i = j-incr; i >= 0 && k < arr[i]; i = i-incr)
arr[i+incr]=arr[i];
arr[i+incr]=k;
}
printf("Increment=%d \n",incr);
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
incr=incr-2; /*Decrease the increment*/
}/*End of while*/
printf("Sorted list is :\n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}

Program of sorting using selection sort

/*Program of sorting using selection sort*/
#include <stdio.h>
#define MAX 20
main()
{
int arr[MAX], i,j,k,n,temp,smallest;
printf("Enter the number of elements : ");
scanf("%d",&n);
for (i = 0; i < n; i++)
{
printf("Enter element %d : ",i+1);
scanf("%d", &arr[i]);
}
printf("Unsorted list is : \n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
/*Selection sort*/
for(i = 0; i< n - 1 ; i++)
{
/*Find the smallest element*/
smallest = i;
for(k = i + 1; k < n ; k++)
{
if(arr[smallest] > arr[k])
smallest = k ;
}
if( i != smallest )
{
temp = arr [i];
arr[i] = arr[smallest];
arr[smallest] = temp ;
}
printf("After Pass %d elements are : ",i+1);
for (j = 0; j < n; j++)
printf("%d ", arr[j]);
printf("\n");
}/*End of for*/
printf("Sorted list is : \n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}

Program of sorting using radix sort

/*Program of sorting using radix sort*/
# include<stdio.h>
# include<malloc.h>
struct node
{
int info ;
struct node *link;
}*start=NULL;
main()
{
struct node *tmp,*q;
int i,n,item;
printf("Enter the number of elements in the list : ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("Enter element %d : ",i+1);
scanf("%d",&item);
/* Inserting elements in the linked list */
tmp= malloc(sizeof(struct node));
tmp->info=item;
tmp->link=NULL;
if(start==NULL) /* Inserting first element */
start=tmp;
else
{
q=start;
while(q->link!=NULL)
q=q->link;
q->link=tmp;
}
}/*End of for*/
printf("Unsorted list is :\n");
display();
radix_sort();
printf("Sorted list is :\n");
display ();
}/*End of main()*/
display()
{
struct node *p=start;
while( p !=NULL)
{
printf("%d ", p->info);
p= p->link;
}
printf("\n");
}/*End of display()*/
radix_sort()
{
int i,k,dig,maxdig,mindig,least_sig,most_sig;
struct node *p, *rear[10], *front[10];
least_sig=1;
most_sig=large_dig(start);
for(k = least_sig; k <= most_sig ; k++)
{
printf("PASS %d : Examining %dth digit from right ",k,k);
for(i = 0 ; i <= 9 ; i++)
{
rear[i] = NULL;
front[i] = NULL ;
}
maxdig=0;
mindig=9;
p = start ;
while( p != NULL)
{
/*Find kth digit in the number*/
dig = digit(p->info, k);
if(dig>maxdig)
maxdig=dig;
if(dig<mindig)
mindig=dig;
/*Add the number to queue of dig*/
if(front[dig] == NULL)
front[dig] = p ;
else
rear[dig]->link = p ;
rear[dig] = p ;
p=p->link;/*Go to next number in the list*/
}/*End while */
/* maxdig and mindig are the maximum amd minimum
digits of the kth digits of all the numbers*/
printf("mindig=%d maxdig=%d\n",mindig,maxdig);
/*Join all the queues to form the new linked list*/
start=front[mindig];
for(i=mindig;i<maxdig;i++)
{
if(rear[i+1]!=NULL)
rear[i]->link=front[i+1];
else
rear[i+1]=rear[i];
}
rear[maxdig]->link=NULL;
printf("New list : ");
display();
}/* End for */
}/*End of radix_sort*/
/* This function finds number of digits in the largest element of the list */
int large_dig()
{
struct node *p=start ;
int large = 0,ndig = 0 ;
while(p != NULL)
{
if(p ->info > large)
large = p->info;
p = p->link ;
}
printf("Largest Element is %d , ",large);
while(large != 0)
{
ndig++;
large = large/10 ;
}
printf("Number of digits in it are %d\n",ndig);
return(ndig);
} /*End of large_dig()*/
/*This function returns kth digit of a number*/
int digit(int number, int k)
{
int digit, i ;
for(i = 1 ; i <=k ; i++)
{
digit = number % 10 ;
number = number /10 ;
}
return(digit);
}/*End of digit()*/

Program of sorting using quick sort through recursion

/*Program of sorting using quick sort through recursion*/
#include<stdio.h>
#define MAX 30
enum bool { FALSE,TRUE };
main()
{
int array[MAX],n,i;
printf("Enter the number of elements : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter element %d : ",i+1);
scanf("%d",&array[i]);
}
printf("Unsorted list is :\n");
display(array,0,n-1);
printf("\n");
quick(array,0,n-1);
printf("Sorted list is :\n");
display(array,0,n-1);
printf("\n");
}/*End of main() */
quick(int arr[],int low,int up)
{
int piv,temp,left,right;
enum bool pivot_placed=FALSE;
left=low;
right=up;
piv=low; /*Take the first element of sublist as piv */
if(low>=up)
return;
printf("Sublist : ");
display(arr,low,up);
/*Loop till pivot is placed at proper place in the sublist*/
while(pivot_placed==FALSE)
{
/*Compare from right to left */
while( arr[piv]<=arr[right] && piv!=right )
right=right-1;
if( piv==right )
pivot_placed=TRUE;
if( arr[piv] > arr[right] )
{
temp=arr[piv];
arr[piv]=arr[right];
arr[right]=temp;
piv=right;
}
/*Compare from left to right */
while( arr[piv]>=arr[left] && left!=piv )
left=left+1;
if(piv==left)
pivot_placed=TRUE;
if( arr[piv] < arr[left] )
{
temp=arr[piv];
arr[piv]=arr[left];
arr[left]=temp;
piv=left;
}
}/*End of while */
printf("-> Pivot Placed is %d -> ",arr[piv]);
display(arr,low,up);
printf("\n");
quick(arr,low,piv-1);
quick(arr,piv+1,up);
}/*End of quick()*/
display(int arr[],int low,int up)
{
int i;
for(i=low;i<=up;i++)
printf("%d ",arr[i]);
}

Program of sorting using merge sort through recursion

/* Program of sorting using merge sort through recursion*/
#include<stdio.h>
#define MAX 20
int array[MAX];
main()
{
int i,n;
printf("Enter the number of elements : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter element %d : ",i+1);
scanf("%d",&array[i]);
}
printf("Unsorted list is :\n");
for( i = 0 ; i<n ; i++)
printf("%d ", array[i]);
merge_sort( 0, n-1);
printf("\nSorted list is :\n");
for( i = 0 ; i<n ; i++)
printf("%d ", array[i]);
printf("\n");
}/*End of main()*/
merge_sort( int low, int high )
{
int mid;
if( low != high )
{
mid = (low+high)/2;
merge_sort( low , mid );
merge_sort( mid+1, high );
merge( low, mid, high );
}
}/*End of merge_sort*/
merge( int low, int mid, int high )
{
int temp[MAX];
int i = low;
int j = mid +1 ;
int k = low ;
while( (i <= mid) && (j <=high) )
{
if(array[i] <= array[j])
temp[k++] = array[i++] ;
else
temp[k++] = array[j++] ;
}/*End of while*/
while( i <= mid )
temp[k++]=array[i++];
while( j <= high )
temp[k++]=array[j++];
for(i= low; i <= high ; i++)
array[i]=temp[i];
}/*End of merge()*/

Program of sorting using merge sort without recursion

/* Program of sorting using merge sort without recursion*/
#include<stdio.h>
#define MAX 30
main()
{
int arr[MAX],temp[MAX],i,j,k,n,size,l1,h1,l2,h2;
printf("Enter the number of elements : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter element %d : ",i+1);
scanf("%d",&arr[i]);
}
printf("Unsorted list is : ");
for( i = 0 ; i<n ; i++)
printf("%d ", arr[i]);
/*l1 lower bound of first pair and so on*/
for(size=1; size < n; size=size*2 )
{
l1=0;
k=0; /*Index for temp array*/
while( l1+size < n)
{
h1=l1+size-1;
l2=h1+1;
h2=l2+size-1;
if( h2>=n ) /* h2 exceeds the limlt of arr */
h2=n-1;
/*Merge the two pairs with lower limits l1 and l2*/
i=l1;
j=l2;
while(i<=h1 && j<=h2 )
{
if( arr[i] <= arr[j] )
temp[k++]=arr[i++];
else
temp[k++]=arr[j++];
}
while(i<=h1)
temp[k++]=arr[i++];
while(j<=h2)
temp[k++]=arr[j++];
/**Merging completed**/
l1=h2+1; /*Take the next two pairs for merging */
}/*End of while*/
for(i=l1; k<n; i++) /*any pair left */
temp[k++]=arr[i];
for(i=0;i<n;i++)
arr[i]=temp[i];
printf("\nSize=%d \nElements are : ",size);
for( i = 0 ; i<n ; i++)
printf("%d ", arr[i]);
}/*End of for loop */
printf("Sorted list is :\n");
for( i = 0 ; i<n ; i++)
printf("%d ", arr[i]);
printf("\n");
}/*End of main()*/
 
 
 
 
 

Program of merging two sorted arrays into a third sorted array

/* Program of merging two sorted arrays into a third sorted array*/
#include<stdio.h>
main()
{
int arr1[20],arr2[20],arr3[40];
int i,j,k;
int max1,max2;
printf("Enter the number of elements in list1 : ");
scanf("%d",&max1);
printf("Take the elements in sorted order :\n");
for(i=0;i<max1;i++)
{
printf("Enter element %d : ",i+1);
scanf("%d",&arr1[i]);
}
printf("Enter the number of elements in list2 : ");
scanf("%d",&max2);
printf("Take the elements in sorted order :\n");
for(i=0;i<max2;i++)
{
printf("Enter element %d : ",i+1);
scanf("%d",&arr2[i]);
}
/* Merging */
i=0; /*Index for first array*/
j=0; /*Index for second array*/
k=0; /*Index for merged array*/
while( (i < max1) && (j < max2) )
{
if( arr1[i] < arr2[j] )
arr3[k++]=arr1[i++];
else
arr3[k++]=arr2[j++];
}/*End of while*/
/*Put remaining elements of arr1 into arr3*/
while( i < max1 )
arr3[k++]=arr1[i++];
/*Put remaining elements of arr2 into arr3*/
while( j < max2 )
arr3[k++]=arr2[j++];
/*Merging completed*/
printf("List 1 : ");
for(i=0;i<max1;i++)
printf("%d ",arr1[i]);
printf("\nList 2 : ");
for(i=0;i<max2;i++)
printf("%d ",arr2[i]);
printf("\nMerged list : ");
for(i=0;i<max1+max2;i++)
printf("%d ",arr3[i]);
printf("\n");
}/*End of main()*/

Program of sorting using insertion sort

/* Program of sorting using insertion sort */
#include <stdio.h>
#define MAX 20
main()
{
int arr[MAX],i,j,k,n;
printf("Enter the number of elements : ");
scanf("%d",&n);
for (i = 0; i < n; i++)
{
printf("Enter element %d : ",i+1);
scanf("%d", &arr[i]);
}
printf("Unsorted list is :\n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
/*Insertion sort*/
for(j=1;j<n;j++)
{
k=arr[j]; /*k is to be inserted at proper place*/
for(i=j-1;i>=0 && k<arr[i];i--)
arr[i+1]=arr[i];
arr[i+1]=k;
printf("Pass %d, Element inserted in proper place: %d\n",j,k);
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
printf("Sorted list is :\n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}/*End of main()*/
 
 
 

Program of sorting through heapsort

/* Program of sorting through heapsort*/
# include <stdio.h>
int arr[20],n;
main()
{
int i;
printf("Enter number of elements : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter element %d : ",i+1);
scanf("%d",&arr[i]);
}
printf("Entered list is :\n");
display();
create_heap();
printf("Heap is :\n");
display();
heap_sort();
printf("Sorted list is :\n");
display();
}/*End of main()*/
display()
{ int i;
for(i=0;i<n;i++)
printf("%d ",arr[i]);
printf("\n");
}/*End of display()*/
create_heap()
{
int i;
for(i=0;i<n;i++)
insert(arr[i],i);
}/*End of create_heap()*/
insert(int num,int loc)
{
int par;
while(loc>0)
{
par=(loc-1)/2;
if(num<=arr[par])
{
arr[loc]=num;
return;
}
arr[loc]=arr[par];
loc=par;
}/*End of while*/
arr[0]=num;
}/*End of insert()*/
 
heap_sort()
{
int last;
for(last=n-1; last>0; last--)
del_root(last);
}/*End of del_root*/
del_root(int last)
{
int left,right,i,temp;
i=0; /*Since every time we have to replace root with last*/
/*Exchange last element with the root */
temp=arr[i];
arr[i]=arr[last];
arr[last]=temp;
left=2*i+1; /*left child of root*/
right=2*i+2;/*right child of root*/
while( right < last)
{
if( arr[i]>=arr[left] && arr[i]>=arr[right] )
return;
if( arr[right]<=arr[left] )
{
temp=arr[i];
arr[i]=arr[left];
arr[left]=temp;
i=left;
}
else
{
temp=arr[i];
arr[i]=arr[right];
arr[right]=temp;
i=right;
}
left=2*i+1;
right=2*i+2;
}/*End of while*/
if( left==last-1 && arr[i]<arr[left] )/*right==last*/
{
temp=arr[i];
arr[i]=arr[left];
arr[left]=temp;
}
}/*End of del_root*/
 
 

Program of sorting using bubble sort(DS using C)

/* Program of sorting            using bubble sort */
#include <stdio.h>
#define MAX 20
main()
{
int arr[MAX],i,j,k,temp,n,xchanges;
printf("Enter the number of elements : ");
scanf("%d",&n);
for (i = 0; i < n; i++)
{
printf("Enter element %d : ",i+1);
scanf("%d",&arr[i]);
}
printf("Unsorted list is :\n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
/* Bubble sort*/
for (i = 0; i < n-1 ; i++)
{
xchanges=0;
for (j = 0; j <n-1-i; j++)
{
if (arr[j] > arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
xchanges++;
}/*End of if*/
}/*End of inner for loop*/
if(xchanges==0) /*If list   is sorted*/
break;
printf("After Pass %d elements are : ",i+1);
for (k = 0; k < n; k++)
printf("%d ", arr[k]);
printf("\n");
}/*End of outer for loop*/
printf("Sorted list is :\n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}/*End of main()*/
 

Program of sorting using address calculation sort

/*Program of sorting using address calculation sort*/
#include<stdio.h>
#include<malloc.h>
#define MAX 20
struct node
{
int info ;
struct node *link;
};
struct node *head[10];
int n,i,arr[MAX];
main()
{
int i;
printf("Enter the               number of elements in the list : ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("Enter element %d : ",i+1);
scanf("%d",&arr[i]);
}/*End of for */
printf("Unsorted list is :\n");
for(i=0;i<n;i++)
printf("%d ",arr[i]);
printf("\n");
addr_sort();
printf("Sorted list is :\n");
for(i=0;i<n;i++)
printf("%d ",arr[i]);
printf("\n");
}/*End of main()*/
addr_sort()
{
int i,k,dig;
struct node *p;
int addr;
k=large_dig();
for(i=0;i<=9;i++)
head[i]=NULL;
for(i=0;i<n;i++)
{
addr=hash_fn( arr[i],k );
insert(arr[i],addr);
}
for(i=0; i<=9 ; i++)
{
printf("head(%d) -> ",i);
p=head[i];
while(p!=NULL)
{
printf("%d ",p->info);
p=p->link;
}
printf("\n");
}
/*Taking the elements of linked lists in array*/
i=0;
for(k=0;k<=9;k++)
{
p=head[k];
while(p!=NULL)
{
arr[i++]=p->info;
p=p->link;
}
}
}/*End of addr_sort()*/
/*Inserts                the number in sorted linked list*/
insert(int num,int addr)
{
struct node *q,*tmp;
tmp= malloc(sizeof(struct node));
tmp->info=num;
/*list empty or item to be added in begining */
if(head[addr] == NULL || num < head[addr]->info)
{
tmp->link=head[addr];
head[addr]=tmp;
return;
}
else
{
q=head[addr];
while(q->link != NULL && q->link->info < num)
q=q->link;
tmp->link=q->link;
q->link=tmp;
}
}/*End of insert()*/
/* Finds number      of digits in the largest element of the list */
int large_dig()
{
int large = 0,ndig = 0 ;
for(i=0;i<n;i++)
{
if(arr[i] > large)
large = arr[i];
}
printf("Largest Element is %d , ",large);
while(large != 0)
{
ndig++;
large = large/10 ;
}
printf("Number of digits in it are %d\n",ndig);
return(ndig);
} /*End of large_dig()*/
hash_fn(int number,int k)
{
/*Find kth digit of the number*/
int digit,addr,i;
for(i = 1 ; i <=k ; i++)
{
digit = number % 10 ;
number = number /10 ;
}
addr=digit;
return(addr);
}/*End of hash_fn()*/
/*
hash_fn(int number,int k)
{
int addr,i,large=0;
float tmp;
for(i=0;i<n;i++)
{
if(arr[i] > large)
large = arr[i];
}
tmp=(float)number/large;
addr=tmp*9;
return(addr);
}/*End of hash_fn()*/
 
 
 

Program of sorted linked list

/* Program of sorted linked list*/
# include <stdio.h>
# include <malloc.h>
struct node
{
int info;
struct node *link;
}*start;
main()
{
int choice,n,m,i;
start=NULL;
while(1)
{
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display \n");
printf("4.Exit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the element to be inserted : ");
scanf("%d",&m);
insert(m);
break;
case 2:
printf("Enter the element to be deleted : ");
scanf("%d",&m);
del(m);
break;
case 3:
display();
break;
case 4:
exit();
default:
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
} /*end of main */
insert(int num)
{
struct node *q,*tmp;
tmp= malloc(sizeof(struct node));
tmp->info=num;
/*list empty or item to be added in begining */
if(start == NULL || num < start->info)
{
tmp->link=start;
start=tmp;
return;
}
else
{
q=start;
while(q->link != NULL && q->link->info < num)
q=q->link;
tmp->link=q->link;
q->link=tmp;
}
}/*End of insert()*/
del(int num)
{
struct node *tmp,*q;
if(start->info==num)
{
tmp=start;
start=start->link; /*first element deleted*/
free(tmp);
return;
}
q=start;
while(q->link->link!=NULL)
{
if(q->link->info==num) /*element deleted in between*/
{
tmp=q->link;
q->link=tmp->link;
free(tmp);
return;
}
q=q->link;
}/*End of while */
if(q->link->info==num) /*last element deleted*/
{
tmp=q->link;
free(tmp);
q->link=NULL;
return;
}
printf("Element %d not found\n",num);
}/*End of del()*/
display()
{
struct node *q;
if(start == NULL)
{
printf("List is empty\n");
return;
}
q=start;
printf("List is :\n");
while(q != NULL)
{
printf("%d ", q->info);
q=q->link;
}
printf("\n");
}/*End of display() */
 

Program of reverse linked list

/* Program of reverse linked list*/
# include <stdio.h>
# include <malloc.h>
struct node
{
int info;
struct node *link;
}*start;
main()
{
int i,n,item;
start=NULL;
printf("How many nodes you want : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the item %d : ",i+1);
scanf("%d",&item);
create_list(item);
}
printf("Initially the linked list is :\n");
display();
reverse();
printf("Linked list after reversing is :\n");
display();
}/*End of main()*/
create_list(int num)
{
struct node *q,*tmp;
tmp= malloc(sizeof(struct node));
tmp->info=num;
tmp->link=NULL;
if(start==NULL)
start=tmp;
else
{
q=start;
while(q->link!=NULL)
q=q->link;
q->link=tmp;
}
}/*End of create_list() */
display()
{
struct node *q;
if(start == NULL)
{
printf("List is empty\n");
return;
}
q=start;
while(q!=NULL)
{
printf("%d ", q->info);
q=q->link;
}
printf("\n");
}/*End of display()*/
reverse()
{
struct node *p1,*p2,*p3;
if(start->link==NULL) /*only one element*/
return;
p1=start;
p2=p1->link;
p3=p2->link;
p1->link=NULL;
p2->link=p1;
while(p3!=NULL)
{
p1=p2;
p2=p3;
p3=p3->link;
p2->link=p1;
}
start=p2;
}/*End of reverse() */
 

Program of polynomial addition using linked list

/* Program of polynomial addition using linked list */
# include <stdio.h>
# include <malloc.h>
struct node
{
float coef;
int expo;
struct node *link;
};
struct node *poly_add(struct node *,struct node *);
struct node *enter(struct node *);
struct node *insert(struct node *,float,int);
main( )
{
struct node *p1_start,*p2_start,*p3_start;
p1_start=NULL;
p2_start=NULL;
p3_start=NULL;
printf("Polynomial 1 :\n");
p1_start=enter(p1_start);
printf("Polynomial 2 :\n");
p2_start=enter(p2_start);
p3_start=poly_add(p1_start,p2_start);
printf("Polynomial 1 is : ");
display(p1_start);
printf("Polynomial 2 is : ");
display(p2_start);
printf("Added polynomial is : ");
display(p3_start);
}/*End of main()*/
struct node *enter(struct node *start)
{
int i,n,ex;
float co;
printf("How many terms u want to enter : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter coeficient for term %d : ",i);
scanf("%f",&co);
printf("Enter exponent for term %d : ",i);
scanf("%d",&ex);
start=insert(start,co,ex);
}
return start;
}/*End of enter()*/
struct node *insert(struct node *start,float co,int ex)
{
struct node *ptr,*tmp;
tmp= malloc(sizeof(struct node));
tmp->coef=co;
tmp->expo=ex;
/*list empty or exp greater than first one */
if(start==NULL || ex>start->expo)
{
tmp->link=start;
start=tmp;
}
else
{
ptr=start;
while(ptr->link!=NULL && ptr->link->expo>ex)
ptr=ptr->link;
tmp->link=ptr->link;
ptr->link=tmp;
if(ptr->link==NULL) /*item to be added in the end */
tmp->link=NULL;
}
return start;
}/*End of insert()*/
struct node *poly_add(struct node *p1,struct node *p2)
{
struct node *p3_start,*p3,*tmp;
p3_start=NULL;
if(p1==NULL && p2==NULL)
return p3_start;
while(p1!=NULL && p2!=NULL )
{
tmp=malloc(sizeof(struct node));
if(p3_start==NULL)
{
p3_start=tmp;
p3=p3_start;
}
else
{
p3->link=tmp;
p3=p3->link;
}
if(p1->expo > p2->expo)
{
tmp->coef=p1->coef;
tmp->expo=p1->expo;
p1=p1->link;
}
else
if(p2->expo > p1->expo)
{
tmp->coef=p2->coef;
tmp->expo=p2->expo;
p2=p2->link;
}
else
if(p1->expo == p2->expo)
{
tmp->coef=p1->coef + p2->coef;
tmp->expo=p1->expo;
p1=p1->link;
p2=p2->link;
}
}/*End of while*/
while(p1!=NULL)
{
tmp=malloc(sizeof(struct node));
tmp->coef=p1->coef;
tmp->expo=p1->expo;
if (p3_start==NULL) /*poly 2 is empty*/
{
p3_start=tmp;
p3=p3_start;
}
else
{
p3->link=tmp;
p3=p3->link;
}
p1=p1->link;
}/*End of while */
while(p2!=NULL)
{
tmp=malloc(sizeof(struct node));
tmp->coef=p2->coef;
tmp->expo=p2->expo;
if (p3_start==NULL) /*poly 1 is empty*/
{
p3_start=tmp;
p3=p3_start;
}
else
{
p3->link=tmp;
p3=p3->link;
}
p2=p2->link;
}/*End of while*/
p3->link=NULL;
return p3_start;
}/*End of poly_add() */
display(struct node *ptr)
{
if(ptr==NULL)
{
printf("Empty\n");
return;
}
while(ptr!=NULL)
{
printf("(%.1fx^%d) + ", ptr->coef,ptr->expo);
ptr=ptr->link;
}
printf("\b\b \n"); /* \b\b to erase the last + sign */
}/*End of display()*/

Program of single linked list

/* Program of single linked list*/
# include <stdio.h>
# include <malloc.h>
struct node
{
int info;
struct node *link;
}*start;
main()
{
int choice,n,m,position,i;
start=NULL;
while(1)
{
printf("1.Create List\n");
printf("2.Add at begining\n");
printf("3.Add after \n");
printf("4.Delete\n");
printf("5.Display\n");
printf("6.Count\n");
printf("7.Reverse\n");
printf("8.Search\n");
printf("9.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("How many nodes you want : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the element : ");
scanf("%d",&m);
create_list(m);
}
break;
case 2:
printf("Enter the element : ");
scanf("%d",&m);
addatbeg(m);
break;
case 3:
printf("Enter the element : ");
scanf("%d",&m);
printf("Enter the position after which this element is inserted : ");
scanf("%d",&position);
addafter(m,position);
break;
case 4:
if(start==NULL)
{
printf("List is empty\n");
continue;
}
printf("Enter the element for deletion : ");
scanf("%d",&m);
del(m);
break;
case 5:
display();
break;
case 6:
count();
break;
case 7:
rev();
break;
case 8:
printf("Enter the element to be searched : ");
scanf("%d",&m);
search(m);
break;
case 9:
exit();
default:
printf("Wrong choice\n");
}/*End of switch */
}/*End of while */
}/*End of main()*/
create_list(int data)
{
struct node *q,*tmp;
tmp= malloc(sizeof(struct node));
tmp->info=data;
tmp->link=NULL;
if(start==NULL) /*If list is empty */
start=tmp;
else
{ /*Element inserted at the end */
q=start;
while(q->link!=NULL)
q=q->link;
q->link=tmp;
}
}/*End of create_list()*/
addatbeg(int data)
{
struct node *tmp;
tmp=malloc(sizeof(struct node));
tmp->info=data;
tmp->link=start;
start=tmp;
}/*End of addatbeg()*/
addafter(int data,int pos)
{
struct node *tmp,*q;
int i;
q=start;
for(i=0;i<pos-1;i++)
{
q=q->link;
if(q==NULL)
{
printf("There are less              than %d elements",pos);
return;
}
}/*End of for*/
tmp=malloc(sizeof(struct node) );
tmp->link=q->link;
tmp->info=data;
q->link=tmp;
}/*End of addafter()*/
del(int data)
{
struct node *tmp,*q;
if(start->info == data)
{
tmp=start;
start=start->link; /*First element deleted*/
free(tmp);
return;
}
q=start;
while(q->link->link != NULL)
{
if(q->link->info==data) /*Element deleted in between*/
{
tmp=q->link;
q->link=tmp->link;
free(tmp);
return;
}
q=q->link;
}/*End of while */
if(q->link->info==data) /*Last element deleted*/
{
tmp=q->link;
free(tmp);
q->link=NULL;
return;
}
printf("Element %d not      found\n",data);
}/*End of del()*/
display()
{
struct node *q;
if(start == NULL)
{
printf("List is empty\n");
return;
}
q=start;
printf("List is :\n");
while(q!=NULL)
{
printf("%d ", q->info);
q=q->link;
}
printf("\n");
}/*End of              display() */
count()
{
struct node *q=start;
int cnt=0;
while(q!=NULL)
{
q=q->link;
cnt++;
}
printf("Number     of elements are %d\n",cnt);
}/*End of count() */
rev()
{
struct node *p1,*p2,*p3;
if(start->link==NULL) /*only one element*/
return;
p1=start;
p2=p1->link;
p3=p2->link;
p1->link=NULL;
p2->link=p1;
while(p3!=NULL)
{
p1=p2;
p2=p3;
p3=p3->link;
p2->link=p1;
}
start=p2;
}/*End of rev()*/
search(int data)
{
struct node *ptr = start;
int pos = 1;
while(ptr!=NULL)
{
if(ptr->info==data)
{
printf("Item %d   found at position %d\n",data,pos);
return;
}
ptr = ptr->link;
pos++;
}
if(ptr == NULL)
printf("Item %d not   found in list\n",data);
}/*

Program of double linked list


/* Program of double linked list*/
# include <stdio.h>
# include <malloc.h>
struct node
{
struct node *prev;
int info;
struct node *next;
}*start;
main()
{
int choice,n,m,po,i;
start=NULL;
while(1)
{
printf("1.Create List\n");
printf("2.Add at begining\n");
printf("3.Add after\n");
printf("4.Delete\n");
printf("5.Display\n");
printf("6.Count\n");
printf("7.Reverse\n");
printf("8.exit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("How many nodes you want : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the element : ");
scanf("%d",&m);
create_list(m);
}
break;
case 2:
printf("Enter the element : ");
scanf("%d",&m);
addatbeg(m);
break;
case 3:
printf("Enter the element : ");
scanf("%d",&m);
printf("Enter the position after which this element is inserted : ");
scanf("%d",&po);
addafter(m,po);
break;
case 4:
printf("Enter the element for deletion : ");
scanf("%d",&m);
del(m);
break;
case 5:
display();
break;
case 6:
count();
break;
case 7:
rev();
break;
case 8:
exit();
default:
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of main()*/
create_list(int num)
{
struct node *q,*tmp;
tmp= malloc(sizeof(struct node));
tmp->info=num;
tmp->next=NULL;
if(start==NULL)
{
tmp->prev=NULL;
start->prev=tmp;
start=tmp;
}
else
{
q=start;
while(q->next!=NULL)
q=q->next;
q->next=tmp;
tmp->prev=q;
}
}/*End of create_list()*/
addatbeg(int num)
{
struct node *tmp;
tmp=malloc(sizeof(struct node));
tmp->prev=NULL;
tmp->info=num;
tmp->next=start;
start->prev=tmp;
start=tmp;
}/*End of addatbeg()*/
addafter(int num,int c)
{
struct node *tmp,*q;
int i;
q=start;
for(i=0;i<c-1;i++)
{
q=q->next;
if(q==NULL)
{
printf("There are less than %d elements\n",c);
return;
}
}
tmp=malloc(sizeof(struct node) );
tmp->info=num;
q->next->prev=tmp;
tmp->next=q->next;
tmp->prev=q;
q->next=tmp;
}/*End of addafter() */
del(int num)
{
struct node *tmp,*q;
if(start->info==num)
{
tmp=start;
start=start->next; /*first element deleted*/
start->prev = NULL;
free(tmp);
return;
}
q=start;
while(q->next->next!=NULL)
{
if(q->next->info==num) /*Element deleted in between*/
{
tmp=q->next;
q->next=tmp->next;
tmp->next->prev=q;
free(tmp);
return;
}
q=q->next;
}
if(q->next->info==num) /*last element deleted*/
{ tmp=q->next;
free(tmp);
q->next=NULL;
return;
}
printf("Element %d not found\n",num);
}/*End of del()*/
display()
{
struct node *q;
if(start==NULL)
{
printf("List is empty\n");
return;
}
q=start;
printf("List is :\n");
while(q!=NULL)
{
printf("%d ", q->info);
q=q->next;
}
printf("\n");
}/*End of display() */
count()
{ struct node *q=start;
int cnt=0;
while(q!=NULL)
{
q=q->next;
cnt++;
}
printf("Number of elements are %d\n",cnt);
}/*End of count()*/
rev()
{
struct node *p1,*p2;
p1=start;
p2=p1->next;
p1->next=NULL;
p1->prev=p2;
while(p2!=NULL)
{
p2->prev=p2->next;
p2->next=p1;
p1=p2;
p2=p2->prev; /*next of p2 changed to prev */
}
start=p1;
}

Program of circular linked list

/* Program of circular linked list*/
# include <stdio.h>
# include <malloc.h>
struct node
{
int info;
struct node *link;
}*last;
main()
{
int choice,n,m,po,i;
last=NULL;
while(1)
{
printf("1.Create List\n");
printf("2.Add at begining\n");
printf("3.Add after \n");
printf("4.Delete\n");
printf("5.Display\n");
printf("6.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("How many nodes you want : ");
scanf("%d",&n);
for(i=0; i < n;i++)
{
printf("Enter the element : ");
scanf("%d",&m);
create_list(m);
}
break;
case 2:
printf("Enter the element : ");
scanf("%d",&m);
addatbeg(m);
break;
case 3:
printf("Enter the element : ");
scanf("%d",&m);
printf("Enter the position after which this element is inserted : ");
scanf("%d",&po);
addafter(m,po);
break;
case 4:
if(last == NULL)
{
printf("List underflow\n");
continue;
}
printf("Enter the number for deletion : ");
scanf("%d",&m);
del(m);
break;
case 5:
display();
break;
case 6:
exit();
default:
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of main()*/
create_list(int num)
{
struct node *q,*tmp;
tmp= malloc(sizeof(struct node));
tmp->info = num;
if(last == NULL)
{
last = tmp;
tmp->link = last;
}
else
{
tmp->link = last->link; /*added at the end of list*/
last->link = tmp;
last = tmp;
}
}/*End of create_list()*/
addatbeg(int num)
{
struct node *tmp;
tmp = malloc(sizeof(struct node));
tmp->info = num;
tmp->link = last->link;
last->link = tmp;
}/*End of addatbeg()*/
addafter(int num,int pos)
{
struct node *tmp,*q;
int i;
q = last->link;
for(i=0; i < pos-1; i++)
{
q = q->link;
if(q == last->link)
{
printf("There are less than %d elements\n",pos);
return;
}
}/*End of for*/
tmp = malloc(sizeof(struct node) );
tmp->link = q->link;
tmp->info = num;
q->link = tmp;
if(q==last) /*Element inserted at the end*/
last=tmp;
}/*End of addafter()*/
del(int num)
{
struct node *tmp,*q;
if( last->link == last && last->info == num) /*Only one element*/
{
tmp = last;
last = NULL;
free(tmp);
return;
}
q = last->link;
if(q->info == num)
{
tmp = q;
last->link = q->link;
free(tmp);
return;
}
while(q->link != last)
{
if(q->link->info == num) /*Element deleted in between*/
{
tmp = q->link;
q->link = tmp->link;
free(tmp);
printf("%d deleted\n",num);
return;
}
q = q->link;
}/*End of while*/
if(q->link->info == num) /*Last element deleted q->link=last*/
{
tmp = q->link;
q->link = last->link;
free(tmp);
last = q;
return;
}
printf("Element %d not found\n",num);
}/*End of del()*/
display()
{
struct node *q;
if(last == NULL)
{
printf("List is empty\n");
return;
}
q = last->link;
printf("List is :\n");
while(q != last)
{
printf("%d ", q->info);
q = q->link;
}
printf("%d\n",last->info);
}/*End of display()*/

Program of linked list using array

/* Program of list using array */
# include<stdio.h>
# define MAX 20
int arr[MAX];
int n; /*Total number of elements in the list */
main()
{
int choice,item,pos;
while(1)
{
printf("1.Input list\n");
printf("2.Insert\n");
printf("3.Search\n");
printf("4.Delete\n");
printf("5.Display\n");
printf("6.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the number of elements to be entered : ");
scanf("%d",&n);
input(n);
break;
case 2:
insert();
break;
case 3:
printf("Enter the element to be searched : ");
scanf("%d", &item);
pos = search(item);
if(pos >= 1)
printf("%d found at position %d\n",item,pos);
else
printf("Element not found\n");
break;
case 4:
del();
break;
case 5:
display();
break;
case 6:
exit();
break;
default:
printf("Wrong choice\n");
} /*End of switch */
}/*End of while */
}/*End of main() */
input()
{
int i;
for(i = 0; i< n ; i++)
{
printf("Input value for element %d : ", i+1);
scanf("%d", &arr[i]);
}
}/*End of input()*/
int search(int item)
{
int i;
for(i=0; i < n; i++)
{
if(item == arr[i])
return(i+1);
}
return(0); /* If element not found */
}/*End of search()*/
insert()
{
int temp,item,position;
if(n == MAX)
{
printf("List overflow\n");
return;
}
printf("Enter position for insertion : ");
scanf("%d", &position);
printf("Enter the value : ");
scanf("%d",&item);
if(position > n+1 )
{
printf("Enter position less than or equal to %d\n",n+1);
return;
}
if( position == n+1 ) /*Insertion at the end */
{
arr[n] = item;
n = n+1;
return;
}
/* Insertion in between */
temp=n-1;
while( temp >= position-1)
{
arr[temp+1] = arr[temp]; /* shifting right */
temp --;
}
arr[position-1] = item;
n = n +1 ;
}/*End of insert()*/
del()
{
int temp,position,item;
if(n == 0)
{
printf("List underflow\n");
return;
}
printf("Enter the element to be deleted : ");
scanf("%d",&item);
if(item==arr[n-1]) /*Deletion at the end*/
{
n = n-1;
return;
}
position=search(item);
if(position==0)
{
printf("Element not present in array\n");
return;
}
/*Deletion in between */
temp=position-1;
while(temp <= n-1)
{
arr[temp] = arr[temp+1]; /* Shifting left */
temp ++;
}
n = n - 1 ;
}/*End of del()*/
display()
{
int i;
if(n==0)
{
printf("List is empty\n");
return;
}
for(i = 0; i< n; i++)
printf("Value at position %d : %d\n", i+1, arr[i]);
}

Thursday, 28 July 2011

Program to draw & fill Rectangle

/* Program to draw & fill Rectangle */
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int x,y,r;
initgraph(&gd,&gm,"c:\\turboc3\\");
x=getmaxx()/2;
y=getmaxy()/2;
setcolor(5);
setbkcolor(3);
setfillstyle(1,5);
rectangle(x-50,y-50,x+50,y+50);
floodfill(x,y,5);
putpixel(x,y,YELLOW);
getch();
closegraph();
}

Program to draw & fill Triangle & Hexagon


/* Program to draw & fill
Triangle & Hexagon */
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int np=4,tri[8]={200,50,150,100,250,100,200,50};
int hex[14]={400,50,450,50,500,100,450,150,400,150,350,100,400,50};
initgraph(&gd,&gm,"c:\\turboc3\\");
drawpoly(np,tri);
np=7;
drawpoly(np,hex);
getch();
closegraph();
}

Program to demonstrate outtextxy

/* Program to demonstrate outtextxy */
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,i,y=0;
char str[3];
initgraph(&gd,&gm,"c:\\turboc3\\");
for(i=1;i<48;i++)
{
if(i%5==0 || i>=32)
{
settextstyle(0,0,0);
itoa(i,str,10);
outtextxy(getmaxx()-100,0,str);
getch();
cleardevice();
y=0;
}
settextstyle(0,0,i);
outtextxy(0,y,"Vivek");
y+=textheight("Vivek");
}
getch();
closegraph();
}

Program to draw Line

/* Program to draw Line */
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int x,y,r;
initgraph(&gd,&gm,"c:\\turboc3\\");
x=getmaxx()/2;
y=getmaxy()/2;
line(x-50,y-50,x+50,y+50);
putpixel(x,y,YELLOW);
getch();
//closegraph();
}

Program to fill Polygon

/* Program to fill Polygon */
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int np=4,tri[8]={200,50,150,100,250,100,200,50};
int hex[14]={400,50,450,50,500,100,450,150,400,150,350,100,400,50};
initgraph(&gd,&gm,"c:\\turboc3\\");
setcolor(5);
setbkcolor(3);
setfillstyle(1,4);
fillpoly(np,tri);
//floodfill(200,75,5);
np=7;
setcolor(5);
setbkcolor(3);
setfillstyle(5,4);
fillpoly(np,hex);
//floodfill(450,100,5);
getch();
closegraph();
}

Program to fill Ellipse

/* Program to fill Ellipse */
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int x,y,r;
initgraph(&gd,&gm,"c:\\turboc3\\");
x=getmaxx()/2;
y=getmaxy()/2;
setcolor(5);//set border color
setbkcolor(3);//set background color
setfillstyle(2,4);//set style to fill color
fillellipse(x,y,100,50);//draw & fill color with set fill color
putpixel(x,y,YELLOW);//
getch();
closegraph();
}

Program to draw Ellipse

/* Program to draw Ellipse */
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int x,y;
initgraph(&gd,&gm,"c:\\turboc3\\");
x=getmaxx()/2;
y=getmaxy()/2;
ellipse(x,y,0,360,50,100);
putpixel(x,y,YELLOW);
getch();
closegraph();
}

Program to draw & fill Circle

/* Program to draw & fill Circle */
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int x,y,r;
initgraph(&gd,&gm,"c:\\turboc3\\");
cleardevice();
x=getmaxx()/2;
y=getmaxy()/2;
r=100;
setbkcolor(MAGENTA);//Set background color with MAGENTA color
setcolor(RED);//to set border color of any shape with CYAN color
setfillstyle(1,YELLOW);//Set fill style with color
circle(x,y,r); //draw circle
floodfill(x,y,4);// fill object color with setfillstyle color
putpixel(x,y,14);//draw pixel
getch();
closegraph();
}

Program to draw circle

/* Program to draw circle */
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int x,y,r;
initgraph(&gd,&gm,"c:\\turboc3\\");
x=getmaxx()/2;
y=getmaxy()/2;
r=100;
circle(x,y,r);
putpixel(x,y,14);
getch();
closegraph();
}

Sunday, 24 July 2011

Project For Banking System (Using C)

//Project For Banking System In C Language
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<dos.h>
struct bank
{
 int accono;
 char name[20];
 float bal;
};
struct bank1
{
 int accono;
 char name[20];
 int duration;
 float bal;
 float amount;
 };
 struct bank2
 {
  char user[10];
  int pass;
  };
void main()
 {
   FILE *fp1;
  struct bank2 holder2[20],holders2;
  int ch=1,ch1,i=0,n,accono,j=0,found,ch3,nfound,ch4,nfound1,no;
  char choice,string1[10],string2[10],user[10];

  music(1);
  printf("\n\n\n\t\t\t\tBank System");
  printf("\n\t\t\t\t===========\n\n");
  printf("\t\t      Developed By Nitin Tumma(MCA-I)");
  printf("\n\t\t      ===============================\n\n\n");
  b:
  music(1);
  printf("\n\t\t   Enter the User Name:(0 to quit):");
  gets(string1);
  if(strcmp(string1,"0")==0)
  {
   exit(0);
  }
  printf("\n\t\t   Enter the Password:");
  for(i=0;i<6;i++)
  {
   string2[i]=getch();
   printf("*");
  }
  string2[i]='\0';
  i=0;
  clrscr();
  fp1=fopen("bank4.txt","r");
  while(fscanf(fp1," %s %d",&holders2.user,&holders2.pass)!=EOF)
  {
     no=holders2.pass;
     strcpy(user,holders2.user);
     if(strcmp(string1,user)==0 && strcmp(string2,no)==0)
     {
 call();
     }
     i++;
  }
  if(strcmp(string1,"nitin")==0 && strcmp(string2,"111111")==0)
   {
 call();
   }
  if(nfound==0)
  {
   clrscr();
   music(1);
   printf("\n\n\n\n\t\tYou entered incorrect user name & password:");
   printf("\n\t\t-------------------------------------------\n");
   printf("\n\t\t\t\tTry Again");
   printf("\n\t\t\t\t---------\n\n");
   goto b;
   }
   fclose(fp1);


 }
  call()
  {
  int ch3=1;
  clrscr();
  music(2);
  printf("\n\t\t    WEL-COME TO BANK OF MAHARASHTRA\n");
  printf("\t\t    -------------------------------\n");
  do
   {
   printf("\n\t\t   Which Account do u want to open:\n");
   printf("\t\t   --------------------------------\n\n");
   printf("\t\t\t\tMENU\n");
   printf("\t\t\t************************\n");
   printf("\t\t\t1.Current Account\n\t\t\t2.Saving Account\n\t\t\t3.Fixed Deposite Account\n\t\t\t4.Other\n\t\t\t5.Exit\n");
   printf("\t\t\t************************\n");
   printf("\t\t\tEnter your choice=");
   scanf("%d",&ch3);
   switch(ch3)
      {
  case 1: clrscr();
   call1(1);

  case 2: clrscr();
   call1(2);
  case 3: clrscr();
   call2();
  case 4: clrscr();
   call3();
  case 5: music(1);
   exit(0);

        default: clrscr();
   ch3=1;
   printf("\n\t\t\tEnter correct choice!!\n");
   printf("\t\t\t----------------------\n");
   printf("\n\t\t\t\tTry Again\n");
   printf("\t\t\t\t---------\n");
      }
   }while(ch3);
  }


 call1(int a)
 {
  FILE *fp1;
  struct bank holder[20],holders;
  int ch=1,ch1,i=0,n,accono,j=0,found,ch3,nfound,ch4,nfound1;
  char choice;
  clrscr();
  do
    {
     music(2);
     if(a==1)
     {
     printf("\n\t\t    WEL-COME TO CURRENT ACCOUNT\n");
     }
     else
     {
     printf("\n\t\t    WEL-COME TO SAVING ACCOUNT\n");
     }
     printf("\t\t    ===========================");
     printf("\n\n\t\t\t      MENU");
     printf("\n\t\t   *******************************");
     printf("\n\t\t    1: Create\n\t\t    2: Opening Account\n\t\t    3: Deposite Amount\n\t\t    4: Withdrawn Amount\n\t\t    5: Search Member Information\n\t\t    6: Display Member Information\n\t\t    7: Closing Account\n\t\t    8: Modify Members Information\n\t\t    9: Display Total Information\n\t\t   10: Home page");
     printf("\n\t\t   *******************************");
     printf("\n\t\t   Enter your choice=");
     scanf("%d",&ch);
     switch(ch)
     {
       case 1: clrscr();
        music(1);
        if(a==1)
        {
        fp1=fopen("bank1.txt","w");
        }
        else
        {
        fp1=fopen("bank2.txt","w");
        }
        fclose(fp1);
        do
   {
    if(a==1)
    {
     fp1=fopen("bank1.txt","r");
    }
    else
    {
     fp1=fopen("bank2.txt","r");
    }
    printf("\nEnter Account No:");
    scanf("%d",&accono);
    nfound=0;
    while(fscanf(fp1," %d %s %f",&holders.accono,&holders.name,&holders.bal)!=EOF)
    {
      if(accono==holders.accono)
      {
      nfound=1;
      }
    }
    fclose(fp1);
    if(nfound==0)
    {
     if(a==1)
     {
      fp1=fopen("bank1.txt","a");
     }
     else
     {
      fp1=fopen("bank2.txt","a");
     }
     printf("\nEnter the name:");
     scanf("%s",&holder[i].name);
     printf("\nEnter the balance:");
     scanf("%f",&holder[i].bal);
     fprintf(fp1," %d %s %f\n",accono,holder[i].name,holder[i].bal);
     i++;
     fclose(fp1);
    }
    else
    {
    clrscr();
    music(1);
    printf("\nThe no already exit\nTry again\n");
    }
    printf("\nAdd more?(yes=1/no=0)");
    scanf("%d",&ch1);
    clrscr();
   }while(ch1==1);
        n=i;
        break;
       case 2: clrscr();
        i=n;
        do
        {
        music(2);
  if(a==1)
  {
   fp1=fopen("bank1.txt","r");
  }
  else
  {
   fp1=fopen("bank2.txt","r");
  }
  printf("\nEnter Account No:");
  scanf("%d",&accono);
  nfound=0;
  while(fscanf(fp1," %d %s %f",&holders.accono,&holders.name,&holders.bal)!=EOF)
  {
   if(accono==holders.accono)
    {
      nfound=1;
    }
  }
  fclose(fp1);
  if(nfound==0)
  {
   if(a==1)
   {
    fp1=fopen("bank1.txt","a");
   }
   else
   {
    fp1=fopen("bank2.txt","a");
   }
   printf("\nEnter the name:");
   scanf("%s",&holder[i].name);
   printf("\nEnter the balance:");
   scanf("%f",&holder[i].bal);
   fprintf(fp1," %d %s %f\n",accono,holder[i].name,holder[i].bal);
   i++;
   printf("\nAccount opened successfully");
   fclose(fp1);
   }
   else
   {
    clrscr();
    music(1);
    printf("\nThe no already exit\nTry again\n");
   }
   printf("\n\nWant to open more Account?(yes=1/no=0)");
   scanf("%d",&ch1);
   clrscr();
        }while(ch1==1);
        n=i;
        break;
       case 3: clrscr();
        music(2);
   if(a==1)
   {
    fp1=fopen("bank1.txt","r");
   }
   else
   {
    fp1=fopen("bank2.txt","r");
   }

   i=0;
   nfound=0;
   printf("\nEnter Account no:");
   scanf("%d",&accono);
   while(fscanf(fp1,"%d %s %f",&holders.accono,&holders.name,&holders.bal)!=EOF)
   {
     if(accono==holders.accono)
     {
       nfound=1;
       printf("\nHow much do u want to deposite=");
       scanf("%d",&ch);
       ch4=holders.bal+ch;
       holder[i].bal=holders.bal+ch;
       strcpy(holder[i].name,holders.name);
       holder[i].accono=holders.accono;
     }
     else
     {
       holder[i].accono=holders.accono;
       strcpy(holder[i].name,holders.name);
       holder[i].bal=holders.bal;
     }
     i++;
   }
   fclose(fp1);
   if(a==1)
   {
    fp1=fopen("bank1.txt","w");
   }
   else
   {
    fp1=fopen("bank2.txt","w");
   }
   for(j=0;j<i;j++)
   {
    fprintf(fp1," %d %s %f\n",holder[j].accono,holder[j].name,holder[j].bal);
   }
   if(nfound==0)
   {
     music(1);
     printf("\nIncorrect Account No\n");
   }
   else
   {
    printf("Rs %d has been deposited\nAccount having Rs. %d\n",ch,ch4);
   }
   fclose(fp1);
  break;

       case 4: clrscr();
        music(2);
        if(a==1)
        {
  fp1=fopen("bank1.txt","r");
        }
        else
        {
  fp1=fopen("bank2.txt","r");
        }
        i=0;
        nfound=0;
        printf("\nEnter Account no:");
        scanf("%d",&accono);
        while(fscanf(fp1,"%d %s %f",&holders.accono,&holders.name,&holders.bal)!=EOF)
        {
  if(accono==holders.accono)
  {
    nfound=1;
    nfound1=1;
    printf("\nHow much do u want to withdrawn=");
    scanf("%d",&ch);
    if((holders.bal-ch)<500)
    {
     holder[i].bal=holders.bal;
     ch4=holders.bal;
     nfound1=0;
     }
     else
     {
    holder[i].bal=holders.bal-ch;
    ch4=holders.bal-ch;
    }
    holder[i].accono=holders.accono;
    strcpy(holder[i].name,holders.name);
  }
  else
  {
    holder[i].accono=holders.accono;
    strcpy(holder[i].name,holders.name);
    holder[i].bal=holders.bal;
  }
  i++;
        }
        fclose(fp1);
        if(a==1)
        {
  fp1=fopen("bank1.txt","w");
        }
        else
        {
  fp1=fopen("bank2.txt","w");
        }
        for(j=0;j<i;j++)
        {
   fprintf(fp1," %d %s %f\n",holder[j].accono,holder[j].name,holder[j].bal);
        }
        if(nfound==0)
        {
   music(1);
   printf("\nIncorrect Account No\n");
        }
        else
        {
  if(nfound1==0)
  {
  music(1);
  printf("cannot able to withdrawn,because of min bal Rs.500 must in ur account\nAccount have only Rs. %d\n,",ch4);
  }
  else
  {
   music(1);
   printf("Your money has been withdrawn by %d\nNow account having Rs.%d",ch,ch4);
  }
        }
        fclose(fp1);
        break;


       case 5: clrscr();
        music(1);
        if(a==1)
        {
        fp1=fopen("bank1.txt","r");
        }
        else
        {
        fp1=fopen("bank2.txt","r");
        }
        i=0;
        found=0;
        printf("\n\n\n\t\t    Enter Account No to search=");
        scanf("%d",&accono);
        while(fscanf(fp1," %d %s %f",&holders.accono,&holders.name,&holders.bal)!=EOF)
        {
   if(accono==holders.accono)
   {
     music(1);
     printf("\t\t    Account found at position %d\n\t\t    Detail is =>",i+1);
     printf("\n\t\t    Account No=%d, Name=%s, Balance=%.2f\n\n",holders.accono,holders.name,holders.bal);
     found=1;
   }
   i++;
        }
        if(found==0)
        {
        music(1);
  printf("\n\t\t\tAccount was not found\n\n");
        }
        fclose(fp1);
        break;


       case 6: clrscr();
        music(1);
        if(a==1)
        {
        fp1=fopen("bank1.txt","r");
        }
        else
        {
        fp1=fopen("bank2.txt","r");
        }
        if(fp1==NULL)
        {
  music(1);
  printf("\t\tThere are no Accounts\n");
  printf("\t\t---------------------\n");
        }
        else
        {
  music(1);
  printf("\t\t  Account No.    Name      Balance");
  printf("\n\t\t  ================================");
  while(fscanf(fp1," %d %s %f",&holders.accono,&holders.name,&holders.bal)!=EOF)
  {
   fprintf(stdout,"\n\t\t%7d%15s%12.2f",holders.accono,holders.name,holders.bal);
  }
        printf("\n\t\t  ================================\n");
        }
        fclose(fp1);
        break;


       case 7: clrscr();
        music(1);
        if(a==1)
        {
        fp1=fopen("bank1.txt","r");
        }
        else
        {
        fp1=fopen("bank2.txt","r");
        }
        i=0;
        found=0;
        printf("\n\t\t         Enter Account No=");
        scanf("%d",&accono);
        while(fscanf(fp1," %d %s %f",&holders.accono,&holders.name,&holders.bal)!=EOF)
        {
   if(accono==holders.accono)
   {
    music(1);
    printf("\n\t\t   Account Closed Successfully!!\n\n");
    found=1;
   }
   else
   {
    holder[i].accono=holders.accono;
    strcpy(holder[i].name,holders.name);
    holder[i].bal=holders.bal;
    i++;
   }
        }
        if(found==0)
        {
  music(1);
  printf("\n\t\t\tAccount was not found!!\n\n");
        }
        fclose(fp1);
        if(a==1)
        {
        fp1=fopen("bank1.txt","w");
        }
        else
        {
        fp1=fopen("bank2.txt","w");
        }
        for(j=0;j<i;j++)
        {
  fprintf(fp1," %d %s %f\n",holder[j].accono,holder[j].name,holder[j].bal);
        }
        fclose(fp1);
        break;

       case 8: clrscr();
        music(2);
        if(a==1)
        {
    fp1=fopen("bank1.txt","r");
        }
        else
        {
    fp1=fopen("bank2.txt","r");
        }
        i=0;
        nfound=0;
        printf("\nEnter Account no to modify members information=");
        scanf("%d",&accono);
        while(fscanf(fp1,"%d %s %f",&holders.accono,&holders.name,&holders.bal)!=EOF)
        {
      if(accono==holders.accono)
      {
        nfound=1;
        printf("\nEnter new Account No,name,Balance=");
        scanf("%d%s%f",&holder[i].accono,&holder[i].name,&holder[i].bal);
      }
      else
      {
        holder[i].accono=holders.accono;
        strcpy(holder[i].name,holders.name);
        holder[i].bal=holders.bal;
      }
      i++;
        }
        fclose(fp1);
        if(a==1)
        {
    fp1=fopen("bank1.txt","w");
        }
        else
        {
     fp1=fopen("bank2.txt","w");
        }
        for(j=0;j<i;j++)
        {
     fprintf(fp1," %d %s %f\n",holder[j].accono,holder[j].name,holder[j].bal);
        }
        if(nfound==0)
        {
   music(1);
   printf("\nIncorrect Account No\n");
        }
        else
        {
  printf("\nInformation modified successfully\n");
        }
        fclose(fp1);
        break;
       case 9: clrscr();
        music(1);
        if(a==1)
        {
        fp1=fopen("bank1.txt","r");
        }
        else
        {
        fp1=fopen("bank2.txt","r");
        }
        if(fp1==NULL)
        {
  music(1);
  printf("\t\t\tThere are no Accounts\n");
  printf("\t\t\t---------------------\n");
        }
        else
        {
  music(1);
  i=0;
  ch=0;
  while(fscanf(fp1," %d %s %f",&holders.accono,&holders.name,&holders.bal)!=EOF)
  {
    i++;
    ch=ch+holders.bal;

  }
        printf("\n\t\t =================================");
        printf("\n\t\t      Total Balance is %d\n\t\t   Total no of Account is %d",ch,i);
        printf("\n\t\t =================================\n");
        }
        fclose(fp1);
        break;
       case 10: call();

       default:clrscr();
        music(1);
        printf("\n\t\t\tEnter Correct choice!!\n\n");
        break;

     }
    }while(ch);
 }


  call2()
 {
  FILE *fp1;
  struct bank1 holder1[20],holders1;
  int ch=1,ch1,i=0,n,accono,j=0,found,ch3,nfound,ch4,nfound1;
  char choice;
  clrscr();
  do
    {
     music(2);
     printf("\n\t\t WEL-COME TO FIXED DEPOSITE ACCOUNT\n");
     printf("\t\t ==================================");
     printf("\n\n\t\t\t      MENU");
     printf("\n\t\t   *****************************");
     printf("\n\t\t   1: Create\n\t\t   2: Opening Account\n\t\t   3: Search Member Information\n\t\t   4: Display Member Information\n\t\t   5: Closing Account\n\t\t   6: Modify Members Information\n\t\t   7: Display Total Information\n\t\t   8: Home page");
     printf("\n\t\t   *****************************");
     printf("\n\t\t   Enter your choice=");
     scanf("%d",&ch);
     switch(ch)
     {
       case 1: clrscr();
        music(1);
        fp1=fopen("bank3.txt","w");
        fclose(fp1);
        do
   {
    fp1=fopen("bank3.txt","r");
    printf("\nEnter Account No:");
    scanf("%d",&accono);
    nfound=0;
    while(fscanf(fp1," %d %s %f %d %f %f",&holders1.accono,&holders1.name,&holders1.duration,&holders1.bal,&holders1.amount)!=EOF)
    {
      if(accono==holders1.accono)
      {
      nfound=1;
      }
    }
    fclose(fp1);
    if(nfound==0)
    {

     fp1=fopen("bank3.txt","a");
     printf("\nEnter the name:");
     scanf("%s",&holder1[i].name);
     b:
     printf("\nEnter the duration of deposite:");
     scanf("%d",&holder1[i].duration);
  //   if(holder1[i].duration!=5 || holder1[i].duration!=2 || holder1[i].duration!=1)
   //  {
    //  printf("\nDuration must among 5 or 2 or 1");
    //  goto b;
    // }
     printf("\nEnter the Amount:");
     scanf("%f",&holder1[i].bal);
     if(holder1[i].duration==5)
     {
     holder1[i].amount=holder1[i].bal*2;
     }
     else
     {
      if(holder1[i].duration==2)
      {
       holder1[i].amount=holder1[i].bal+holder1[i].bal*15;
      }
      else
      {
      holder1[i].amount=holder1[i].bal+holder1[i].bal*10;
      }
     }
     fprintf(fp1," %d %s %d %f %f\n",accono,holder1[i].name,holder1[i].duration,holder1[i].bal,holder1[i].amount);
     i++;
     fclose(fp1);
    }
    else
    {
    clrscr();
    music(1);
    printf("\nThe no already exit\nTry again\n");
    }
    printf("\nAdd more?(yes=1/no=0)");
    scanf("%d",&ch1);
    clrscr();
   }while(ch1==1);
        n=i;
        break;
       case 2: clrscr();
        i=n;
        do
        {
        music(2);
        fp1=fopen("bank3.txt","r");
        printf("\nEnter Account No:");
  scanf("%d",&accono);
  nfound=0;
  while(fscanf(fp1," %d %s %d %f %f",&holders1.accono,&holders1.name,&holders1.duration,&holders1.bal,&holders1.amount)!=EOF)
  {
   if(accono==holders1.accono)
    {
      nfound=1;
    }
  }
  fclose(fp1);
  if(nfound==0)
  {
  fp1=fopen("bank3.txt","a");
  printf("\nEnter the name:");
  scanf("%s",&holder1[i].name);
  c:
  printf("\nEnter the duration of deposite:");
  scanf("%d",&holder1[i].duration);
       // if(holder1[i].duration!=5 || holder1[i].duration!=2 || holder1[i].duration!=1)
       // {
  //    printf("\nDuration must among 5 or 2 or 1");
   //   goto c;
       // }
  printf("\nEnter the Amount:");
  scanf("%f",&holder1[i].bal);
  if(holder1[i].duration==5)
  {
     holder1[i].amount=holder1[i].bal*2;
  }
  else
  {
      if(holder1[i].duration==2)
      {
       holder1[i].amount=holder1[i].bal+holder1[i].bal*15;
      }
      else
      {
      holder1[i].amount=holder1[i].bal+holder1[i].bal*10;
      }
  }
  fprintf(fp1," %d %s %d %f %f\n",accono,holder1[i].name,holder1[i].duration,holder1[i].bal,holder1[i].amount);
  i++;
  printf("\nAccount opened successfully");
  fclose(fp1);
  }
  else
  {
    clrscr();
    music(1);
    printf("\nThe no already exit\nTry again\n");
  }
  printf("\n\nWant to open more Account?(yes=1/no=0)");
  scanf("%d",&ch1);
  clrscr();
        }while(ch1==1);
        n=i;
        break;



       case 3: clrscr();
        music(1);
        fp1=fopen("bank3.txt","r");
        i=0;
        found=0;
        printf("\n\n\n\t\t    Enter Account No to search=");
        scanf("%d",&accono);
        while(fscanf(fp1," %d %s %d %f %f",&holders1.accono,&holders1.name,&holders1.duration,&holders1.bal,&holders1.amount)!=EOF)
        {
   if(accono==holders1.accono)
   {
     music(1);
     printf("\t\t    Account found at position %d\n\t\t    Detail is =>",i+1);
     printf("\n\t\t    Account No=%d, Name=%s, Duration=%d\n\t\t    Deposite Amount=%.2f, Total Amount%.2f\n\n",holders1.accono,holders1.name,holders1.duration,holders1.bal,holders1.amount);
     found=1;
   }
   i++;
        }
        if(found==0)
        {
        music(1);
  printf("\n\t\t\tAccount was not found\n\n");
        }
        fclose(fp1);
        break;


       case 4: clrscr();
        music(1);
        fp1=fopen("bank3.txt","r");
        if(fp1==NULL)
        {
  music(1);
  printf("\t\t\tThere are no Accounts\n");
  printf("\t\t\t---------------------\n");
        }
        else
        {
  music(1);
  printf("      Account No.    Name      Duration  Deposite Amount  Total Amount");
  printf("\n      ================================================================");
  while(fscanf(fp1," %d %s %d %f %f",&holders1.accono,&holders1.name,&holders1.duration,&holders1.bal,&holders1.amount)!=EOF)
  {
   fprintf(stdout,"\n    %7d%15s%12d%12.2f%17.2f",holders1.accono,holders1.name,holders1.duration,holders1.bal,holders1.amount);
  }
        printf("\n      ================================================================\n");
        }
        fclose(fp1);
        break;


       case 5: clrscr();
        music(1);
        fp1=fopen("bank3.txt","r");
        i=0;
        found=0;
        printf("\n\t\t         Enter Account No=");
        scanf("%d",&accono);
        while(fscanf(fp1," %d %s %d %f %f",&holders1.accono,&holders1.name,&holders1.duration,&holders1.bal,&holders1.amount)!=EOF)
        {
   if(accono==holders1.accono)
   {
    music(1);
    printf("\n\t\t   Account Closed Successfully!!\n\n");
    found=1;
   }
   else
   {
    holder1[i].accono=holders1.accono;
    strcpy(holder1[i].name,holders1.name);
    holder1[i].duration=holders1.duration;
    holder1[i].bal=holders1.bal;
    holder1[i].amount=holders1.amount;
    i++;
   }
        }
        if(found==0)
        {
  music(1);
  printf("\n\t\t\tAccount was not found!!\n\n");
        }
        fclose(fp1);
        fp1=fopen("bank3.txt","w");
        for(j=0;j<i;j++)
        {
  fprintf(fp1," %d %s %d %f %f\n",holder1[j].accono,holder1[j].name,holder1[j].duration,holder1[j].bal,holder1[j].amount);
        }
        fclose(fp1);
        break;

       case 6: clrscr();
        music(2);
        fp1=fopen("bank3.txt","r");
        i=0;
        nfound=0;
        printf("\nEnter Account no=");
        scanf("%d",&accono);
        while(fscanf(fp1,"%d %s %d %f %f",&holders1.accono,&holders1.name,&holders1.duration,&holders1.bal,&holders1.amount)!=EOF)
        {

        if(accono==holders1.accono)
        {
  clrscr();
  nfound=1;
  printf("\nEnter new Account no=");
        scanf("%d",&holder1[i].accono);
        printf("\nEnter the name:");
        scanf("%s",&holder1[i].name);
  d:
  printf("\nEnter the duration of deposite:");
  scanf("%d",&holder1[i].duration);
        // if(holder1[i].duration!=5 || holder1[i].duration!=2 || holder1[i].duration!=1)
      // {
       //     printf("\nDuration must among 5 or 2 or 1");
       //     goto d;
        // }
  printf("\nEnter the Amount:");
  scanf("%f",&holder1[i].bal);
  if(holder1[i].duration==5)
  {
     holder1[i].amount=holder1[i].bal*2;
  }
  else
  {
      if(holder1[i].duration==2)
      {
       holder1[i].amount=holder1[i].bal+holder1[i].bal*15;
      }
      else
      {
      holder1[i].amount=holder1[i].bal+holder1[i].bal*10;
      }
  }
        }
        else
        {
        holder1[i].accono=holders1.accono;
        strcpy(holder1[i].name,holders1.name);
        holder1[i].duration=holders1.duration;
        holder1[i].bal=holders1.bal;
        holder1[i].amount=holders1.amount;
      }
      i++;
        }

        fclose(fp1);
        fp1=fopen("bank3.txt","w");
        for(j=0;j<i;j++)
        {
     fprintf(fp1," %d %s %d %f %f\n",holder1[j].accono,holder1[j].name,holder1[j].duration,holder1[j].bal,holder1[j].amount);
        }
        if(nfound==0)
        {
   music(1);
   printf("\nIncorrect Account No\n");
        }
        else
        {
  printf("\nInformation modified successfully\n");
        }
        fclose(fp1);
        break;

       case 7: clrscr();
        music(1);
        fp1=fopen("bank3.txt","r");
        if(fp1==NULL)
        {
  music(1);
  printf("\t\t\tThere are no Accounts\n");
  printf("\t\t\t---------------------\n");
        }
        else
        {
  music(1);
  i=0;
  ch=0;
  ch1=0;
  while(fscanf(fp1," %d %s %d %f %f",&holders1.accono,&holders1.name,&holders1.duration,&holders1.bal,&holders1.amount)!=EOF)
  {
    i++;
    ch=ch+holders1.bal;
    ch1=ch1+holders1.amount;
  }

        printf("\n\t\t ====================================");
        printf("\n\t\t   Total Deposited Amount is %d\n\t\t   Total no of Account is %d\n\t\t   Total Amount to be payable is %d",ch,i,ch1);
        printf("\n\t\t ====================================\n");
        }
        fclose(fp1);
        break;
       case 8: call();

       default:clrscr();
        music(1);
        printf("\n\t\t\tEnter Correct choice!!\n\n");
        break;

     }
    }while(ch);
 }
  call3()
   {
    FILE *fp1;
  struct bank2 holder2[20],holders2;
  int ch=1,ch1,i=0,n,accono,j=0,found,ch3,nfound,ch4,nfound1,no;
  char choice,string1[10],string2[10],user[10];
   clrscr();
   do
    {
     music(2);
     printf("\t\t    ===========================");
     printf("\n\n\t\t\t      MENU");
     printf("\n\t\t   *******************************");
     printf("\n\t\t    1: Create User\n\t\t    2: Add User\n\t\t    3: Modify Password  4: Delete User\n\t\t    5: Home page\n\t\t");
     printf("\n\t\t   *******************************");
     printf("\n\t\t   Enter your choice=");
     scanf("%d",&ch);
     switch(ch)
     {
       case 1: clrscr();
        music(1);
        fp1=fopen("bank4.txt","w");
        fclose(fp1);
        do
        {
    fp1=fopen("bank4.txt","r");
    printf("\nEnter User Name:");
    gets(user);
    printf("\nEnter password:");
    scanf("%d",&accono);
    nfound=0;
    while(fscanf(fp1," %s %d",&holders2.user,&holders2.pass)!=EOF)
    {
      if(strcmp(accono,holders2.pass)==0 && strcmp(user,holders2.user)==0)
      {
      nfound=1;
      }
    }
    fclose(fp1);
    if(nfound==0)
    {
     fp1=fopen("bank4.txt","a");
     fprintf(fp1," %s %d\n",user,accono);
     i++;
     fclose(fp1);
    }
    else
    {
    clrscr();
    music(1);
    printf("\nThe user already exit\nTry again\n");
    }
    printf("\nAdd more?(yes=1/no=0)");
    scanf("%d",&ch1);
    clrscr();
   }while(ch1==1);
        n=i;
        break;
       case 2: clrscr();
        i=n;
        do
        {
        music(2);
        fp1=fopen("bank4.txt","r");
        printf("\nEnter User Name:");
        gets(user);
        printf("\nEnter password:");
        scanf("%d",&accono);
        nfound=0;
        while(fscanf(fp1," %s %d",&holders2.user,&holders2.pass)!=EOF)
  {
     if(strcmp(accono,holders2.pass)==0 && strcmp(user,holders2.user)==0)
    {
      nfound=1;
    }
  }
  fclose(fp1);
  if(nfound==0)
  {
   fp1=fopen("bank4.txt","a");
   fprintf(fp1," %s %d\n",user,accono);
   i++;
   printf("\nNew user added successfully");
   fclose(fp1);
   }
   else
   {
    clrscr();
    music(1);
    printf("\nThe user already exist\nTry again\n");
   }
   printf("\n\nWant to add more user?(yes=1/no=0)");
   scanf("%d",&ch1);
   clrscr();
        }while(ch1==1);
        n=i;
        break;

     case 3: clrscr();
        music(2);
        fp1=fopen("bank4.txt","r");
        i=0;
        nfound=0;
        printf("\nEnter User Name=");
        gets(user);
        printf("\nEnter the password=");
        scanf("%d",&accono);
        while(fscanf(fp1," %s %d",&holders2.user,&holders2.pass)!=EOF)
        {
     if(strcmp(accono,holders2.pass)==0 && strcmp(user,holders2.user)==0)

      {
        nfound=1;
        printf("\nEnter new password=");
        scanf("%d%s%f",&holder2[i].pass);
        strcpy(holder2[i].user,user);
      }
      else
      {
        holder2[i].pass=holders2.pass;
        strcpy(holder2[i].user,holders2.user);
      }
      i++;
        }
        fclose(fp1);
        fp1=fopen("bank4.txt","w");
        for(j=0;j<i;j++)
        {
     fprintf(fp1," %s %d\n",holder2[j].user,holder2[j].pass);
        }
        if(nfound==0)
        {
   music(1);
   printf("\nIncorrect User No\n");
        }
        else
        {
  printf("\nModified successfully\n");
        }
        fclose(fp1);
        break;

       case 4: clrscr();
        music(1);
        fp1=fopen("bank4.txt","r");
        i=0;
        found=0;
        printf("\n\t\t         Enter User Name=");
        gets(user);
        printf("\n\t\t         Enter password=");
        scanf("%d",&accono);
        while(fscanf(fp1," %s %d",&holders2.user,&holders2.pass)!=EOF)
        {
    if(strcmp(accono,holders2.pass)==0 && strcmp(user,holders2.user)==0)
    {

    music(1);
    printf("\n\t\t    User Deleted Successfully!!\n\n");
    found=1;
   }
   else
   {
    holder2[i].pass=holders2.pass;
    strcpy(holder2[i].user,holders2.user);
    i++;
   }
        }
        if(found==0)
        {
  music(1);
  printf("\n\t\t\tUser was not found!!\n\n");
        }
        fclose(fp1);
        fp1=fopen("bank4.txt","w");
        for(j=0;j<i;j++)
        {
  fprintf(fp1," %s %d\n",holder2[j].user,holder2[j].pass);
        }
        fclose(fp1);
        break;


       case 5: call();
       default:clrscr();
        music(1);
        printf("\n\t\t\tEnter Correct choice!!\n\n");
        break;

     }
    }while(ch);

   }
  }

 music(int type)
 {
 float octave[7]={130.81,146.83,164.81,174.61,196,220,246.94};
 int n,i;
 switch(type)
 {
 case 1 :
 for(i=0;i<7;i++)
 {
 sound(octave[i]*8);
 delay(30);
 }
 nosound();
 break;

 case 2:
 for(i=0;i<15;i++)
 {
 n=random(7);
 sound(octave[n]*4);
 delay(100);
 }
 nosound();
 break;

 case 3:
 while(!kbhit())
 {
 n=random(7);
 sound(octave[n]*4);
 delay(100);
 }
 nosound();
 if(getch()==0)
 getch();
 break;
 case 4:
 for(i=4;i>=0;i--)
 {
 sound(octave[i]*4);
 delay(15);
 }
 nosound();
 break;

 case 5:
 sound(octave[6]*2);
 delay(50);
 nosound();
 }
}



 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | cheap international calls