Tuesday, August 21, 2018

Program On "Queue"


"Nice One Program"


//DIVIDE always divide your work and proceed for that
 if You divide Your code and then go for it that would be Easy     Task it Just looks 50 60 Lines of Code But not A difficult Task   For beginers.


Queue: Is the Abstract Data Structure.Like a Stack where insertionEnqueue ) And Deletion( Dequeue . Works on FIFO that is First In First Out. Hence Insertion will at end And Deletion will be at the beginning.



Check Out Its Code. Do Write Queries in Comment.

//Header Files
//Code Executed In TurboC++


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

#define size 50
int front=-1;
int rear=-1;

void enq(int *s)
{
        int data;
        if(rear==size-1)
            cout<<"Error:Overflow";
       else
       {
       cout<<"\nEnter data:";
       cin>>data;
       if(front==-1)
           {
             front=rear=0;
             s[front]=data;

      }

      else
     { 
                     rear++;
             s[rear]=data;
      }
     
        }

}    //Insertion In Queue End

//Code For Deletion

void deq()
{
     if(front==-1||front>rear)
         cout<<"\n\nError:UnderFlow can't Perform Deletion:\n";
    else
   {
         front=front+1;

    }


}  //Code For deletion End

  //Main Method

void main()
{   
        clrscr();

        int choice;
        int queue[size];
        //int queue=new queue[];
        cout<<"Size of Queue Is Predefined:\n\n";
        cout<<"\nEnter Choice:\n";
        cout<<"Enqueue(1):";
        cout<<"\nDequeue(2):";
        cout<<"\nExit(3):";
        cin>>choice;


        while(choice!=3)
        {
               if(choice==1)
               enq(queue);
               
               else if(choice==2)
              {
                   if(front==-1)
              {
            cout<<"UnderFlow:Erorr";
            break;
               }

       cout<<"\nDeleting Element:"<<queue[front]<<endl;
           
               deq();
              
               }
             
               else
                  break;



              cout<<"\nEnter Choice:\n";
              cout<<"Enqueue(1):";
              cout<<"\nDequeue(2):";
              cout<<"\nExit(3):";
              cin>>choice;

        }//While loop end

  if(front!=-1)
  for(int i=front;i<=rear;i++)
  cout<<queue[i]<<" ";

  getch();

}  //End for Main Method;;






Monday, August 13, 2018

Program On Dynamic Stack

"Nice One Program"

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

int top=-1,size;
void push(int *p,int val)
{
if(top==-1)
{top=0;
p[top++]=val;
}
else if(top==size)
cout<<"\nOverflow:\n";
else
p[top++]=val;
}
//pop func

void pop()
{
       if(top==-1)
       cout<<"\nUnderflow:\n";
       else
       top--;

}

void main()
{  
     clrscr();
    
    cout<<"Enter size of array:";
    cin>>size;
    int *p=new int[size];
    int ch,item;
    cout<<"1 PUSH:";
    cout<<"2 POP:";
    cout<<"3 Exit:";
    cin>>ch;
   
  while(ch!=3)

{
if(ch==1)
{    
                 cout<<"Enter Ele:";

         cin>>item;
         push(p,item);

}

else if(ch==2)

             pop();
}

else if(ch==3)
break;

cout<<"1 PUSH:";
cout<<"2 POP:";
cout<<"3 Exit:";
cin>>ch;

}

for(int i=0;i<size;i++)
        cout<<p[i]<<" ";

getch();
}