Total Pageviews

Tuesday, February 13, 2024

17. WAP to reverse the order of the elements in the stack using additional Queue.

 17. WAP to reverse the order of the elements in the stack using additional Queue. 

PROGRAME CODE

#include<stdio.h>

#include<iostream>

using namespace std;

class stack_rev{

            int *arr,top,size,*arr1,front,rear;

            public:

                        stack_rev(){

                                    arr=NULL;

                                    arr1=NULL;

                                    top=-1;

                                    size=0;

                                    front=0;

                                    rear=-1;

                        }

                        void getdata(){

                                    cout<<"ENTER THE LENGTH OF THE STACK ->"<<endl;

                                    cin>>size;

                                    arr=new int[size];

                                    arr1=new int[size];

                        }

                        void push(){

                                    if(top==(size-1))

                                                cout<<"THE STACK OVERFLOW...."<<endl;

                                    else{

                                                top++;

                                                cout<<"ENTER THE DATA ->"<<endl;

                                                cin>>arr[top];

                                    }

                        }

                        void stack_rev1(){

                                    if(rear==(top-1)&&front==0)

                                                cout<<"THE QUEUE IS OVER FLOW...."<<endl;

                                    else{

                                                while(top>=0){

                                                            rear++;

                                                            arr1[rear]=arr[top];

                                                            top--;

                                                }

                                    }

                        }

                        void display(){

                                    cout<<"THE ORIGINAL STACK IS->"<<endl;

                                    int x=top+1;

                                    while(--x>=0)

                                                cout<<arr[x]<<endl;

                        }

                        void display1(){

                                    cout<<"THE REVERSE STACK ->"<<endl;

                                    int x=rear+1;

                                    while(--x>=front)

                                                cout<<arr1[x]<<endl;

                        }

};

int main(){

            stack_rev ob;

            int opt=0;

            do{

                        cout<<"\nPRESS 0 TO EXIT ......"<<endl;

                        cout<<"PRESS 1 TO SET STACK LENGTH ......"<<endl;

                        cout<<"PRESS 2 TO ENTER DATA IN STACK .........."<<endl;

                        cout<<"PRESS 3 TO DISPLAY THE STACK......."<<endl;

                        cout<<"PRESS 4 TO REVERSE THE STACK ......"<<endl;

                        cout<<"PRESS 5 TO PRINT THE REVERSE OF THE STACK..........."<<endl;

                        cin>>opt;

                        switch(opt){

                                    case 0:

                                                break;

                                    case 1:

                                                ob.getdata();

                                                break;

                                    case 2:

                                                ob.push();

                                                break;

                                    case 3:

                                                ob.display();

                                                break;

                                    case 4:

                                                ob.stack_rev1();

                                                cout<<"REVERSE SUCESSFULL ....."<<endl;

                                                break;

                                    case 5:

                                                ob.display1();

                                                break;

                                    default:

                                                cout<<"ERROR OPTION CHOICE........."<<endl;

                        }

            }while(opt!=0);

            return 0;

}

 


16. WAP to reverse the order of the elements in the stack using additional stack.

 16. WAP to reverse the order of the elements in the stack using additional stack. 

PROGRAME CODE

#include<stdio.h>

#include<iostream>

using namespace std;

class stack_rev{

                int size,*arr,*arr1,top,top1;

                public:

                                stack_rev(){

                                                size=0;

                                                arr=NULL;

                                                arr1=NULL;

                                                top=-1;

                                                top1=-1;

                                }

                                void getdata(){

                                                cout<<"ENTER THE SIZE OF THE STACK-> "<<endl;

                                                cin>>this->size;

                                                arr=new int[this->size];

                                                arr1=new int[this->size];

                                }

                                void push(){

                                                if(top==(size-1))

                                                                cout<<"THE STACK IS OVER-FLOW...."<<endl;

                                                else{

                                                                int x=0;

                                                                cout<<"ENTER THE ELEMENT ->"<<endl;

                                                                cin>>x;

                                                                top++;

                                                                arr[top]=x;

                                                }

                                }

                                void revstack(){

                                                if(top1==(size-1))

                                                                cout<<"THE STACK IS OVER-FLOW...."<<endl;

                                                else{

                                                                while(top>=0){

                                                                                top1++;

                                                                                arr1[top1]=arr[top];

                                                                                top--;

                                                                }

                                                }

                                               

                                }

                                void display1(){

                                                cout<<"THE ORIGINAL STACK IS ->"<<endl;

                                                int x=top+1;

                                                while(--x>=0)

                                                                cout<<arr[x]<<endl;

                                }

                                void display2(){

                                                cout<<"THE REVERSE STACK IS ->"<<endl;

                                                int x=top1+1;

                                                while(--x>=0)

                                                                cout<<arr1[x]<<endl;

                                }                             

};

int main(){

                stack_rev ob;

                int opt=0;

                do{

                                cout<<"\nPRESS 0 TO EXIT ...."<<endl;

                                cout<<"PRESS 1 TO SET STACK SIZE...."<<endl;

                                cout<<"PRESS 2 TO PUSH DATA...."<<endl;

                                cout<<"PRESS 3 TO DISPLAY THE STACK...."<<endl;

                                cout<<"PRESS 4 TO REV THE STACK...."<<endl;

                                cout<<"PRESS 5 TO DISPLAY THE REV STACK....."<<endl;

                                cin>>opt;

                                switch(opt){

                                                case 0:

                                                                break;

                                                case 1:

                                                                ob.getdata();

                                                                break;

                                                case 2:

                                                                ob.push();

                                                                break;

                                                case 3:

                                                                ob.display1();

                                                                break;

                                                case 4:

                                                                ob.revstack();

                                                                cout<<"THE REVERSE SUCCESSFULL.........."<<endl;

                                                                break;

                                                case 5:

                                                                ob.display2();

                                                                break;

                                                default:

                                                                cout<<"ERROR OPTION CHOICE ........."<<endl;

                                                                break;

                                }

                }while(opt!=0);

                return 0;

}


15. WAP to convert the Sparse Matrix into non-zero form and vice-versa.

 15. WAP to convert the Sparse Matrix into non-zero form and vice-versa. 


PROGRAME CODE

#include<stdio.h>

#include<iostream>

using namespace std;

class sparse_matrix{

            int A[50][50],B[50][3],row,col,k;

            public:

                        void datains(int row,int col){

                                    this->row=row;

                                    this->col=col;

                                    cout<<"ENTER THE DATA ->"<<endl;

                                    for(int i=0;i<row;i++){

                                                for(int j=0;j<col;j++)

                                                            cin>>A[i][j];

                                    }

                                    display();

                                    k=check();

                                    if(k){

                                                cout<<"THIS IS A PURSE MATRIX ...."<<endl;

                                                cout<<"THE TOTAL ELEMENTS ARE -> "<<(row*col)<<" THE TOTAL NON-ZERO ELEMENTS ARE ->"<<(row*col)-k<<endl;

                                    }

                                    else

                                                cout<<"THIS IS NOT A PURSE MATRIX ...."<<endl;                                     

                        }

                        int check(){

                                    int count=0;

                                    for(int i=0;i<row;i++){

                                                for(int j=0;j<col;j++){

                                                            if(A[i][j]==0)

                                                                        count++;

                                                }

                                    }

                                    if(count>=(row*col)/2)

                                                return count;

                                    else

                                                return 0;

                        }

                        void purse(){

                                    int p=0;

                                    for(int i=0;i<row;i++){

                                                for(int j=0;j<col;j++){

                                                            if(A[i][j]!=0){

                                                                        B[p][0]=i;

                                                                        B[p][1]=j;

                                                                        B[p][2]=A[i][j];

                                                                        p++;

                                                            }

                                                }

                                    }

                        }

                        void display(){

                                    cout<<"THE MATRIX IS->"<<endl;

                                    for(int i=0;i<row;i++){

                                                for(int j=0;j<col;j++){

                                                            cout<<A[i][j]<<"  ";

                                                }

                                                cout<<endl;

                                    }                      

                        }

                        void display1(){

                                    cout<<"THE NEW MATRIX IS->"<<endl;

                                    for(int i=0;i<((row*col)-k);i++){

                                                for(int j=0;j<col;j++){

                                                            cout<<B[i][j]<<"  ";

                                                }

                                                cout<<endl;

                                    }                      

                        }

};

int main(){

            int row=0,col=0;

            sparse_matrix ob;

            cout<<"ENTER THE ROW VALUE -> "<<endl;

            cin>>row;

            cout<<"ENTER THE COL VALUE -> "<<endl;

            cin>>col;

            ob.datains(row,col);

            ob.purse();

            ob.display1();

            return 0;

}