Deque 不在 C# 中,但我们可以通过 Vector 和 List 来归档功能,下面是一个通过 List 归档 Deque 的示例程序。
using System;
using System.Collections.Generic;
public class GFG{
// Function to generate the array by
// inserting array elements one by one
// followed by reversing the array
static void generateArray(int []arr, int n)
{
// Doubly ended Queue
List<int> ans = new List<int>();
// Start adding functionality at both end
// Iterate over the array
// Even no at the front and odd no at the rear
for(int i = 0; i < n; i++)
{
// Push array elements
// alternately to the front
// and back
if (arr[i]%2==0)
ans.Insert(0,arr[i]);
else
ans.Add(arr[i]);
}
printDeque(ans);
// Output 8 6 4 2 6 5 1 3
// Start removing functionality at both end
// Let i want to remove first(8) and last(3) element from Deque
ans.RemoveAt(0);
printDeque(ans);
// Output 6 4 2 6 5 1 3
ans.RemoveAt(ans.Count-1);
printDeque(ans);
// Output 6 4 2 6 5 1
}
static void printDeque(List<int> q){
// Print the elements
// of the Deque
foreach(int x in q)
{
Console.Write(x + " ");
}
Console.WriteLine();
}
// Driver Code
public static void Main(String[] args)
{
int []arr = {5, 6, 1, 2, 3, 4,6, 8 };
int n = arr.Length;
generateArray(arr, n);
}
}