Thursday 22 September 2016

C++ program for simulating job queue.


Group D: (Assignment No:01) (SPPU Syllabus Asignment No:28)

Problem Statement:

Queues are frequently used in computer programming, and a typical example is the creation of a job queue by an operating system. If the operating system does not use priorities, then the jobs are processed in the order they enter the system. Write C++ program for simulating job queue. Write functions to add job and delete job from queue.



Program Code:

//========================================
// Name        : priosimpq.cpp
// Author      : Nitin Shivale
// Version     : 1
// Copyright   : Your copyright notice
// Description : Operating System simple job queue and priority job queue in C++, Ansi-style
//================================================

#include <iostream>
using namespace std;
#define size 5

class spq
{
    int f,r,job,djob;            //data members
    int simpq[size],prioq[size];
public:
    spq() //Default constructor
    {
     f=r=-1; //init front and rear to -1.
     job=djob=0;
     prioq[-1]=0;
    }
    //To check Q is full or not
    int isQfull()
    {
        if(r==size-1)
            return 1;
        else
            return 0;
    }
    //To check Q is empty or not
    int isQempty()
    {
        if((f==-1)||(f>r))
            return 1;
        else
            return 0;
    }
    void simpqadd(); //member functions.
    void showsimpleQ();
    void delsimpleQ();
    void prioqadd();
    void delprioQ();
    void showprioQ();
};

//To insert the job inside the simple queue.
void spq::simpqadd()
{
    cout<<"\nEnter the Job: ";
    cin>>job;
    if(isQfull())
        cout<<"\nSorry !! Queue is full....\n";
    else
    {
        if(f==-1)
        {
            f=r=0;
            simpq[r]=job;
        }
        else
        {
            r=r+1;
            simpq[r]=job;
        }
    }

}

//To delete job from the simple queue.
void spq::delsimpleQ()
{
    if(isQempty())
        cout<<"\nSorry Q is empty...\n";
    else
    {
        djob=simpq[f];
        f=f+1;
        cout<<"\nDeleted job is: "<<djob;
    }
}

//To show all the jobs from SimpleQueue.
void spq::showsimpleQ()
{
    cout<<"\nThe simple Queue job are as follows....\n";
    int temp;
    for(temp=f;temp<=r;temp++)
    {
        cout<<"\t"<<simpq[temp];
    }
}

//To delete job from the simple queue.
void spq::delprioQ()
{
    if(isQempty())
        cout<<"\nSorry Q is empty...\n";
    else
    {
        djob=prioq[f];
        f=f+1;
        cout<<"\nDeleted job is: "<<djob;
    }
}

//To show all the jobs from PrioQueue.
void spq::showprioQ()
{
    cout<<"\nThe priority Queue job are as follows....\n";
    int temp;
    for(temp=f;temp<=r;temp++)
    {
        cout<<"\t"<<prioq[temp];
    }
}

//To add the jobs as per the priority.
void spq::prioqadd()
{
    int t=0;
    cout<<"\nEnter the job: ";
    cin>>job;
    if(isQfull())
        cout<<"\nSorry!! Priority Queue is full...\n";
    else
    {
        if(f==-1)
        {
            f=r=0; //initially when q is empty insert first job.
            prioq[r]=job;
        }
        else if(job<prioq[r]) //Check the priority(Ascending) of incoming job if it is high
        {
         t=r;
         while(job<prioq[t])//do until priority is high
         {
            prioq[t+1]=prioq[t]; //then shift all the jobs towards right
            t=t-1; //decrement index to check another job.
         }
         t=t+1; //increment index
         prioq[t]=job; //store job at its appropriate location.
         r=r+1; //increment rear index by one
        }
        else
        {
            r=r+1; // as per the priority store in Q.
            prioq[r]=job;
        }
    }
}

int main()
{
    spq s1,s2; //object creation. s1 for simple Q and s2 for priority Q
    int ch;
    do
    {
     cout<< "\n\t!!!Operating System Job Queue!!!" << endl; // prints the msg.
     cout<<"\n1.SimpleQ Add_Job\n2.SimpleQ Del_Job\n3.Show SimpleQ\n4.PrioQ Add_Job\n5.PrioQ Del_Job\n6.Show PrioQ";
     cout<<"\nEnter Your Choice:";
     cin>>ch;
     switch(ch)
     {
      case 1:s1.simpqadd();break;//calling adding element in simple Q without priority.
      case 2:s1.delsimpleQ();break;
      case 3:s1.showsimpleQ();break;
      case 4:s2.prioqadd();break;//calling adding element in priority Q with priority.
      case 5:s2.delprioQ();break;
      case 6:s2.showprioQ();break;
     }
    }while(ch!=7);
    return 0;
}
/************************** OUTPUT ***********************************


    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:1

Enter the Job: 10

    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:1

Enter the Job: 20

    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:1

Enter the Job: 15

    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:1

Enter the Job: 5

    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:1

Enter the Job: 6

    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:1

Enter the Job: 2

Sorry !! Queue is full....

    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:3

The simple Queue job are as follows....
    10    20    15    5    6
    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:4

Enter the job: 10

    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:6

The priority Queue job are as follows....
    10
    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:4

Enter the job: 20

    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:6

The priority Queue job are as follows....
    10    20
    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:4

Enter the job: 15

    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:6

The priority Queue job are as follows....
    10    15    20
    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:4

Enter the job: 5

    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:6

The priority Queue job are as follows....
    5    10    15    20
    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:4

Enter the job: 6

    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:6

The priority Queue job are as follows....
    5    6    10    15    20
    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:5

Deleted job is: 5
    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:6

The priority Queue job are as follows....
    6    10    15    20
    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:5

Deleted job is: 6
    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:6

The priority Queue job are as follows....
    10    15    20
    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:
 **************************************************************************/
data structures and algorithms Web Developer

Monday 19 September 2016

Pizza parlor accepting maximum M orders.


Group D : (Assignment No: 02) (SPPU Syllabus Assignmnet No: 31)

Problem Statement:
Pizza parlor accepting maximum M orders. Orders are served in first come first served basis. Order once placed cannot be cancelled. Write C++ program to simulate the system using circular queue using array.

Program Code:

//==============================================
// Name        : Pizza_parlor.cpp
// Author      : Nitin Shivale
// Version     : 1.
// Copyright   : Your copyright notice
// Description : ervedPizza parlor accepting maximum M orders.
//==============================================

#include <iostream>
using namespace std;
#define size 5
class pizza
{
    int porder[size];
    int front,rear;
public:
    pizza()
    {
     front=rear=-1;
    }
    int qfull()
    {
     if((front==0)&&(rear==(size-1))||(front==(rear+1)%size))
         return 1;
     else
         return 0;
    }
    int qempty()
    {
        if(front==-1)
            return 1;
        else
            return 0;
    }
    void accept_order(int);
    void make_payment(int);
    void order_in_queue();
};
void pizza::accept_order(int item)
{
    if(qfull())
        cout<<"\nVery Sorry !!!! No more orders....\n";
    else
    {
        if(front==-1)
        {
            front=rear=0;
        }
        else
        {
            rear=(rear+1)%size;
        }
        porder[rear]=item;
    }
}

void pizza::make_payment(int n)
{
    int item;
    char ans;
    if(qempty())
        cout<<"\nSorry !!! order is not there...\n";
    else
    {
      cout<<"\nDeliverd orders as follows...\n";
      for(int i=0;i<n;i++)
      {
          item=porder[front];
          if(front==rear)
          {
               front=rear=-1;
          }
          else
          {
              front=(front+1)%size;
          }
          cout<<"\t"<<item;
      }
      cout<<"\nTotal amount to pay : "<<n*100;
      cout<<"\nThank you visit Again....\n";
    }
}

void pizza::order_in_queue()
{
    int temp;
    if(qempty())
    {
        cout<<"\nSorry !! There is no pending order...\n";
    }
    else
    {
        temp=front;
        cout<<"\nPending Order as follows..\n";
        while(temp!=rear)
        {
            cout<<"\t"<<porder[temp];
            temp=(temp+1)%size;
        }
        cout<<"\t"<<porder[temp];
    }
}
int main()
{
    pizza p1;
    int ch,k,n;
    do
    {
     cout<<"\n\t***** Welcome To Pizza Parlor *******\n";
     cout << "\n1.Accept order\n2.Make_payment\n3.Pending Orders\nEnter u r choice: ";
     cin>>ch;
     switch(ch)
     {
      case 1:cout<<"\nWhich Pizza do u like most....\n";
             cout<<"\n1.Veg Soya Pizza\n2.Veg butter Pizza\n3.Egg_Pizza";
             cout<<"\nPlease enter u r order: ";
             cin>>k;
             p1.accept_order(k);
             break;
      case 2:cout<<"\nHow many Pizza ?";
             cin>>n;
             p1.make_payment(n);
             break;
      case 3:cout<<"\n Following orders are in queue to deliver....as follows..\n";
             p1.order_in_queue();
             break;
     }
    }while(ch!=4);

    return 0;
}
/******************* OUTPUT *********************************************************

    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 1

Which Pizza do u like most....

1.Veg Soya Pizza
2.Veg butter Pizza
3.Egg_Pizza
Please enter u r order: 2

    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 1

Which Pizza do u like most....

1.Veg Soya Pizza
2.Veg butter Pizza
3.Egg_Pizza
Please enter u r order: 3

    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 1

Which Pizza do u like most....

1.Veg Soya Pizza
2.Veg butter Pizza
3.Egg_Pizza
Please enter u r order: 2

    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 1

Which Pizza do u like most....

1.Veg Soya Pizza
2.Veg butter Pizza
3.Egg_Pizza
Please enter u r order: 3

    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 3

 Following orders are in queue to deliver....as follows..

Pending Order as follows..
    2    3    2    3
    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 1

Which Pizza do u like most....

1.Veg Soya Pizza
2.Veg butter Pizza
3.Egg_Pizza
Please enter u r order: 1

    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 3

 Following orders are in queue to deliver....as follows..

Pending Order as follows..
    2    3    2    3    1
    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 1

Which Pizza do u like most....

1.Veg Soya Pizza
2.Veg butter Pizza
3.Egg_Pizza
Please enter u r order: 2

Very Sorry !!!! No more orders....

    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 2

How many Pizza ?2

Deliverd orders as follows...
    2    3
Total amount to pay : 200
Thank you visit Again....

    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 3

 Following orders are in queue to deliver....as follows..

Pending Order as follows..
    2    3    1
    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 2

How many Pizza ?3

Deliverd orders as follows...
    2    3    1
Total amount to pay : 300
Thank you visit Again....

    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 3

 Following orders are in queue to deliver....as follows..

Sorry !! There is no pending order...

    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 1

Which Pizza do u like most....

1.Veg Soya Pizza
2.Veg butter Pizza
3.Egg_Pizza
Please enter u r order: 1

    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 1

Which Pizza do u like most....

1.Veg Soya Pizza
2.Veg butter Pizza
3.Egg_Pizza
Please enter u r order: 2

    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 1

Which Pizza do u like most....

1.Veg Soya Pizza
2.Veg butter Pizza
3.Egg_Pizza
Please enter u r order: 3

    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 1

Which Pizza do u like most....

1.Veg Soya Pizza
2.Veg butter Pizza
3.Egg_Pizza
Please enter u r order: 1

    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 1

Which Pizza do u like most....

1.Veg Soya Pizza
2.Veg butter Pizza
3.Egg_Pizza
Please enter u r order: 2

    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 1

Which Pizza do u like most....

1.Veg Soya Pizza
2.Veg butter Pizza
3.Egg_Pizza
Please enter u r order: 3

Very Sorry !!!! No more orders....

    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 3

 Following orders are in queue to deliver....as follows..

Pending Order as follows..
    1    2    3    1    2
    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 2

How many Pizza ?2

Deliverd orders as follows...
    1    2
Total amount to pay : 200
Thank you visit Again....

    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 3

 Following orders are in queue to deliver....as follows..

Pending Order as follows..
    3    1    2
    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 1

Which Pizza do u like most....

1.Veg Soya Pizza
2.Veg butter Pizza
3.Egg_Pizza
Please enter u r order: 1

    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 3

 Following orders are in queue to deliver....as follows..

Pending Order as follows..
    3    1    2    1
    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 1

Which Pizza do u like most....

1.Veg Soya Pizza
2.Veg butter Pizza
3.Egg_Pizza
Please enter u r order: 2

    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 3

 Following orders are in queue to deliver....as follows..

Pending Order as follows..
    3    1    2    1    2
    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 1

Which Pizza do u like most....

1.Veg Soya Pizza
2.Veg butter Pizza
3.Egg_Pizza
Please enter u r order: 1

Very Sorry !!!! No more orders....

    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice:

 *******************************************************/

data structures and algorithms Web Developer

Sunday 18 September 2016

Infix to Postfix conversion code in c++


Group C: (Assignment No: 02) (SPPU Syllabus Assignment No: 25)
Problem Statement:
Implement C++ program for expression conversion as infix to postfix and its evaluation using stack based on given conditions
i. Operands and operator, both must be single character.
ii. Input Postfix expression must be in a desired format.
iii. Only '+', '-', '*' and '/ ' operators are expected.

Program Code:

//================================================================================================================================
// Name        : infix_postfix.cpp
// Author      : Nitin Shivale
// Version     : 1
// Copyright   : Your copyright notice
// Description : Conversion of infix expression to postfix expression and evaluation of postfix expression using stack, Ansi-style
//================================================================================================================================

#include <iostream>
#include<ctype.h>
using namespace std;
//class inpo containing data member and member functions
class inpo
{
    char in[20],po[20],stk[10];//stack for expression conversion.
    int i,j,is,ic,top,top1; //Two top one for stack and one for stack 1.
    int stk1[10]; //Stack 1 for expression evaluation.
public:
    inpo()
    {
     i=j=is=ic=0; //initialization of data members
     top=top1=-1;
    }
    bool IsOperand(char C)
    {
        if(C >= '0' && C <= '9') return true;
        if(C >= 'a' && C <= 'z') return true;
        if(C >= 'A' && C <= 'Z') return true;
        return false;
    }
    void getinfix() //Accept the infix expression
    {
        cout<<"\nEnter valid infix Expression: ";
        cin>>in; //Stored infix exp in in[]char array.
    }
    void showinfix() //Show the infix expression
    {
        cout<<"\t"<<in;
    }
    int isempty() //Stack basic functions.
    {
        if(top==-1)
            return 1;
        else
            return 0;
    }
    int isfull()
    {
        if(top==9)
            return 1;
        else
            return 0;
    }
    void push1(int x1)//for integer stack
    {
        top=top+1;
        stk1[top]=x1;
    }
    int pop1() //for integer stack
    {
       int s1;
       s1=stk1[top];
       top=top-1;
       return s1;
    }
    void push(char x)//for character stack
    {
        top=top+1;
        stk[top]=x;
    }
    char pop()//for character stack
    {
        char s;
        s=stk[top];
        top=top-1;
        return s;
    }
    void showpostfix()
    {
        cout<<"\t"<<po;
    }
    void convert();
    int instackprio();
    int incomingprio(char); //member functions
    void postfixExpEval();
};
void inpo::postfixExpEval() //To evaluate the postfix expression
{
    i=0;
    char ch;
    int op1,op2,res,tot;
        while(po[i]!='\0')//read postfix expression one by one
        {
            ch=po[i];
            if((ch=='+')||(ch=='-')||(ch=='*')||(ch=='/')||(ch=='^')) //isdigit(ch) built in function to check digit.
            {
                switch(ch)//if operator pop and perform operation and push back into the stack.
                {
                case '+':op2=pop1();
                         op1=pop1();
                         res=op1+op2;
                         push1(res);
                         break;
                case '-':op2=pop1();
                         op1=pop1();
                         res=op1-op2;
                         push1(res);
                         break;
                case '*':op2=pop1();
                         op1=pop1();
                         res=op1*op2;
                         push1(res);
                         break;
                case '/':op2=pop1();
                         op1=pop1();
                         res=op1/op2;
                         push1(res);
                         break;
                case '^':op2=pop1();
                         op1=pop1();
                         res=op1;
                         while(op2>1)
                         {
                          res=res*op1;
                          op2--;
                         }
                         push1(res);
                         break;
                }//end of switch

            } //end of if
            else if(IsOperand(ch))//To check operand we use IsOperand function.
            {
                push1(ch-'0'); //if operand push it inside stack
            } //end of else
            i=i+1;
        }//end of while

    tot=pop1(); //final evaluated result at the top of stack.
    cout<<"\nResult is:"<<tot;
}//end of fun

/*******************************************************************
 * Function Name: To convert infix expression to postfix expression.
 * return type: Void
 *******************************************************************/

void inpo::convert()
{
    i=j=0;
    char p,k;
    while(in[i]!='\0')//do until null character not found
    {
     p=in[i];//to read one by one from infix expression
     if((p=='(')||(p=='+')||(p=='-')||(p=='*')||(p=='/')||(p=='^')||(p==')'))
     {
         if(isempty()) //here we are dealing with operator only as per their priority.
         {
             push(p); //if initially stack is empty push
         }
         else if(p==')') //when we encountered with ')' bracket pop from stack until we r not encountered with '(' bracket.
         {
             k=pop();
             while(k!='(') //check the pop element with '(' bracket. if equal stop poping.
             {
                 po[j]=k; //pop and store it inside the postfix expression array po[].
                 j++;     //increment po[] array index by 1.
                 k=pop(); //pop next element
             }
         }
         else
         {
             is=instackprio(); //when we are pushing the operator inside the stack.
             ic=incomingprio(p);// we are always checking their incoming and instack priority.
             if(is>ic)//if instack priority is gretter than incoming priority
             {
              k=pop(); //pop the stack top operator whose priority is bigger than incoming operator from stack.
              po[j]=k;//store it in postfix expression array po[j]
              j++;  //increment j by one.
              push(p);//then push the incoming operator in stack.
             }
             else
             {
                 push(p); //if incoming operator priority is gretter than instack operator priority then
             }            // directly push the incoming operator inside the stack.
         }
     }
     else// if opearnd is their directly store it inside the postfix expression array po[j].
     {
        po[j]=p;
        j++; //increment j by one.
     }
     i=i+1; //read the next character of infix expression for conversion.
    }//end of while loop
    if(in[i]=='\0')//if we encountered with NULL Character of infix expression then
    {
        while(!isempty())//pop the stack containts until stack is not empty and
        {
         k=pop(); //pop from stack
         po[j]=k; //store it inside the postfix expression array po[j].
         j++;  //increment j by one.
        }
    }
 po[j]='\0'; // at the end of the postfix expression store null character to indicate end of the expression.
}

/* * Function to check the instack priority of operator * */
int inpo::instackprio()
{
    char b;
    b=stk[top];
    switch(b)
    {
     case '(':return 0; break;
     case '+':return 2; break;
     case '-':return 2; break;
     case '*':return 4; break;
     case '/':return 4; break;
     case '^':return 5; break;
    }
}
/* * Function to check the priority of incoming operator * */
int inpo::incomingprio(char ch)
{
    switch(ch)
    {
     case '(':return 9; break;
     case '+':return 1; break;
     case '-':return 1; break;
     case '*':return 3; break;
     case '/':return 3; break;
     case '^':return 6; break;
    }
}

int main()
{
    inpo p1;
    p1.getinfix();
    p1.showinfix();
    cout<<"\nAfter conversion from infix to postfix...\n";
    p1.convert();
    p1.showpostfix();
    cout << "\n\n!!!POSTFIX EXPRESSION EVALUATION ARE AS FOLLOWS..!!!" << endl;
    p1.postfixExpEval();
    return 0;
}

/************** OUTPUT***********************************************************
 *
 Enter valid infix Expression: 2^4
    2^4
After conversion from infix to postfix...
    24^

!!!POSTFIX EXPRESSION EVALUATION ARE AS FOLLOWS..!!!

Result is:16

Enter valid infix Expression: (9-1)/(4-2)^2
    (9-1)/(4-2)^2
After conversion from infix to postfix...
    91-42-2^/

!!!POSTFIX EXPRESSION EVALUATION ARE AS FOLLOWS..!!!

Result is:2

[student@localhost SE_Comp_A]$ g++ conv_in_po_eval.cpp
[student@localhost SE_Comp_A]$ ./a.out

Enter valid infix Expression: (8*6)/(4-2)^3
    (8*6)/(4-2)^3
After conversion from infix to postfix...
    86*42-3^/

!!!POSTFIX EXPRESSION EVALUATION ARE AS FOLLOWS..!!!

Result is:6

 ***********************************************************************************/
data structures and algorithms Web Developer

Linked list program to store negative and positive number


Group B: (Assignment No:04)(SPPU Syllabus assignment No :18)
Problem Statement: Write C++ program to store set of negative and positive numbers using linked list. Write functions to 
a) Insert numbers
b) Delete nodes with negative numbers
c) Create two more linked lists using this list, one containing all positive numbers and other containing negative numbers
d) For two lists that are sorted; Merge these two lists into third resultant list that is sorted.

Program Code:
//Author: Prof.Anjali Almale
#include<iostream>
using namespace std;
struct node   //node defined to allocate memory
{
    int num; //To store data
    node *next; //To store address of next node.
};

class numbers  //class name is number
{
 public:

    node *head,*head1,*head2; //data memebers

    numbers() //default constructor
    {
        head=NULL;
        head1=NULL;
        head2=NULL;
    }
    void create();       //member function declaration.
    void display(node *);
    void insert();
    void sort(node *);
    void display2();
    void remove();
    void seperate();
    void merge();
}; //end of class

void numbers::create()
{

  node *current,*new_node;
    char c;
 cout<<"\n------------CREATION OF NUMBERS LIST-------------\n\n ";
    do
    {

    new_node=new node; //dynamic memory allocation
    cout<<"\n Enter the no: \n ";
    cin>>new_node->num; //To store data

    new_node->next=NULL; //make node next null(init)
    if(head==NULL) //check header if null
    {
        head=new_node;
        current=new_node;
    }
    else
    {
        current->next=new_node;
        current=new_node;

    }
    cout<<"\nDo you want to add new member";
    cin>>c;
       }while(c!='n');
}
void numbers::seperate()//To separate the positive numbers and negative numbers.
{
    node *temp,*current1,*current2,*n1,*n2;
    temp=head;

    while(temp!=NULL)
    {
        if((temp->num)<0)
        {
            n1=new node;
            n1->num=temp->num;
            n1->next=NULL;

            if(head1==NULL)
            {
                current1=n1;
                head1=n1;
            }
            else
            {
            current1->next=n1;
            current1=n1;
            }

        }
        else if((temp->num)>=0)
        {
            n2=new node;
            n2->num=temp->num;
            n2->next=NULL;
            if(head2==NULL)

            {
                current2=n2;
                head2=n2;
            }
            else
            {
              current2->next=n2;
              n2->next=NULL;
              current2=n2;
            }
        }
          temp=temp->next;
     }
}

void numbers::display(node *head)//To display the list
{
    node *p;
    p=head;
    if(p==NULL)
    {
        cout<<"\nThe list is Empty";
    }
    else
    {
        while(p!=NULL)
        {
            cout<<p->num<<"\t";
             p=p->next;
        }
    }
}

void numbers::sort(node *head_d) //Before merge u hv to sort them
{
    node *temp1,*temp2,*temp3;
    temp1 = head_d;

 for( ;temp1->next!=NULL;temp1=temp1->next)
 {
     for(temp2=temp1->next;temp2!=NULL;temp2=temp2->next)
     {
         if(temp1->num>temp2->num)
         {
             int temp= temp1->num;
             temp1->num = temp2->num;
             temp2->num = temp;
         }
     }
 }
 temp3 = head_d;

while (temp3!=NULL)
{
cout<<"\t"<< temp3->num;
temp3 =temp3->next;
}
}

void numbers::merge()//To merge both the list
{
    node *temp;
    temp=head1;

    while(temp->next!=NULL)
    {
        temp=temp->next;
    }
    temp->next=head2;
}
void numbers::insert()
{
    node *p,*temp;
        p=new node;
        cout<<"\n\n Enter New Number to be insert  :  ";
        cin>>p->num;
        p->next=NULL;
        temp=head;
        while(temp->next!=NULL)
        {
    temp=temp->next;
        }
    temp->next=p;
}

int main()
{
    numbers n;
    n.create();
    cout<<"\n \nTHE LIST OF +VE AND -VE NUMBERS IS: \n";
    n.display(n.head);
    n.insert();
    cout<<"\n \nTHE LIST AFTER INSERTION IS: \n";
    n.display(n.head);
    n.seperate();
    cout<<"\n\n THE LIST OF ONLY -VE NUMBERS IS: \n";
    n.display(n.head1);
    cout<<"\n\n THE LIST OF ONLY +VE NUMBERS IS: \n";
    n.display(n.head2);
    cout<<"\n\n THE LIST OF ONLY SORTED -VE NUMBERS IS : \n";
    n.sort(n.head1);
    cout<<"\n\n THE LIST OF ONLY SORTED +VE NUMBERS IS : \n";
    n.sort(n.head2);
    n.merge();
    cout<<"\n AFTER MERGE .....THE LIST IS AS FOLLOWS: \n";
    n.display(n.head1);
    cout<<"\n\n THE LIST OF SORTED +VE AND -VE NUMBERS IS : \n";
    n.display(n.head1);
    return 0;
}

/*   OUTPUT *************************************************************


------------CREATION OF NUMBERS LIST-------------


 Enter the no:
 1

Do you want to add new membery

 Enter the no:
 -5

Do you want to add new membery

 Enter the no:
 6

Do you want to add new membery

 Enter the no:
 -7

Do you want to add new membery

 Enter the no:
 -3

Do you want to add new membery

 Enter the no:
 -4

Do you want to add new membery

 Enter the no:
 7

Do you want to add new membern


THE LIST OF +VE AND -VE NUMBERS IS:
1    -5    6    -7    -3    -4    7

 Enter New Number to be insert  :  10


THE LIST AFTER INSERTION IS:
1    -5    6    -7    -3    -4    7    10

 THE LIST OF ONLY -VE NUMBERS IS:
-5    -7    -3    -4

 THE LIST OF ONLY +VE NUMBERS IS:
1    6    7    10

 THE LIST OF ONLY SORTED -VE NUMBERS IS :
    -7    -5    -4    -3

 THE LIST OF ONLY SORTED +VE NUMBERS IS :
    1    6    7    10
 AFTER MERGE .....THE LIST IS AS FOLLOWS:
-7    -5    -4    -3    1    6    7    10

 THE LIST OF SORTED +VE AND -VE NUMBERS IS :
-7    -5    -4    -3    1    6    7    10

********************************************************************************************/
data structures and algorithms Web Developer

Tuesday 6 September 2016

C++ program using stack to check whether given expression is well parenthesized or not.


Group C: Assignment No: 01 (SPPU Syllabus Assignment No:24)

Problem Statement:
In any language program mostly syntax error occurs due to unbalancing delimiter such as (),{},[]. Write C++ program using stack to check whether given expression is well parenthesized or not.


Program Code:

//============================================================================
// Name        : wellformness.cpp
// Author      : Nitin Shivale
// Version     : 1
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;
#define size 10

class stackexp
{
    int top;
    char stk[size];
public:
    stackexp()
    {
     top=-1;
    }
    void push(char);
    char pop();
    int isfull();
    int isempty();
};

void stackexp::push(char x)
{
    top=top+1;
    stk[top]=x;
}

char stackexp::pop()
{
    char s;
    s=stk[top];
    top=top-1;
    return s;
}

int stackexp::isfull()
{
    if(top==size)
        return 1;
    else
        return 0;
}

int stackexp::isempty()
{
    if(top==-1)
        return 1;
    else
        return 0;
}

int main()
{
    stackexp s1;
    char exp[20],ch;
    int i=0;
    cout << "\n\t!!Well Formness of Parenthesis..!!!!" << endl; // prints !!!Hello World!!!
    cout<<"\nEnter the expression to check whether it is in well form or not :  ";
    cin>>exp;
    if((exp[0]==')')||(exp[0]==']')||(exp[0]=='}'))
    {
        cout<<"\n Invalid Expression.....\n";
        return 0;
    }
    else
    {
        while(exp[i]!='\0')
        {
            ch=exp[i];
            switch(ch)
            {
            case '(':s1.push(ch);break;
            case '[':s1.push(ch);break;
            case '{':s1.push(ch);break;
            case ')':s1.pop();break;
            case ']':s1.pop();break;
            case '}':s1.pop();break;
            }
            i=i+1;
        }
    }
    if(s1.isempty())
    {
        cout<<"\nExpression is well parenthesis...\n";
    }
    else
    {
        cout<<"\nSorry !!! Invalid Expression or not in well parenthesized....\n";
    }
    return 0;
}

/******************* output   *****************************************************

  !!Well Formness of Parenthesis..!!!!

Enter the expression to check whether it is in well form or not :  (m<(n[8]+0))

Expression is well parenthesis...


!!Well Formness of Parenthesis..!!!!

Enter the expression to check whether it is in well form or not :  )(m+n)*(a-b)

 Invalid Expression.....



     !!Well Formness of Parenthesis..!!!!

Enter the expression to check whether it is in well form or not :  (m+b(n[8)/2+3)

Sorry !!! Invalid Expression or not in well parenthesized....


*********************************************************************************/


data structures and algorithms Web Developer

Tuesday 30 August 2016

Pinnacle Club implementation using Singly Linked List


Group B : Assignment No: 01 (SPPU Syllabus Assignment No :14)
Problem Statement:
Department of Computer Engineering has student's club named 'Pinnacle Club'. Students of Second, third and final year of department can be granted membership on request. Similarly one may cancel the membership of club. First node is reserved for president of club and last node is reserved for secretary of club. Write C++ program to maintain club member‘s information using singly linked list. Store student PRN and Name. Write functions to
a)Add and delete the members as well as president or even secretary.
b)Compute total number of members of club
c)Display members
d)Display list in reverse order using recursion
e) Two linked lists exists for two divisions. Concatenate two lists.



Program Code:

//============================================================================
// Name        : panclub.cpp
// Author      : Nitin Shivale (9850562515)
// Version     : 1
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include<stdlib.h>
#include<string.h>
using namespace std;
//data structure for student information i:e node
struct node
{
 int prn;
 char name[20];
 node *next;
};

class panclub
{
    int num,cnt;
    char nm[20];  //Data memebers
    node *head;
 public:
    panclub()//Constructor to initialize object
    {
     num=cnt=0;
     head=NULL;
    }
    node *create();
    void display(node *);
    node *concat(node *,node *);  //Memeber Functions with arguments
    void reverse(node *);
    node* insert_president(node *);
    void insert_sec(node *);
        void insert_member(node *);
    node* del_president(node *);
        node* del_secretary(node *);
        node* del_member(node *);
};

//To Create the list of Divisions.
node* panclub::create()
{
    node *temp,*n1;
    temp=n1=NULL;
    //int flag=1;
    //char ans='y';
    cout<<"\nHow many students data u want to insert in panclub database: ";
    cin>>cnt;
    do
    {
        n1=new node; //first of allocate the memory for all feilds of struct.
        cout<<"\nEnter the prn number of student:";
        cin>>num;
        n1->prn=num;
        //storing the prn in node feild prn;
        cout<<"\nEnter the name of student:";
        cin>>nm;
        strcpy(n1->name,nm);
        //Storing the name in node feild name.;
        n1->next=NULL; //making the next feild null.
        if(head==NULL) //check for head is empty.
        {
            head=n1;   //make new node as head
            temp=head;
        }
        else
        {
            temp=head;
            while(temp->next!=NULL) //attach at the end of list
            {
                temp=temp->next;
            }
            temp->next=n1;
        }
        cnt--;
    }while(cnt>0);

    return head;
}

void panclub::display(node *head) //display the list of both divisions.
{
 node *temp;
 temp=head;
 while(temp!=NULL)
 {
     if(temp->next==NULL)
     {
      cout<<"["<<temp->prn<<"|"<<temp->name<<"]->NULL";
     }
     else
     {
      cout<<"["<<temp->prn<<"|"<<temp->name<<"]->";
     }
     temp=temp->next;
 }
}

node* panclub::concat(node *head1,node *head2) //To concatinate both the divisions data in one list.
{
    node *head3,*temp,*temp1;
    head3=temp=temp1=NULL;
    temp=head1;
    head3=head1;
    while(temp->next!=NULL)
    {
        temp=temp->next;
    }
    temp1=head2;
    temp->next=temp1;
    return head3;
}
void panclub::reverse(node *head)
{
    node *temp;
    temp=head;
    if(temp==NULL)
        return;
    reverse(temp->next);
    cout<<"["<<temp->prn<<"|"<<temp->name<<"]->";
}

node* panclub::insert_president(node *head)
{
 node *temp,*n2;
 temp=n2=NULL;
 temp=head;
 n2=new node;
 cout<<"\nEnter the PRN number of President: ";
 cin>>n2->prn;
 cout<<"\nEnter the name of President: ";
 cin>>n2->name;
 n2->next=temp;
 head=n2;
 return head;
}

void panclub::insert_member(node *head)
{
 node *temp,*n2;
 int pn;
 temp=head;
 n2=new node;
 cout<<"\nEnter the PRN number of Member: ";
 cin>>n2->prn;
 cout<<"\nEnter the name of Member: ";
 cin>>n2->name;
 n2->next=NULL;
 cout<<"\nEnter the PRN number after which u want to add this member: ";
 cin>>pn;
 while(temp!=NULL)
 {
  if(temp->prn==pn)
  {
   n2->next=temp->next;
   temp->next=n2;
   break;
  }
  temp=temp->next;
 }
 cout<<"\n\nMember added sucessfully....!!";
}

void panclub::insert_sec(node *head)
{
 node *temp,*n2;
 temp=head;
 n2=new node;
 cout<<"\nEnter the PRN number of Secretary: ";
 cin>>n2->prn;
 cout<<"\nEnter the Name of Secretary: ";
 cin>>n2->name;
 n2->next=NULL;
 while(temp->next!=NULL)
 {
  temp=temp->next;
 }
 temp->next=n2;
}
//delete the president node from list
node* panclub::del_president(node *head)
{
 node *temp;
 temp=head;
 head=temp->next;
 free(temp);
 return head;
}
//Delete the secretary node from the list.
node* panclub::del_secretary(node *head)
{
 node *temp,*t1;
 temp=head;
 while(temp->next!=NULL)
 {
  t1=temp;
  temp=temp->next;
 }
 t1->next=NULL;
 free(temp);
 return head;
}

//Delete the memeber from the list.

node* panclub::del_member(node *head)
{
 node *temp,*t1;
 int pn;
 temp=head;
 cout<<"\nEnter the PRN number after which u want to delete member: ";
 cin>>pn;
 while(temp!=NULL)
 {
  if(temp->prn==pn)
  {
   t1=temp->next;
   temp->next=t1->next;
   free(t1);
   break;
  }
  temp=temp->next;
 }
 cout<<"\n\nMember removed sucessfully....!!";
 return head;
}
int main()
{
    panclub p1,p2,p3;
    node *h1,*h2,*h3;
    h1=h2=h3=NULL;
    int ch;
    cout << "\n\t!!!Group B:Assignment No:01!!!" << endl; // prints !!!Assignment number and group!!!
   do
   {
    cout<<"\n\n1.Enter data of SE A Division:";
    cout<<"\n2.Enter data of SE B Division:";
    cout<<"\n3.Concatination of List..";
    cout<<"\nEnter your choice: ";
    cin>>ch;
    switch(ch)
    {
     case 1:
            cout<<"\n\nPlease enter the student info who is register memeber..";
            cout<<"\n\nEnter the Panclub Data of SE A Division:\n";
            h1=p1.create();
        cout<<"\nSE Comp Division A List are as follows..\n\n";
        p1.display(h1);
        cout<<"\n\nReverse List of SE Div A:\n\n";
        p1.reverse(h1);
        p1.insert_sec(h1);
        cout<<"\nAfter insertion of Secretary: \n";
        p1.display(h1);
        h1=p1.insert_president(h1);
        cout<<"\nAfter insertion of President: \n";
        p1.display(h1);
        p1.insert_member(h1);
        cout<<"\n After insertion of member...\n";
        p1.display(h1);
        h1=p1.del_president(h1);
        cout<<"\n\nAfter deletion of president...\n";
        p1.display(h1);
        h1=p1.del_secretary(h1);
        cout<<"\n\nAfter deletion of secretary...\n";
        p1.display(h1);
        h1=p1.del_member(h1);
        cout<<"\n\nAfter deletion of member...\n";
        p1.display(h1);
        break;
    case 2:
        cout<<"\n\nEnter the Panclub Data of SE B Division:\n";
        h2=p2.create();
        cout<<"\nSE Comp Division B List are as follows..\n\n";
        p2.display(h2);
        cout<<"\n\nReverse List of SE Div B:\n\n";
        p1.reverse(h2);
        p2.insert_sec(h2);
        cout<<"\nAfter insertion of Secretary: \n";
        p2.display(h2);
        h2=p2.insert_president(h2);
        cout<<"\nAfter insertion of President: \n";
        p2.display(h2);
        p2.insert_member(h2);
        cout<<"\n After insertion of member...\n";
        p2.display(h2);
        h2=p2.del_president(h2);
        cout<<"\n\nAfter deletion of president...\n";
        p1.display(h2);
        h2=p2.del_secretary(h2);
        cout<<"\n\nAfter deletion of secretary...\n";
        p1.display(h2);
        h2=p2.del_member(h2);
        cout<<"\n\nAfter deletion of member...\n";
        p2.display(h2);
            break;
    case 3:
        h3=p3.concat(h1,h2);
        cout<<"\n\nThe concatenation of Div : A and Div : B of SE Comp Class are as follows.\n\n";
        p3.display(h3);
            break;
  }
 }while(ch!=4);
        return 0;

}


/********************** OUTPUT **********************************************
[student@localhost Documents]$ g++ panclub.cpp
[student@localhost Documents]$ ./a.out

    !!!Group B:Assignment No:01!!!


1.Enter data of SE A Division:
2.Enter data of SE B Division:
3.Concatination of List..
Enter your choice: 1


Please enter the student info who is register memeber..

Enter the Panclub Data of SE A Division:

How many students data u want to insert in panclub database: 2

Enter the prn number of student:333

Enter the name of student:nitin

Enter the prn number of student:444

Enter the name of student:sham

SE Comp Division A List are as follows..

[333|nitin]->[444|sham]->NULL

Reverse List of SE Div A:

[444|sham]->[333|nitin]->
Enter the PRN number of Secretary: 555

Enter the Name of Secretary: sachin

After insertion of Secretary:
[333|nitin]->[444|sham]->[555|sachin]->NULL
Enter the PRN number of President: 111

Enter the name of President: tejas

After insertion of President:
[111|tejas]->[333|nitin]->[444|sham]->[555|sachin]->NULL
Enter the PRN number of Member: 222

Enter the name of Member: ram

Enter the PRN number after which u want to add this member: 111


Member added sucessfully....!!
 After insertion of member...
[111|tejas]->[222|ram]->[333|nitin]->[444|sham]->[555|sachin]->NULL

After deletion of president...
[222|ram]->[333|nitin]->[444|sham]->[555|sachin]->NULL

After deletion of secretary...
[222|ram]->[333|nitin]->[444|sham]->NULL
Enter the PRN number after which u want to delete member: 222


Member removed sucessfully....!!

After deletion of member...
[222|ram]->[444|sham]->NULL

1.Enter data of SE A Division:
2.Enter data of SE B Division:
3.Concatination of List..
Enter your choice: 2


Enter the Panclub Data of SE B Division:

How many students data u want to insert in panclub database: 3

Enter the prn number of student:123

Enter the name of student:sudhir

Enter the prn number of student:124

Enter the name of student:mohit

Enter the prn number of student:125

Enter the name of student:kohali

SE Comp Division B List are as follows..

[123|sudhir]->[124|mohit]->[125|kohali]->NULL

Reverse List of SE Div B:

[125|kohali]->[124|mohit]->[123|sudhir]->
Enter the PRN number of Secretary: 126

Enter the Name of Secretary: Dough

After insertion of Secretary:
[123|sudhir]->[124|mohit]->[125|kohali]->[126|Dough]->NULL
Enter the PRN number of President: 121

Enter the name of President: Bill

After insertion of President:
[121|Bill]->[123|sudhir]->[124|mohit]->[125|kohali]->[126|Dough]->NULL
Enter the PRN number of Member: 122

Enter the name of Member: Gates

Enter the PRN number after which u want to add this member: 111


Member added sucessfully....!!
 After insertion of member...
[121|Bill]->[123|sudhir]->[124|mohit]->[125|kohali]->[126|Dough]->NULL

After deletion of president...
[123|sudhir]->[124|mohit]->[125|kohali]->[126|Dough]->NULL

After deletion of secretary...
[123|sudhir]->[124|mohit]->[125|kohali]->NULL
Enter the PRN number after which u want to delete member: 123


Member removed sucessfully....!!

After deletion of member...
[123|sudhir]->[125|kohali]->NULL

1.Enter data of SE A Division:
2.Enter data of SE B Division:
3.Concatination of List..
Enter your choice: 3


The concatenation of Div : A and Div : B of SE Comp Class are as follows.

[222|ram]->[333|nitin]->[123|sudhir]->[125|kohali]->NULL

1.Enter data of SE A Division:
2.Enter data of SE B Division:
3.Concatination of List..
Enter your choice: 4
[student@localhost Documents]$


   

**************************************************************************************/
data structures and algorithms Web Developer

Sparse Matrix Realization and Operations on it


Group A : Assignment No : 04 (SPPU Syllabus Assignment No: 11)
Problem Statement:
Write C++ program for sparse matrix realization and operations on it- Transpose, Fast Transpose and addition of two matrices.

Program Code:

//Author:Prof.Nitin Shivale
//Program:Sparse Matrix and it's Operations like 1.Addition, 2.Transpose(Simple/Fast).
//Group A:Assignment No:4
#include<iostream>
#include<string.h>
using namespace std;

//define structure to store non zero values of sparse matrix.
typedef struct spm
{
 int row,col;
 int nval;
};

class sparse
{
 spm sp1[10],sp2[10],sp3[20],spltr[10],fstr[10];
 int mat1[10][10],mat2[10][10]; //data members
 int r1,r2,c1,c2,i,j,k;
 public:
 sparse()             //default constructor
 {
  r1=r2=i=0;
  j=k=0;
  c1=c2=0;
 }
 void getmat();
 void showmat();       //member function
 void addspm();
 void simpl_trnspose();
 void fast_trnspose();
};

//To accept sparse matrix from user
//sparse matrix is the matrix where most of the elements are zero in it.
void sparse::getmat()
{
 //To store first matrix
 cout<<"\nHow many rows in mat1:";
 cin>>r1;
 cout<<"\nHow many cols in mat1:";
 cin>>c1;
 for(i=0;i<r1;i++)
 {
  for(j=0;j<c1;j++)
  {
   cout<<"\nEnter ["<<i<< "]["<<j<<"] value of mat1: ";
   cin>>mat1[i][j];
  }
 }
 //To store second matrix
 cout<<"\nHow many rows in mat2:";
 cin>>r2;
 cout<<"\nHow many cols in mat2:";
 cin>>c2;
 for(i=0;i<r2;i++)
 {
  for(j=0;j<c2;j++)
  {
   cout<<"\nEnter ["<<i<< "]["<<j<<"] value of mat2: ";
   cin>>mat2[i][j];
  }
 }
}

//To display values inside both the sparse matrix.
void sparse::showmat()
{
 //To display value of first sparse matrix.
 cout<<"\n Matrix 1 value are as follows\n";
 for(i=0;i<r1;i++)
 {
  for(j=0;j<c1;j++)
  {
   cout<<"\t"<<mat1[i][j];
  }
  cout<<"\n";
 }
 //To display value of second spaese matrix
 cout<<"\n Matrix 2 value are as follows\n";
 for(i=0;i<r2;i++)
 {
  for(j=0;j<c2;j++)
  {
   cout<<"\t"<<mat2[i][j];
  }
  cout<<"\n";
 }
}

//Addition of both the sparse matrix.
//Addition of non zero element only which is stored in structure object sp1 and sp2.
void sparse::addspm()
{
 int n1,n2;
 n1=n2=0;
 //conversion of sparse matrix to non zero value code as follows.
 //conversion of first sparse matrix mat1 into sp1
 sp1[0].row=r1;
 sp1[0].col=c1;
 k=1;
 cout<<"\n Sparse matrix1 non_zero value are as follows:\n";
 for(i=0;i<r1;i++)
 {
  for(j=0;j<c1;j++)
  {
   if(mat1[i][j]!=0)
   {
    sp1[k].row=i;
    sp1[k].col=j;
    sp1[k].nval=mat1[i][j];
    k=k+1;
   }
  }
 }
 sp1[0].nval=k-1;
 //display non zero values of sparse matrix mat1 which is inside sp1.
 cout<<"\nRow\tCol\tnval";
 for(i=0;i<k;i++)
 {
  cout<<"\n"<<sp1[i].row<<"\t"<<sp1[i].col<<"\t"<<sp1[i].nval;
 }
 //conversion of second sparse matrix mat2 into sp2
 cout<<"\n\nSparse matrix2 non_zero value are as follows:\n";
 sp2[0].row=r2;
 sp2[0].col=c2;
 k=1;
 for(i=0;i<r2;i++)
 {
  for(j=0;j<c2;j++)
  {
   if(mat2[i][j]!=0)
   {
    sp2[k].row=i;
    sp2[k].col=j;
    sp2[k].nval=mat2[i][j];
    k=k+1;
   }
  }
 }
 sp2[0].nval=k-1;
 //display non zero values of sparse matrix mat2 which is inside sp2.
 cout<<"\nRow\tCol\tnval";
 for(i=0;i<k;i++)
 {
  cout<<"\n"<<sp2[i].row<<"\t"<<sp2[i].col<<"\t"<<sp2[i].nval;
 }
 //Addition of non zero values sp1 and sp2 logic are as follows.
 //1.Store number of non zero term of mat1 in n1 and number of non zero term of mat2 in n2.
 n1=sp1[0].nval;
 n2=sp2[0].nval;
 i=j=k=1;
 cout<<"\n No of term in sp1:"<<n1<<"\tNo of term in sp2:"<<n2;
 //2.Store the largest row and colom term of both matrices in sp3 row and col.
 if(sp1[0].row>sp2[0].row)
 {
  sp3[0].row=sp1[0].row;
 }
 else
 {
  sp3[0].row=sp2[0].row;
 }

 if(sp1[0].col>sp2[0].col)
 {
  sp3[0].col=sp1[0].col;
 }
 else
 {
  sp3[0].col=sp2[0].col;
 }
 //3. Start addition by checking following conditions.
 while((i<=n1)&&(j<=n2))//check for both term
 {
  if(sp1[i].row==sp2[j].row)//check if rows are same in sp1 and sp2.
  {
   if(sp1[i].col==sp2[j].col)//check if cols are same in sp1 and sp2.
   {
    sp3[k].row=sp1[i].row;
    sp3[k].col=sp1[i].col;
    sp3[k].nval=sp1[i].nval+sp2[j].nval; //if yes add both non zero values and store in sp3 with row,col and values detail.
    cout<<"\nvalu of add:"<<sp3[k].nval;
    i++;
    j++; //increment all indexes.
    k++;
    cout<<"\nval of i:"<<i<<"\tval of j:"<<j<<"\tval of k:"<<k;
   
   }
   else if(sp1[i].col<sp2[j].col)//check col of sp1 is less than col of sp2
   {
    sp3[k].row=sp1[i].row;
    sp3[k].col=sp1[i].col;
    sp3[k].nval=sp1[i].nval; //if yes store all values of sp1 in sp3
    i++;
    k++; //increment i and k.
    cout<<"\nval of i:"<<i<<"\tval of K:"<<k;
   }
   else
   {
    sp3[k].row=sp2[j].row;
    sp3[k].col=sp2[j].col;   //if no store all values of sp2 in sp3
    sp3[k].nval=sp2[j].nval;
    j++;
    k++; //increment j and k.
    cout<<"\nval of j:"<<j<<"\tval of K:"<<k;
   }
  }
  else if(sp1[i].row<sp2[j].row)//check row of sp1 is less than row of sp2
  {
   sp3[k].row=sp1[i].row;
   sp3[k].col=sp1[i].col;
   sp3[k].nval=sp1[i].nval; //if yes store all values of sp1 in sp3
   i++;
   k++;
   cout<<"\nval of i:"<<i<<"\tval of K:"<<k;
  }
  else
  {
   sp3[k].row=sp2[j].row;
   sp3[k].col=sp2[j].col;     //if no store all values of sp2 in sp3
   sp3[k].nval=sp2[j].nval;
   j++;
   k++;
   cout<<"\nval of j:"<<j<<"\tval of K:"<<k;
  }
 }//end of while loop
 while(i<=n1)    //Store remaining terms of sp1 in sp3 if above while (i<=n1&&j<=n2) is not true
 {  
  sp3[k].row=sp1[i].row;
  sp3[k].col=sp1[i].col;
  sp3[k].nval=sp1[i].nval;
  i++;
  k++;
  cout<<"\nval of outer while i:"<<i<<"\tval of K:"<<k;
 }
 while(j<=n2)
 {
  sp3[k].row=sp2[j].row; //Store remaining terms of sp2 in sp3 if above while (i<=n1&&j<=n2) is not true.
  sp3[k].col=sp2[j].col;
  sp3[k].nval=sp2[j].nval;
  j++;
  k++;
  cout<<"\nval of  outer while j:"<<j<<"\tval of K:"<<k;
 }
 sp3[0].nval=k-1;
 //Display the addition result which is inside sp3.
 cout<<"\nRow\tCol\tnval";
 for(i=0;i<k;i++)
 {
  cout<<"\n"<<sp3[i].row<<"\t"<<sp3[i].col<<"\t"<<sp3[i].nval;
 }
}
//To Transpose the sparse matrix
//Method: simple Transpose O(n*n)
//To chnage the rows into column and their values.
void sparse::simpl_trnspose()
{
 //chainging the rows of sp1  to columns in spltr structure.
 spltr[0].row=sp3[0].col;
 spltr[0].col=sp3[0].row;
 spltr[0].nval=sp3[0].nval;
 k=1;
 for(i=0;i<=spltr[0].row;i++)//To check number of colms
 {
  for(j=0;j<=spltr[0].nval;j++)//To check value in total no of non zero values
  {
   if(sp3[j].col==i)// if found swap the rows into colom and colm into rows.
   {
    spltr[k].row=sp3[j].col;
    spltr[k].col=sp3[j].row;
    spltr[k].nval=sp3[j].nval;
    k++;
   }
  }
 }//Total time required to execute is O(n*n).
 cout<<"\n\n Simple Transpose of addition matrix are as follows...";
 cout<<"\nRow\tCol\tnval";
 for(i=0;i<k-1;i++)
 {
  cout<<"\n"<<spltr[i].row<<"\t"<<spltr[i].col<<"\t"<<spltr[i].nval;
 }
}

void sparse::fast_trnspose()
{
 int term[5],pos[5];
 //First 0th row interchange row and col value.
 fstr[0].row=sp3[0].col;
 fstr[0].col=sp3[0].row;
 fstr[0].nval=sp3[0].nval;
 //initialize the term as zero.
 for(i=0;i<=sp3[0].col;i++)
 {
  term[i]=0;
 }
 //Now find out how mnay times the col occures in nonzero value.
 for(i=1;i<=sp3[0].nval;i++)
 {
  //term[sp3[i].col]=term[sp3[i].col]+1;
  term[sp3[i].col]++;
 }
 //find out the actual position of each term i:e coloms in row.
 pos[0]=1;
 for(i=1;i<=sp3[0].col;i++)
 {
  pos[i]=pos[(i-1)]+term[(i-1)];
 }
 //fid the position and interchange the value.
 for(i=1;i<=sp3[0].nval;i++)
 {
  j=pos[sp3[i].col];
  fstr[j].row=sp3[i].col;
  fstr[j].col=sp3[i].row;
  fstr[j].nval=sp3[i].nval;
  pos[sp3[i].col]=j+1;
 }//Totla time required to execute is O(n)+O(n)+O(n)+O(n) i:e O(n)
 cout<<"\n\n Fast Transpose of addition matrix are as follows...";
 cout<<"\nRow\tCol\tnval";
 for(i=0;i<=fstr[0].nval;i++)
 {
  cout<<"\n"<<fstr[i].row<<"\t"<<fstr[i].col<<"\t"<<fstr[i].nval;
 }
}
 
//Main function logic.
//Program execution starts from here.
int main()
{
 int n;
 sparse s1;  //s1 object of class sparse
 s1.getmat();
 s1.showmat();  //calling of member functions.
 s1.addspm();
 s1.simpl_trnspose();
 s1.fast_trnspose();
// getch();
 cout<<"\nPress one to exit....";
 cin>>n;

 return 0;
}

/********************** OUTPUT ****************************************
[student@localhost Documents]$ g++ SPMAT.CPP
SPMAT.CPP:10:1: warning: ‘typedef’ was ignored in this declaration [enabled by default]
 };
 ^
[student@localhost Documents]$ ./a.out

How many rows in mat1:3

How many cols in mat1:3

Enter [0][0] value of mat1: 0

Enter [0][1] value of mat1: 0

Enter [0][2] value of mat1: 3

Enter [1][0] value of mat1: 4

Enter [1][1] value of mat1: 0

Enter [1][2] value of mat1: 5

Enter [2][0] value of mat1: 0

Enter [2][1] value of mat1: 0

Enter [2][2] value of mat1: 0

How many rows in mat2:3

How many cols in mat2:3

Enter [0][0] value of mat2: 0

Enter [0][1] value of mat2: 2

Enter [0][2] value of mat2: 0

Enter [1][0] value of mat2: 2

Enter [1][1] value of mat2: 0

Enter [1][2] value of mat2: 4

Enter [2][0] value of mat2: 0

Enter [2][1] value of mat2: 0

Enter [2][2] value of mat2: 2

 Matrix 1 value are as follows
    0    0    3
    4    0    5
    0    0    0

 Matrix 2 value are as follows
    0    2    0
    2    0    4
    0    0    2

 Sparse matrix1 non_zero value are as follows:

Row    Col    nval
3    3    3
0    2    3
1    0    4
1    2    5

Sparse matrix2 non_zero value are as follows:

Row    Col    nval
3    3    4
0    1    2
1    0    2
1    2    4
2    2    2
 No of term in sp1:3    No of term in sp2:4
val of j:2    val of K:2
val of i:2    val of K:3
valu of add:6
val of i:3    val of j:3    val of k:4
valu of add:9
val of i:4    val of j:4    val of k:5
val of  outer while j:5    val of K:6
Row    Col    nval
3    3    5
0    1    2
0    2    3
1    0    6
1    2    9
2    2    2

 Simple Transpose of addition matrix are as follows...
Row    Col    nval
3    3    5
0    1    6
1    0    2
2    0    3
2    1    9
2    2    2

 Fast Transpose of addition matrix are as follows...
Row    Col    nval
3    3    5
0    1    6
1    0    2
2    0    3
2    1    9
2    2    2
Press one to exit....

*******************************************************************************************/
data structures and algorithms Web Developer