Database Operations without pointers
Objective
#include
#include
#include
typedef struct employee
{ int code;
char name[20];
int salary;
}employee;
void insert(employee st[],int position,int n);
void Delete(employee st[],int position,int n);
int search(employee st[],int code,int n);
void print(employee st[],int n);
void read(employee st[],int n);
void sort(employee st[],int n);
void modify(employee st[],int n);
void main()
{ employee st[30];
int n,i,op,position,code;
clrscr();
do{
flushall();
printf("\n1)create\n2)insert\n3)delete\n4)search\n5)print\n6)Sort\n7)Modify");
printf("\n8)Quit");
printf("\nEnter Your Choice:");
scanf("%d",&op);
switch(op)
{ case 1: printf("\nEnter No. of employees:");
scanf("%d",&n);
read(st,n);
break;
case 2: printf("\n enter the position(no of records=%d):",n);
scanf("%d",&position);
if(position<=n+1)
{
insert(st,position,n);
n++;
print(st,n);
}
else
printf("\n can not insert");
break;
case 3:printf("\n enter the code : ");
scanf("%d",&code);
position=search(st,code,n);
if(position != -1 )
{
Delete(st,position,n);
n--;
print(st,n);
}
else
printf("\n can not delete ");
break;
case 4: printf("\nenter code:");
scanf("%d",&code);
position=search(st,code,n);
if(position==-1)
printf("\nnot found");
else
{ printf("\n found at location=%d",position+1);
printf("\n %s\t%d\t%d",st[position].name,st[position].code,st[position].salary);
}
break;
case 5: print(st,n);
break;
case 6: sort(st,n);print(st,n);break;
case 7: modify(st,n);print(st,n);break;
}
}while(op!=8);
}
void insert( employee st[],int position,int n)
{ int i;
printf("\n enter data(name code salary): ");
for(i=n-1;i>=position-1;i--) /*index is 1 less than position*/
st[i+1]=st[i];
scanf("%s%d%d",st[position-1].name,&st[position-1].code,&st[position-1].salary);
}
void Delete(employee st[],int position,int n)
{ int i;
for(i=position+1;i st[j+1].code)
{
temp=st[j];
st[j]=st[j+1];
st[j+1]=temp;
}
}
Output
1)create 2)insert 3)delete 4)search 5)print 6)Sort 7)Modify 8)Quit Enter Your Choice:1 Enter No. of employees:2 enter data(name code salary): John 1 2000 Jenny 2 3000 1)create 2)insert 3)delete 4)search 5)print 6)Sort 7)Modify 8)Quit Enter Your Choice:2 enter the position(no of records=2):3 enter data(name code salary): Merry 3 5000 John 1 2000 Jenny 2 3000 Merry 3 5000 1)create 2)insert 3)delete 4)search 5)print 6)Sort 7)Modify 8)Quit Enter Your Choice:3 enter the code : 2 John 1 2000 Merry 3 5000 1)create 2)insert 3)delete 4)search 5)print 6)Sort 7)Modify 8)Quit Enter Your Choice:4 enter code:3 found at location=2 Merry 3 5000 1)create 2)insert 3)delete 4)search 5)print 6)Sort 7)Modify 8)Quit Enter Your Choice:5 John 1 2000 Merry 3 5000 1)create 2)insert 3)delete 4)search 5)print 6)Sort 7)Modify 8)Quit Enter Your Choice:6 John 1 2000 Merry 3 5000 1)create 2)insert 3)delete 4)search 5)print 6)Sort 7)Modify 8)Quit Enter Your Choice:7 Enter the code : 3 enter data(name code salary): Diya 5 4000 John 1 2000 Diya 5 4000 1)create 2)insert 3)delete 4)search 5)print 6)Sort 7)Modify 8)Quit Enter the code : 8