定义
在c#中,stack<t> 是一个后进先出(lifo,last-in-first-out)集合类,位于system.collections.generic 命名空间中。stack<t> 允许你将元素压入栈顶,并从栈顶弹出元素。
不难看出,最先放入栈中的元素在栈底,最后放入的元素在栈顶,而删除元素刚好相反,最后放入的元素最先删除,最先放入的元素最后删除
即有泛型版本,又有非泛型版本,我们常用泛型版本(二者思想上差别不大,非泛型会涉及装箱拆箱),栈的结构如下
常规用法
1. 声明一个栈
无参构造函数
获取一个空的栈对象
public stack(); stack<int> stack = new stack<int>();
接受一个可枚举对象
此构造函数允许你用一个已有的集合(如数组、列表等)来初始化栈,并按集合的顺序将元素压入栈中,集合的最后一个元素将成为栈的顶部。
public stack(ienumerable<t> collection); list<int> list = new list<int> { 1, 2, 3 }; stack<int> stack = new stack<int>(list);
不常用
指定栈的初始容量,减少扩展时的性能开销。这个参数不是常用的,因为栈会自动调整大小,但对于需要明确控制内存的场景,可以使用这个构造函数。
public stack(int capacity); stack<int> stack = new stack<int>(100); // 初始化一个容量为100的栈
2. 将元素压入栈 (push)
stack.push(1); stack.push(2); stack.push(3);
3. 从栈顶弹出元素 (pop)
pop 会移除并返回栈顶的元素。 因为是后进先出,所以我们弹出的最后入栈的元素3 int topelement = stack.pop(); // topelement is 3
4. 查看栈顶元素但不移除 (peek)
peek 只返回栈顶元素,但不会从栈中移除它。 int topelement = stack.peek(); // topelement is 2
5. 检查栈是否为空 (count 和 any)
你可以使用 count 属性或 any 方法来检查栈中是否有元素。 bool isempty = stack.count == 0; // or stack.any() == false
6. 遍历栈中的元素
你可以通过 foreach 遍历栈中的元素,注意遍历的顺序是从栈顶到栈底。 foreach (var item in stack) { console.writeline(item); }
7. 清空栈 (clear)
你可以使用 clear 方法来移除栈中的所有元素。 stack.clear();
8.contains 方法
判断栈中是否包含某个元素,返回 true 或 false。 bool exists = stack.contains(2); // 检查栈中是否包含2
9.trypeek 和 trypop 方法
trypeek(out t result) trypeek 允许你尝试获取栈顶元素,而不会抛出异常。如果栈为空,它返回 false,否则返回 true 并输出栈顶元素。 if (stack.trypeek(out int topelement)) { console.writeline($"top element: {topelement}"); } else { console.writeline("stack is empty"); }
trypop(out t result) trypop 允许你尝试弹出栈顶元素,同样不会抛出异常。如果栈为空,它返回 false,否则返回 true 并输出并移除栈顶元素。 if (stack.trypop(out int poppedelement)) { console.writeline($"popped element: {poppedelement}"); } else { console.writeline("stack is empty"); }
使用频率较低的方法 10.trimexcess 方法
trimexcess 方法用于将内部存储的容量调整到栈中实际元素的数量。 栈动态调整大小时,可能会预留一些额外的空间。 此方法通过移除额外的空间来优化内存使用。 stack.trimexcess(); // 移除多余的容量,减少内存浪费
11. copyto 方法
注意是一个浅拷贝方法
将栈中的元素复制到一个数组中,且从指定的数组索引位置开始放置。元素顺序是从栈顶到栈底. stack<myclass> stack = new stack<myclass>(); stack.push(new myclass { value = 1 }); stack.push(new myclass { value = 2 }); myclass[] array = new myclass[stack.count]; //传入需要复制的数组,第二个参数是目标数组中开始复制的索引位置 //栈中的第一个元素会被复制到目标数组的该索引位置,后续的栈元素将按顺序依次复制到数组的后续位置。 stack.copyto(array, 0); // 修改数组中的对象 array[0].value = 100; // 栈中的对象也会反映这个修改 console.writeline(stack.peek().value); // 输出 100
12. toarray 方法
注意是一个浅拷贝方法
将栈中的所有元素复制到一个新的数组中 会返回一个包含栈所有元素的新数组。栈中的元素会以 lifo(后进先出)的顺序放入数组中, 也就是说,栈顶的元素会成为数组的第一个元素,栈底的元素会成为数组的最后一个元素。 stack<int> stack = new stack<int>(); stack.push(1); stack.push(2); stack.push(3); int[] array = stack.toarray(); foreach (var item in array) { console.writeline(item); }
一个完整示例
using system; using system.collections.generic; class program { static void main() { // 创建一个栈 stack<string> stack = new stack<string>(); // 向栈中添加元素 stack.push("first"); stack.push("second"); stack.push("third"); // 查看栈顶元素(不移除) console.writeline("peek: " + stack.peek()); // 弹出栈顶元素 console.writeline("pop: " + stack.pop()); // 再次查看栈顶元素 console.writeline("peek after pop: " + stack.peek()); // 遍历栈 console.writeline("stack contents:"); foreach (var item in stack) { console.writeline(item); } // 检查栈是否为空 console.writeline("is stack empty? " + (stack.count == 0)); // 清空栈 stack.clear(); console.writeline("stack cleared. is stack empty? " + (stack.count == 0)); } }
输出结果
peek: third
pop: third
peek after pop: second
stack contents:
second
first
is stack empty? false
stack cleared. is stack empty? true
到此这篇关于c#常用数据结构栈的介绍的文章就介绍到这了,更多相关c#数据结构栈内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论