数据结构 3.2.2链栈完成括号的匹配(国外英文资料).docVIP

  • 12
  • 0
  • 约3.59万字
  • 约 8页
  • 2017-06-08 发布于河南
  • 举报

数据结构 3.2.2链栈完成括号的匹配(国外英文资料).doc

数据结构 3.2.2链栈完成括号的匹配(国外英文资料)

数据结构 3.2.2链栈实现括号的匹配(国外英文资料) The 3.2.2 stack implements the parentheses match / * * * chain stack implementation bracket matching * * / # include iostream Using namespace STD. 1 # define OK # define the ERROR 0 # define OVERFLOW - 2 Typedef char SElemType; Typedef int the Status; Typedef struct SNode { The int data; Struct SNode * next; } SNode, * LinkStack; The Status InitStack (LinkStack S) { S = NULL; Return OK; } Bool StackEmpty (LinkStack S) { If (! S) Return true; Return false; } Status Push (LinkStack S, SElemType e) { SNode * p = new SNode; If (!) p { Return OVERFLOW; } P minus data = e; P minus , next = S; S = p; Return OK; } Status Pop (LinkStack S, SElemType e) { SNode * p; If (! S) Return the ERROR; E is equal to S minus data. P = S; S = s- next; The delete p; Return OK; } Status GetTop (LinkStack S, SElemType e) { If (! S) Return the ERROR; E is equal to S minus data. Return OK; } / / the match for the 3.9 bracket of the algorithm The Status Matching (LinkStack S) { / / verify that the parentheses in the expression are correctly matched, or false if the match returns true. / / the expression ends in # Int flag = 1; / / tags find results to control loop and return results Char c; SElemType x; Cin c; / / read the first character While (c! = # flag) { The switch (c) { Case [ : Case ( : / / if left parenthesis, press it into the stack Push (S, c); Break; Case ) : / / if the right parenthesis is), it is considered based on the value of the current stack top element GetTop (S, x); If (! StackEmpty (S) x = = = = = () / / if the stack is not empty and the stack top element is (, the match is successful Pop (S, x); The else Flag = 0; / / if the stack empty or stack top element is not (, it is illegal Break; Case : / / if the right parenthesis is, it is considered based on the value of the current stack top element GetTop (S, x); If (! StackEmpty (S) x = = = = =) / / if the top element of the stack is [, the match is successful Pop (S, x); The e

您可能关注的文档

文档评论(0)

1亿VIP精品文档

相关文档