-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
133 lines (117 loc) · 2.66 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System;
using System.Collections.Generic;
namespace DesignCircularQueue
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
public class MyCircularQueue1
{
private List<int> nums;
private int capacity;
public MyCircularQueue1(int k)
{
nums = new List<int>();
capacity = k;
}
public bool EnQueue(int value)
{
if (nums.Count >= capacity) return false;
nums.Add(value);
return true;
}
public bool DeQueue()
{
if (nums.Count == 0) return false;
nums.RemoveAt(0);
return true;
}
public int Front()
{
if (nums.Count == 0) return -1;
return nums[0];
}
public int Rear()
{
if (nums.Count == 0) return -1;
return nums[^1];
}
public bool IsEmpty()
{
return nums.Count == 0;
}
public bool IsFull()
{
return nums.Count == capacity;
}
}
public class MyCircularQueue
{
public class ListNode
{
public int val;
public ListNode next;
public ListNode(int val)
{
this.val = val;
}
}
public ListNode front;
public ListNode rear;
public int size;
public int k;
public MyCircularQueue(int k)
{
size = k;
this.k = k;
}
public bool EnQueue(int value)
{
if (size == 0) return false;
if (front == null)
{
front = new ListNode(value);
rear = front;
}
else
{
rear.next = new ListNode(value);
rear = rear.next;
}
size--;
return true;
}
public bool DeQueue()
{
if (front != null)
{
front = front.next;
if (front == null)
rear = null;
size++;
return true;
}
return false;
}
public int Front()
{
return front == null ? -1 : front.val;
}
public int Rear()
{
return rear == null ? -1 : rear.val;
}
public bool IsEmpty()
{
return size == k;
}
public bool IsFull()
{
return size == 0;
}
}
}