1个回答
展开全部
这是我以前写过的作业,可能有不完善的地方,可以修改有问题也可以给我发电邮dream.guo@gmail.com
我还有关于队列的源码,都是我自己写的,如果要的话可以发给你
数组实现的堆栈: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;
}
}
我还有关于队列的源码,都是我自己写的,如果要的话可以发给你
数组实现的堆栈: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;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询