Skip to content

Commit

Permalink
queueing functionality fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
ehilst515 committed Sep 10, 2020
1 parent f77aa71 commit 096b71f
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 38 deletions.
Binary file modified Assets/AnimalShelter1.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Assets/AnimalShelter2.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 9 additions & 12 deletions DataStructures.Tests/AnimalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
using Xunit;
using DataStructures.StacksAndQueues;



namespace DataStructures.Tests
{
public class AnimalTests
Expand All @@ -13,7 +11,7 @@ public class AnimalTests
public void Shelter_starts_empty()
{
// Arrange
AnimalShelter<object> shelter = new AnimalShelter<object>();
AnimalShelter shelter = new AnimalShelter();

// Act
bool result = shelter.ShelterIsEmpty();
Expand All @@ -26,7 +24,7 @@ public void Shelter_starts_empty()
public void Shelter_can_add_animals()
{
// Arrange
AnimalShelter<object> shelter = new AnimalShelter<object>();
AnimalShelter shelter = new AnimalShelter();

// Act
shelter.AnimalEnqueue(Animal.Type.Dog);
Expand All @@ -40,34 +38,33 @@ public void Shelter_can_add_animals()
public void Dequeue_returns_dog()
{
// Arrange
AnimalShelter<object> shelter = new AnimalShelter<object>();
AnimalShelter shelter = new AnimalShelter();
shelter.AnimalEnqueue(Animal.Type.Bird);
shelter.AnimalEnqueue(Animal.Type.Dog);
shelter.AnimalEnqueue(Animal.Type.Cat);

// Act
AnimalShelter<object>.Node result = shelter.AnimalDequeue(Animal.Type.Dog);
Animal.Type? result = shelter.AnimalDequeue(Animal.Type.Dog);

// Assert
Assert.Equal(Animal.Type.Dog, result.Value);
Assert.Equal(Animal.Type.Dog, result);
}

[Fact]
public void Dequeue_returns_dog_or_cat_or_null()
{
// Arrange
AnimalShelter<object> shelter = new AnimalShelter<object>();
AnimalShelter shelter = new AnimalShelter();
shelter.AnimalEnqueue(Animal.Type.Bird);
shelter.AnimalEnqueue(Animal.Type.Dog);
shelter.AnimalEnqueue(Animal.Type.Cat);

// Act
AnimalShelter<object>.Node result = shelter.AnimalDequeue(Animal.Type.Bird);
Animal.Type? result = shelter.AnimalDequeue(Animal.Type.Bird);

// Assert
Assert.Null(result);
Assert.Null(result);
}



}
}
49 changes: 29 additions & 20 deletions DataStructures/StacksAndQueues/AnimalShelter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Text;
using System.Transactions;
using DataStructures.StacksAndQueues;

namespace DataStructures.StacksAndQueues
Expand All @@ -13,51 +14,63 @@ public enum Type {
}
}

public class AnimalShelter<T>
public class AnimalShelter
{
public class Node
{
public Node(T value)
public Node(Animal.Type value)
{
Value = value;
}
// Value of this node
public T Value { get; set; }
public Animal.Type Value { get; set; }
// Pointer to the next node in the list
public Node Next { get; set; }

public Node Prev { get; set; }
}

public Node Front { get; set; }
public Node Rear { get; set; }


public void AnimalEnqueue(T animal)
public void AnimalEnqueue(Animal.Type animal)
{
Node node = new Node(animal);
if (Front == null)
if (Rear == null)
{
node.Next = node;
Front = node;
}
Front = Rear = node;

}
else
{
node.Next = node;
Node prev = Rear;
Rear = node;
prev.Next = node;
node.Prev = prev;
}
}

public Node AnimalDequeue(Animal.Type pref)
public Animal.Type? AnimalDequeue(Animal.Type pref)
{
if (pref == Animal.Type.Dog || pref == Animal.Type.Dog)
var current = Front;
if (pref == Animal.Type.Dog || pref == Animal.Type.Cat)
{
Node temp = Front;
Front = Front.Next;
temp.Next = null;
return temp;
while (current != null)
{
if(current.Value == pref)
{
Node temp = current;
current.Prev.Next = temp.Next;
current.Next.Prev = temp.Prev;
return current.Value;
}
current = current.Next;
if (current == Rear)
break;
}
return null;
}

else return null;
}

Expand All @@ -70,9 +83,5 @@ public bool ShelterIsEmpty()
else
return false;
}




}
}
11 changes: 7 additions & 4 deletions DataStructures/StacksAndQueues/Queueing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,18 @@ public Node(T value)
public void Enqueue(T value)
{
Node node = new Node(value);
if (Front == null)
if (Rear == null)
{
node.Next = node;
Front = node;
Front = Rear = node;

}

else
{
node.Next = node;
Node prev = Rear;
Rear = node;
prev.Next = node;
node.Prev = prev;
}
}

Expand Down
3 changes: 1 addition & 2 deletions DataStructures/StacksAndQueues/Stacking.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ namespace DataStructures.StacksAndQueues
{
public class Stack<T>
{
internal AnimalShelter<T>.Node Front;


public class Node
{

Expand Down

0 comments on commit 096b71f

Please sign in to comment.