189 8069 5689

java栈堆代码 java栈与堆

用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编程实现堆栈出栈操作!

数组实现的堆栈:ArrayStack.java

public class ArrayStack {

Object[] m_elements;

int m_size;

public ArrayStack(int len) {

m_elements = new Object[len];

m_size = 0;

}

public ArrayStack() {

this(50);

}

// insert onto stack

public void push(Object element) {

m_elements[m_size] = element;

m_size++;

}

// return and remove the top element

public Object pop() {

if (!this.isEmpty()) {

Object obj = m_elements[m_size - 1];

m_elements[m_size - 1] = null;

m_size--;

return obj;

} else {

return null;

}

}

// return the top element

public Object top() {

if (!this.isEmpty()) {

return m_elements[m_size - 1];

} else {

return null;

}

}

// return 1 -- is empty

// return 0 -- is not empty

public boolean isEmpty() {

return this.size() == 0;

}

public int size() {

return m_size;

}

}

使用链表实现(单链表) :

public class Stacklist {

Node m_header;

int m_size;

public ListStack() {

m_header = null;

m_size = 0;

}

public void push(Object value) {

m_header = new Node(value, m_header);

}

public Object pop() {

if (!this.isEmpty()) {

throw new RuntimeException("Stack underflow");

}

Object obj = m_header.element;

m_header = m_header.next;

return obj;

}

// return reference to most recently added elemenet

public Object peek() {

if (!this.isEmpty()) {

throw new RuntimeException("Stack underflow");

}

return m_header.element;

}

public boolean isEmpty() {

return this.size() == 0;

}

//return the number of the queue's elements;

public int size() {

return m_size;

}

}

链表的需要用到一个结点类 Node.java 代码如下

public class Node {

Object element;

Node next;

public Node(Object theElement) {

this(theElement, null);

}

public Node(Object theElement, Node n) {

element = theElement;

next = n;

}

public Object getElement() {

return element;

}

public void setElement(Object element) {

this.element = element;

}

public Node getNext() {

return next;

}

public void setNext(Node next) {

this.next = next;

}

}

java堆栈代码行数不一致

java堆栈代码行数不一致,需要4个步骤进行修复。

1、这个错误明显是字符串转int时出现的,但当前行号701不会出现此问题。

2、观察其前后代码,发现703行开始有转换代码。

3、检查代码和业务,发现是调用接口传参有问题。

4、代码修复。


网站标题:java栈堆代码 java栈与堆
地址分享:http://cdxtjz.com/article/hhhded.html

其他资讯