Sunday, 9 June 2013

PL/SQL program to update Table with select query

Create table place_jack (room_id varchar2(5), building varchar2(20), room_no number(5), no_of_seats number(5),description varchar2(50)) ;

Inserting values in the table:

Insert into place_jack (room_id, building, room_no, no_of_seats) values(‘p001’,’A’,101,30);
Insert into place_jack (room_id, building, room_no, no_of_seats) values(‘p002’,’B’,202,15);
Insert into place_jack (room_id, building, room_no, no_of_seats) values(‘p003’,’C’,204,35);


PL\SQL Block:

DECLARE
nos number(5);
rid varchar2(5);
BEGIN
rid:='&rid';
SELECT no_of_seats INTO nos from place_jack where room_id=rid;
if nos<20 then
update place_jack set description='fairly small' where room_id=rid;
else
if nos between 20 and 40 then
update place_jack set description='a little bigger' where room_id=rid;
else
if nos between 20 and 40 then
update place_jack set description='lots of room' where room_id=rid;
end if;
end if;
end if;
END;

PL/SQL program to update a table

Create table emp29(name varchar2(22),startdate date,salary number(33),flag number(22));

Inserting values in the table:

Insert into emp29 values('abc','1-mar-99',4000,1);
Insert into emp29 values('ac','1-mar-99',40040,0);
Insert into emp29 values('bc','1-mar-99',1000,1);


PL/SQL block:

DECLARE
date_compare date;
date_ofjoin date;
BEGIN
date_compare:='&date_compare';
if(to_char(date_ofjoin) < to_char(date_compare)) then
update emp29
set salary=salary+(salary*0.15);
else
update emp29 set salary=salary+(salary*0.5) ;
end if;
END;

PL/SQL program for Inserting Values in a table.

Creating the table:

Create table suppliers29 (s_id varchar2(20), name varchar2(20), salary number(10));


PL\SQL Block:

DECLARE
status varchar2(20);
BEGIN
status:='&status';
loop
if(status='y')then
insert into supplier29 values('s1001','abc',8765);
end if;
status:='&status';
dbms_output.put_line('want  another(y/n)');
if(status='n')then
exit;
end if;
end loop;
END;

PL/SQL program for defining and calling simple function

create or replace function Addition(x number,y number)
return number is
z number;
begin
z:=x+y;
return z;
end;
---------------------------------------
declare
z number;
begin
z:=Addition(5,2);
dbms_output.put_line('The Addition:-'||z);
end;


Output:
The Addition:-7

PL/SQL procedure successfully completed.

Basic PL/SQL Loop Program

DECLARE
counts number(2);
BEGIN
counts:=1;
dbms_output.put_line('before my loop');
loop
if(counts>4)then
exit;
end if;
dbms_output.put_line('iteration no'||counts);
counts:=counts+1;
end loop;
END;

Wednesday, 5 June 2013

Queue using Array

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>


class queue
{
    int array[15];

    int front,rear;
    public:
    queue()
    {
        front=-1;
        rear=-1;
    }
    void enqueue(int);
    void dequeue();
    void print();

};

void queue::print()
{
if(front <=rear)
{
    for(int i=front;i<=rear;i++)
    {
    cout<<endl<<array[i];
    }
}
else
cout<<"\n queue is empty ......";

}

void queue::dequeue()
{
if(rear < front)
{
rear=-1;
front=-1;
cout<<"\n queue is empty ......";

}
else
{
cout<<array[front]<<" is dequeue out form the queue";
front++;


}
}

void queue::enqueue(int data)
{

    if(rear<15)
    {

        rear=rear+1;
        array[rear]=data;
        if(front<0) front=0;
    }
    else
    cout<<"\n\t queue Full";


}


void main()
{
    clrscr();
    queue s;

    int count,data;

    char choice,option;

do
{
    clrscr();
        cout<<"\n\t   Select Operation \n";
        cout<<"\n\t1. To Create the queue";
        cout<<"\n\t2. To Display the queue";
        cout<<"\n\t3. To Add the data in the queue";
        cout<<"\n\t4. To Delete the data from the queue";
        cout<<"\n\t0. Exit";
        cout<<"\n\n\t   Enter your choice:- ";
        cin>>option;
        switch(option)
        {
            case '1':
                    {
                     cout<<"\n \t How many item you want to added in the queue : ";
                     cin>>count;
                        for(int i=0;i<count;i++)
                        {
                            cout<<"\n \t Enter data which is need to be added in the queue: ";
                            cin>>data;
                            s.enqueue(data);
                        }
                        break;
                    }
            case '2':
                    {
                     cout<<"\n \t  Data in the queue";
                     s.print();
                     break;
                    }

            case '3':
                    {
                     cout<<"\n \t Enter data which is need to be added in the queue: ";
                     cin>>data;
                     s.enqueue(data);
                     break;
                    }
            case '4':
                    {
                        s.dequeue();
                        break;
                    }
            case '0':
                    {
                     exit(0);
                     break;
                    }
        default:
                    {
                        cout<<"\n \tInvalid Option";
                        break;
                    }
        }
        cout<<"\n\n \tDo you want to continue (y/n):- ";
        cin>>choice;
    }while(choice!='n');
    getch();
}

Priority Queue in Data Structure

#include<iostream.h>
#include<conio.h>
class pqueue
{
    pqueue *pre;
    int data;
    int pri;
    int num,prio;
    pqueue *next;
    void add(int,int);
    int remove(void);
    public :
        void display(void);
        void priority(void);
};
pqueue *front;
pqueue *rear;
void pqueue :: add(int num,int pri)
{

    if((front==NULL) && (rear==NULL))
    {
        front=rear= new pqueue;
        rear->next=NULL;
        rear->data=num;
        rear->pri=pri;
        rear->pre=NULL;

    }
    else
    {

        rear->next= new pqueue;
        rear->next->pre= rear;
        rear->next->data = num;
        rear->next->next=NULL;
        rear->next->pri=pri;
        rear = rear->next;

    }
}
void pqueue :: display(void)
{
    pqueue *q = front;

    if(front==NULL && rear== NULL)
    {
        cout<<"Queue is empty"<<endl;
    }
    else
    {
        while(q !=NULL)
        {
            cout<<"Numbers are:"<<endl<< q->data<<endl;
            q = q->next;
        }
    }
}
int pqueue :: remove (void)
{
    int result;
      //    queue *q=front;

    if(front==NULL && rear==NULL)
    {
        cout<<"Queue is empty"<<endl;
        return -32767;
    }
        result = front->data;
        front = front->next;
        front->pre = NULL;
        if(front != NULL)
        {
            delete (rear->next);
            rear ->next = NULL;
        }
        else
        {
            delete (front);
            front=NULL;
        }
        return result;
}
void pqueue :: priority(void)
{
    if(front==NULL && rear == NULL)
    {
        cout<<"Queue is Empty" <<endl;
        char ch='y';
        cout<<"Do you want to add new?"<<endl;
        cin>>ch;
        while(ch=='y')
        {
            cout<<"Enter the data "<<endl;
            cin>>data;
            cout<<"Enter the priority"<<endl;
            cin>>pri;

            add(data,pri);

            break;
          }


    }
    else
    {

        if( rear->pri >= front->pri)
        {
            cout<<"Enter The data"<<endl;
            cin>>data;

            cout<<"Enter the priority"<<endl;
            cin>>pri;

            add(data,pri);



        }
         else
        {
            int r= remove();
            cout<<"Removed = "<<r<<endl;

        }
         

    }
}

void main()
{
    clrscr();

    int num;
    char choice='y';
    pqueue q;
    front=rear=NULL;
    int option;
    while(choice == 'y')
    {
        cout<<"1.Priority operation"<<endl;
      
        cout<<"2.Display"<<endl;
       

        cout<<"enter the option"<<endl;
        cin>>option;

        switch(option)
        {
            case 1:
            {
               
                q.priority();
                cout<<"Do you want to cont...?"<<endl;
                break;
            }
       
            case 2:
            {
                q.display();
                cout<<"Do you want to cont...?"<<endl;
                break;
            }
       
            case 5:
            {
                int r = q.remove();
                cout<<"Removed ="<<r<<endl;
                cout<<"Do you want to cnt....?"<<endl;
                break;
            }      */
            default:
            {
                cout<<"Wrong input"<<endl;
                cout<<"Do you want to cont...?"<<endl;
                break;

            }
        }
        choice=getch();
    }


}