forked from wangzheng0822/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
26 changed files
with
474 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
namespace _06_linked_list | ||
namespace algo06_linked_list | ||
{ | ||
/// <summary> | ||
/// 使用单链表实现LRU缓存淘汰算法 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
/// 单链表的插入、删除、清空、查找 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
csharp/Tests/_07_linkedlist_tests/SingleLinkedListAlgo.Tests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.