This commit is contained in:
krahets
2023-02-08 22:16:25 +08:00
parent 30ed83e5b1
commit af3542e3c0
17 changed files with 258 additions and 100 deletions

View File

@@ -732,7 +732,9 @@ comments: true
=== "C#"
```csharp title="linkedlist_deque.cs"
[class]{ListNode}-[func]{}
[class]{LinkedListDeque}-[func]{}
```
=== "Swift"

View File

@@ -690,21 +690,25 @@ comments: true
{
private ListNode? front, rear; // 头结点 front ,尾结点 rear
private int queSize = 0;
public LinkedListQueue()
{
front = null;
rear = null;
}
/* 获取队列的长度 */
public int size()
{
return queSize;
}
/* 判断队列是否为空 */
public bool isEmpty()
{
return size() == 0;
}
/* 入队 */
public void push(int num)
{
@@ -724,6 +728,7 @@ comments: true
}
queSize++;
}
/* 出队 */
public int poll()
{
@@ -733,6 +738,7 @@ comments: true
queSize--;
return num;
}
/* 访问队首元素 */
public int peek()
{
@@ -740,6 +746,22 @@ comments: true
throw new Exception();
return front.val;
}
/* 将链表转化为 Array 并返回 */
public int[] toArray()
{
if (front == null)
return Array.Empty<int>();
ListNode node = front;
int[] res = new int[size()];
for (int i = 0; i < res.Length; i++)
{
res[i] = node.val;
node = node.next;
}
return res;
}
}
```
@@ -975,11 +997,10 @@ comments: true
/* 将数组转化为 Vector 并返回 */
vector<int> toVector() {
int cap = queCapacity;
// 仅转换有效长度范围内的列表元素
vector<int> arr(queSize);
for (int i = 0, j = front; i < queSize; i++, j++) {
arr[i] = nums[j % cap];
arr[i] = nums[j % queCapacity];
}
return arr;
}
@@ -1328,6 +1349,18 @@ comments: true
throw new Exception();
return nums[front];
}
/* 返回数组 */
public int[] toArray()
{
// 仅转换有效长度范围内的列表元素
int[] res = new int[queSize];
for (int i = 0, j = front; i < queSize; i++, j++)
{
res[i] = nums[j % this.capacity()];
}
return res;
}
}
```

View File

@@ -638,22 +638,26 @@ comments: true
/* 基于链表实现的栈 */
class LinkedListStack
{
private ListNode stackPeek; // 将头结点作为栈顶
private ListNode? stackPeek; // 将头结点作为栈顶
private int stkSize = 0; // 栈的长度
public LinkedListStack()
{
stackPeek = null;
}
/* 获取栈的长度 */
public int size()
{
return stkSize;
}
/* 判断栈是否为空 */
public bool isEmpty()
{
return size() == 0;
}
/* 入栈 */
public void push(int num)
{
@@ -662,21 +666,42 @@ comments: true
stackPeek = node;
stkSize++;
}
/* 出栈 */
public int pop()
{
if (stackPeek == null)
throw new Exception();
int num = peek();
stackPeek = stackPeek?.next;
stackPeek = stackPeek.next;
stkSize--;
return num;
}
/* 访问栈顶元素 */
public int peek()
{
if (size() == 0)
if (size() == 0 || stackPeek == null)
throw new Exception();
return stackPeek.val;
}
/* 将 List 转化为 Array 并返回 */
public int[] toArray()
{
if (stackPeek == null)
return Array.Empty<int>();
ListNode node = stackPeek;
int[] res = new int[size()];
for (int i = res.Length - 1; i >= 0; i--)
{
res[i] = node.val;
node = node.next;
}
return res;
}
}
```
@@ -1047,21 +1072,25 @@ comments: true
// 初始化列表(动态数组)
stack = new();
}
/* 获取栈的长度 */
public int size()
{
return stack.Count();
}
/* 判断栈是否为空 */
public bool isEmpty()
{
return size() == 0;
}
/* 入栈 */
public void push(int num)
{
stack.Add(num);
}
/* 出栈 */
public int pop()
{
@@ -1071,6 +1100,7 @@ comments: true
stack.RemoveAt(size() - 1);
return val;
}
/* 访问栈顶元素 */
public int peek()
{
@@ -1078,6 +1108,12 @@ comments: true
throw new Exception();
return stack[size() - 1];
}
/* 将 List 转化为 Array 并返回 */
public int[] toArray()
{
return stack.ToArray();
}
}
```