java 行列,java 行列

java 队列

先进先出

1、先写个队列类

package com.zg;

class Queue //队列类

{

private int maxSize; //队列长度,由构造函数初始化

private long[] queArray; // 队列

private int front; //队头

private int rear; //队尾

private int nItems; //元素的个数

//--------------------------

public Queue(int s) // 构造函数

{

maxSize = s;

queArray = new long[maxSize];

front = 0;

rear = -1;

nItems = 0;

}

//--------------------------

public void insert(long j) // 进队列

{

if(rear == maxSize-1) // 处理循环

rear = -1;

queArray[++rear] = j; // 队尾指针加1,把值j加入队尾

nItems++;

}

//--------------------------

public long remove() // 取得队列的队头元素。

{

long temp = queArray[front++]; // 取值和修改队头指针

if(front == maxSize) // 处理循环

front = 0;

nItems--;

return temp;

}

//--------------------------

public long peekFront() // 取得队列的队头元素。该运算与 remove()不同,后者要修改队头元素指针。

{

return queArray[front];

}

//--------------------------

public boolean isEmpty() // 判队列是否为空。若为空返回一个真值,否则返回一个假值。

{

return (nItems==0);

}

//--------------------------

public boolean isFull() // 判队列是否已满。若已满返回一个真值,否则返回一个假值。

{

return (nItems==maxSize);

}

//--------------------------

public int size() // 返回队列的长度

{

return nItems;

}

//--------------------------

}

2、调用一下这个队列

package com.zg;

public class IntegerQueue {

public static void main(String[] args)

{

Queue theQueue = new Queue(5); // 队列有5个元素

theQueue.insert(10); // 添加4个元素

theQueue.insert(20);

theQueue.insert(30);

theQueue.insert(40);

theQueue.remove(); // 移除3个元素

theQueue.remove(); // (10, 20, 30)

theQueue.remove();

theQueue.insert(50); // 添加4个元素

theQueue.insert(60);

theQueue.insert(70);

theQueue.insert(80);

while( !theQueue.isEmpty() ) // 遍历队列并移除所有元素

{

long n = theQueue.remove(); // (40, 50, 60, 70, 80)

System.out.print(n);

System.out.print(" ");

}

System.out.println("");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值