MyStack<T>

  根据ArrayList和LinkedList,仿写了MyStack。

  人生苦短,未言自明。

1. MyArrayStack

package myStack;
/**
 * 2021/7/19.
 * 
 * @author CatInCoffee.
 */
public class MyArrayStack<T> {

	private static final int DEFAULT_CAPACITY = 10;

	private int indexOfTop;
	private T[] theItems;

	// *****************************************************************************************

	public MyArrayStack() {
		clear();
	} // Of the first constructor

	public void clear() {
		indexOfTop = -1;
		ensureCapacity(DEFAULT_CAPACITY);
	} // of clear()

	// O(N)
	private void ensureCapacity(int newCapacity) {
		if (newCapacity < size()) {
			return;
		} // if
		T[] old = theItems;
		theItems = (T[]) new Object[newCapacity];
		for (int i = 0; i < size(); i++) {
			theItems[i] = old[i];
		} // of for i
	} // of ensureCapacity(int)

	// O(N)
	public MyArrayStack(T[] paraArray) {
		indexOfTop = paraArray.length - 1;
		theItems = (T[]) (new Object[indexOfTop * 2 + 1]);
		ensureCapacity(DEFAULT_CAPACITY);

		for (int i = 0; i < paraArray.length; i++) {
			theItems[i] = paraArray[i];
		} // Of for i
	} // Of the second constructor

	// *****************************************************************************************

	public int size() {
		return indexOfTop + 1;
	} // of size()

	public boolean isEmpty() {
		return size() == 0;
	} // of isEmpty()

	// *****************************************************************************************

	// O(1)
	public T pop() {
		if (isEmpty()) {
			return null;
		} else {
			return theItems[indexOfTop--];
		} // of if-else
	}// of pop()

	// O(1)
	public boolean push(T x) {
		if (theItems.length == size()) {
			ensureCapacity(size() * 2 + 1);
		} // of if

		theItems[++indexOfTop] = x;
		return true;
	}// of push(T)

	// O(1)
	public T top() {
		return theItems[indexOfTop];
	}// of top

	// *****************************************************************************************

	public String toString() {
		String s = "It's an empty stack.";

		if (!isEmpty()) {
			s = "{ ";
			for (int i = 0; i < indexOfTop; i++) {
				s += theItems[i] + ", ";
			} // of for i
			s += theItems[indexOfTop] + " }";
		} // of if

		return s;
	}// of toString()

	public static void main(String[] args) {
		Character[] temp = { Character.valueOf('A'), Character.valueOf('%'), Character.valueOf('&') };
		MyArrayStack<Character> tempStack = new MyArrayStack<>(temp);
		System.out.println("The current stack is: " + tempStack);
		tempStack.clear();
		System.out.println("Cleared, the current stack is: " + tempStack);

		for (char ch = 'a'; ch < 'l'; ch++) {
			tempStack.push(Character.valueOf(ch));
			System.out.println("The current stack is: " + tempStack);
		} // Of for ch
		System.out.println();

		Character tempChar;
		for (int i = 12; i > 5; i--) {
			tempChar = tempStack.pop();
			System.out.print("Poped: " + (tempChar != null ? tempChar.charValue() : "nothing, "));
			System.out.println(" The current stack is: " + (tempChar != null ? tempStack : "NULL and cannot pop."));
		} // Of for i

		for (int i = 5; i > 0; i--) {
			tempChar = tempStack.pop();
			System.out.print("Poped: " + (tempChar != null ? tempChar.charValue() : "nothing, "));
			System.out.println(" The current stack is: "
					+ (!tempStack.isEmpty() ? (tempStack + ", and the top element is: " + tempStack.top().charValue())
							: "NULL, and cannot pop"));
		} // Of for i

	} // of main

}// of class MyArrayStack

  结果:

The current stack is: { A, %, & }
Cleared, the current stack is: It's an empty stack.
The current stack is: { a }
The current stack is: { a, b }
The current stack is: { a, b, c }
The current stack is: { a, b, c, d }
The current stack is: { a, b, c, d, e }
The current stack is: { a, b, c, d, e, f }
The current stack is: { a, b, c, d, e, f, g }
The current stack is: { a, b, c, d, e, f, g, h }
The current stack is: { a, b, c, d, e, f, g, h, i }
The current stack is: { a, b, c, d, e, f, g, h, i, j }
The current stack is: { a, b, c, d, e, f, g, h, i, j, k }

Poped: k The current stack is: { a, b, c, d, e, f, g, h, i, j }
Poped: j The current stack is: { a, b, c, d, e, f, g, h, i }
Poped: i The current stack is: { a, b, c, d, e, f, g, h }
Poped: h The current stack is: { a, b, c, d, e, f, g }
Poped: g The current stack is: { a, b, c, d, e, f }
Poped: f The current stack is: { a, b, c, d, e }
Poped: e The current stack is: { a, b, c, d }
Poped: d The current stack is: { a, b, c }, and the top element is: c
Poped: c The current stack is: { a, b }, and the top element is: b
Poped: b The current stack is: { a }, and the top element is: a
Poped: a The current stack is: NULL, and cannot pop
Poped: nothing,  The current stack is: NULL, and cannot pop

2. MyLinkedStack

package myStack;

/**
 * @author CatInCoffee. 2021/7/19.
 */
public class MyLinkedStack<T> {

	private static class Node<T> {
		public T data;
		public Node<T> lower;

		public Node(T paraData) {
			data = paraData;
			lower = null;
		} // of Node's first constructor

		public Node(T paraData, Node<T> paraLower) {
			data = paraData;
			lower = paraLower;
		} // of Node's second constructor
	} // of class Node<T>

	private Node<T> topNode;
	private Node<T> bottomMarker;
	private int theSize;

	// *****************************************************************************************

	public MyLinkedStack() {
		topNode = new Node<>(null);
		bottomMarker = new Node<>(null);
		clear();
	}// Of the first constructor

	public void clear() {
		topNode = bottomMarker;
		theSize = 0;
	}// of clear()

	// O(N)
	public MyLinkedStack(T[] paraArray) {
		this();
		theSize = paraArray.length;

		for (int i = 0; i < paraArray.length; i++) {
			push(paraArray[i]);
		} // Of for i

	}// Of the second constructor

	// *****************************************************************************************

	public int size() {
		return theSize;
	}// of size()

	public boolean isEmpty() {
		return size() == 0;
	}// of isEmpty()

	// *****************************************************************************************

	// O(1)
	public T pop() {
		if (isEmpty()) {
			return null;
		} else {
			T old = topNode.data;
			topNode = topNode.lower;
			theSize--;
			return old;
		} // of if-else
	}// of pop()

	// O(1)
	public boolean push(T x) {
		Node<T> newNode = new Node<>(x, topNode);
		topNode = newNode;
		theSize++;
		return true;
	}// of push(T)

	// O(1)
	public T top() {
		return topNode.data;
	}// of top()

	// *****************************************************************************************

	public String toString() {
		String s = "It's an empty stack.";

		if (!isEmpty()) {
			s = "{ ";

//			Node<T> temp = topNode;
//			while(temp.lower != bottomMarker) {
//				s += temp.data + ", ";
//				temp = temp.lower;
//			}// of while
//			s += temp.data + " }";

			for (Node<T> temp = topNode; temp != bottomMarker; temp = temp.lower) {
				s += temp.data + " ";
			} // of for temp
			s += "}";

		} // of if

		return s;
	}// of toString()

	public static void main(String[] args) {
//		Character[] temp;
//		MyLinkedStack<Character> tempStack = new MyLinkedStack<>();

		Character[] temp = { Character.valueOf('A'), Character.valueOf('%'), Character.valueOf('&') };
		MyLinkedStack<Character> tempStack = new MyLinkedStack<>(temp);
		System.out.println("The current stack is: " + tempStack);
		tempStack.clear();
		System.out.println("Cleared, the current stack is: " + tempStack);

		for (char ch = 'a'; ch < 'l'; ch++) {
			tempStack.push(Character.valueOf(ch));
			System.out.println("The current stack is: " + tempStack);
		} // Of for ch
		System.out.println();

		Character tempChar;
		for (int i = 12; i > 5; i--) {
			tempChar = tempStack.pop();
			System.out.print("Poped: " + (tempChar != null ? tempChar.charValue() : "nothing, "));
			System.out.println(" The current stack is: " + (tempChar != null ? tempStack : "NULL and cannot pop."));
		} // Of for i

		for (int i = 5; i > 0; i--) {
			tempChar = tempStack.pop();
			System.out.print("Poped: " + (tempChar != null ? tempChar.charValue() : "nothing, "));
			System.out.println(" The current stack is: "
					+ (!tempStack.isEmpty() ? (tempStack + ", and the top element is: " + tempStack.top().charValue())
							: "NULL, and cannot pop"));
		} // Of for i

	}// of main

}// of class MyLinkedStack<T> 

  结果:

The current stack is: { & % A }
Cleared, the current stack is: It's an empty stack.
The current stack is: { a }
The current stack is: { b a }
The current stack is: { c b a }
The current stack is: { d c b a }
The current stack is: { e d c b a }
The current stack is: { f e d c b a }
The current stack is: { g f e d c b a }
The current stack is: { h g f e d c b a }
The current stack is: { i h g f e d c b a }
The current stack is: { j i h g f e d c b a }
The current stack is: { k j i h g f e d c b a }

Poped: k The current stack is: { j i h g f e d c b a }
Poped: j The current stack is: { i h g f e d c b a }
Poped: i The current stack is: { h g f e d c b a }
Poped: h The current stack is: { g f e d c b a }
Poped: g The current stack is: { f e d c b a }
Poped: f The current stack is: { e d c b a }
Poped: e The current stack is: { d c b a }
Poped: d The current stack is: { c b a }, and the top element is: c
Poped: c The current stack is: { b a }, and the top element is: b
Poped: b The current stack is: { a }, and the top element is: a
Poped: a The current stack is: NULL, and cannot pop
Poped: nothing,  The current stack is: NULL, and cannot pop

  注意,基于单链表的输出,只能从栈顶输出至栈底,要栈底开始输出,就需要折腾一下。单链表实现的栈,链是自顶向下的,同样我们保留起止两个不用的节点使整个代码统一。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值