2007-09-30
Java数据结构和算法--栈与队列
(1)栈
(2)队列
(3)优先队列
package ChapterOne;
public class Stack {
//栈数组
long stackArr[];
//栈的大小
int maxSize;
//栈的顶部
int top;
//初始化一个大小为size的栈
public Stack(int size){
maxSize = size;
stackArr = new long[size];
top = -1;
}
//出栈操作
public long pop(){
return stackArr[top--];
}
//进栈操作
public void push(long value){
stackArr[++top] = value;
}
//判断栈是否为空
public boolean isEmpty(){
return top == -1;
}
//判断栈是否已满
public boolean isFull(){
return top == maxSize-1;
}
//取栈顶元素
public long peek(){
return stackArr[top];
}
public static void main(String[] args) {
Stack stack = new Stack(10);
while(!stack.isFull()){
long v = (long) (Math.random()*100);
stack.push(v);
System.out.print(v+" ");
}
System.out.println();
while(!stack.isEmpty()){
long topValue = stack.pop();
System.out.print(topValue+" ");
}
System.out.println();
}
}
(2)队列
package ChapterOne;
public class Queue {
//队列数组
private long queueArr[];
//队列的前端下标
private int front;
//队列的尾端下标
private int rear;
//队列的大小
private int maxSize;
//队列中元素的个数
private int nItems;
//初始化一个大小为size的队列
public Queue(int size){
queueArr = new long[size];
maxSize = size;
front = 0;
rear = -1;
nItems = 0;
}
//插入操作
public void insert(long value){
//队列已满
if(rear == maxSize-1)
rear = -1;
queueArr[++rear] = value;
nItems++;
}
//删除操作
public long remove(){
long temp = queueArr[front++];
if(front == maxSize)
front = 0;
nItems--;
return temp;
}
//返回队列第一个元素
public long peakFront(){
return queueArr[front];
}
//判断是否为空
public boolean isEmpty(){
return nItems == 0;
}
//判断是否已满
public boolean isFull(){
return nItems == maxSize;
}
//返回队列中元素的个数
public int size(){
return nItems;
}
public void print(){
for(int i = front;i < front+nItems;i++){
System.out.print(queueArr[i]+" ");
}
System.out.println();
}
public static void main(String[] args) {
Queue q = new Queue(10);
while(!q.isFull()){
long value = (long)(Math.random()*100);
q.insert(value);
}
q.print();
while(!q.isEmpty()){
q.remove();
q.print();
}
q.print();
System.out.println(q.isEmpty());
}
}
(3)优先队列
package ChapterOne;
public class PriorityQueue {
private int nItems;
private long pqArr[];
private int maxSize;
public PriorityQueue(int size){
maxSize = size;
pqArr = new long[size];
nItems = 0;
}
public void insert(long value){
int i;
if(nItems == 0)
pqArr[nItems++] = value;
else{
for(i = nItems-1;i >= 0;i--){
if(value < pqArr[i]){
pqArr[i+1] = pqArr[i];
}
else
break;
}
pqArr[i+1] = value;
nItems++;
}
}
public long remove(){
return pqArr[--nItems];
}
public boolean isEmpty(){
return nItems == 0;
}
public boolean isFull(){
return nItems == maxSize;
}
public void print(){
for(int i = 0;i < nItems;i++)
System.out.print(pqArr[i]+" ");
System.out.println();
}
public static void main(String[] args) {
PriorityQueue pq = new PriorityQueue(10);
while(!pq.isFull()){
long value = (long)(Math.random()*100);
pq.insert(value);
}
pq.print();
}
}
- 22:01
- 浏览 (531)
- 评论 (0)
- 分类: Java数据结构和算法
- 进入论坛
- 相关推荐
发表评论
提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则
- 浏览: 10115 次
- 性别:

- 来自: 武汉

- 详细资料
搜索本博客
我的相册
Image00000
共 3 张
共 3 张
最近加入圈子
最新评论
-
学习EXT过程中做的一个例 ...
跟EXT的例子有区别吗!
-- by e-ant -
学习EXT过程中做的一个例 ...
看见一个tab面板
-- by peter_wu -
学习EXT过程中做的一个例 ...
楼主你说的不是很清楚啊,什么做的什么例子啊
-- by wzh1008 -
学习EXT过程中做的一个例 ...
有的,上面不是说的很清楚了吗
-- by mawt -
Java数据结构和算法--排序
有 什么 比较好的 数据结构 入门的书籍吗 国外的
-- by knight2010






评论排行榜