Wednesday, 22 November 2017

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.

#include<iostream>
using namespace std;
#define size 20
class stack{
char stk[size];
int top;
public:
stack(){
top=-1;
}
void push(char x){
stk[++top]=x;
}
char pop(){
char temp=stk[top];
top--;
return temp;
}
bool empty(){
if(top==-1)
return true;
else return false;
}
char tops(){
char ch;
ch=stk[top];
return ch;
}


};
int main(){
stack st;
string s;
char ch, temp;
int i=0;
cout<<"\nEnter the expression : ";
cin>>s;
if(s[0]==')' || s[0]==']' || s[0]=='}')
cout<<"\nInvalid";
else{
while(s[i]!='\0'){
ch=s[i];
switch(ch){
case '(' : st.push(ch);break;
case '{' : st.push(ch);break;
case '[' : st.push(ch);break;
case ')' : if(st.tops()=='(')temp=st.pop();break;
case '}' : if(st.tops()=='{')temp=st.pop();break;
case ']' : if(st.tops()=='[')temp=st.pop();break;
}
i++;
}
if(st.empty())
cout<<"\nValid ";
else
cout<<"\nInvalid";
}

return 0;
}

Implement C++ program for expression conversion-
a) infix to prefix, b)prefix to postfix,
c) prefix to infix, d) postfix to infix and e) postfix to prefix.
#include"iostream"
#include"string"
using namespace std;

class post
{
      char a[20];
      int top;
      string s[20];
public:
      post()
      {
            top = -1;
      }
      void push(string );
      string pop();
      void get();
      void post2infix();
      void post2prefix();
};

void post::get()
{
      cout << "Enter a postfix Equation : ";
      cin >> a;
}

void post::push(string s1)
{
      top++;
      s[top] = s1;
}

string post::pop()
{
      string s1;
      s1 = s[top];
      top--;
      return s1;
}

void post::post2infix()
{
      int i = 0;
      string s1, s2, s3, ch;

      while (a[i] != '\0')
      {
            ch = a[i];
            if (a[i] == '+' || a[i] == '-' || a[i] == '*' || a[i] == '/')
            {
                  s1 = pop();
                  s2 = pop();
                  s3 = s2 + a[i] + s1;
                  push(s3);
                  i++;
            }
            else
            {
                  push(ch);
                  i++;
            }
      }

      cout << "Infix Equation is : ";
      for (int m = 0; m <= top; m++)
      {
            cout << s[m];
      }
      cout << endl;
}

void post::post2prefix()
{
      int i = 0;
      string s1, s2, s3, ch;

      while (a[i] != '\0')
      {
            ch = a[i];
            if (a[i] == '+' || a[i] == '-' || a[i] == '*' || a[i] == '/')
            {
                  s1 = pop();
                  s2 = pop();
                  s3 = a[i] + s2 + s1;
                  push(s3);
                  i++;
            }
            else
            {
                  push(ch);
                  i++;
            }
      }

      cout << "Prefix Equation is : ";
      for (int m = 0; m <= top; m++)
      {
            cout << s[m];
      }
      cout << endl;
}

int main()
{
      int x, t;
      post s;
      do
      {
            cout << "\n 1.Insert Equation \n 2.Postfix2Infix \n 3.Postfix2Prefix : ";
            cin >> t;
            switch (t)
            {
            case 1:
                  s.get();
                  break;
            case 2:
                  s.post2infix();
                  break;
            case 3:
                  s.post2prefix();
                  break;

            default:
                  cout << "\n Invalid Input ";
            }
            cout << "\n Display Menu 1.Yes 2.No ";
            cin >> x;
      } while (x == 1);
}


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>
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;
}