隨筆:20 文章:1 評論:8 引用:0
╰⊙д⊙╯。oо○
面朝大?!ご号ㄩ_
BlogJava
首頁
發新隨筆
發新文章
聯系
聚合
管理
『第四章』隊列的基本使用
//
Queue.java
//
demonstrates queue
//
to run this program: C>java QueueApp
////////////////////////////////////////////////////////////////
class
Queue
{
private
int
maxSize;
private
long
[] queArray;
private
int
front;
private
int
rear;
private
int
nItems;
//
--------------------------------------------------------------
public
Queue(
int
s)
//
constructor
{
maxSize
=
s;
queArray
=
new
long
[maxSize];
front
=
0
;
rear
=
-
1
;
nItems
=
0
;
}
//
--------------------------------------------------------------
public
void
insert(
long
j)
//
put item at rear of queue
{
if
(rear
==
maxSize
-
1
)
//
deal with wraparound
rear
=
-
1
;
queArray[
++
rear]
=
j;
//
increment rear and insert
nItems
++
;
//
one more item
}
//
--------------------------------------------------------------
public
long
remove()
//
take item from front of queue
{
long
temp
=
queArray[front
++
];
//
get value and incr front
if
(front
==
maxSize)
//
deal with wraparound
front
=
0
;
nItems
--
;
//
one less item
return
temp;
}
//
--------------------------------------------------------------
public
long
peekFront()
//
peek at front of queue
{
return
queArray[front];
}
//
--------------------------------------------------------------
public
boolean
isEmpty()
//
true if queue is empty
{
return
(nItems
==
0
);
}
//
--------------------------------------------------------------
public
boolean
isFull()
//
true if queue is full
{
return
(nItems
==
maxSize);
}
//
--------------------------------------------------------------
public
int
size()
//
number of items in queue
{
return
nItems;
}
//
--------------------------------------------------------------
}
//
end class Queue
////////////////////////////////////////////////////////////////
class
QueueApp
{
public
static
void
main(String[] args)
{
Queue theQueue
=
new
Queue(
5
);
//
queue holds 5 items
theQueue.insert(
10
);
//
insert 4 items
theQueue.insert(
20
);
theQueue.insert(
30
);
theQueue.insert(
40
);
theQueue.remove();
//
remove 3 items
theQueue.remove();
//
(10, 20, 30)
theQueue.remove();
theQueue.insert(
50
);
//
insert 4 more items
theQueue.insert(
60
);
//
(wraps around)
theQueue.insert(
70
);
theQueue.insert(
80
);
while
(
!
theQueue.isEmpty() )
//
remove and display
{
//
all items
long
n
=
theQueue.remove();
//
(40, 50, 60, 70, 80)
System.out.print(n);
System.out.print(
"
"
);
}
System.out.println(
""
);
}
//
end main()
}
//
end class QueueApp
////////////////////////////////////////////////////////////////
發表于 2008-04-26 11:20
dreamingnest
閱讀(196)
評論(0)
編輯
收藏
所屬分類:
鏈表和棧(結)
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
網站導航:
博客園
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)
應用程序(4)
(rss)
數據結構(java)
(rss)
算法程序總結(2)
(rss)
鏈表和棧(結)(7)
(rss)
隨筆檔案
(21)
2008年10月 (1)
2008年5月 (7)
2008年4月 (13)
外面的世界
懶散狂徒的專欄(天行健,君子以自強不息 地勢坤,君子以厚德載物)
(rss)
這里的朋友
保爾任(思想比知識更重要 成長比成功更重要)
搜索
最新評論
1.?re: BFS和DFS兩種方法獲取指定目錄下的所有目錄和文件
學習了
--fejay
2.?re: 關于螞蟻問題(Ants)
實際過程可以這么進行抽象模擬:
序列中的元素帶有方向,進行負值部分移動到負值區域,正值部分移動到正值區域時就不再發生碰撞,此時絕對值最小的值決定剩余爬行時間
--zdh
3.?re: 關于螞蟻問題(Ants)
這個問題看到實質就很簡單,所有的螞蟻都是相同的螞蟻,因此可以看成所有的螞蟻都可以穿過對面爬過來的螞蟻就ok啦,最長時間就是兩端的螞蟻向另一端爬出去,最短的就是兩端的四個螞蟻向所在端爬出:)
--zdh
4.?re: 關于螞蟻問題(Ants)
評論內容較長,點擊標題查看
--blues
5.?re: 關于螞蟻問題(Ants)
評論內容較長,點擊標題查看
--dreamingnest
閱讀排行榜
1.?關于螞蟻問題(Ants)(2250)
2.?通過排序總結java泛型數組列表(1651)
3.?堆棧解(非遞歸)決迷宮問題(1417)
4.?ACM中使用JAVA的介紹(1048)
5.?~·掃雷小游戲·~(1038)
評論排行榜
1.?關于螞蟻問題(Ants)(7)
2.?BFS和DFS兩種方法獲取指定目錄下的所有目錄和文件(1)
3.?一著名軟件公司的java筆試算法題的答案 (0)
4.?堆棧解(非遞歸)決迷宮問題(0)
5.?堆排序代碼(0)
Powered By:
博客園
模板提供
:
滬江博客
主站蜘蛛池模板:
龙州县
|
山东省
|
沁水县
|
沅陵县
|
安图县
|
康定县
|
东乌珠穆沁旗
|
上栗县
|
浠水县
|
远安县
|
增城市
|
永平县
|
杭州市
|
馆陶县
|
绥滨县
|
阿合奇县
|
旅游
|
土默特左旗
|
依安县
|
竹北市
|
蕲春县
|
孙吴县
|
大渡口区
|
禹城市
|
江华
|
和平县
|
浪卡子县
|
苏尼特右旗
|
九龙县
|
英吉沙县
|
梁山县
|
罗甸县
|
东城区
|
卢湾区
|
繁昌县
|
安义县
|
乐陵市
|
饶河县
|
右玉县
|
东城区
|
曲靖市
|