Use the C ++ chain stack to solve the brackets matching problem in the data structure.

Source: Internet
Author: User

I believe all those who have learned the data structure know this classic problem: matching brackets. No more nonsense. Check the code.

Linkstack. h

# Include <stdio. h> <br/> template <class T> class linkstacknode <br/>{< br/> Public: <br/> T data; <br/> linkstacknode <t> * link; <br/> linkstacknode (T & Value): Link (null), data (value) {}< br/> }; <br/> template <class T> class linkstack <br/> {<br/> linkstacknode <t> * TOS; <br/> Public: <br/> linkstack (): TOS (null) {}< br/> void push (T & value); <br/> t pop (); <br/> T & gettop (); <br/> bool isempty (); <br/> void makeempty (); <Br/>}; <br/> template <class T> <br/> void linkstack <t>: Push (T & value) <br/> {<br/> linkstacknode <t> * Add = new linkstacknode <t> (value); <br/> Add-> link = TOS; <br/> TOS = add; <br/>}< br/> template <class T> <br/> T linkstack <t>: Pop () <br/>{< br/> // assert (TOs! = NULL); <br/> linkstacknode <t> * Old = TOS; <br/> TOS = TOS-> link; <br/> T data = old-> data; <br/> delete old; <br/> return data; <br/>}< br/> template <class T> <br/> T & linkstack <t> :: gettop () <br/>{< br/> return TOS-> data; <br/>}< br/> template <class T> <br/> bool linkstack <t>: isempty () <br/>{< br/> return TOS = NULL; <br/>}< br/> template <class T> <br/> void linkstack <t> :: makeempty () <br/>{< br/> // while (TOs! = NULL) <br/> // POP (); <br/> while (! This-> isempty () <br/>{< br/> This-> POP (); <br/>}< br/>}

The main function is as follows:

# Include <iostream> <br/> # include <stdio. h> <br/> # include <assert. h> <br/> # include "linkstack. H "<br/> using namespace STD; <br/> int main () <br/>{< br/> cout <" input bracket sequence (ended with 0 ): "; <br/> linkstack <char> small; <br/> char a; <br/> DO <br/>{< br/> CIN>; <br/> switch (a) <br/> {<br/> case '(': <br/> small. push (a); <br/> break; <br/> case ')': <br/> If (! Small. isempty () <br/>{< br/> small. pop (); <br/> break; <br/>}< br/> If (small. isempty () <br/>{< br/> cout <"wrong! "<Endl; <br/> exit (0); <br/> break; <br/>}</P> <p >}while (! = '0'); <br/> If (small. isempty () <br/>{< br/> cout <"OK! "<Endl; <br/>}< br/> else <br/> cout <" wrong! "<Endl; <br/> return 0; </P> <p>}

A simple implementation is mainly the implementation of the template class of the chain stack, which is somewhat important.

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.