没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
Java-用数组实现栈-队列-线性列表
Java-用数组实现栈
public class StackByArray {
private int length ;
private String stack[];
private int top = -1;
public StackByArray()
{
this(3);
}
/**
*
* @param n 栈的初始大小
*/
public StackByArray(int n)
{
this.length = n;
this.stack = new String[length];
}
public boolean push(String num){
if(isFull()){
return false;
}
stack[++top] = num;
return true;
}
public String pop(){
if(isEmpty()){
return null;
}
String temp = stack[top];
stack[top] = null; //清空当前值
top--;
return temp;
}
/**
* 是否为空
* @return
*/
public boolean isEmpty(){
return top == -1;
}
public boolean isFull(){
return top == (length - 1);
}
/**
* 实际栈中的数据个数
* @return
*/
public int size(){
return top + 1;
}
public static void main(String args[]){
StackByArray stack = new StackByArray();
System.out.println("当前栈的大小: " + stack.size());
stack.showMsg(stack.push("1号"));
stack.showMsg(stack.push("2号"));
stack.showMsg(stack.push("3号"));
stack.showMsg(stack.push("4号"));
System.out.println("当前栈的大小: " + stack.size());
stack.showMsg(stack.push("5hao"));
System.out.println("当前栈的大小: " + stack.size());
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
}
public void showMsg(boolean flag){
if(flag){
System.out.println("入栈成功!");
}else {
System.out.println("入栈失败!");
}
}
}
输出:
当前栈的大小: 0
入栈成功!
入栈成功!
入栈成功!
入栈失败!
当前栈的大小: 3
入栈失败!
当前栈的大小: 3
3号
2号
1号
null
Java-用数组实现队列
import java.util.Arrays;
/**
* 队列通过循环数组实现,循环通过小标求余实现
*/
public class QueneByArray
{
private int arrayLength = 10; //数组的长度 初始为10;代表队列能够存的元素
的最大个数
private int[] array = new int[arrayLength];
private int head =0 ; //队列的头,指示下一个出来的元素的下标
private int trail = 0; //队列的尾,指示下一个进去的元素的下标
private int count = 0; //用来记录循环数组中元素的个数,并在判断数组为空和满时
起作用
private int temp ; //取出队列中元素时元素的暂存地
public int getTemp()
{
return temp;
}
/**
*
* @return 返回队列元素的个数
*/
public int getCount()
{
return count;
}
/**
* 无参构造函数 队列大小为实例默认值10
*/
public QueneByArray()
{
}
/**
* 有参构造函数;参数n用来初始化队列大小
* @param n
*/
public QueneByArray(int n)
{
arrayLength = n;
array = new int[arrayLength];
}
/**
* 向队列中放入元素 成功返回true,失败返回false
*/
public boolean inputElement(int element)
{
if(!this.isFull())
{
array[trail++] = element;
count++;
trail = trail % arrayLength;
return true;
}
return false;
}
/**
* 加入元素到队列,如果队列满了,则加大队列大小
* @param element
* @return 永远返回true
*/
public boolean inputElementNoLimitedSize(int element)
{
if(!this.isFull())
{
array[trail++] = element;
count++;
trail = trail % arrayLength;
return true;
}
else
{
array = Arrays.copyOf(array, arrayLength * 2);
trail = arrayLength ;
arrayLength = arrayLength * 2;
inputElement(element);
return true;
}
}
/**
* 从队列中取出元素放到temp暂存,成功返回true,失败返回flase
*/
public boolean outputElement()
{
if(!this.isEmpty())
{
temp = array[head++];
count--;
head = head % arrayLength;
return true;
}
return false;
剩余15页未读,继续阅读
bareheadzzq
- 粉丝: 48
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页