Monday, April 6, 2015

C program for preorder inorder postorder in data structure

#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<stdlib.h>
struct node
{
 int info;
 struct node *llink;
 struct node *rlink;
}*root=NULL;
void insert();
void preorder(struct node *q);
void inorder(struct node *q);
void postorder(struct node *q);
int stack[10];
int top=-1;
void main()
{
 int n;
 clrscr();
 while(1)
 {
  printf("\n **menu**");
  printf("\n 1.insert \n 2.preorder");
  printf("\n 3.inorder \n 4.postorder");
  printf("\n 5.exit");
  printf("\n enter your choice:-");
  scanf("%d",&n);
  switch(n)
  {
   case 1 : insert();
   break;
   case 2 : preorder(root);
   break;
   case 3 : inorder(root);
   break;
   case 4 : postorder(root);
   break;
   case 5 : exit(0);
  }
 }
 getch();
}
void insert()
{
 struct node *temp,*q;
 int x,i,n;
 printf("\n\n enter how many nodes you want:-");
 scanf(" %d",&n);
 for(i=0;i<n;i++)
 {

  temp=(struct node*)malloc(sizeof(struct node));
  if(temp==NULL)
  {
    printf("\n insufficient memory");
  }
  else
  {
   printf("\n\n enter number:-");
   scanf("%d",&x);
   temp->info=x;
   temp->llink=NULL;
   temp->rlink=NULL;
   if(root==NULL)
   {
    root=temp;
   }
   else
   {
    q=root;
    while(1)
    {
     if(temp->info==q->info)
     {
       printf("\n\n element already exist");
       break;
     }
     if(temp->info>q->info)
     {
      if(q->rlink==NULL)
      {
       q->rlink=temp;
       break;
      }
      q=q->rlink;
     }
     if(temp->info<q->info)
     {
      if(q->llink==NULL)
      {
       q->llink=temp;
       break;
      }
      q=q->llink;
     }
    }
   }
  }
 }
}
void preorder(struct node *q)
{
 top++;
 stack[top]=q;
 while(top!=-1)
 {
  q=stack[top];
  top--;
  if(q!=NULL)
  {
   printf("  %d->",q->info);
   top++;
   stack[top]=q->rlink;
   top++;
   stack[top]=q->llink;
  }
 }
}
void inorder(struct node *q)
{

  while(top!=-1||q!=NULL)
  {
   if(q!=NULL)
   {
    top++;
    stack[top]=q;
    q=q->llink;
   }
   else
   {
    q=stack[top];
    top--;
    printf(" %d->",q->info);
    q=q->rlink;
   }
  }
}


void postorder(struct node *q)
{
 int f[5];
 int top_p;
 stack[++top]=NULL;
 do
 {
  while(q!=NULL)
  {
   stack[++top]=q;
   f[top]=1;
   if(q->rlink!=NULL)
   {
    stack[++top]=q->rlink;
    f[top]=-1;
   }
   q=q->llink;
  }
  top_p=top;
  q=stack[top--];
  while(f[top_p]==1)
  {
   printf(" %d->",q->info);
   top_p=top;
   q=stack[top--];
  }
 }
  while(q!=NULL);

}

C program in data structure for tree

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
 int info;
 struct node *llink,*rlink;
}*root=NULL;
void create();
void node(struct node *q);
void degree(struct node *q);
void leaf(struct node *q);
void interior(struct node *q);
void child(struct node *q);
int no,l,in,ch,p;
void main()
{
 int n;
 clrscr();
 while(1)
 {
  printf("\n **menu**");
  printf("\n 1.create a tree \n 2.count num of nodes");
  printf("\n 3.degree of tree \n 4.leaf nodes \n 5.interior nodes");
  printf("\n 6.childrens and parent \n 7.exit");
  printf("\n enter your choice:-\t");
  scanf("%d",&n);
  switch(n)
  {
   case 1 : create();
   break;
   case 2 : node(root);
   printf("\n\n total nodes are:-  %d",no);
   break;
   case 3 : degree(root);
   break;
   case 4 : leaf(root);
   printf("\n\n leaf nodes is:-  %d",l);
   break;
   case 5 : interior(root);
   printf("\n\n interior nodes are:-  %d",in);
   break;
   case 6 : child(root);
   printf("\n\n childrean is:- %d and parents is:-  %d",ch,p);
   break;
   case 7 : exit(0);
  }
 }
}
void create()
{
 struct node *temp,*q;
 int x,n,i;
 printf("\nEnter how many nodes you want:-");
 scanf("  %d",&n);
 for(i=0;i<n;i++)
 {
  temp=(struct node *)malloc(sizeof(struct node));
  if(temp==NULL)
  {
   printf("\n insufficient memory");
  }
  else
  {
   printf("\n enter number:- ");
   scanf(" %d",&x);
   temp->info=x;
   temp->llink=NULL;
   temp->rlink=NULL;
   if(root==NULL)
   {
    root=temp;
   }
   else
   {
    q=root;
    while(1)
    {
     if(temp->info==q->info)
     {
      printf("\n number is aleardy exist");
      break;
     }
     if(temp->info>q->info)
     {
      if(q->rlink==NULL)
      {
       q->rlink=temp;
       break;
      }
      q=q->rlink;
     }
     if(temp->info<q->info)
     {
      if(q->llink==NULL)
      {
       q->llink=temp;
       break;
      }
      q=q->llink;
     }
    }
   }
  }
 }
}
void node(struct node *q)
{
 if(q!=NULL)
 {
  node(q->llink);
  node(q->rlink);
  no=no+1;
 }
}
void degree(struct node *q)
{
 if(q!=NULL)
 {
  if(q->llink!=NULL&&q->rlink!=NULL)
  {
   printf("\n\n degree of %d node is:= 2",q->info);
  }
  if(q->llink!=NULL&&q->rlink==NULL)
  {
   printf("\n\n degree of %d node is:= 1",q->info);
  }
  if(q->llink==NULL&&q->rlink!=NULL)
  {
   printf("\n\n degree of %d node is:= 1",q->info);
  }
  if(q->llink==NULL&&q->rlink==NULL)
  {
   printf("\n\n degree of %d node is:= 0",q->info);
  }
  degree(q->llink);
  degree(q->rlink);
 }
}
void leaf(struct node *q)
{
 if(q!=NULL)
 {
  leaf(q->llink);
  leaf(q->rlink);
  if(q->llink==NULL||q->rlink==NULL)
  {
   l=l+1;
  }
 }
}
void interior(struct node *q)
{
 if(q!=NULL)
 {
  interior(q->llink);
  interior(q->rlink);
  if(q->llink!=NULL||q->rlink!=NULL)
  {
   in=in+1;
  }
 }
}
void child(struct node *q)
{
 if(q!=NULL)
 {
  child(q->llink);
  child(q->rlink);
  if(q->llink!=NULL||q->rlink!=NULL)
  {
   p=p+1;
  }
  if(q->llink==NULL||q->rlink==NULL)
  {
   ch=ch+1;
  }
 }
}

C program in data structure for mirror of a tree

#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<stdlib.h>
struct node
{
 int info;
 struct node *llink;
 struct node *rlink;
}*root=NULL;
void insert();
void preorder(struct node *q);
void mirror(struct node *q);
void main()
{
 int n;
 clrscr();
 while(1)
 {
  printf("\n **menu**");
  printf("\n 1.insert \n 2.preorder");
  printf("\n 3.mirror \n 4.exit");
  printf("\n enter your choice:-");
  scanf("%d",&n);
  switch(n)
  {
   case 1 : insert();
     break;
   case 2 : printf("\n\n our tree in preorder \n\n");
     preorder(root);
     break;
   case 3 :
     mirror(root);
     printf("\n\n mirror image is:-\n\n");
     preorder(root);
     break;
   case 4 : exit(0);
  }
 }
 getch();
}




void insert()
{
 struct node *temp,*q;
 int x,i,n;
 printf("\n\n enter how many node you want:=");
 scanf("%d",&n);
 for(i=0;i<n;i++)
 {

  temp=(struct node*)malloc(sizeof(struct node));
  if(temp==NULL)
  {
   printf("\n insufficient memory");
  }
  else
  {
   printf("\n\n enter number:-");
   scanf("%d",&x);
   temp->info=x;
   temp->llink=NULL;
   temp->rlink=NULL;
   if(root==NULL)
   {
    root=temp;
   }
   else
   {
    q=root;
    while(1)
    {
     if(temp->info==q->info)
     {
      printf("\n\n element already exist");
      break;
     }
     if(temp->info>q->info)
     {
      if(q->rlink==NULL)
      {
       q->rlink=temp;
       break;
      }
      q=q->rlink;
     }
     if(temp->info<q->info)
     {
      if(q->llink==NULL)
      {
       q->llink=temp;
       break;
      }
      q=q->llink;
     }//end of if
    }//end of while
   }//end of else2
  }//end of else1
 }//end of for loop
}
void preorder(struct node *q)
{
 if(q!=NULL)
 {
  printf(" %d->",q->info);
  preorder(q->llink);
  preorder(q->rlink);
 }
}
void mirror(struct node *q)
{
 struct node *temp1;
 if(q!=NULL)
 {
  mirror(q->llink);
  mirror(q->rlink);
  temp1=q->llink;
  q->llink=q->rlink;
  q->rlink=temp1;
 }
}

Wednesday, April 1, 2015

C program in stack data structure for infix and postfix expression

#define max 50
#include<stdio.h>
#include<conio.h>
#include<process.h>
void push(char ch);
char pop();
char stack[max];
int top=-1;
int prec(char chr);
void main()
{
 char a[60],b[60],ch;
 int i,j,p,r,x,y,re;
 clrscr();
 printf("\n Enter Infix Expression:-\n");
 scanf("%s",&a);
 i=0;
 j=0;
 for(i=0;a[i]!='\0';i++)
 {
  switch(a[i])
  {
   case '^':
   case '$':
   case '*':
   case '/':
   case '+':
   case '-': p=prec(a[i]);
    while(top!=-1&&p<=prec(stack[top]))
    {
      b[j]=pop();
      j++;
    }
    push(a[i]);  //push operator into stack
    break;
   case '(': push(a[i]);
    break;

   case ')':
    do
    {
     ch=pop();
     if(ch!='(')
     {
      b[j]=ch;
      j++;
     }
    }while(ch!='(');
     break;
   default : b[j]=a[i];  //output exp
    j++;
  }
 }
 while(top!=-1)
 {
  b[j]=pop();  //last pop the all operators.
  j++;
 }
 b[j]='\0';
 printf("\n Postfix Expression Is:-\n %s",b);
 printf("\n Evaluation of the Postfix expression:-\n");
 i=0;
 while(b[i]!='\0')
 {
  if(b[i]>='0'&&b[i]<='9')
  {
   push(b[i]-48); //convert char into nuumber
  }
  else
  {
   y=pop(); //pop the two poerands and prefrom operation
   x=pop();
   switch(b[i])
   {
      case '+' : r=x+y;
break;
      case '-' : r=x-y;
break;
      case '*' : r=x*y;
break;
      case '/' : r=x/y;
break;
      case '$' :
      case '^' : r=pow(x,y);
break;
   }
   push(r); //push the result into stack
  }
  i++;
 }
 re=pop(); // at the last pop the result and display.
 printf("\n %d",re);
 getch();
}

int prec(char chr)
{
 switch(chr)   // check the priority of the operators.
 {
  case '$' :
  case '^' : return(5);
  case '*' :
  case '/' : return(4);
  case '+' :
  case '-' : return(3);
  case '(' : return(2);
 }
 return(0);
}
void push(char x)
{
  top++;
  stack[top]=x;
}
char pop()
{
 char y;
 y=stack[top];
 top--;
 return(y);
}



RDBMS example for one to many relationship

Q.).
         Customer (cno,cname,city)
         Account (ano,acc_type,balance)

          Relationships between  customer and account  is one-to-many.
 
         Constraints :-primary key,
                                 Balance should be>100

SQL> create table customer2
  2  ( cno number(5)primary key,
  3    cname varchar2(30),
  4    city varchar2(40)
  5  );
Table created.

SQL> insert into customer2
  2  values('&cno','&cname','&city');
Enter value for cno: 101
Enter value for cname: mahendra
Enter value for city: pali
old   2: values('&cno','&cname','&city')
new   2: values('101','mahendra','pali')
1 row created.
SQL> select * from customer2;

  CNO   CNAME   CITY
--------   -----------  ------------------
  101 mahendra       pali
  102 babulal          pune
 103 amit              mumbai

 CNO   CNAME   CITY
--------   -----------  ------------------
 104        raju                 pune
105          akash          Ajmer

SQL> create table account
  2  ( ano number(5)primary key,
  3    acc_type varchar2(20),
  4    balance number(5) check(balance>100),
  5    cno number(5)references customer2(cno)
  6  );
Table created.

SQL> insert into account
2 values('&ano','&acc_type','&balance','&cno');
SQL> select * from account;

       ANO ACC_TYPE                          BALANCE        CNO
---------- ------------------------------ ---------- ----------
      1201 saving                              13000        101
      1202 current                             20000        102
      1203 saving                              14000        103
      1204 saving                              50000        104
      1205 current                             14000        105
      1206 saving                              25000        104
      1207 current                             19000        105


B). wirte a cursor to add interest of 3% to the balance of all account whose balance is greater than 10000.

  1  declare
  2     cursor c1 is select ano from account
  3     where balance>10000;
  4  begin
  5     for x in c1 loop
  6       update account
  7       set balance=balance+balance*3/100
  8       where ano=x.ano;
  9       commit;
 10     end loop;
 11* end;
SQL> /
PL/SQL procedure successfully completed.

SQL> select * from account;

       ANO ACC_TYPE                          BALANCE        CNO
---------- ------------------------------ ---------- ----------
      1201 saving                              13390        101
      1202 current                             20600        102
      1203 saving                              14420        103
      1204 saving                              51500        104
      1205 current                             14420        105
      1206 saving                              25750        104
      1207 current                             19570        105

7 rows selected.


a). create  or replace a PL/ SQL procedure to find total balance of all customers of pune city.
  1  create or replace procedure dis(x in varchar)
  2  is
  3      b number(20);
  4  begin
  5      select sum(a.balance) into b from customer2 c,
  6      account a where c.cno=a.cno and
  7      c.city=x;
  8        dbms_output.put_line('total balance of all customers of pune city:-'||b);
  9* end;
 10  /

Procedure created.

SQL> declare
  2
  3  begin
  4    dis('pune');
  5  end;
  6  /
total balance of all customers of pune city:-97850

PL/SQL procedure successfully completed.




RDBMS example of many to many relationship

Q.).    book ( bno, bname, pubname, price)
 
         Author ( ano, aname)
 
         Relationships between book and author is many to many.

         Constraints:-primary key,
                                Aname and pubname should NOT NULL.


SQL> select * from book_1;

       BNO BNAME                PUBNAME                             PRICE
---------- -------------------- ------------------------------ ----------
       101 C++                           nirali                                150
       102 M A/C                      vision                                250
       103 RDBMS                    BPB                                   175
       104 data structre              BPB                                   165
       105 software engineering   nirali                                135

SQL> select * from author;

       ANO ANAME
---------- --------------------
      1201  mr.dewasi
      1202  mr.shiravi b
      1203  mr.pankaj
      1204  kanetkar
      1205  babulal

SQL> select * from bookauth;

    BNO        ANO
---------- ----------
       101       1201
       102       1202
       103       1203
       104       1204
       102       1204
       105       1205
       101       1203

7 rows selected.


************************************************************************
a). create or replace a PL/SQL procedure to display details of all books written by ‘kanetkar’.

SQL>
  1  create or replace procedure disa(t in varchar2)
  2  is cursor ca is select b.bno,b.bname,b.pubname,b.price from book_1 b,
  3     author a, bookauth ba where b.bno=ba.bno and a.ano=ba.ano and
  4     a.aname=t;
  5  begin
  6     for x in ca loop
  7        dbms_output.put_line(x.bno||' '||x.bname||' '||x.pubname||' '||x.price);
  8     end loop;
  9* end;
SQL> /

Procedure created.

SQL>
  1  declare
  2  begin
  3     disa('kanetkar');
  4* end;
  5  /
104 data structre  BPB 165
102 M A/C vision 250

PL/SQL procedure successfully completed.


************************************************************************

b). create or replace a trigger that restricts insertion or updation of books having price less than 0.

SQL>
  1  create or replace trigger ta1
  2  before insert or update
  3  on book_1
  4  for each row
  5  declare
  6    p book_1.price%type;
  7  begin
  8    p:=:new.price;
  9    if(p<0) then
 10      raise_application_error(-20089,'book price must be>0');
 11    end if;
 12* end;
SQL> /

Trigger created.

SQL> insert into book_1
  2  values(106,'SE','nirali',-45);
insert into book_1
            *
ERROR at line 1:
ORA-20089: book price must be>0
ORA-06512: at "SCOTT.TA1", line 6
ORA-04088: error during execution of trigger 'SCOTT.TA1'


SQL> update book_1
  2  set price=-45
  3  where bno=101;
update book_1
       *
ERROR at line 1:
ORA-20089: book price must be>0
ORA-06512: at "SCOTT.TA1", line 6
ORA-04088: error during execution of trigger 'SCOTT.TA1'

************************************************************************

RDBMS example of book and dept many to one relationship

Q.) Book( bno, bname, pudname, price)

        Department( dno, dname)

        Relationships between book and department is many to one.
   
        Constraints:-primary key,  Price should bo>0.

SQL> select * from department_1;
    DNO DNAME
---------- ------------------------------
       101 computer
       102 science
       103 arts
       104 MBA
       105 MSC
SQL> select * from books_1;
 BNO     BNAME   PUBNAME    PRICE  DNO
-------   -------------  ---------------   --------- --------
201      C++              nirali               150      101
1202    numerical     BPB                180      102
             method  
1203     RDBMS     vision               140      101


  BNO     BNAME   PUBNAME    PRICE  DNO
-------   -------------  ---------------   --------- --------
1204    data structure       nirali          145       104
1205     social sic         vision            120      105
1206       POM              BPB             130      103

6 rows selected.
************************ ** ** ******************************************

a).create or replace a PL/SQL function to return total expenditure on books of a given department.

SQL>
  1  create or replace function disb(x in varchar2)return number
  2  is
  3    c number(5);
  4  begin
  5    select sum(b.price) into c from department_1 d,books_1 b
  6    where d.dno=b.dno and d.dname=x;
  7    return(c);
  8* end;
SQL> /
Function created.

SQL>
  1  declare
  2     dn department_1.dname%type;
  3     t number(10);
  4  begin
  5     dn:='&dn';
  6     t:=disb(dn);
  7     dbms_output.put_line('total expenditure on books of a'||' '||dn||' '||'is:-'||t);
  8* end;
SQL> /
Enter value for dn: computer
old   5:    dn:='&dn';
new   5:    dn:='computer';
total expenditure on books of a computer is:-290

PL/SQL procedure successfully completed.


************************************************************************
************************************************************************

b). write a cursor to display details of all books brought for a ‘computer’ department.

SQL>
  1  declare
  2    cursor c8 is select b.bno,b.bname,b.pubname,b.price from department_1 d,
  3    books_1 b where d.dno=b.dno and d.dname='computer';
  4  begin
  5    for x in c8
  6    loop
  7      dbms_output.put_line(x.bno||' '||x.bname||' '||x.pubname||' '||x.price);
  8    end loop;
  9* end;
SQL> /
1201 C++ nirali 150
1203 RDBMS vision 140

PL/SQL procedure successfully completed.


************************************************************************

Program to develop for cost saving in hotel industry

 To develop a program for cost-saving in a hotel, you can consider the following features: Key Features 1. *Room Management*: Optimize room ...