From 236314abe7c49917576d2b799ce6a8a7deaa1a8c Mon Sep 17 00:00:00 2001 From: Mina Asham Date: Sun, 3 May 2015 22:04:42 +0100 Subject: [PATCH] Adding Stack and Queue to algs4 (List based) --- algs4/algs4.csproj | 2 + algs4/algs4/Queue.cs | 213 +++++++++++++++++++++++++++++++++++++++++++ algs4/algs4/Stack.cs | 200 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 415 insertions(+) create mode 100644 algs4/algs4/Queue.cs create mode 100644 algs4/algs4/Stack.cs diff --git a/algs4/algs4.csproj b/algs4/algs4.csproj index 5c09430..7018714 100644 --- a/algs4/algs4.csproj +++ b/algs4/algs4.csproj @@ -65,9 +65,11 @@ + + diff --git a/algs4/algs4/Queue.cs b/algs4/algs4/Queue.cs new file mode 100644 index 0000000..a21fb3a --- /dev/null +++ b/algs4/algs4/Queue.cs @@ -0,0 +1,213 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Text; +using algs4.stdlib; + +namespace algs4.algs4 +{ + public class Queue : IEnumerable + { + /// + /// Number of elements on queue + /// + private int _n; + + /// + /// Beginning of queue + /// + private Node _first; + + /// + /// End of queue + /// + private Node _last; + + /// + /// Helper linked list class + /// + /// + private class Node + { + public TItemInner Item { get; set; } + public Node Next { get; set; } + } + + /// + /// Initializes an empty queue. + /// + public Queue() + { + _first = null; + _last = null; + _n = 0; + } + + /// + /// Is this queue empty? + /// + /// true if this queue is empty; false otherwise + public bool IsEmpty() + { + return _first == null; + } + + /// + /// Returns the number of items in this queue. + /// + /// the number of items in this queue + public int Size() + { + return _n; + } + + /// + /// Returns the item least recently added to this queue. + /// + /// the item least recently added to this queue + public TItem Peek() + { + if (IsEmpty()) + { + throw new InvalidOperationException("Queue underflow"); + } + return _first.Item; + } + + /// + /// Adds the item to this queue. + /// + /// the item to add + public void Enqueue(TItem item) + { + Node oldlast = _last; + _last = new Node(); + _last.Item = item; + _last.Next = null; + if (IsEmpty()) + { + _first = _last; + } + else + { + oldlast.Next = _last; + } + _n++; + } + + /// + /// Removes and returns the item on this queue that was least recently added. + /// + /// the item on this queue that was least recently added + public TItem Dequeue() + { + if (IsEmpty()) + { + throw new InvalidOperationException("Queue underflow"); + } + TItem item = _first.Item; + _first = _first.Next; + _n--; + if (IsEmpty()) + { + // to avoid loitering + _last = null; + } + return item; + } + + /// + /// Returns a string representation of this queue. + /// + /// the sequence of items in FIFO order, separated by spaces + public override string ToString() + { + StringBuilder s = new StringBuilder(); + foreach (TItem item in this) + { + s.Append(item + " "); + } + return s.ToString(); + } + + /// + /// Returns an iterator that iterates over the items in this queue in FIFO order. + /// + /// an iterator that iterates over the items in this queue in FIFO order + public IEnumerator GetEnumerator() + { + return new ListIterator(_first); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + + /// + /// An iterator + /// + /// + private class ListIterator : IEnumerator + { + private readonly Node _first; + private Node _current; + + public TItemInner Current { get; private set; } + + object IEnumerator.Current + { + get { return Current; } + } + + public ListIterator(Node first) + { + _first = first; + Reset(); + } + + public bool MoveNext() + { + bool hasNext = _current != null; + if (hasNext) + { + Current = _current.Item; + _current = _current.Next; + } + + return hasNext; + } + + public void Reset() + { + _current = _first; + } + public void Dispose() + { + _current = null; + } + } + + /// + /// Unit tests the Queue data type. + /// + /// + public static void RunMain(string[] args) + { + Queue q = new Queue(); + while (!StdIn.IsEmpty()) + { + string item = StdIn.ReadString(); + if (item != "-") + { + q.Enqueue(item); + } + else if (!q.IsEmpty()) + { + StdOut.Print(q.Dequeue() + " "); + } + } + StdOut.PrintLn("(" + q.Size() + " left on queue)"); + } + } +} diff --git a/algs4/algs4/Stack.cs b/algs4/algs4/Stack.cs new file mode 100644 index 0000000..6857626 --- /dev/null +++ b/algs4/algs4/Stack.cs @@ -0,0 +1,200 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Text; +using algs4.stdlib; + +namespace algs4.algs4 +{ + public class Stack : IEnumerable + { + /// + /// Size of the stack + /// + private int _n; + + /// + /// Top of stack + /// + private Node _first; + + /// + /// Helper linked list class + /// + /// + private class Node + { + public TItemInner Item { get; set; } + public Node Next { get; set; } + } + + /// + /// Initializes an empty stack. + /// + public Stack() + { + _first = null; + _n = 0; + } + + /// + /// Is this stack empty? + /// + /// true if this stack is empty; false otherwise + public bool IsEmpty() + { + return _first == null; + } + + /// + /// Returns the number of items in the stack. + /// + /// the number of items in the stack + public int Size() + { + return _n; + } + + /// + /// Adds the item to this stack. + /// + /// item the item to add + public void Push(TItem item) + { + Node oldfirst = _first; + _first = new Node(); + _first.Item = item; + _first.Next = oldfirst; + _n++; + } + + /// + /// Removes and returns the item most recently added to this stack. + /// + /// the item most recently added + public TItem Pop() + { + if (IsEmpty()) + { + throw new InvalidOperationException("Stack underflow"); + } + + // save item to return + TItem item = _first.Item; + + // delete first node + _first = _first.Next; + _n--; + + // return the saved item + return item; + } + + /// + /// Returns (but does not remove) the item most recently added to this stack. + /// + /// the item most recently added to this stack + public TItem Peek() + { + if (IsEmpty()) + { + throw new InvalidOperationException("Stack underflow"); + } + return _first.Item; + } + + /// + /// Returns a string representation of this stack. + /// + /// the sequence of items in the stack in LIFO order, separated by spaces + public override string ToString() + { + StringBuilder s = new StringBuilder(); + foreach (TItem item in this) + { + s.Append(item + " "); + } + return s.ToString(); + } + + /// + /// Returns an iterator to this stack that iterates through the items in LIFO order. + /// + /// an iterator to this stack that iterates through the items in LIFO order. + public IEnumerator GetEnumerator() + { + return new ListIterator(_first); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + + /// + /// An iterator + /// + /// + private class ListIterator : IEnumerator + { + private readonly Node _first; + private Node _current; + + public TItemInner Current { get; private set; } + + object IEnumerator.Current + { + get { return Current; } + } + + public ListIterator(Node first) + { + _first = first; + Reset(); + } + + public bool MoveNext() + { + bool hasNext = _current != null; + if (hasNext) + { + Current = _current.Item; + _current = _current.Next; + } + + return hasNext; + } + + public void Reset() + { + _current = _first; + } + public void Dispose() + { + _current = null; + } + } + + /// + /// Unit tests the Stack data type. + /// + /// + public static void RunMain(string[] args) + { + Stack s = new Stack(); + while (!StdIn.IsEmpty()) + { + string item = StdIn.ReadString(); + if (item != "-") + { + s.Push(item); + } + else if (!s.IsEmpty()) + { + StdOut.Print(s.Pop() + " "); + } + } + StdOut.PrintLn("(" + s.Size() + " left on stack)"); + } + } +}