隨筆:20 文章:1 評論:8 引用:0
╰⊙д⊙╯。oо○
面朝大海·春暖花開
BlogJava
首頁
發(fā)新隨筆
發(fā)新文章
聯(lián)系
聚合
管理
『第四章』棧的應(yīng)用:單詞逆序&分隔符匹配
輸入字符串,然后按照逆序輸出:
//
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á)式錯誤,會顯示不匹配的位置
//
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
閱讀(421)
評論(0)
編輯
收藏
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發(fā)表評論。
網(wǎng)站導(dǎo)航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
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
常用鏈接
我的隨筆
我的文章
我的評論
我的參與
最新評論
留言簿
(1)
給我留言
查看公開留言
查看私人留言
隨筆分類
(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)不息 地勢坤,君子以厚德載物)
(rss)
這里的朋友
保爾任(思想比知識更重要 成長比成功更重要)
搜索
最新評論
1.?re: BFS和DFS兩種方法獲取指定目錄下的所有目錄和文件
學(xué)習(xí)了
--fejay
2.?re: 關(guān)于螞蟻問題(Ants)
實(shí)際過程可以這么進(jìn)行抽象模擬:
序列中的元素帶有方向,進(jìn)行負(fù)值部分移動到負(fù)值區(qū)域,正值部分移動到正值區(qū)域時就不再發(fā)生碰撞,此時絕對值最小的值決定剩余爬行時間
--zdh
3.?re: 關(guān)于螞蟻問題(Ants)
這個問題看到實(shí)質(zhì)就很簡單,所有的螞蟻都是相同的螞蟻,因此可以看成所有的螞蟻都可以穿過對面爬過來的螞蟻就ok啦,最長時間就是兩端的螞蟻向另一端爬出去,最短的就是兩端的四個螞蟻向所在端爬出:)
--zdh
4.?re: 關(guān)于螞蟻問題(Ants)
評論內(nèi)容較長,點(diǎn)擊標(biāo)題查看
--blues
5.?re: 關(guān)于螞蟻問題(Ants)
評論內(nèi)容較長,點(diǎn)擊標(biāo)題查看
--dreamingnest
閱讀排行榜
1.?關(guān)于螞蟻問題(Ants)(2242)
2.?通過排序總結(jié)java泛型數(shù)組列表(1649)
3.?堆棧解(非遞歸)決迷宮問題(1414)
4.?ACM中使用JAVA的介紹(1048)
5.?~·掃雷小游戲·~(1035)
評論排行榜
1.?關(guān)于螞蟻問題(Ants)(7)
2.?BFS和DFS兩種方法獲取指定目錄下的所有目錄和文件(1)
3.?一著名軟件公司的java筆試算法題的答案 (0)
4.?堆棧解(非遞歸)決迷宮問題(0)
5.?堆排序代碼(0)
Powered By:
博客園
模板提供
:
滬江博客
主站蜘蛛池模板:
久久电影网午夜鲁丝片免费
|
亚洲一区二区三区高清视频
|
亚洲熟妇av一区二区三区漫画
|
日韩免费高清视频网站
|
国产精品二区三区免费播放心
|
久久精品亚洲男人的天堂
|
亚洲VA成无码人在线观看天堂
|
中文字幕亚洲一区二区三区
|
国产免费丝袜调教视频
|
成人性生交大片免费看无遮挡
|
国产在线观看免费不卡
|
亚洲国产第一站精品蜜芽
|
亚洲成a人片毛片在线
|
国产成人亚洲午夜电影
|
国产亚洲欧美在线观看
|
两个人看的www高清免费观看
|
亚洲成人免费电影
|
国产成人啪精品视频免费网
|
亚洲午夜久久久影院
|
亚洲乱码在线视频
|
无码免费又爽又高潮喷水的视频
|
毛片免费全部免费观看
|
中文字幕av无码无卡免费
|
免费又黄又硬又爽大片
|
亚洲天堂在线视频
|
日韩一卡2卡3卡4卡新区亚洲
|
亚洲视频在线观看地址
|
国产午夜亚洲精品不卡
|
久久爰www免费人成
|
57pao国产成永久免费视频
|
麻豆国产精品入口免费观看
|
国产精品国产自线拍免费软件
|
国产精品亚洲片在线
|
亚洲人成在线影院
|
噜噜噜亚洲色成人网站
|
99精品视频在线视频免费观看
|
久久国产免费福利永久
|
亚洲国产专区一区
|
亚洲一区二区影视
|
免费视频成人手机在线观看网址
|
色www永久免费视频
|