C + + implementation of sequential stack and its implementation of parentheses matching

Source: Internet
Author: User

#include <iostream>
#include <cassert>
#include <cstring>
#include <string>
using namespace Std;
int maxlength=100;
const int stackincreament = 20;
Template<class t>
Class stack{
Public
Stack () {}
virtual void Push (const t& x) = 0;
virtual bool Pop (t& x) = 0;
virtual bool Getpop (t& x) = 0;
virtual bool IsEmpty () = 0;
virtual bool Isfull () = 0;
virtual int getsize () = 0;
};
Template<class t>
Class Seqstack:public stack<t>{
Public
Seqstack (int sz=50);
~seqstack () {delete []elements;}
void Push (const t& x);
BOOL Pop (t& x);
BOOL Getpop (t& x);
BOOL IsEmpty () {return (top==-1)? True:false;}
BOOL Isfull () {return (top==maxsize-1)? True:false;}
int GetSize () {return top+1;}
void Makeempty () {top=-1;}


Private
T *elements;
int top;
int maxSize;
void Overflowprocess ();

};
Template<class t>
Seqstack<t>::seqstack (int sz) {
Top=-1;
Maxsize=sz;
elements = new T[maxsize];
ASSERT (Elements!=null);
}

Template<class t>
void Seqstack<t>::P ush (const t& x)
{
if (Isfull () ==true)
Overflowprocess ();
Elements[++top]=x;
}

Template<class t>
BOOL Seqstack<t>::P op (t& x)
{
if (IsEmpty () ==true)
return false;//cout<<top<<endl;
x=elements[top--];

return true;
}

Template<class t>
BOOL Seqstack<t>::getpop (t& x)
{
if (IsEmpty () ==true)
return false;
X=elements[top];
return true;

}

Template<class t>
void Seqstack<t>::overflowprocess () {
T *newarray = new T[maxsize+stackincreament];
if (newarray==null)
{
cout<< "Memory allocation failed" <<endl;
return;
}
for (int i=0;i<=top;i++)
{
Newarray[i]=elements[i];
}
Maxsize=maxsize+stackincreament;
delete []elements;
Elements =newarray;
}

void Printmatchedpairs (string expression)
{
seqstack<int> s (maxLength);
Seqstack<int> A (maxLength);
Seqstack<int> b (maxLength);
int J,c,d,r,t=0,length=expression.length ();
int e=0,m=0;
for (int i=1;i<=length+1;i++)
{
if (expression[i-1]== ' (')
S.push (i);
else if (expression[i-1]== ') ')
{
for (int r=1;r<=i;r++)
if (expression[r-1]== ') ' | | expression[r-1]== '} ')
t++;
if (t!=0)
{
cout<< "Failure";
m++;
Break
}
S.pop (j);
}
else if (expression[i-1]== ' [')
A.push (i);
else if (expression[i-1]== '] ')
{
for (int r=1;r<=i;r++)
if (expression[r-1]== '} ')
e++;
if (e!=0)
{
cout<< "Failure";
m++;
Break
}
A.pop (j);
}
else if (expression[i-1]== ' {')
B.push (i);
else if (expression[i-1]== '} ')
B.pop (d);
}
if (m==0)
{
if (S.isempty () ==true&&a.isempty () ==true&&b.isempty () ==true)
{
cout<< "Success";

}
Else
cout<< "Failure";
}

}
int main ()
{
string expression;
cin>>expression;
Printmatchedpairs (expression);

return 0;
}

C + + implementation of the sequential stack and its use to implement parentheses matching

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.