Using the stack to implement the matching of three parentheses in the string C + + language implementation

Source: Internet
Author: User
Tags class definition

write an algorithm that checks the curly brackets in a program, whether the square brackets and parentheses are paired, or 1 if all pairs are returned, otherwise 0.

Head.h:

#ifndef head_h_included

#define Head_h_included

#include <iostream>

struct Linkednode

{

int data;

Linkednode*next;

};

class definition for linkedstack//chain Stacks

{

Public

Linkedstack ();

~linkedstack () {makeempty ();};

void Push (Linkednode &);

int Pop ();

BOOL GetTop (linkednode&x) const;

BOOL IsEmpty () Const{return (top==null)? True:false;}

int getsize () const;

void Makeempty ();

void print ();

Private

Linkednode *top;

};

#endif//head_h_included

Methods.cpp:

#include "Head.h"

#include <iostream>

using namespace Std;

Linkedstack::linkedstack () {top=null;}

void Linkedstack::makeempty ()

{

Linkednode*p;

while (Top!=null)

{

P=top;top=top->next;delete p;

}

}

void Linkedstack::P ush (Linkednode &x)

{

Linkednode *p=top;

x.next=p;

top=&x;

}

int Linkedstack::P op ()

{int data=top->data;

Linkednode*p=top;

top=top->next;

Delete p;

return data;

}

BOOL Linkedstack::gettop (LINKEDNODE&X) const

{

if (IsEmpty () ==true) return false;

X=*top;

return true;

}

int linkedstack::getsize () const

{

int k=0;

Linkednode*p=top;

while (p!=null) {p=p->next;k++;}

return k;

}

void Linkedstack::p rint ()

{

cout<< " number of elements in stack =" <<getsize () <<endl;

Linkednode*p=top;int i=0;

while (P!=null)

{

cout<<++i<< ":" <<p->data<<endl;

p=p->next;

}

}

Main.cpp:

#include "Head.h"

#include <iostream>

#include <string.h>

using namespace Std;

The required algorithm for judging the matching brackets

void Printmatchedpairs (char *expression)

{

Linkedstack S1; Linkedstack S2; Linkedstack S3;

int J,length=strlen (expression);

cout<< " for parentheses () :" <<endl;

for (int i=1;i<=length;i++)

{

if (expression[i-1]== ' (') {Linkednode t;t.data=i;s1. Push (t);} left parenthesis, position into stack

else if (expression[i-1]== ') ')

{

if (S1. IsEmpty () ==false) {j=s1. Pop ();cout<<j<< " and " <<i<< " match " <<ENDL;}

else cout<< " does not have an opening parenthesis that matches the " <<i<< " closing parenthesis !" <<endl;

}

}

while (S1. IsEmpty () ==false)

{

J=s1. Pop ();

cout<< " There is no closing parenthesis that matches the " <<j<< " bracket !" <<endl;

}

cout<< " for brackets [] :" <<endl;

for (int i=1;i<=length;i++)

{

if (expression[i-1]== ' [') {Linkednode t;t.data=i;s2. Push (t);} left parenthesis, position into stack

else if (expression[i-1]== '] ')

{

if (S2. IsEmpty () ==false) {j=s2. Pop ();cout<<j<< " and " <<i<< " match " <<ENDL;}

else cout<< " does not have an opening parenthesis that matches the " <<i<< " closing parenthesis !" <<endl;

}

}

while (S2. IsEmpty () ==false)

{

J=s2. Pop ();

cout<< " There is no closing parenthesis that matches the " <<j<< " bracket !" <<endl;

}

cout<< " for curly braces {} :" <<endl;

for (int i=1;i<=length;i++)

{

if (expression[i-1]== ' {') {Linkednode t;t.data=i;s3. Push (t);} left parenthesis, position into stack

else if (expression[i-1]== '} ')

{

if (S3. IsEmpty () ==false) {j=s3. Pop ();cout<<j<< " and " <<i<< " match " <<ENDL;}

else cout<< " does not have an opening parenthesis that matches the " <<i<< " closing parenthesis !" <<endl;

}

}

while (S3. IsEmpty () ==false)

{

J=s3. Pop ();

cout<< " There is no closing parenthesis that matches the " <<j<< " bracket !" <<endl;

}

}

int main ()

{

Char s[100];

cout<< " Please enter a string with three parentheses to be judged " <<endl;

cin>>s;

Printmatchedpairs (s);

return 0;

}

Operation result :

Using the stack to implement the matching of three parentheses in the string C + + language implementation

Related Article

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.