java代码模拟栈 java怎么写栈
用java实现数据结构“栈
Java栈的实现
杭锦后网站制作公司哪家好,找创新互联!从网页设计、网站建设、微信开发、APP开发、响应式网站开发等网站项目制作,到程序开发,运营维护。创新互联自2013年起到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联。
public
class
MyStack
{
//定义一个堆栈类
int[]
array;
//用int数组来保存数据,根据需要可以换类型
int
s_size;
//定义堆栈的宽度
public
MyStack(int
i){
//定义一个带参数构造器
array=new
int[i];
//动态定义数组的长度
s_size=0;
//堆栈的默认宽度为0
}
public
MyStack(){
//默认构造器
this(50);
//默认构造器可容纳50个元素
}
public
void
push(int
i){
//压栈
array[this.s_size]=i;
this.s_size++;
}
public
int
pop(){
//从堆栈中取元素,从栈顶开始取
if(this.s_size!=0){
int
t=array[s_size-1];
//用中间变量保存栈顶的元素
array[s_size-1]=0;
//取完元素该位置设为0
s_size--;
//栈的大小减1
return
t;
//返回栈顶元素
}else{
System.out.println("This
stack
is
empty");
//当栈为空时显示提示信息,返回0
return
0;
}
}
public
boolean
isEmpty(){
//判断栈是否为空
return
this.s_size==0;
}
public
int
top(){
//从栈顶取值,功能和
pop()
方法一样
if(!this.isEmpty()){
int
t=array[this.s_size-1];
array[this.s_size-1]=0;
this.s_size--;
return
t;
}else{
System.out.println("This
stack
is
empty!");
return
0;
}
}
public
void
printAll(){
//打印出堆栈中的所有元素的值,不是取出,元素依然在堆栈里
if(!this.isEmpty()){
for(int
i=this.s_size
-
1;i=0;i--){
System.out.println(array[i]);
}
}
}
//下面是测试代码
public
static
void
main(String[]
args){
MyStack
stack=new
MyStack();
stack.push(4);
stack.push(5);
stack.push(6);
stack.push(7);
//System.out.println(stack.isEmpty());
stack.printAll();
System.out.println("===========");
System.out.println(stack.top());
System.out.println(stack.top());
System.out.println(stack.top());
System.out.println(stack.top());
System.out.println(stack.top());
}
}
java 中用双向链表模拟栈
这里只是实现了Stack的部分功能
public class Stack{
private Node top;
public Stack(){
this.top = null;
}
public void push(Node node){
if(node == null)
return;
if(this.top == null){
this.top = node;
node.setNext(null);
node.setPre(null);
}
else{
this.top.setNext(node);
node.setPre(this.top);
node.setNext(null);
this.top = node;
}
}
public Node pop(){
if(this.top == null)
return null;
Node curr = this.top;
Node pre = curr.getPre();
pre.setNext(null);
this.top = pre;
return curr;
}
public Node top(){
return this.top;
}
public boolean isEmpty(){
return this.top == null ? true : false;
}
public void empty(){
this.top = null;
}
public static void main(String[] args){
Stack stack = new Stack();
Node n1 = new Node(1);
Node n2 = new Node(2);
Node n3 = new Node(3);
System.out.println(stack.isEmpty());
stack.push(n1);
System.out.println(stack.top().getValue());
stack.push(n2);
stack.push(n3);
System.out.println(stack.pop().getValue());
stack.empty();
}
}
class Node {
private int value;
private Node next;
private Node pre;
public Node(int value, Node next, Node pre){
this.value = value;
this.next = next;
this.pre = pre;
}
public Node(int value){
this.value = value;
this.next = null;
this.pre = null;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public Node getPre() {
return pre;
}
public void setPre(Node pre) {
this.pre = pre;
}
}
java如何用数组来模拟栈的先进后出
import java.util.Arrays;
public class StackTest {
public static void main(String[] args) {
Stack stack = new Stack();
stack.put(1);
stack.put(2);
stack.put(3);
stack.put(4);
stack.show();
System.out.println(stack.push());
System.out.println(stack.push());
System.out.println(stack.push());
System.out.println(stack.push());
}
}
class Stack{
private int index;
private int length = 3;
private int[] stack;
public Stack() {
stack= new int[length];
index = 0;
}
public void put(int i){
if(index = length -1){
stack[index++] = i;
} else {
System.out.println("Stack 已满");
}
}
public int push(){
if (index 0){
return stack[--index];
}else {
System.out.print("Stack 已空");
return -1;
}
}
public void show() {
System.out.println(Arrays.toString(stack));
}
}
当前文章:java代码模拟栈 java怎么写栈
浏览路径:http://cdiso.cn/article/doheijj.html