C#数据结构练习——栈
记录数据结构的练习 强调:线程不安全
直接上代码
using System
;
using System
.Collections
;
using System
.Collections
.Generic
;
using System
.Linq
;
using System
.Text
;
using System
.Threading
;
public class MyStack<T
> : IEnumerator
<T
>,IEnumerable
<T
>, ICollection
{
private int top
;
private T
[] datas
;
public int Top
{ get => top
; }
public int Count
{ get => top
; }
public bool IsEmpty
=> top
< 0;
private const int DefaultCapacity
= 4;
#region Enumerator迭代器
private int currIndex
= -1;
public T Current
=> datas
[currIndex
];
object IEnumerator
.Current
=> Current
;
private object syncRoot
;
public object SyncRoot
{
get
{
if (this.syncRoot
== null)
Interlocked
.CompareExchange<object>(ref this.syncRoot
, new object(), null);
return this.syncRoot
;
}
}
public bool IsSynchronized
=> false;
public bool MoveNext()
{
currIndex
--;
return currIndex
>= 0;
}
public void Reset()
{
currIndex
= top
;
}
IEnumerator IEnumerable
.GetEnumerator()
{
return GetEnumerator();
}
public IEnumerator
<T
> GetEnumerator()
{
Reset();
return this;
}
public void Dispose() { }
#endregion
public MyStack() : this(DefaultCapacity
) { }
public MyStack(int cap
)
{
if (cap
<= 0) cap
= DefaultCapacity
;
datas
= new T[cap
];
top
= 0;
}
public void Add(T item
)
{
Push(item
);
}
public void Clear()
{
Array
.Clear(datas
, 0, datas
.Length
);
top
= 0;
}
public bool Contains(T item
)
{
int size
= Count
;
EqualityComparer
<T
> comparer
= EqualityComparer
<T
>.Default
;
while (size
-- > 0)
{
if (item
== null)
{
if (datas
[size
] == null) return true;
}
else
{
if (datas
[size
] != null && comparer
.Equals(datas
[size
], item
)) return true;
}
}
return false;
}
public void CopyTo(Array array
, int arrayIndex
)
{
if (array
== null) throw new NullReferenceException("array 参数为空");
if (array
.Rank
!= 1) throw new RankException("Rank Not Matching");
if (arrayIndex
< 0 || arrayIndex
>= array
.Length
) throw new IndexOutOfRangeException("下标越界");
if (Count
> array
.Length
- arrayIndex
) throw new Exception("给定集合和起始编号不合适");
try
{
Array
.Copy(datas
, 0, array
, arrayIndex
, Count
);
Array
.Reverse(array
, arrayIndex
, Count
);
}
catch (Exception e
)
{
Console
.WriteLine(e
.StackTrace
);
throw;
}
}
public void Push(T item
)
{
if (top
== datas
.Length
) Resize();
datas
[top
] = item
;
Console
.WriteLine($
"数据入栈:{item},Top:{top}");
top
++;
}
public void Push(IEnumerable
<T
> items
)
{
if (items
== null) throw new ArgumentNullException("参数为空");
IEnumerator
<T
> enumerator
= items
.GetEnumerator();
if (enumerator
== null)
{
throw new Exception("参数必须是集合或迭代器");
}
while (enumerator
.MoveNext())
{
Push(enumerator
.Current
);
}
}
private void Resize()
{
int len
= datas
.Length
;
len
= (int)(len
* 1.5);
T
[] newArr
= new T[len
];
Array
.Copy(datas
, newArr
, datas
.Length
);
datas
= newArr
;
Console
.WriteLine($
"重置容量:{len}");
}
public T Pop()
{
top
--;
if (IsEmpty
) throw new Exception("空栈");
T item
= datas
[top
];
Console
.WriteLine($
"数据出栈:{item}");
return item
;
}
public T Peek()
{
if (IsEmpty
) throw new Exception("空栈");
T item
= datas
[top
- 1];
Console
.WriteLine($
"{top}***{item}");
return item
;
}
public void Reverse()
{
Array
.Reverse(datas
, 0, Count
);
}
}
要点分析
1.栈的特性是后进先出,只能在一端操作,所以只需要维护一个栈顶标记即可 2.要注意栈顶标记在不同地方操作时的数值调整(代码内有注解) 3.其他细节分析见代码内注释
欢迎留言交流 转载请注明出处
转载请注明原文地址:https://tech.qufami.com/read-25551.html