Skip to content

Instantly share code, notes, and snippets.

@DeepSky8
Created March 1, 2016 20:33
Show Gist options
  • Save DeepSky8/5169179e36d3ad5a4f74 to your computer and use it in GitHub Desktop.
Save DeepSky8/5169179e36d3ad5a4f74 to your computer and use it in GitHub Desktop.
Learning about Stacks and Queues, simultaneously. Please forgive the way that I call this a Stack, and then use all Queue terms for the methods.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Stack
{
class Program
{
static void Main(string[] args)
{
//int i = 0;
//While(() => { return i < 10; }, () =>
//{
// //Console.WriteLine(i);
// i++;
//});
var doubleList = new DoublyLinkedListQueue<int>();
doubleList.enqueue(12);
doubleList.enqueue(6);
doubleList.enqueue(18);
doubleList.enqueue(3);
doubleList.enqueue(9);
var peekList1 = doubleList.peek();
Console.WriteLine(peekList1.ToString());
var dequeueList1 = doubleList.dequeue();
Console.WriteLine(dequeueList1.ToString());
var test1 = doubleList.TestGetter();
Console.WriteLine(test1.Next.ToString());
Console.WriteLine(test1.Next.Previous.ToString());
var peekList2 = doubleList.peek();
Console.WriteLine(peekList2.ToString());
var dequeueList2 = doubleList.dequeue();
Console.WriteLine(dequeueList2.ToString());
}
public class Node<T>
{
public T Value { get; set; }
public Node<T> Next { get; set; }
public Node<T> Previous { get; set; }
}
public class DoublyLinkedListQueue<T>
{
private Node<T> head;
private Node<T> lastNode;
private int Count = 0;
/// <summary>
/// If list is empty, designates node as first element in the list. Else sets the Previous value of new node to lastNode, and sets lastNode.Next value to the new node.
/// </summary>
/// <param name="input"></param>
public void enqueue(T input)
{
var node = new Node<T>();
node.Value = input;
if (head == null)
{
head = node;
}
else
{
node.Previous = lastNode;
lastNode.Next = node;
}
lastNode = node;
Count += 1;
}
/// <summary>
/// Extracts Value of the first node, sets the Previous of the second node to null, sets head to the second node, and decrements the Count. Returns Value from the former first node.
/// </summary>
/// <returns></returns>
public T dequeue()
{
var value = head.Value;
head.Next.Previous = null;
head = head.Next;
Count -= 1;
return value;
}
/// <summary>
/// Returns the Value of the head.
/// </summary>
/// <returns></returns>
public T peek()
{
return head.Value;
}
/// <summary>
/// Returns the int Count that is updated every time a node is added or subtracted from the list.
/// ((Always begins counting from the first node in the list. While a node value exists in Next, adds 1 to the count, and progresses to the next value.))
/// </summary>
/// <returns></returns>
public int GetLength()
{
//Node<T> pointer = head;
//int counter = 0;
//while (pointer != null)
//{
// counter += 1;
// pointer = pointer.Next;
//}
//return counter;
return Count;
}
/// <summary>
/// Accepts the Object to place in one of the nodes, as well as an Int to indicate which node to update. The node location must be valid - no error catching is currently implemented
/// </summary>
/// <param name="value"></param>
/// <param name="index"></param>
public void UpdateValue(T value, int index)
{
int clicker = 0;
var nodeSelector = new Node<T>();
nodeSelector = head;
if (index <= GetLength())
{
while (clicker < index)
{
clicker += 1;
nodeSelector = nodeSelector.Next;
}
nodeSelector.Value = value;
}
else
{
Console.WriteLine("I'm sorry, the list is not long enough to insert {0} at index {1}", value, index);
}
}
/// <summary>
/// Requires index to be valid. No error handling is currently implemented. Returns the value stored in the index-referenced node.
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public T Get(int index)
{
int clicker = 0;
var nodeSelector = new Node<T>();
nodeSelector = head;
while (clicker < index)
{
clicker += 1;
nodeSelector = nodeSelector.Next;
}
return nodeSelector.Value;
}
/// <summary>
/// Returns the node stored in head for test purposes.
/// </summary>
/// <returns></returns>
public Node<T> TestGetter()
{
return head;
}
public bool Any() { return !(head == null); }
/// <summary>
/// If this node is the first node of the list, sets lastNode and head to both point to this node. Increments Count.
/// If this node is an addition to a list, sets the former head.Previous to this node. Sets the Next node as the former head. Points head at this node. Increments Count.
/// </summary>
/// <param name="input"></param>
public void AddToHead(T input)
{
var newHead = new Node<T>();
newHead.Value = input;
if (head != null)
{
head.Previous = newHead;
newHead.Next = head;
}
if (lastNode == null)
{
lastNode = newHead;
}
head = newHead;
Count += 1;
}
public override string ToString()
{
var items = new string[GetLength()];
for (int i = 0; i < items.Length; i++)
{
items[i] = Get(i).ToString();
}
return string.Format("List({0})", string.Join(",", items));
}
}
//static void While(Func<bool> test, Action body)
//{
// if (test())
// {
// body();
// While(test, body);
// }
//}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment