算法学习之栈的实现
来源:互联网 发布:win7 64位优化版下载 编辑:程序博客网 时间:2024/06/07 02:54
Stack<template> 称为类的泛型。也就是说指定了stack类中的数据为统一数据。比如将所有整数,或者字符类数据压入栈。如果出现将一个字符类的数据压入一个数据类的stack,系统则会报错。;比如下面的程序。Ops是一个string 类型的stack,vals是一个整数类型的stack。但是在ops中压入整数,在vals压入字符串,则会报错
public class study{
public static void main(String[] args) {
// TODO Auto-generated method stub
// Circle c = new Circle(47);
Stack<String> ops=new Stack<String>();
Stack<Integer> vals=new Stack<Integer>();
ops.push(1);
vals.push("test1");
}
}
提示如下错误:
Themethod push(String) in the type Stack<String> is not applicable for thearguments (int)
The method push(Integer) in the typeStack<Integer> is not applicable for the arguments (String)
改成如下形式则正确
public class study{
public static void main(String[] args) {
// TODO Auto-generated method stub
// Circle c = new Circle(47);
Stack<String> ops=new Stack<String>();
Stack<Integer> vals=new Stack<Integer>();
ops.push("test");
ops.push("test1");
vals.push(2);
vals.push(1);
for(Stringt:ops){
System.out.println("The string is:" +t);
}
}
}
也可以用类创建一个固定类长度的stack。如下自定义一个类,并用数组来模拟stack
classFixedCapacityStackofStrings{
private Stringa[];
private int n;
public FixedCapacityStackofStrings(intcap){
a=new String[cap];
}
public boolean isEmpty(){
returnn==0;
}
public int size(){
returnn;
}
public void push(String item){
a[n++]=item;
}
public String pop(){
returna[--n];
}
}
public class study{
public static void main(String[] args) {
// TODO Auto-generated method stub
// Circle c = new Circle(47);
FixedCapacityStackofStrings s=new FixedCapacityStackofStrings(100);
Scanner input=new Scanner(System.in);
while (true){
String item=input.next();
if (item.equals("quit")){
break;
}
if (!item.equals("-")){
s.push(item);
}
else if(!s.isEmpty()){
System.out.println(s.pop());
}
}
System.out.println(s.size());
System.out.println(s.pop());
}
}
FixedCapacityStackofStrings这个类只能处理String的数据,如果要处理double或者int的数据,则必须另外写一个类。这样的话就显得很冗余。可以创建泛型类来解决这个问题。
classFixedCapacityStack<Item>{
private Itema[];
private int n;
public FixedCapacityStack(intcap){
a=(Item[])new Object[cap];
}
public boolean isEmpty(){
returnn==0;
}
public int size(){
returnn;
}
public void push(Item item){
a[n++]=item;
}
public Item pop(){
returna[--n];
}
}
public class study{
public static void main(String[] args) {
// TODO Auto-generated method stub
// Circle c = new Circle(47);
FixedCapacityStack s=newFixedCapacityStack<Integer>(100);
Scanner input=new Scanner(System.in);
while (true){
String item=input.next();
if (item.equals("quit")){
break;
}
if (!item.equals("-")){
s.push(item);
}
else if(!s.isEmpty()){
System.out.println(s.pop());
}
}
System.out.println(s.size());
System.out.println(s.pop());
}
}
- 算法学习之栈的实现
- 【机器学习算法】之KNN算法的实现
- 【机器学习算法】之决策树算法的实现
- 算法学习之哈希表实现
- Cordic算法的FPGA实现之原理学习随记
- 【机器学习算法】之朴素贝叶斯的实现
- 算法学习之路(2):stack的数组实现
- 算法学习之快速排序的C语言实现
- 机器学习之logistic回归算法的java实现
- 《算法导论》学习笔记之Chapter10---队列的数组实现
- 算法(第四版)学习笔记之java实现能够动态调整数组大小的栈
- 栈和队列的数组实现—《算法导论》学习笔记之六
- 算法学习之旅,初级篇(21)--栈的实现
- 《算法导论》学习笔记之Chapter10基本数据结构---栈的数组实现
- 算法之栈的实现2
- Mahout聚类算法学习之Canopy算法的分析与实现
- 算法学习之排序算法的C++实现——冒泡排序
- BP学习算法的实现
- MyBatis的联合查询
- 【LeetCode】24. Swap Nodes in Pairs
- addroid实现广告倒计时跳Activity之CountDownTimer
- C#在窗体上画椭圆和三角形
- 循环的边界溢出
- 算法学习之栈的实现
- 图解HTTP之HTTPS详解
- Android Animation 动画(一)
- Android native应用开发简明教程 (1)
- NPoco源码怎么看
- 1.自白
- hibernate两级缓存
- 操作系统特性与微内核
- lazy ideas in programming