add cs file to comply dequeue
This commit is contained in:
parent
0950e43fd7
commit
8ae16634c7
@ -1,49 +1,96 @@
|
|||||||
/**
|
public class DLinkNumbers
|
||||||
* File: deque.cs
|
{
|
||||||
* Created Time: 2022-12-30
|
public int value;
|
||||||
* Author: moonache (microin1301@outlook.com)
|
public DLinkNumbers front;
|
||||||
*/
|
public DLinkNumbers rear;
|
||||||
|
}
|
||||||
using NUnit.Framework;
|
|
||||||
|
public class Deque
|
||||||
namespace hello_algo.chapter_stack_and_queue
|
{
|
||||||
{
|
private static DLinkNumbers head, end;
|
||||||
public class deque
|
private static int length;
|
||||||
{
|
|
||||||
[Test]
|
public int Empty()
|
||||||
public void Test()
|
{
|
||||||
{
|
return length;
|
||||||
/* 初始化双向队列 */
|
}
|
||||||
// 在 C# 中,将链表 LinkedList 看作双向队列来使用
|
|
||||||
LinkedList<int> deque = new LinkedList<int>();
|
public int EnHead(int value)
|
||||||
|
{
|
||||||
/* 元素入队 */
|
DLinkNumbers temp = new DLinkNumbers { value = value };
|
||||||
deque.AddLast(2); // 添加至队尾
|
if (head == null)
|
||||||
deque.AddLast(5);
|
{
|
||||||
deque.AddLast(4);
|
end = temp;
|
||||||
deque.AddFirst(3); // 添加至队首
|
head = temp;
|
||||||
deque.AddFirst(1);
|
}
|
||||||
Console.WriteLine("双向队列 deque = " + String.Join(",", deque.ToArray()));
|
else
|
||||||
|
{
|
||||||
/* 访问元素 */
|
temp.front = head;
|
||||||
int peekFirst = deque.First.Value; // 队首元素
|
head.rear = temp;
|
||||||
Console.WriteLine("队首元素 peekFirst = " + peekFirst);
|
head = temp;
|
||||||
int peekLast = deque.Last.Value; // 队尾元素
|
}
|
||||||
Console.WriteLine("队尾元素 peekLast = " + peekLast);
|
length += 1;
|
||||||
|
return 1;
|
||||||
/* 元素出队 */
|
}
|
||||||
deque.RemoveFirst(); // 队首元素出队
|
|
||||||
Console.WriteLine("队首元素出队后 deque = " + String.Join(",", deque.ToArray()));
|
public int OutHead()
|
||||||
deque.RemoveLast(); // 队尾元素出队
|
{
|
||||||
Console.WriteLine("队尾元素出队后 deque = " + String.Join(",", deque.ToArray()));
|
if (length == 0)
|
||||||
|
return -1;
|
||||||
/* 获取双向队列的长度 */
|
int number = head.value;
|
||||||
int size = deque.Count;
|
head = head.front;
|
||||||
Console.WriteLine("双向队列长度 size = " + size);
|
length -= 1;
|
||||||
|
return number;
|
||||||
/* 判断双向队列是否为空 */
|
}
|
||||||
bool isEmpty = deque.Count == 0;
|
|
||||||
Console.WriteLine("双向队列是否为空 = " + isEmpty);
|
public int EnEnd(int value)
|
||||||
}
|
{
|
||||||
}
|
DLinkNumbers temp = new DLinkNumbers { value = value };
|
||||||
}
|
if (end == null)
|
||||||
|
{
|
||||||
|
end = temp;
|
||||||
|
head = temp;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
temp.front = end;
|
||||||
|
end.rear = temp;
|
||||||
|
end = temp;
|
||||||
|
}
|
||||||
|
length += 1;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int OutEnd()
|
||||||
|
{
|
||||||
|
if (length == 0)
|
||||||
|
return -1;
|
||||||
|
int number = end.value;
|
||||||
|
end = end.front;
|
||||||
|
length -= 1;
|
||||||
|
return number;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class TestForDeque
|
||||||
|
{
|
||||||
|
[Test]
|
||||||
|
public void Do()
|
||||||
|
{
|
||||||
|
Deque de = new Deque();
|
||||||
|
de.EnHead(12);
|
||||||
|
de.EnHead(13);
|
||||||
|
de.EnHead(14);
|
||||||
|
de.OutEnd();
|
||||||
|
de.OutEnd();
|
||||||
|
de.OutEnd();
|
||||||
|
de.OutEnd();
|
||||||
|
de.OutEnd();
|
||||||
|
de.EnEnd(12);
|
||||||
|
de.EnEnd(13);
|
||||||
|
de.EnEnd(14);
|
||||||
|
de.OutHead();
|
||||||
|
de.OutHead();
|
||||||
|
de.OutHead();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user