Work week Four

Source: Internet
Author: User

I feel that I can't write anything. Recently in the crazy tutorial hands to get a self-written link stack symbol matching application

Definition.h file

#ifndef Definition_h_
#define Definition_h_
#include <stdio.h>
#include <stdlib.h>
typedef char DataType;
typedef struct node{
DataType data;
struct node *next;
}stack,*linkstack;
#endif

function functions

#include "Definition.h" Linkstack init_linkstack (Stack *top);//empty stack int empty_stack (stack *top);//Judgment stack empty Linkstack push_ Linkstack (Stack *top,datatype x);//Press Stack DataType pop_linkstack (stack *top);//stack DataType top_linkstack;// Stack first element int Bracketscheck (stack *top, DataType a[]);//Bracket Pairing

Linkstack init_linkstack (Stack *top) { top = NULL;  return top;} int empty_stack (stack *top) { if (top = = NULL)   return 1;  else   return 0; } linkstack push_linkstack (stack *top,datatype x) { stack *s;  s = (Stack *) malloc (sizeof (stack));  s-> data = x;  s->next = top;  top = s;  return top; } DataType pop_linkstack (Stack *top) { stack *s;  if (top) empty_stack NULL;   return  else{ Bsp; datatype x;   x = top->data;   s = top;   top = top->next;   free (s);   return x;  }

} DataType top_linkstack (Stack *top) { if (Empty_stack (TOP))   return NULL;  else    Return top->data; } int Bracketscheck (stack *top,datatype a[]) { int i = 0;//starting from 0 scans the entire string  while (A[i]) {  datatype ch = a[i++];//The character variable to be scanned to CH      switch (CH) {   //switch The function of the string inside the parentheses in the case of processing     case ' {':    case ' [':    case ' (':     push_linkstack (Top, CH); Any kind of left parenthesis in the stack      break;    case '} ':     if (! Empty_stack (top) && (top) = = '} ')//Compare the brackets at the top of the stack with the right parenthesis to be scanned      pop_ Linkstack (top);//stack top left bracket     else      return 0;     break;    case ':     if (! Empty_stack (top) && (Top_linkstack (top) = = '} ')      pop_linkstack (top);     else      return 0;     break;    case ') ':     if (! Empty_stack (top) && (Top_linkstack (top) = = '} '))      pop_linkstack (top);     else      return 0;     break;   }//end Swithch

}//end while if (Empty_stack (top)) return 3; else return 0; }//end Bracketscheck

Keynote function Main

#define _CRT_SECURE_NO_WARNINGS//Header and vs2013 version above resolve macro Disable unsafe function
#include "Definition.h"
#include "Function.h"
int main () {
Stack *s;
int x;
s = (stack *) malloc (sizeof (stack));
s = init_linkstack (s);
printf ("Initialize successfully \ n");
DataType a[80];
printf ("Please enter a string:"); scanf ("%s", a);
X=bracketscheck (S,a);
if (x==1)
printf ("Match failed");
else if (x = = 0)
printf ("match success");
Else
printf ("No input brackets");
System ("pause");
}

Work week Four

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.