首页 > 学院 > 开发设计 > 正文

用链表或者数组实现一个栈

2019-11-06 06:03:16
字体:
来源:转载
供稿:网友
1.用链表实现一个栈
package com.sun;import java.util.EmptyStackException;public class DLinkedStackEx {	PRivate Node head;	private Node tail;	// 表示栈的大小	private int count;	public Object push(String element) {		if (null == head) {			head = new Node(element, null, null);			tail = head;			count += 1;		} else {			Node tmp = new Node(element, null, null);			// 尾节点的下一个节点是新创建的节点			tail.next = tmp;			// 新节点的前一个节点是尾节点			tmp.prev = tail;			// 把为节点放在最后的节点上(就是新创建的节点)			tail = tmp;			count += 1;		}		return element;	}	public Object pop() {		if (isEmpty()) {			throw new EmptyStackException();		} else {			Object pop = tail.element;			// 创建一个临时节点引用指向前一个			Node tmp = tail.prev;			// 断开最后一个与前一个			tail.prev = null;			// 断开前一个与最后一个			tmp.next = null;			// 把最后一个指向前一个			tail = tmp;			count -= 1;			return pop;		}	}	public boolean isEmpty() {		if (null == head || 0 == count || null == tail) {			return true;		}		return false;	}	// 获得栈顶元素	public Object peek() {		if (isEmpty()) {			return null;		} else {			return tail.getElement();		}	}	public int size() {		return count;	}	public int search(String elment) {		if (isEmpty()) {			throw new EmptyStackException();		} else {			Node tmphead = head;			Node tmptail = tail;			for (int i = 0; i <= count / 2; i++) {				if (elment.equals(tmptail.element)) {					return i;				}				if (elment.equals(tmphead.element)) {					return count - 1 - i;				}				tmptail = tmptail.prev;				tmphead = tmphead.next;			}			return -1;		}	}	public DLinkedStackEx() {	}	private class Node {		// 要保存的数据		private Object element;		// 当前节点的下一个节点		private Node next;		public Object getElement() {			return element;		}		@SuppressWarnings("unused")		public void setElement(Object element) {			this.element = element;		}		@SuppressWarnings("unused")		public Node getNext() {			return next;		}		@SuppressWarnings("unused")		public void setNext(Node next) {			this.next = next;		}		@SuppressWarnings("unused")		public Node getPrev() {			return prev;		}		@SuppressWarnings("unused")		public void setPrev(Node prev) {			this.prev = prev;		}		// 当前节点的前一个节点		private Node prev;		public Node(String element, Node pnext, Node pprev) {			this.element = element;			this.next = pnext;			this.prev = pprev;		}	}}

2.用数组实现一个栈

package com.sun;public class Stack {/** 栈的规则:先进后出*/public class DStack {private static final int STACK_DEFAULT_SIZE = 16;private static final int STACK_EMPTY_SIZE = 0;// 用来保存元素private Object[] arrayElement;// 用来表示栈中元素的个数,一般解释就是栈的大小private int count;public DStack() {arrayElement = new Object[STACK_DEFAULT_SIZE];count = STACK_EMPTY_SIZE;}// 压栈@SuppressWarnings("unused")public Object push(String element) {// 1.栈是空的if (isEmpty()) {arrayElement[0] = element;count += 1;}// 2. 栈不空也不满if (!isFull() && !isEmpty()) {arrayElement[count] = element;count += 1;}// 3. 栈满了if (isFull()) {// 1. 分配一个临时的原来大小+16的空间Object[] tmp = new Object[arrayElement.length+ STACK_DEFAULT_SIZE];if (null == tmp) {throw new RuntimeException("has no xxx space, please change LinkedStack.");}// 2. 把原来数组中的元素拷贝到新的数组中,再把新的元素增加进来。for (int j = 0; j < arrayElement.length; j++) {tmp[j] = arrayElement[j];}tmp[arrayElement.length] = element;count += 1;// 4. 释放原来的空间for (int j = 0; j < arrayElement.length; j++) {arrayElement[j] = null;}arrayElement = null;// 5. 把临时的数组引用指向原来的数组(这步一定不能省略)arrayElement = tmp;}return element;}// 从栈中弹出元素public Object pop() {Object popObject = arrayElement[count - 1];arrayElement[count - 1] = null;count -= 1;return popObject;}// 获得栈顶的元素的拷贝public Object peek() {return arrayElement[count - 1];}// 判断栈是否为空// true 表示栈为空的。public boolean isEmpty() {return count == STACK_EMPTY_SIZE ? true : false;}// 获得元素在栈中的位置public int search(String element) {for (int i = 0; i < count; i++) {if (arrayElement[i].equals(element)) {return count - 1 - i;}}return -1;}// 判断栈是否为满// true 表示栈为满的。private boolean isFull() {return count == arrayElement.length ? true : false;}}}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表