Thursday, 12 October 2017

A double-ended queue(deque) is a linear list in which additions and deletions may be made at either end. Obtain a data representation mapping a deque into a one-dimensional array. Write C++ program to simulate deque with functions to add and delete elements from either end of the deque.
--------------------------------------------------------------------------------------------------------------------------

#include<iostream>
//Author : Vivek S. Sharma
//Copyright : Your copyright notice
using namespace std;
#define size 5
class dqueue{
      int q[size];
      int f,r;
public :
      dqueue(){
            f=r=-1;
            q[-1]=NULL;
      }

      void display(){
            for(int i=f ; i<=r ; i++){
                  cout<<"\n"<<q[i];
            }
      }
      void insert_end(int data){
            if(r==size-1){
                  cout<<"\nNotpossibel";
            }
            else{
            if(f==-1 && r==-1){
                              f=r=0;
                              q[r]=data;
                        }
                        else{
                              r=r+1;
                              q[r]=data;
                        }

                        display();
                  }}
      void delete_end()
      {
              if(f==-1)
              {
                  cout<<"deletion is not possible::dequeue is empty";
                  return;
              }
              else
              {
                  cout<<"the deleted element is"<<q[r];
                  if(f==r)
                  {
                      f=r=-1;
                  }
                  else
                      r=r-1;
              }
              display();
      }
      void delete_front(){
            if(f==-1)
                    {
                        cout<<"deletion is not possible::dequeue is empty";
                        return;
                    }
                    else
                    {
                        cout<<"the deleted element is"<<q[f];
                        if(f==r)
                        {
                            f=r=-1;
                            return;
                        }
                        else
                            f=f+1;
                    }
            display();

      }
      void insert_begin(int data){

            if(f==-1 && r==-1){
                  f=r=0;
                  q[f]=data;
            }

            else if(f!=0){
                  f=f-1;
                  q[f]=data;
            }
            else
                  cout<<"\nNot possible ";

            display();
      }
};
int main(){
      int ch;
      int data;
      dqueue q;
      do{
            cout<<"\n1.Add at begin \n2.Add at end \n3.Delete front \n4.Delete end\n5.Exit";
            cin>>ch;
            switch(ch){
            case 1 : cout<<"\nEnter data : ";
                        cin>>data;
                        q.insert_begin(data);
                        break;
            case 2 : cout<<"\nEnter data : ";
                        cin>>data;
                        q.insert_end(data);
                        break;
            case 3 :q.delete_front();break;
            case 4 :q.delete_end();break;
            }
      }while(ch!=5);
      return 0;

}
--------------------------------------------------------------------------------------------------------------------------



**If some mistakes, plz let me know!-----Vivek S. Sharma
Write C++ program to store roll numbers of student in array who attended training program in random order. Write function for-
a) Searching whether particular student attended training program or not using linear search and sentinel search.
b) Searching whether particular student attended training program or not using binary search and Fibonacci search.

--------------------------------------------------------------------------------------------------------------------------
#include<iostream>
//Author : Vivek S. Sharma

//Copyright : Your copyright notice
using namespace std;
class search
{
int a[10],n,search;
public:
void insert_roll()
{

  cout<<"\nenter the no of student present";
  cin>>n;
  cout<<"\nenter the roll_no of student:";
  for( int i=0;i<n;i++)
   {
      cin>>a[i];
   }

}
void display()
{
cout<<"\nthese student are present";
 for( int i=0;i<n;i++)
{
cout<<"\n"<<a[i];
}
}

void find_binary(){
      int first,middle, last;
      cout<<"\nEnter Number to find :";
      cin>>search;
      first=0;
      last=n-1;
      middle=(first+last)/2;
      while(first<=last){

      if(a[middle] < search){
      first=middle+1;
      }

      else if(a[middle]==search)
      {cout<<"\nFound at "<<middle+1<<" location ";
      break;}

      else
      {last=middle-1;}

      middle=(first+last)/2;


      }
      if(first>last)
      cout<<"Not found";

}


void find_linear(){
      int c=0,pos;
      cout<<"\nEnter Number to find :";
            cin>>search;
      for(int i=0; i<n; i++)
            {
                  if(a[i]==search)
                  {
                        c=1;
                        pos=i+1;
                        break;
                  }
            }
            if(c==0)
            {
                  cout<<"\nNot found..!!";
            }
            else
            {
                  cout<<search<<" found at position "<<pos;
            }
}

void find_sentinel()
{
int i=0;
int last;
cout<<"\nenter the number to find:";
cin>>search;
last=a[n-1];
a[n-1]=search;


while(a[i]!=search)
{
i++;
}
a[n-1]=last;
if(i<n-1||search==a[n-1])
{
cout<<"Found : "<<search<<" at position : "<<i+1;
}
else
{
cout<<"!!not found";
}
}
};
int main()
{
search s1;
int ch;
s1.insert_roll();
s1.display();
do{
cout<<"\n\nSearch Using : \n1.Sentinel Serach \n2.Linear Search \n3.Binary Search \n4.Fibinacci Serach \n5.Exit";
cin>>ch;
switch(ch){
case 1: s1.find_sentinel();break;
case 3: s1.find_binary();break;
case 2: s1.find_linear();break;
};

}while(ch!=4);

return 0;
}
--------------------------------------------------------------------------------------------------------------------------



**If some mistakes, plz let me know!-----Vivek S. Sharma