Stack<T>类

Stack<T> 作为数组来实现。

Stack<T> 的容量是 Stack<T>
能够包括的元素数。

当向 Stack<T> 中加入元素时,将通过又一次分配内部数组来依据须要自己主动增大容量。

可通过调用 TrimExcess 来降低容量。 假设 Count 小于堆栈的容量,则 Push 的运算复杂度是 O(1)。 假设须要添加容量以容纳新元素,则 Push 的运算复杂度成为 O(n)。当中 n 为 Count。 Pop 的运算复杂度为 O(1)。

Stack<T>
接受 null 作为引用类型的有效值而且同意有反复的元素。

命名控件:System.Collections.Generic

程序集:System(在System.dll中)

语法:public class Stack<T>:IEnumerable<T>, ICollection, IEnumerable

List<T>实现了IList<T>、 ICollection<T>、IEnumerable<T>、IList、ICollection、IEnumerable接口

因此能够看出与List1T>相比:

Stack<T>没有继承ICollection<T>接口,由于这个接口定义的Add()和Remove()方法不能用于栈;

Stack<T>没有继承IList<T>接口,所以不能使用索引器訪问栈。

所以队列仅仅同意在栈的顶部加入元素,删除元素。

经常使用的Stack<T>类的成员:

Count : 返回栈中元素的个数。

Push(): 在栈顶加入一个元素。

Pop() : 从栈顶删除一个元素。

假设栈是空,就会抛出异常InvalidOperationException异常。

Peek(): 返回栈顶的元素,但不删除它。

Contains(): 确定某个元素是否在栈中。假设是,返回true。

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

经常使用Stack1T>类的成员函数的源代码例如以下:

public bool Contains(T item)

{

int index = this._size;

EqualityComparer<T> comparer = EqualityComparer<T>.Default;

while (index-- > 0)

{

if (item == null)

{

if (this._array[index] == null)

{

return true;

}

}

else if ((this._array[index] != null) && comparer.Equals(this._array[index], item))

{

return true;

}

}

return false;

}

public T Peek()

{

if (this._size == 0)

{

ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EmptyStack);

}

return this._array[this._size - 1];

}

public T Pop()

{

if (this._size == 0)

{

ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EmptyStack);

}

this._version++;

T local = this._array[--this._size];

this._array[this._size] = default(T);

return local;

}

public void Push(T item)

{

if (this._size == this._array.Length)

{

T[] destinationArray = new T[(this._array.Length == 0) ? 4 : (2 * this._array.Length)];

Array.Copy(this._array, 0, destinationArray, 0, this._size);

this._array = destinationArray;

}

this._array[this._size++] = item;

this._version++;

}

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

以下的代码演示样例演示了 Stack 泛型类的几种方法。 此代码演示样例创建具有默认容量的字符串堆栈,并使用 Push 方法将五个字符串压入堆栈。

枚举堆栈的元素,这不会更改该堆栈的状态。

使用 Pop 方法将第一个字符串弹出堆栈。 使用 Peek 方法查看此堆栈中的下一个项,然后使用 Pop 方法将其弹出。

使用 ToArray 方法创建数组并将堆栈元素拷贝到当中,然后将数组传递给具有 IEnumerable 的 Stack 构造函数,以元素的反向顺序创建堆栈副本。

将显示副本的元素。

创建大小为堆栈大小两倍的数组,并使用 CopyTo 方法从数组的中间開始复制数组元素。

再次使用 Stack 构造函数以元素的反向顺序创建堆栈副本;这样。三个空元素就位于堆栈的底部。

使用 Contains 方法显示字符串“four”在第一个堆栈副本中。然后使用 Clear 方法清除该副本,并由 Count 属性显示此堆栈为空。

using System;

using System.Collections.Generic;

class Example

{

public static void Main()

{

Stack<string> numbers = new Stack<string>();

numbers.Push("one");

numbers.Push("two");

numbers.Push("three");

numbers.Push("four");

numbers.Push("five");

// A stack can be enumerated without disturbing its contents.

foreach( string number in numbers )

{

Console.WriteLine(number);

}

Console.WriteLine("\nPopping '{0}'", numbers.Pop());

Console.WriteLine("Peek at next item to destack: {0}", numbers.Peek());

Console.WriteLine("Popping '{0}'", numbers.Pop());

// Create a copy of the stack, using the ToArray method and the

// constructor that accepts an IEnumerable.

Stack stack2 = new Stack(numbers.ToArray());

Console.WriteLine("\nContents of the first copy:");

foreach( string number in stack2 )

{

Console.WriteLine(number);

}

// Create an array twice the size of the stack and copy the

// elements of the stack, starting at the middle of the

// array.

string[] array2 = new string[numbers.Count * 2];

numbers.CopyTo(array2, numbers.Count);

// Create a second stack, using the constructor that accepts an

// IEnumerable(Of T).

Stack stack3 = new Stack(array2);

Console.WriteLine("\nContents of the second copy, with duplicates and nulls:");

foreach( string number in stack3 )

{

Console.WriteLine(number);

}

Console.WriteLine("\nstack2.Contains(\"four\") = {0}",stack2.Contains("four"));

Console.WriteLine("\nstack2.Clear()");

stack2.Clear();

Console.WriteLine("\nstack2.Count = {0}", stack2.Count);

}

}

/* This code example produces the following output:

five

four

three

two

one

Popping 'five'

Peek at next item to destack: four

Popping 'four'

Contents of the first copy:

one

two

three

Contents of the second copy, with duplicates and nulls:

one

two

three

stack2.Contains("four") = False

stack2.Clear()

stack2.Count = 0

*/

C#中Stack&lt;T&gt;类的使用及部分成员函数的源代码分析的更多相关文章

  1. 类1(this指针/const成员函数/类作用域/外部成员函数/返回this对象的函数)

    假设我们要设计一个包含以下操作的 Sales_data 类: 1.一个 isbn 成员函数,用于返回对象的 book_no 成员变量 2.一个 combine 成员函数,用于将一个 Sales_dat ...

  2. C++ 空类,默认产生哪些成员函数

    C++ 空类,默认产生哪些成员函数.     默认构造函数.默认拷贝构造函数.默认析构函数.默认赋值运算符 这四个是我们通常大都知道的.但是除了这四个,还有两个,那就是取址运算符和 取址运算符 con ...

  3. C++类内存布局图(成员函数和成员变量分开讨论)

    一.成员函数 成员函数可以被看作是类作用域的全局函数,不在对象分配的空间里,只有虚函数才会在类对象里有一个指针,存放虚函数的地址等相关信息. 成员函数的地址,编译期就已确定,并静态绑定或动态的绑定在对 ...

  4. C++ 友元 (全局函数做友元) (类做友元) (成员函数做友元)

    1 //友元 全局函数做友元 2 /* 3 #include <iostream> 4 #include <string> 5 using namespace std; 6 7 ...

  5. 类中用const限定的成员函数

    本文转自http://blog.csdn.net/whyglinux/article/details/602329 类的成员函数后面加 const,表明这个函数不会对这个类对象的数据成员(准确地说是非 ...

  6. javascript函数中的实例对象、类对象、局部变量(局部函数)

    定义 function Person(national,age) { this.age = age; //实例对象,每个示例不同 Person.national = national; //类对象,所 ...

  7. python类内部调用自己的成员函数必须加self

    class A: def a(self): print("hello world") def b(self): return self.a() 上面的self.a()中self是不 ...

  8. String类的四个默认成员函数

    优化版的拷贝构造函数,先创建一个暂时实例tmp,接着把tmp._ptr和this->_ptr交换,因为tmp是一个局部变量.程序执行到该函数作用域外,就会自己主动调用析构函数.释放tmp._pt ...

  9. 在C#中使用C++编写的类

    现在在Windows下的应用程序开发,VS.Net占据了绝大多数的份额.因此很多以前搞VC++开发的人都转向用更强大的VS.Net.在这种情况下,有很多开发人员就面临了如何在C#中使用C++开发好的类 ...

随机推荐

  1. Vue 数组和对象更新,但是页面没有刷新

    在使用数组的时候,数组内部数据发生改变,但是与数组绑定的页面的数据却没有发生变化. <ul> <li v-for="(item,index) in todos" ...

  2. K近邻法(K-Nearest Neighbor,KNN)

    KNN是一种基本分类与回归方法,本篇只总结分类问题中的KNN. 输入:样本的特征向量,对应于特征空间中的点 输出:样本的类别,可取多类 算法思想:给定一个样本类别已知的训练数据集,对于新样本,根据其K ...

  3. WEB下的excel批量导入功能

    新手学习中,记录一下excel导出功能实现的整个流程.使用框架ssm. control层 importExcel+parseDataItem: @RequestMapping("import ...

  4. splice用法解析

    splice()方法算是最强大的数组方法了,它有很多种用法,主要用于删除指定位置的数组项,在指定的位置插入数组项,在指定位置替换数组项,slpice()方法始终都会返回一个数组,该数组包括从原始数组中 ...

  5. bat2exe 就是这么简单 白研究半天VC++了

    bat2exe 就是这么简单 白研究半天VC++了 结果:bat2exe编译的执行文件会被杀毒软件查杀.

  6. 富文本编辑器复制Wod字体问题

    目前常用的富文本编辑器:百度版UEditor,wangEditor,ckeditor,kindeditor,TinyMCE.当Word复制文本粘贴到编辑器时,几乎都无法保证字体大小完全一致的问题. 想 ...

  7. html页面比较长,如何用js实现网页一打开显示在网页的中部?

    加入js代码 <style type="text/css"> body { height: 2000px; } </style> <script ty ...

  8. stark组件之显示页面内容搭建(六)

    之前主要介绍了前端页面list_fiter功能的显示,但是list_display功能的展示并没有过多介绍,这里介绍一下是如何实现的. 可以看到凡是蓝线圈起来的都是通过字段名反射一个个取出来的,红线的 ...

  9. python爬虫框架—Scrapy安装及创建项目

    linux版本安装 pip3 install scrapy 安装完成 windows版本安装 pip install wheel 下载twisted,网址:http://www.lfd.uci.edu ...

  10. SpringMVC Controller的返回类型

    Controller的三种返回类型中 ModelAndView类型 带数据带跳转页面 String 跳转页面不带数据 void 通常是ajax格式请求时使用 1返回ModelAndView contr ...