Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How python uses arrays to implement stacks

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report