Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How python uses arrays to implement stacks

Shulou Source: shulou.com Published: 2022-06-01 17:59:47 09月17日 Update

Editor to share with you how python uses the array stack, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Using arrays to implement stacks

Implement a stack by yourself, which requires that the stack has basic methods such as push (), pop () (return the top element of the stack and exit the stack), peek () (return the top element of the stack without leaving the stack), isEmpty (), and size ().

Tip: judge whether the capacity of the stack is sufficient before entering the stack. If not, use Arrays.copyOf () to expand the capacity.

Public class MyStack {private int [] storage;// the capacity of the array private int capacity;// stack where elements in the stack are stored. Number of elements in the private int count;// stack private static final int GROW_FACTOR = 2

/ / TODO: construction method without initial capacity. The default capacity is 8 public MyStack () {this.capacity = 8; this.storage=new int [8]; this.count = 0;}

/ / TODO: construction method with initial capacity public MyStack (int initialCapacity) {if (initialCapacity < 1) throw new IllegalArgumentException ("Capacity too small.")

This.capacity = initialCapacity; this.storage = new int [initialCapacity]; this.count = 0;}

/ / TODO: stack public void push (int value) {if (count = = capacity) {ensureCapacity ();} storage [count++] = value;}

/ / TODO: make sure the capacity size private void ensureCapacity () {int newCapacity = capacity * GROW_FACTOR; storage = Arrays.copyOf (storage, newCapacity); capacity = newCapacity;}

/ / TODO: return the top element of the stack and exit the stack private int pop () {count--; if (count = =-1) throw new IllegalArgumentException ("Stack is empty.")

Return storage [count];}

/ / TODO: returns the top element of the stack without leaving the stack private int peek () {if (count = = 0) {throw new IllegalArgumentException ("Stack is empty.");} else {return storage [count-1];}}

/ / TODO: determine whether the stack is empty private boolean isEmpty () {return count = = 0;}

/ / TODO: returns the number of elements in the stack private int size () {return count;}

}

Verify:

MyStack myStack = new MyStack (3); myStack.push (1); myStack.push (2); myStack.push (3); myStack.push (4); myStack.push (5); myStack.push (6); myStack.push (7); myStack.push (8); System.out.println (myStack.peek ()); / / 8System.out.println (myStack.size ()); / / 8for (int I = 0; I < 8; iTunes +) {System.out.println (myStack.pop ()) } System.out.println (myStack.isEmpty ()); / / truemyStack.pop (); / / error: java.lang.IllegalArgumentException: Stack is empty. These are all the contents of the article "how to use array stacks in python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Elements capacity arrays methods articles content and output not enough not much number enough size most quantity more knowledge industry information information channels channels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker MySQL macOS Microsoft NVidia