隨筆:20 文章:1 評(píng)論:8 引用:0
╰⊙д⊙╯。oо○
面朝大海·春暖花開
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
閱讀(427)
評(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)
給我留言
查看公開留言
查看私人留言
隨筆分類
(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)(2255)
2.?通過(guò)排序總結(jié)java泛型數(shù)組列表(1654)
3.?堆棧解(非遞歸)決迷宮問(wèn)題(1420)
4.?ACM中使用JAVA的介紹(1050)
5.?~·掃雷小游戲·~(1041)
評(píng)論排行榜
1.?關(guān)于螞蟻問(wèn)題(Ants)(7)
2.?BFS和DFS兩種方法獲取指定目錄下的所有目錄和文件(1)
3.?一著名軟件公司的java筆試算法題的答案 (0)
4.?堆棧解(非遞歸)決迷宮問(wèn)題(0)
5.?堆排序代碼(0)
Powered By:
博客園
模板提供
:
滬江博客
主站蜘蛛池模板:
一区二区三区免费精品视频
|
色天使亚洲综合在线观看
|
热久久精品免费视频
|
老汉色老汉首页a亚洲
|
久久er国产精品免费观看2
|
亚洲熟女一区二区三区
|
又硬又粗又长又爽免费看
|
亚洲国产成人久久综合野外
|
国产成人亚洲精品播放器下载
|
国产成人免费ā片在线观看
|
亚洲av无码专区青青草原
|
永久黄网站色视频免费直播
|
妇女自拍偷自拍亚洲精品
|
免费一级毛片免费播放
|
jizz在线免费观看
|
久久精品国产亚洲麻豆
|
永久在线免费观看
|
学生妹亚洲一区二区
|
国产亚洲精品免费
|
国产精品永久免费视频
|
亚洲乱色熟女一区二区三区丝袜
|
在线免费观看韩国a视频
|
亚洲aⅴ无码专区在线观看
|
亚洲性在线看高清h片
|
免费无码一区二区
|
国产精品亚洲综合一区
|
国产精品免费大片
|
亚洲人成电影网站国产精品
|
caoporn成人免费公开
|
久久亚洲一区二区
|
成年女人视频网站免费m
|
亚洲av成人中文无码专区
|
国产精品亚洲视频
|
95老司机免费福利
|
亚洲av午夜国产精品无码中文字
|
亚洲一级片内射网站在线观看
|
国内永久免费crm系统z在线
|
免费一级一片一毛片
|
a毛片在线看片免费
|
77777午夜亚洲
|
亚洲黄片手机免费观看
|