求助!如何用Java编写一个栈可以利用LinkedList 来写一个栈. 例如
import java.util.* ;
public class testStack{
private LinkedList list = new LinkedList() ;
public void push(Object o ){
try{
list.addFirst( o ) ;
}catch(Exception e){
}
}
public Object pop(){
return list.removeFirst() ;
}
public Object top() {
return list.getLast() ;
}
public static void main(String[] args){
//code here
testStack s = new testStack() ;
for( int i = 0 ; i10 ;i){
s.push( i );
}
System.out.println ( s.pop());
System.out.println(s.top());
}
}
你可以好好看看Thinking in JAVA中对集合的讲解 。。
Java如何实现堆栈//这是JDK提供的栈
import java.util.Stack;
public class UsingStack {
public static void main(String[] args) {
//构造栈对象 , 使用类型限制,只能存储Integer数据
StackInteger s = new StackInteger();
//1、2、3依次入栈
s.push(1);
s.push(2);
s.push(3);
//3、2、1依次出栈
System.out.println(s.pop());
System.out.println(s.pop());
System.out.println(s.pop());
}
}
//这是我写的顺序结构的栈
import java.util.EmptyStackException;
import java.util.Vector;
public class UsingStack{
public static void main(String[] args){
//构造栈对象,使用类型限制,只能存储Integer数据
MyStackInteger s = new MyStackInteger();
//1、2、3依次入栈
s.push(1);
s.push(2);
s.push(3);
//3、2、1依次出栈
System.out.println(s.pop());
System.out.println(s.pop());
System.out.println(s.pop());
}
}
/**
* 栈类
* @author developer_05
* @param T
*/
class MyStackT extends VectorT{
/**
* 构造方法
*/
public MyStack(){
}
/**
* 入栈方法
* @param item 待入栈的元素
* @return 返回入栈的元素
*/
public T push(T item) {
addElement(item);
return item;
}
/**
* 出栈方法(同步处理)
* @return 返回出栈元素
*/
public synchronized T pop() {
T obj;
int len = size();
if (len == 0)
throw new EmptyStackException();
obj = elementAt(len - 1);
removeElementAt(len - 1);
return obj;
}
/**
* 判断栈是否为空的方法
* @return 返回true(栈空)或false(栈非空)
*/
public boolean empty() {
return size() == 0;
}
private static final long serialVersionUID = 1L;
}
用java编写出来:用数组实现一个栈public class Stack {
private Object[] stack;
//这个不需要;
//private int top = 0; //初始化栈顶
//这个也不需要;
//写一个栈出来,最好是可以动态的,可以自己改变大小的,即数组的长度;
//private int size = 0; // 初始化大小
//元素个数;
private int size;
//默认长度为10;
public Stack(){
this(10);
}
//也可以自己设置长度,即容量;
public Stack(int len){
stack = new Object[len];
}
//返回元素个数;
public int size(){
return size;
}
//返回数组长度,即容量;
public int capacity(){
return stack.length;
}
//实现动态的数组;
public void ensureCapacity(){
if(size() == capacity()){
Object[] newStack = new Object[size() * 3 / 21];
System.arraycopy(stack, 0, newStack, 0, size());
stack = newStack;
}
}
//入栈;
public void push(Object o){
size;
ensureCapacity();
stack[size - 1] = o;
}
/*
public void push(Object object) {
if (isFull()) {
System.out.println("栈满!入栈失败");
}
stack[top] = object;
}
*/
//判空;
public boolean isEmpty(){
return size == 0;
}
//出栈;
public Object pop(){
//首先要判空;
if(isEmpty()){
throw new ArrayIndexOutOfBoundsException("不能为空");
}
Object o = stack[--size];
stack[size] = null;
return o;
}
/*
// 出栈
public Object pop() {
Object object = stack[--top];
stack[top] = null;
return object;
}
*/
/*
// 计算栈当前大小
public int size() {
return top;
}
// 判断是否是空栈
public boolean isEmpey() {
return top == 0;
}
// 判断是否栈满
public boolean isFull() {
return top = size;
}
public Stack(int size) {
this.size = size;
}
*/
public static void main(String[] args) {
Stack stack = new Stack(3);
String[] data = https://www.04ip.com/post/new String[] {"a", "b", "c" };
for (int i = 0; idata.length; i) {
stack.push(data[i]);
System.out.println(data[i]"");
}
System.out.println("***********");
while (!stack.isEmpty()) {
System.out.println(stack.pop()"");
}
//}
}
}
你自己对比一下,我是在你的里面修改的
用java编写程序,利用线程同步编写一个栈操作程序,包括数据的进栈和出栈 。Stack.java
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Stack {
private int[] data;
private int index;
private Lock lock;
private Condition moreSpace;
private Condition moreEelment;
public Stack(int size){
this.data = https://www.04ip.com/post/new int[size];
this.index = 0;
this.lock = new ReentrantLock();
this.moreSpace = lock.newCondition();
this.moreEelment = lock.newCondition();
}
public void push(int value){
lock.lock();
try {
while(index == data.length){
moreSpace.await();
}
data[index] = value;
moreEelment.signalAll();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public int popup(){
lock.lock();
int value = https://www.04ip.com/post/0;
try {
while(index == 0){
moreEelment.await();
}
value = https://www.04ip.com/post/data[--index];
moreSpace.signalAll();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
return value;
}
}
写入线程 WriteStack.java
import java.util.Random;
public class WriteStack implements Runnable {
private Stack stack;
public WriteStack(Stack stack){
this.stack = stack;
}
@Override
public void run() {
Random r = new Random(System.currentTimeMillis());
for(int i = 0;i10; i){
int value = https://www.04ip.com/post/r.nextInt(500);
stack.push(value);
System.out.printf("Write: push %d in stack\n",value);
try {
Thread.sleep(r.nextInt(500));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
读取线程 ReadStack.java
import java.util.Random;
public class ReadStack implements Runnable {
private Stack stack;
public ReadStack(Stack stack){
this.stack = stack;
}
@Override
public void run() {
Random r = new Random(System.currentTimeMillis());
for(int i = 0;i10; i){
int value = https://www.04ip.com/post/stack.popup();
System.out.printf("Read: popup an element %d\n",value);
try {
Thread.sleep(r.nextInt(500));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
主测试线程 StackExample.java
public class StackExample {
public static void main(String[] args) throws InterruptedException {
Stack stack = new Stack(5);
WriteStack writeStack = new WriteStack(stack);
ReadStack readStack = new ReadStack(stack);
Thread writeThread = new Thread(writeStack);
Thread readThread = new Thread(readStack);
writeThread.start();
readThread.start();
}
}
【java实现栈代码 用java实现栈结构】java实现栈代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于用java实现栈结构、java实现栈代码的信息别忘了在本站进行查找喔 。
推荐阅读
- asp.netmvc网站开发,aspnet mvc开发框架
- mysql创建分区出错,mysql创建分区表语法
- gis标注表达式保存,arcgis标注表达式vb语句
- 玩网络游戏,玩网络游戏对青少年的危害
- mysql怎么区分英文 mysql英文版如何变成中文版
- 包含怎么辨别移动硬盘内存真假的词条
- js简易时钟的制作方法,js简易时钟的制作方法
- 台湾益智类游戏三国演义,台湾益智类游戏三国演义有哪些
- php读取数据库blob php读取数据库内容显示