參看: 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 }
目前創(chuàng)新互聯(lián)建站已為上千多家的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管、成都網(wǎng)站托管、企業(yè)網(wǎng)站設(shè)計、烏拉特前網(wǎng)站維護等服務(wù),公司將堅持客戶導向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
數(shù)組實現(xiàn)的堆棧: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;
}
}
使用鏈表實現(xiàn)(單鏈表) :
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;
}
}
鏈表的需要用到一個結(jié)點類 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堆棧代碼行數(shù)不一致,需要4個步驟進行修復(fù)。
1、這個錯誤明顯是字符串轉(zhuǎn)int時出現(xiàn)的,但當前行號701不會出現(xiàn)此問題。
2、觀察其前后代碼,發(fā)現(xiàn)703行開始有轉(zhuǎn)換代碼。
3、檢查代碼和業(yè)務(wù),發(fā)現(xiàn)是調(diào)用接口傳參有問題。
4、代碼修復(fù)。