Application of data structure stack (nearest match)

Source: Internet
Author: User

//application of Stacks--nearest match#include <stdio.h>#include<stdlib.h>#include<string.h>#include"LinkStack.h"//referencing linked list stack dynamic library/*idea: Traverse each character, encounter the left symbol pressure stack, encountered the right symbol out of the stack, compare the symbol of the stack and the right symbol matches, encountered ordinary symbols regardless*///Symbolic processingintProtectsymbol (CharCH, Linkstack *stack) {    CharTEMPC =0, * tempc2=null,*temp3=NULL; Switch(CH) { Case '<':     Case '(':     Case '[':     Case '{':        //Press Stack Operation//you have to malloc the memory or the local variable will be released if you press the stack.Temp3 = (Char*)malloc(sizeof(Char)); memset (Temp3,0,sizeof(Char)); *temp3 =ch;        Linkstack_push (Stack, Temp3);  Break;  Case '>': TEMPC='<';  Break;  Case ')': TEMPC='(';  Break;  Case ']': TEMPC='[';  Break;  Case '}': TEMPC='{'; default:         Break; }    if(tempc!=0)    {        //out of stack operationTEMPC2 = (Char*) Linkstack_pop (stack); if(TEMPC2! =NULL) {            if(TEMPC! = *TEMPC2) {printf ("The mismatched symbol is%c\n", TEMPC); //Freeing Memory                 Free(TEMPC2); TEMPC2=NULL; return 1; }            //Freeing Memory             Free(TEMPC2); TEMPC2=NULL; }    }    return 0;}//Traversing StringsvoidPROTECTSTR (Char*pin, Linkstack *stack) {    if(pin==NULL) {        return; }    Char*temp =pin; Char*temp3 =NULL; intRET =0;  while(*temp) {ret= Protectsymbol (*temp, stack); if(ret!=0) {printf ("symbols do not match! \ n"); return; } temp++; }    //determine if there are elements in the stack that are missing from the proof sign     while(linkstack_size (Stack)) {//remove top element of stackTemp3 = (Char*) Linkstack_pop (stack); if(Temp3! =NULL) {printf ("symbol%c does not match \ n", *Temp3); //Freeing Memory             Free(Temp3); Temp3=NULL; }    }}voidMain () {Char*restr ="#include <stdio.h> int main () {int a[4][4]; int (*p) [4]; p = a[0]; return 0;"; //char *restr = "<1"; //prepare the Stack objectLinkstack *stack =linkstack_create (); if(stack==NULL) {        return;    } protectstr (restr, stack); //Destroy linked listLinkstack_destroy (&stack); System ("Pause");}

Application of data structure stack (nearest match)

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.