Skip to content

Commit

Permalink
rename project names;
Browse files Browse the repository at this point in the history
add stack implementation with csharp
  • Loading branch information
songzheng45 committed Jan 21, 2019
1 parent aee7f91 commit 9b540eb
Show file tree
Hide file tree
Showing 26 changed files with 474 additions and 38 deletions.
2 changes: 1 addition & 1 deletion csharp/05-array/Array.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace _05_array
namespace algo05_array
{
public sealed class Array<T> where T : IComparable<T>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
5 changes: 2 additions & 3 deletions csharp/06-linkedlist/LRUWithArray.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Text.RegularExpressions;
using _05_array;
using algo05_array;

namespace _06_linked_list
namespace algo06_linked_list
{
/// <summary>
/// 使用数组实现LRU缓存淘汰算法
Expand Down
2 changes: 1 addition & 1 deletion csharp/06-linkedlist/LRUWithLinkedList.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace _06_linked_list
namespace algo06_linked_list
{
/// <summary>
/// 使用单链表实现LRU缓存淘汰算法
Expand Down
2 changes: 1 addition & 1 deletion csharp/06-linkedlist/SingleLinkedList.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace _06_linked_list
namespace algo06_linked_list
{
/// <summary>
/// 单链表的插入、删除、清空、查找
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\05-array\_05_array.csproj" />
<ProjectReference Include="..\05-array\algo05_array.csproj" />
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions csharp/07-linkedlist/_07_linkedlist/SingleLinkedListAlgo.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using _06_linked_list;
using algo06_linked_list;

namespace _07_linkedlist
namespace algo07_linkedlist
{
/// <summary>
/// 单链表常用算法操作
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\06-linkedlist\_06_linked_list.csproj" />
<ProjectReference Include="..\..\06-linkedlist\algo06_linked_list.csproj" />
</ItemGroup>

</Project>
41 changes: 41 additions & 0 deletions csharp/08-stack/algo08_stack/ArrayStack.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;

namespace algo08_stack
{
public class ArrayStack<T>
{
private readonly int _capacity;

private readonly T[] _data;

private int _top = -1; // 指向栈顶元素,当为-1时表示栈为空

public ArrayStack(int capacity)
{
_capacity = capacity;

_data = new T[capacity];
}

public int Count => _top + 1;

public void Push(T val)
{
if (Count == _capacity) throw new InvalidOperationException("Stack full.");

_top++;

_data[_top] = val;
}

public T Pop()
{
if (_top == -1) throw new InvalidOperationException("Stack empty.");

T val = _data[_top];
_top--;

return val;
}
}
}
51 changes: 51 additions & 0 deletions csharp/08-stack/algo08_stack/LinkedStack.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;

namespace algo08_stack
{
public class LinkedStack<T>
{
private StackListNode<T> _top;

public int Count { get; private set; }

public void Push(T val)
{
var newNode = new StackListNode<T>(val);
newNode.Next = _top;
_top = newNode;

Count++;
}

public T Pop()
{
if (_top == null) throw new InvalidOperationException("Stack empty");

T val = _top.Value;
_top = _top.Next;

Count--;

return val;
}

public void Clear()
{
while (Count > 0)
{
Pop();
}
}
}

public class StackListNode<T>
{
public StackListNode(T nodeValue)
{
Value = nodeValue;
}

public T Value { get; set; }
public StackListNode<T> Next { get; set; }
}
}
39 changes: 39 additions & 0 deletions csharp/08-stack/algo08_stack/LinkedStackBrowser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace algo08_stack
{
/// <summary>
/// 利用链栈实现浏览器怎么进后退
/// </summary>
public class LinkedStackBrowser
{
private readonly LinkedStack<string> _backStack = new LinkedStack<string>();
private readonly LinkedStack<string> _forwardStack = new LinkedStack<string>();

public void Open(string url)
{
_backStack.Push(url);

_forwardStack.Clear();
}

public string Backward()
{
if (_backStack.Count == 0) return string.Empty;

string url = _backStack.Pop();

_forwardStack.Push(url);

return url;
}

public string Forward()
{
if (_forwardStack.Count == 0) return string.Empty;

string url = _forwardStack.Pop();
_backStack.Push(url);

return url;
}
}
}
7 changes: 7 additions & 0 deletions csharp/08-stack/algo08_stack/algo08_stack.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>

</Project>
2 changes: 1 addition & 1 deletion csharp/Tests/_05_array_tests/Array.Tests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using algo05_array;
using Xunit;
using Xunit.Abstractions;
using _05_array;

namespace _05_array_tests
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\05-array\_05_array.csproj" />
<ProjectReference Include="..\..\05-array\algo05_array.csproj" />
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions csharp/Tests/_06_linkedlist_tests/BaseLinkedListTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using _06_linked_list;
using algo06_linked_list;

namespace _06_linkedlist_tests
namespace algo06_linkedlist_tests
{
public class BaseLinkedListTests
{
Expand Down
6 changes: 3 additions & 3 deletions csharp/Tests/_06_linkedlist_tests/LRUWithArray.Tests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using algo05_array;
using algo06_linked_list;
using Xunit;
using Xunit.Abstractions;
using _05_array;
using _06_linked_list;

namespace _06_linkedlist_tests
namespace algo06_linkedlist_tests
{
public class LRUWithArrayTests
{
Expand Down
4 changes: 2 additions & 2 deletions csharp/Tests/_06_linkedlist_tests/LRUWithLinkedList.Tests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Xunit;
using Xunit.Abstractions;
using _06_linked_list;
using algo06_linked_list;

namespace _06_linkedlist_tests
namespace algo06_linkedlist_tests
{
public class LRUWithLinkedListTests : BaseLinkedListTests
{
Expand Down
4 changes: 2 additions & 2 deletions csharp/Tests/_06_linkedlist_tests/SingleLinkedList.Tests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using Xunit;
using Xunit.Abstractions;
using _06_linked_list;
using algo06_linked_list;

namespace _06_linkedlist_tests
namespace algo06_linkedlist_tests
{
public class SingleLinkedListTests : BaseLinkedListTests
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<RootNamespace>_06_linkedlist_tests</RootNamespace>

<IsPackable>false</IsPackable>
</PropertyGroup>
Expand All @@ -14,8 +13,8 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\05-array\_05_array.csproj" />
<ProjectReference Include="..\..\06-linkedlist\_06_linked_list.csproj" />
<ProjectReference Include="..\..\05-array\algo05_array.csproj" />
<ProjectReference Include="..\..\06-linkedlist\algo06_linked_list.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using Xunit;
using _06_linkedlist_tests;
using _06_linked_list;
using _07_linkedlist;
using algo06_linkedlist_tests;
using algo06_linked_list;
using algo07_linkedlist;

namespace _07_linkedlist_tests
namespace algo07_linkedlist_tests
{
public class SingleLinkedListAlgoTests : BaseLinkedListTests
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\06-linkedlist\_06_linked_list.csproj" />
<ProjectReference Include="..\..\07-linkedlist\_07_linkedlist\_07_linkedlist.csproj" />
<ProjectReference Include="..\_06_linkedlist_tests\_06_linkedlist_tests.csproj" />
<ProjectReference Include="..\..\06-linkedlist\algo06_linked_list.csproj" />
<ProjectReference Include="..\..\07-linkedlist\_07_linkedlist\algo07_linkedlist.csproj" />
<ProjectReference Include="..\_06_linkedlist_tests\algo06_linkedlist_tests.csproj" />
</ItemGroup>

</Project>
Loading

0 comments on commit 9b540eb

Please sign in to comment.