Skip to content

Commit 9b540eb

Browse files
committed
rename project names;
add stack implementation with csharp
1 parent aee7f91 commit 9b540eb

26 files changed

+474
-38
lines changed

csharp/05-array/Array.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22

3-
namespace _05_array
3+
namespace algo05_array
44
{
55
public sealed class Array<T> where T : IComparable<T>
66
{

csharp/05-array/_05_array.csproj renamed to csharp/05-array/algo05_array.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
<PropertyGroup>
44
<TargetFramework>netcoreapp2.2</TargetFramework>
5-
65
<IsPackable>false</IsPackable>
76
</PropertyGroup>
87

csharp/06-linkedlist/LRUWithArray.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
using System.Text.RegularExpressions;
2-
using _05_array;
1+
using algo05_array;
32

4-
namespace _06_linked_list
3+
namespace algo06_linked_list
54
{
65
/// <summary>
76
/// 使用数组实现LRU缓存淘汰算法

csharp/06-linkedlist/LRUWithLinkedList.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace _06_linked_list
1+
namespace algo06_linked_list
22
{
33
/// <summary>
44
/// 使用单链表实现LRU缓存淘汰算法

csharp/06-linkedlist/SingleLinkedList.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22

3-
namespace _06_linked_list
3+
namespace algo06_linked_list
44
{
55
/// <summary>
66
/// 单链表的插入、删除、清空、查找

csharp/06-linkedlist/_06_linked_list.csproj renamed to csharp/06-linkedlist/algo06_linked_list.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<ProjectReference Include="..\05-array\_05_array.csproj" />
10+
<ProjectReference Include="..\05-array\algo05_array.csproj" />
1111
</ItemGroup>
1212

1313
</Project>

csharp/07-linkedlist/_07_linkedlist/SingleLinkedListAlgo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
2-
using _06_linked_list;
2+
using algo06_linked_list;
33

4-
namespace _07_linkedlist
4+
namespace algo07_linkedlist
55
{
66
/// <summary>
77
/// 单链表常用算法操作

csharp/07-linkedlist/_07_linkedlist/_07_linkedlist.csproj renamed to csharp/07-linkedlist/_07_linkedlist/algo07_linkedlist.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<ProjectReference Include="..\..\06-linkedlist\_06_linked_list.csproj" />
8+
<ProjectReference Include="..\..\06-linkedlist\algo06_linked_list.csproj" />
99
</ItemGroup>
1010

1111
</Project>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
3+
namespace algo08_stack
4+
{
5+
public class ArrayStack<T>
6+
{
7+
private readonly int _capacity;
8+
9+
private readonly T[] _data;
10+
11+
private int _top = -1; // 指向栈顶元素,当为-1时表示栈为空
12+
13+
public ArrayStack(int capacity)
14+
{
15+
_capacity = capacity;
16+
17+
_data = new T[capacity];
18+
}
19+
20+
public int Count => _top + 1;
21+
22+
public void Push(T val)
23+
{
24+
if (Count == _capacity) throw new InvalidOperationException("Stack full.");
25+
26+
_top++;
27+
28+
_data[_top] = val;
29+
}
30+
31+
public T Pop()
32+
{
33+
if (_top == -1) throw new InvalidOperationException("Stack empty.");
34+
35+
T val = _data[_top];
36+
_top--;
37+
38+
return val;
39+
}
40+
}
41+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
3+
namespace algo08_stack
4+
{
5+
public class LinkedStack<T>
6+
{
7+
private StackListNode<T> _top;
8+
9+
public int Count { get; private set; }
10+
11+
public void Push(T val)
12+
{
13+
var newNode = new StackListNode<T>(val);
14+
newNode.Next = _top;
15+
_top = newNode;
16+
17+
Count++;
18+
}
19+
20+
public T Pop()
21+
{
22+
if (_top == null) throw new InvalidOperationException("Stack empty");
23+
24+
T val = _top.Value;
25+
_top = _top.Next;
26+
27+
Count--;
28+
29+
return val;
30+
}
31+
32+
public void Clear()
33+
{
34+
while (Count > 0)
35+
{
36+
Pop();
37+
}
38+
}
39+
}
40+
41+
public class StackListNode<T>
42+
{
43+
public StackListNode(T nodeValue)
44+
{
45+
Value = nodeValue;
46+
}
47+
48+
public T Value { get; set; }
49+
public StackListNode<T> Next { get; set; }
50+
}
51+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
namespace algo08_stack
2+
{
3+
/// <summary>
4+
/// 利用链栈实现浏览器怎么进后退
5+
/// </summary>
6+
public class LinkedStackBrowser
7+
{
8+
private readonly LinkedStack<string> _backStack = new LinkedStack<string>();
9+
private readonly LinkedStack<string> _forwardStack = new LinkedStack<string>();
10+
11+
public void Open(string url)
12+
{
13+
_backStack.Push(url);
14+
15+
_forwardStack.Clear();
16+
}
17+
18+
public string Backward()
19+
{
20+
if (_backStack.Count == 0) return string.Empty;
21+
22+
string url = _backStack.Pop();
23+
24+
_forwardStack.Push(url);
25+
26+
return url;
27+
}
28+
29+
public string Forward()
30+
{
31+
if (_forwardStack.Count == 0) return string.Empty;
32+
33+
string url = _forwardStack.Pop();
34+
_backStack.Push(url);
35+
36+
return url;
37+
}
38+
}
39+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.2</TargetFramework>
5+
</PropertyGroup>
6+
7+
</Project>

csharp/Tests/_05_array_tests/Array.Tests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
2+
using algo05_array;
23
using Xunit;
34
using Xunit.Abstractions;
4-
using _05_array;
55

66
namespace _05_array_tests
77
{

csharp/Tests/_05_array_tests/_05_array_tests.csproj renamed to csharp/Tests/_05_array_tests/algo05_array_tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
</ItemGroup>
1616

1717
<ItemGroup>
18-
<ProjectReference Include="..\..\05-array\_05_array.csproj" />
18+
<ProjectReference Include="..\..\05-array\algo05_array.csproj" />
1919
</ItemGroup>
2020

2121
</Project>

csharp/Tests/_06_linkedlist_tests/BaseLinkedListTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
2-
using _06_linked_list;
2+
using algo06_linked_list;
33

4-
namespace _06_linkedlist_tests
4+
namespace algo06_linkedlist_tests
55
{
66
public class BaseLinkedListTests
77
{

csharp/Tests/_06_linkedlist_tests/LRUWithArray.Tests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
using algo05_array;
2+
using algo06_linked_list;
13
using Xunit;
24
using Xunit.Abstractions;
3-
using _05_array;
4-
using _06_linked_list;
55

6-
namespace _06_linkedlist_tests
6+
namespace algo06_linkedlist_tests
77
{
88
public class LRUWithArrayTests
99
{

csharp/Tests/_06_linkedlist_tests/LRUWithLinkedList.Tests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using Xunit;
22
using Xunit.Abstractions;
3-
using _06_linked_list;
3+
using algo06_linked_list;
44

5-
namespace _06_linkedlist_tests
5+
namespace algo06_linkedlist_tests
66
{
77
public class LRUWithLinkedListTests : BaseLinkedListTests
88
{

csharp/Tests/_06_linkedlist_tests/SingleLinkedList.Tests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
using System;
22
using Xunit;
33
using Xunit.Abstractions;
4-
using _06_linked_list;
4+
using algo06_linked_list;
55

6-
namespace _06_linkedlist_tests
6+
namespace algo06_linkedlist_tests
77
{
88
public class SingleLinkedListTests : BaseLinkedListTests
99
{

csharp/Tests/_06_linkedlist_tests/_06_linkedlist_tests.csproj renamed to csharp/Tests/_06_linkedlist_tests/algo06_linkedlist_tests.csproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
<PropertyGroup>
44
<TargetFramework>netcoreapp2.2</TargetFramework>
5-
<RootNamespace>_06_linkedlist_tests</RootNamespace>
65

76
<IsPackable>false</IsPackable>
87
</PropertyGroup>
@@ -14,8 +13,8 @@
1413
</ItemGroup>
1514

1615
<ItemGroup>
17-
<ProjectReference Include="..\..\05-array\_05_array.csproj" />
18-
<ProjectReference Include="..\..\06-linkedlist\_06_linked_list.csproj" />
16+
<ProjectReference Include="..\..\05-array\algo05_array.csproj" />
17+
<ProjectReference Include="..\..\06-linkedlist\algo06_linked_list.csproj" />
1918
</ItemGroup>
2019

2120
</Project>

csharp/Tests/_07_linkedlist_tests/SingleLinkedListAlgo.Tests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
using System;
22
using Xunit;
3-
using _06_linkedlist_tests;
4-
using _06_linked_list;
5-
using _07_linkedlist;
3+
using algo06_linkedlist_tests;
4+
using algo06_linked_list;
5+
using algo07_linkedlist;
66

7-
namespace _07_linkedlist_tests
7+
namespace algo07_linkedlist_tests
88
{
99
public class SingleLinkedListAlgoTests : BaseLinkedListTests
1010
{

csharp/Tests/_07_linkedlist_tests/_07_linkedlist_tests.csproj renamed to csharp/Tests/_07_linkedlist_tests/algo07_linkedlist_tests.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
</ItemGroup>
1414

1515
<ItemGroup>
16-
<ProjectReference Include="..\..\06-linkedlist\_06_linked_list.csproj" />
17-
<ProjectReference Include="..\..\07-linkedlist\_07_linkedlist\_07_linkedlist.csproj" />
18-
<ProjectReference Include="..\_06_linkedlist_tests\_06_linkedlist_tests.csproj" />
16+
<ProjectReference Include="..\..\06-linkedlist\algo06_linked_list.csproj" />
17+
<ProjectReference Include="..\..\07-linkedlist\_07_linkedlist\algo07_linkedlist.csproj" />
18+
<ProjectReference Include="..\_06_linkedlist_tests\algo06_linkedlist_tests.csproj" />
1919
</ItemGroup>
2020

2121
</Project>

0 commit comments

Comments
 (0)