隨筆:20 文章:1 評(píng)論:8 引用:0
╰⊙д⊙╯。oо○
面朝大海·春暖花開(kāi)
BlogJava
首頁(yè)
發(fā)新隨筆
發(fā)新文章
聯(lián)系
聚合
管理
『第四章』棧的應(yīng)用:?jiǎn)卧~逆序&分隔符匹配
輸入字符串,然后按照逆序輸出:
//
reverse.java
//
stack used to reverse a string
//
to run this program: C>java ReverseApp
import
java.io.
*
;
//
for I/O
////////////////////////////////////////////////////////////////
class
StackX
{
private
int
maxSize;
private
char
[] stackArray;
private
int
top;
//
--------------------------------------------------------------
public
StackX(
int
max)
//
constructor
{
maxSize
=
max;
stackArray
=
new
char
[maxSize];
top
=
-
1
;
}
//
--------------------------------------------------------------
public
void
push(
char
j)
//
put item on top of stack
{
stackArray[
++
top]
=
j;
}
//
--------------------------------------------------------------
public
char
pop()
//
take item from top of stack
{
return
stackArray[top
--
];
}
//
--------------------------------------------------------------
public
char
peek()
//
peek at top of stack
{
return
stackArray[top];
}
//
--------------------------------------------------------------
public
boolean
isEmpty()
//
true if stack is empty
{
return
(top
==
-
1
);
}
//
--------------------------------------------------------------
}
//
end class StackX
////////////////////////////////////////////////////////////////
class
Reverser
{
private
String input;
//
input string
private
String output;
//
output string
//
--------------------------------------------------------------
public
Reverser(String in)
//
constructor
{ input
=
in; }
//
--------------------------------------------------------------
public
String doRev()
//
reverse the string
{
int
stackSize
=
input.length();
//
get max stack size
StackX theStack
=
new
StackX(stackSize);
//
make stack
for
(
int
j
=
0
; j
<
input.length(); j
++
)
{
char
ch
=
input.charAt(j);
//
get a char from input
theStack.push(ch);
//
push it
}
output
=
""
;
while
(
!
theStack.isEmpty() )
{
char
ch
=
theStack.pop();
//
pop a char,
output
=
output
+
ch;
//
append to output
}
return
output;
}
//
end doRev()
//
--------------------------------------------------------------
}
//
end class Reverser
////////////////////////////////////////////////////////////////
class
ReverseApp
{
public
static
void
main(String[] args)
throws
IOException
{
String input, output;
while
(
true
)
{
System.out.print(
"
Enter a string:
"
);
System.out.flush();
input
=
getString();
//
read a string from kbd
if
( input.equals(
""
) )
//
quit if [Enter]
break
;
//
make a Reverser
Reverser theReverser
=
new
Reverser(input);
output
=
theReverser.doRev();
//
use it
System.out.println(
"
Reversed:
"
+
output);
}
//
end while
}
//
end main()
//
--------------------------------------------------------------
public
static
String getString()
throws
IOException
{
InputStreamReader isr
=
new
InputStreamReader(System.in);
BufferedReader br
=
new
BufferedReader(isr);
String s
=
br.readLine();
return
s;
}
//
--------------------------------------------------------------
}
//
end class ReverseApp
////////////////////////////////////////////////////////////////
分隔符匹配:只有( )和[ ],如果輸入的表達(dá)式錯(cuò)誤,會(huì)顯示不匹配的位置
//
brackets.java
//
stacks used to check matching brackets
//
to run this program: C>java bracketsApp
import
java.io.
*
;
//
for I/O
////////////////////////////////////////////////////////////////
class
StackX
{
private
int
maxSize;
private
char
[] stackArray;
private
int
top;
//
--------------------------------------------------------------
public
StackX(
int
s)
//
constructor
{
maxSize
=
s;
stackArray
=
new
char
[maxSize];
top
=
-
1
;
}
//
--------------------------------------------------------------
public
void
push(
char
j)
//
put item on top of stack
{
stackArray[
++
top]
=
j;
}
//
--------------------------------------------------------------
public
char
pop()
//
take item from top of stack
{
return
stackArray[top
--
];
}
//
--------------------------------------------------------------
public
char
peek()
//
peek at top of stack
{
return
stackArray[top];
}
//
--------------------------------------------------------------
public
boolean
isEmpty()
//
true if stack is empty
{
return
(top
==
-
1
);
}
//
--------------------------------------------------------------
}
//
end class StackX
////////////////////////////////////////////////////////////////
class
BracketChecker
{
private
String input;
//
input string
//
--------------------------------------------------------------
public
BracketChecker(String in)
//
constructor
{ input
=
in; }
//
--------------------------------------------------------------
public
void
check()
{
int
stackSize
=
input.length();
//
get max stack size
StackX theStack
=
new
StackX(stackSize);
//
make stack
for
(
int
j
=
0
; j
<
input.length(); j
++
)
//
get chars in turn
{
char
ch
=
input.charAt(j);
//
get char
switch
(ch)
{
case
'
{
'
:
//
opening symbols
case
'
[
'
:
case
'
(
'
:
theStack.push(ch);
//
push them
break
;
case
'
}
'
:
//
closing symbols
case
'
]
'
:
case
'
)
'
:
if
(
!
theStack.isEmpty() )
//
if stack not empty,
{
char
chx
=
theStack.pop();
//
pop and check
if
( (ch
==
'
}
'
&&
chx
!=
'
{
'
)
||
(ch
==
'
]
'
&&
chx
!=
'
[
'
)
||
(ch
==
'
)
'
&&
chx
!=
'
(
'
) )
System.out.println(
"
Error:
"
+
ch
+
"
at
"
+
j);
}
else
//
prematurely empty
System.out.println(
"
Error:
"
+
ch
+
"
at
"
+
j);
break
;
default
:
//
no action on other characters
break
;
}
//
end switch
}
//
end for
//
at this point, all characters have been processed
if
(
!
theStack.isEmpty() )
System.out.println(
"
Error: missing right delimiter
"
);
}
//
end check()
//
--------------------------------------------------------------
}
//
end class BracketChecker
////////////////////////////////////////////////////////////////
class
BracketsApp
{
public
static
void
main(String[] args)
throws
IOException
{
String input;
while
(
true
)
{
System.out.print(
"
Enter string containing delimiters:
"
);
System.out.flush();
input
=
getString();
//
read a string from kbd
if
( input.equals(
""
) )
//
quit if [Enter]
break
;
//
make a BracketChecker
BracketChecker theChecker
=
new
BracketChecker(input);
theChecker.check();
//
check brackets
}
//
end while
}
//
end main()
//
--------------------------------------------------------------
public
static
String getString()
throws
IOException
{
InputStreamReader isr
=
new
InputStreamReader(System.in);
BufferedReader br
=
new
BufferedReader(isr);
String s
=
br.readLine();
return
s;
}
//
--------------------------------------------------------------
}
//
end class BracketsApp
////////////////////////////////////////////////////////////////
發(fā)表于 2008-04-26 11:18
dreamingnest
閱讀(424)
評(píng)論(0)
編輯
收藏
新用戶注冊(cè)
刷新評(píng)論列表
只有注冊(cè)用戶
登錄
后才能發(fā)表評(píng)論。
網(wǎng)站導(dǎo)航:
博客園
IT新聞
Chat2DB
C++博客
博問(wèn)
管理
CALENDER
<
2008年4月
>
日
一
二
三
四
五
六
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1
2
3
4
5
6
7
8
9
10
常用鏈接
我的隨筆
我的文章
我的評(píng)論
我的參與
最新評(píng)論
留言簿
(1)
給我留言
查看公開(kāi)留言
查看私人留言
隨筆分類
(13)
應(yīng)用程序(4)
(rss)
數(shù)據(jù)結(jié)構(gòu)(java)
(rss)
算法程序總結(jié)(2)
(rss)
鏈表和棧(結(jié))(7)
(rss)
隨筆檔案
(21)
2008年10月 (1)
2008年5月 (7)
2008年4月 (13)
外面的世界
懶散狂徒的專欄(天行健,君子以自強(qiáng)不息 地勢(shì)坤,君子以厚德載物)
(rss)
這里的朋友
保爾任(思想比知識(shí)更重要 成長(zhǎng)比成功更重要)
搜索
最新評(píng)論
1.?re: BFS和DFS兩種方法獲取指定目錄下的所有目錄和文件
學(xué)習(xí)了
--fejay
2.?re: 關(guān)于螞蟻問(wèn)題(Ants)
實(shí)際過(guò)程可以這么進(jìn)行抽象模擬:
序列中的元素帶有方向,進(jìn)行負(fù)值部分移動(dòng)到負(fù)值區(qū)域,正值部分移動(dòng)到正值區(qū)域時(shí)就不再發(fā)生碰撞,此時(shí)絕對(duì)值最小的值決定剩余爬行時(shí)間
--zdh
3.?re: 關(guān)于螞蟻問(wèn)題(Ants)
這個(gè)問(wèn)題看到實(shí)質(zhì)就很簡(jiǎn)單,所有的螞蟻都是相同的螞蟻,因此可以看成所有的螞蟻都可以穿過(guò)對(duì)面爬過(guò)來(lái)的螞蟻就ok啦,最長(zhǎng)時(shí)間就是兩端的螞蟻向另一端爬出去,最短的就是兩端的四個(gè)螞蟻向所在端爬出:)
--zdh
4.?re: 關(guān)于螞蟻問(wèn)題(Ants)
評(píng)論內(nèi)容較長(zhǎng),點(diǎn)擊標(biāo)題查看
--blues
5.?re: 關(guān)于螞蟻問(wèn)題(Ants)
評(píng)論內(nèi)容較長(zhǎng),點(diǎn)擊標(biāo)題查看
--dreamingnest
閱讀排行榜
1.?關(guān)于螞蟻問(wèn)題(Ants)(2252)
2.?通過(guò)排序總結(jié)java泛型數(shù)組列表(1652)
3.?堆棧解(非遞歸)決迷宮問(wèn)題(1418)
4.?ACM中使用JAVA的介紹(1049)
5.?~·掃雷小游戲·~(1039)
評(píng)論排行榜
1.?關(guān)于螞蟻問(wèn)題(Ants)(7)
2.?BFS和DFS兩種方法獲取指定目錄下的所有目錄和文件(1)
3.?一著名軟件公司的java筆試算法題的答案 (0)
4.?堆棧解(非遞歸)決迷宮問(wèn)題(0)
5.?堆排序代碼(0)
Powered By:
博客園
模板提供
:
滬江博客
主站蜘蛛池模板:
织金县
|
磐安县
|
客服
|
姚安县
|
东丽区
|
凭祥市
|
舞阳县
|
合川市
|
大英县
|
西畴县
|
资溪县
|
分宜县
|
巴南区
|
双辽市
|
突泉县
|
晋宁县
|
金昌市
|
黎城县
|
靖边县
|
盐津县
|
凤凰县
|
湘潭县
|
阳谷县
|
祁东县
|
体育
|
龙门县
|
石首市
|
岳池县
|
漳浦县
|
崇信县
|
涟源市
|
桦川县
|
黄大仙区
|
达日县
|
临漳县
|
汨罗市
|
崇左市
|
黑水县
|
若尔盖县
|
东乡
|
内丘县
|