//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)