生活随笔
收集整理的這篇文章主要介紹了
数据结构--括号匹配检验(数据结构习题)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
《數(shù)據(jù)結(jié)構(gòu)》嚴(yán)蔚敏版習(xí)題3.2.2,括號匹配問題。是順序棧的課后習(xí)題。原問題:假設(shè)表達(dá)式中允許包括兩種括號:圓括號和方括號,其嵌套方式隨意,即(【】())等都是正確的格式,【(】)是不正確的格式。設(shè)計(jì)一個算法檢查輸入的字符串中的括號是否是匹配的。 /-------------------------------------------------------------/ 思路:使用棧來解決,遇到(、【就入棧,遇到)、】且剛還能和棧頂元素匹配是就出棧,遇到其他元素不做處理。字符轉(zhuǎn)中所有的元素都處理完后,查看棧的長度,若為空棧說明括號匹配,棧非空則說明括號不匹配。 所以需要一個可以存儲字符元素的棧,以及匹配的各種方法:
#include <stdio.h>
#include <stdlib.h>
#define INITSIZE 100
#define INCRSIZE 10
#define ERROR 0
#define OK 1 typedef int Status
;
typedef struct { char * base
; char * top
; int size
;
} SqStack
; Status
InitStack ( SqStack
* s
) { s
-> base
= ( char * ) malloc ( INITSIZE
* sizeof ( char ) ) ; if ( ! s
-> base
) { return ERROR
; } s
-> top
= s
-> base
; s
-> size
= INITSIZE
; return OK
;
} Status
DestroyStack ( SqStack
* s
) { if ( ! s
-> base
) { exit ( 1 ) ; } free ( s
-> base
) ; s
-> base
= NULL ;
} Status
ClearStack ( SqStack
* s
) { s
-> top
= s
-> base
;
} Status
StackEmpty ( SqStack
* s
) { if ( s
-> top
== s
-> base
) { return 1 ; } else { return 0 ; }
} int StackLength ( SqStack
* s
) { return ( s
-> top
- s
-> base
) ;
} Status
GetTop ( SqStack s
, char * e
) { if ( s
. top
== s
. base
) { return ERROR
; } * e
= * ( s
. top
- 1 ) ; return OK
;
} Status
Push ( SqStack
* s
, char e
) { if ( s
-> top
- s
-> base
>= s
-> size
) { s
-> base
= ( char * ) realloc ( s
-> base
, ( s
-> size
+ INCRSIZE
) * sizeof ( char ) ) ; if ( ! s
-> base
) { exit ( 1 ) ; } s
-> top
= s
-> base
+ s
-> size
; s
-> size
+ = INCRSIZE
; } * ( s
-> top
) = e
; s
-> top
++ ; return OK
;
} Status
Pop ( SqStack
* s
, char * e
) { if ( s
-> top
== s
-> base
) { return ERROR
; } * e
= * ( -- s
-> top
) ; return OK
;
} void StackTraverse ( SqStack
* s
) { if ( ! s
-> base
) { exit ( 1 ) ; } char * t
= s
-> top
- 1 ; while ( t
>= s
-> base
) { printf ( "%c" , * t
) ; t
-- ; }
}
保存為stack.h的頭文件: 然后是設(shè)計(jì)主函數(shù)的算法: 先畫出算法流程圖: 實(shí)現(xiàn)代碼:
#include <stdio.h>
#include <stdlib.h>
#include "stack.h" int main ( ) { SqStack s
, * sp
= & s
; InitStack ( sp
) ; char str
[ 20 ] ; scanf ( "%s" , str
) ; if ( str
[ 0 ] == ']' || str
[ 0 ] == ')' ) { printf ( "括號不匹配\n" ) ; exit ( 1 ) ; } char c
, d
, * cp
= & c
, * dp
= & d
; for ( int i
= 0 , c
= str
[ 0 ] ; c
!= '\0' ; i
++ ) { c
= str
[ i
] ; if ( ! StackEmpty ( sp
) ) { GetTop ( s
, dp
) ; } char t
, * tp
= & t
; switch ( c
) { case '[' : Push ( sp
, c
) ; break ; case '(' : Push ( sp
, c
) ; break ; case ']' : if ( d
== '[' ) { Pop ( sp
, tp
) ; } else { Push ( sp
, c
) ; } break ; case ')' : if ( d
== '(' ) { Pop ( sp
, tp
) ; } else { Push ( sp
, c
) ; } break ; default : break ; } } printf ( "當(dāng)前棧長:%d\n" , StackLength ( sp
) ) ; if ( StackEmpty ( sp
) ) { printf ( "括號匹配\n" ) ; } else { printf ( "括號不匹配\n" ) ; } return 0 ;
}
運(yùn)行實(shí)驗(yàn):
( x
+ y
) + [ z
- t
* ( 8 * 7 ) ]
當(dāng)前棧長:
0
括號匹配
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Process exited after
49.2 seconds with
return value
0
請按任意鍵繼續(xù)
. . .
/ -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - / ( ] )
當(dāng)前棧長:
3
括號不匹配
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Process exited after
10.03 seconds with
return value
0
請按任意鍵繼續(xù)
. . .
代碼運(yùn)行正確。
總結(jié)
以上是生活随笔 為你收集整理的数据结构--括号匹配检验(数据结构习题) 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔 推薦給好友。