「java实现一个栈」java实现栈的基本操作
今天给各位分享java实现一个栈的知识,其中也会对java实现栈的基本操作进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、java中栈是如何实现的?
- 2、怎样用java实现栈
- 3、用java编写程序,利用线程同步编写一个栈操作程序,包括数据的进栈和出栈。
- 4、Java如何实现堆栈
- 5、用java代码编写堆栈?
- 6、怎么用java代码实现栈内存?
java中栈是如何实现的?
这是java.util包下的Stack类,你可以看一下它是如何实现的,至于用法,无非就是push,pop,peek等操作等 /* * @(#)Stack.java 1.30 05/11/17 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package java.util; /** * The Stack class represents a last-in-first-out * (LIFO) stack of objects. It extends class Vector with five * operations that allow a vector to be treated as a stack. The usual * push and pop operations are provided, as well as a * method to peek at the top item on the stack, a method to test * for whether the stack is empty, and a method to search * the stack for an item and discover how far it is from the top. * p * When a stack is first created, it contains no items. * * pA more complete and consistent set of LIFO stack operations is * provided by the {@link Deque} interface and its implementations, which * should be used in preference to this class. For example: * {@code * Deque stack = new ArrayDeque();} * * @author Jonathan Payne * @version 1.30, 11/17/05 * @since JDK1.0 */ public class Stack extends Vector { /** * Creates an empty Stack. */ public Stack() { } /** * Pushes an item onto the top of this stack. This has exactly * the same effect as: * * addElement(item) * * @param item the item to be pushed onto this stack. * @return the item argument. * @see java.util.Vector#addElement
怎样用java实现栈
这个...怎么说了,直接用啊。Java本身就有 java.util.Stack 这个专门处理栈的类。
如果是要交作业,直接把Stack的代码贴过去吧,jdk自带的东西,全部都有源码的,呵呵。
用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 = 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 = 0;
try {
while(index == 0){
moreEelment.await();
}
value = 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;i 10; i++){
int value = 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;i 10; i++){
int value = 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如何实现堆栈
//这是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代码编写堆栈?
参看: 1 import java.util.*; 2 3 public class TestStack { 4 public static void main(String[] args) { 5 Stack stack = new Stack(); 6 7 for(int i = 0; i 10; i++) { 8 stack.push(new Integer(i)); 9 }1011 if(!stack.empty()) {12 System.out.println(stack.pop());13 }14 }15 }
怎么用java代码实现栈内存?
使用java.util包中的Stack类创建一个栈对象
public Object push(Object data);输入数据,实现压栈
public Object pop();输出数据,实现弹栈
public boolean empty()判空
public Object peek();查看栈顶元素
可以去查查API嘛
我也是学java的,大家一起进步。
java实现一个栈的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java实现栈的基本操作、java实现一个栈的信息别忘了在本站进行查找喔。
发布于:2022-12-02,除非注明,否则均为
原创文章,转载请注明出处。