tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
38
Task/Queue-Definition/C-sharp/queue-definition.cs
Normal file
38
Task/Queue-Definition/C-sharp/queue-definition.cs
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
public class FIFO<T>
|
||||
{
|
||||
class Node
|
||||
{
|
||||
public T Item { get; set; }
|
||||
public Node Next { get; set; }
|
||||
}
|
||||
Node first = null;
|
||||
Node last = null;
|
||||
public void push(T item)
|
||||
{
|
||||
if (empty())
|
||||
{
|
||||
//Uses object initializers to set fields of new node
|
||||
first = new Node() { Item = item, Next = null };
|
||||
last = first;
|
||||
}
|
||||
else
|
||||
{
|
||||
last.Next = new Node() { Item = item, Next = null };
|
||||
last = last.Next;
|
||||
}
|
||||
}
|
||||
public T pop()
|
||||
{
|
||||
if (first == null)
|
||||
throw new System.Exception("No elements");
|
||||
if (last == first)
|
||||
last = null;
|
||||
T temp = first.Item;
|
||||
first = first.Next;
|
||||
return temp;
|
||||
}
|
||||
public bool empty()
|
||||
{
|
||||
return first == null;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue