public void StackTest() { var stack = new LinkedListStack <int>(); //insert 1 to 10 into stack for (int i = 1; i <= 10; i++) { stack.Push(i); } //size should be 10 Assert.Equal(10, stack.Size()); //pop items and assert to ensure FIFO for (int i = 10; i > 0; i--) { var value = stack.Pop(); Assert.Equal(i, value); } //size should be 0 Assert.Equal(0, stack.Size()); //poping on empty stack should throw an exception Assert.Throws <InvalidOperationException>(() => stack.Pop()); }
public void LinkedListStackTest() { var stack = new LinkedListStack <int>(); stack.Push(1); stack.Push(2); Assert.IsTrue(stack.ItemCount == 2); Assert.IsTrue(stack.IsEmpty() == false); Assert.IsTrue(stack.Peek() == 2); Assert.IsTrue(stack.Pop() == 2); Assert.IsTrue(stack.ItemCount == 1); stack.Pop(); Assert.IsTrue(stack.IsEmpty()); for (int i = 0; i < 1000; i++) { stack.Push(i); } Assert.IsTrue(stack.ItemCount == 1000); for (int i = 0; i < 1000; i++) { stack.Pop(); } Assert.IsTrue(stack.ItemCount == 0); }
public void Pop_popOnly1Item_expectTrue() { _teStack.Push("wo"); Assert.AreEqual(_teStack.Peek(), "wo"); Assert.AreEqual(_teStack.Count(), 1); Assert.AreEqual(_teStack.Pop(), "wo"); Assert.AreEqual(_teStack.Count(), 0); }
public void LinkedListStackIsEnptyWhenAllElementsAreRemoved() { var sut = new LinkedListStack <int>(); sut.Push(1); sut.Push(2); Assert.Equal(2, sut.Pop()); Assert.Equal(1, sut.Pop()); Assert.True(sut.IsEmpty); }
public void LinkedListStackIncreasesCapacityOnDemand() { var sut = new LinkedListStack <int>(); sut.Push(1); sut.Push(2); Assert.Equal(2, sut.Pop()); Assert.Equal(1, sut.Pop()); Assert.True(sut.IsEmpty); }
public void AddFiveItemsAndVerify() { int linkedListStackLast = 0; int arrayStackLast = 0; for (int i = 1; i <= 5; i++) { linkedListStack.Push(i); arrayStack.Push(i); } Assert.AreEqual(linkedListStack.Peek(), 5, "invalid top element"); Assert.AreEqual(linkedListStack.Count, 5, "invalid count"); Assert.AreEqual(arrayStack.Peek(), 5, "invalid top element"); Assert.AreEqual(arrayStack.Count, 5, "invalid count"); while (linkedListStack.Count > 0) { linkedListStackLast = linkedListStack.Pop(); arrayStackLast = arrayStack.Pop(); } Assert.AreEqual(linkedListStackLast, 1, "invalid last element"); Assert.AreEqual(arrayStackLast, 1, "invalid last element"); }
public void LinkedLinkStack() { var stack = new LinkedListStack <string>(); stack.Push("turtles"); stack.Push("all"); stack.Push("the"); stack.Push("way"); stack.Push("down"); Assert.AreEqual("down", stack.Pop()); Assert.AreEqual("way", stack.Pop()); Assert.AreEqual("the", stack.Pop()); Assert.AreEqual("all", stack.Pop()); Assert.AreEqual("turtles", stack.Pop()); }
public static void LinkedListStack() { LinkedListStack <int> linkedListStack = new LinkedListStack <int>(); linkedListStack.Push(10); linkedListStack.Push(9); linkedListStack.Push(8); linkedListStack.Push(7); linkedListStack.Push(6); linkedListStack.Push(5); linkedListStack.PrintAll(); linkedListStack.Pop(); linkedListStack.Pop(); linkedListStack.PrintAll(); Console.Read(); }
public void LinkedListStackWorksInLifoOrder() { var sut = new LinkedListStack <int>(); sut.Push(1); sut.Push(2); Assert.Equal(2, sut.Pop()); }
public static void Main(string[] args) { var llStack = new LinkedListStack(); llStack.Print(); llStack.Pop(); llStack.Push(30); llStack.Push(20); llStack.Print(); llStack.Push(50); llStack.Push(120); llStack.Print(); llStack.Pop(); llStack.Pop(); llStack.Print(); llStack.Search(20); }
public void LinkedListStackPushTest() { for (var i = 0; i < Elements - 1; i++) { _linkedListStackStack.Pop(); } Assert.AreEqual(0, _linkedListStackStack.Peek()); }
public void Peek_PushTwoItemsAndPop_ReturnsHeadElement() { var stack = new LinkedListStack <int>(); stack.Push(1); stack.Push(2); stack.Pop(); Assert.AreEqual(1, stack.Peek()); }
public decimal EvaluatePostfix(string input) { LinkedListStack <decimal> stack = new LinkedListStack <decimal>(); foreach (var i in input) { if (IsOperator(i)) { decimal result; var right = decimal.Parse(stack.Pop().ToString()); var left = decimal.Parse(stack.Pop().ToString()); switch (i) { case '+': result = left + right; break; case '-': result = left - right; break; case '*': result = left * right; break; case '/': result = left / right; break; default: result = 0; break; } stack.Push(result); } else { stack.Push(decimal.Parse(i.ToString())); } } return(stack.First()); }
private void PrepareOutStack() { CheckUnderflow(); if (_ouStack.IsEmpty) { while (!_inStack.IsEmpty) { _ouStack.Push(_inStack.Pop()); } } }
static void Main(string[] args) { var arrayStack = new ArrayStack <string>(); arrayStack.Push("first"); arrayStack.Push("second"); arrayStack.Push("third"); arrayStack.Push("forth"); arrayStack.Pop(); arrayStack.Pop(); arrayStack.Pop(); var linkedListStack = new LinkedListStack <string>(); linkedListStack.Push("first"); linkedListStack.Push("second"); linkedListStack.Push("third"); linkedListStack.Push("forth"); linkedListStack.Pop(); linkedListStack.Pop(); linkedListStack.Pop(); }
private string ShuntingYard(string input) { LinkedListStack <char> operators = new LinkedListStack <char>(); Queue <char> output = new LinkedListQueue <char>(); foreach (var i in input) { if (Char.IsNumber(i)) { output.Enqueue(i); } if (IsOperator(i)) { while (!operators.IsEmpty() && OperatorPrecedence(operators.Peek()) >= OperatorPrecedence(i)) { output.Enqueue(operators.Pop()); } operators.Push(i); } if (i == '(') { operators.Push(i); } if (i == ')') { while (operators.Peek() != '(') { output.Enqueue(operators.Pop()); } operators.Pop(); } } while (!operators.IsEmpty()) { output.Enqueue(operators.Pop()); } return(new string(output.ToArray())); }
public void TestLinkedListStack() { var stack = new LinkedListStack <int>(); for (var i = 0; i < 5; i++) { stack.Push(i); Console.WriteLine(stack); } stack.Pop(); Console.WriteLine(stack); }
/// <summary> /// Begins asynchronous processing of a single <see cref="T:MySpace.DataRelay.RelayMessage"/>. /// </summary> /// <param name="message">The <see cref="T:MySpace.DataRelay.RelayMessage"/>.</param> /// <param name="state">Callers can put any state they like here.</param> /// <param name="callback">The method to call upon completion.</param> /// <returns> /// Returns an <see cref="T:System.IAsyncResult"/>. /// </returns> public virtual IAsyncResult BeginHandleMessage(RelayMessage message, object state, AsyncCallback callback) { if (!message.IsTwoWayMessage) { // cheat for now and just handle in messages synchronously // as long as the type doesn't use sync in messages then // we won't block on IO anyway. HandleMessage(message); return(SynchronousAsyncResult.CreateAndComplete(callback, state)); } LinkedListStack <Node> nodes = PrepareMessage(message); Node node; if (nodes.Pop(out node)) { var result = new AsynchronousResult <Node>((ar, n, m) => { try { n.EndHandleOutMessage(ar); RetryHandleMessageOnError(message, node); } catch (Exception ex) { log.Error(ex); } }, node, message); var origCallback = callback; if (callback != null) { callback = ar => { result.InnerResult = ar; origCallback(result); }; } result.InnerResult = node.BeginHandleOutMessage(message, callback, state); return(result); } message.SetError(RelayErrorType.NoNodesAvailable); return(SynchronousAsyncResult.CreateAndComplete(callback, state)); }
public void LinkedListStack() { Random random = new Random(); LinkedListStack <string> Stacklist = new LinkedListStack <string>(); for (int i = 0; i < CountItems; i++) { Stacklist.Push($"{i.ToString()}"); } for (int i = 0; i < CountItems; i++) { if (random.Next(i - RndMax, i + RndMin) == i) { Stacklist.Pop(); } } }
internal void Add(RelayMessage message, LinkedListStack <Node> nodes) { TypeSetting typeSetting = NodeManager.Instance.Config.TypeSettings.TypeSettingCollection[message.TypeId]; Node node; while (nodes.Pop(out node)) { bool typesettingThrowOnSyncFailure = false; bool typesettingSyncInMessages = false; if (null != typeSetting && !node.NodeCluster.MeInThisCluster) { typesettingSyncInMessages = typeSetting.SyncInMessages; typesettingThrowOnSyncFailure = typeSetting.ThrowOnSyncFailure; } // Contains no longer works now that the NodeWithMessages structure has // been added, loop through and check the settings bool bAdded = false; foreach (NodeWithMessages nwm in this) { NodeWithInfo nwi = GetKeyForItem(nwm); if ((nwi.Node == node) && (nwi.SyncInMessages == typesettingSyncInMessages) && (nwi.SkipErrorQueueForSync == typesettingThrowOnSyncFailure)) { bAdded = true; this[nwi].Messages.Add(message); break; } } if (!bAdded) { NodeWithInfo newNWI = new NodeWithInfo(node, typesettingSyncInMessages, typesettingThrowOnSyncFailure); NodeWithMessages newNode = new NodeWithMessages(newNWI); Add(newNode); this[newNWI].Messages.Add(message); } } }
static void Main(string[] args) { LinkedList list = new LinkedList(); LinkedListStack stack = new LinkedListStack(); LinkedListQueue queue = new LinkedListQueue(); list.Add(56); list.Add(70); list.Add(90); list.Add(12); list.Add(30); list.Add(60); list.AddInReverseOrder(22); list.AddInReverseOrder(44); list.InsertAtParticularPosition(2, 12); list.Display(); list.Total(); list.DeleteFirst(); list.DeleteLast(); list.SearchValueInList(90); stack.Push(10); stack.Push(20); stack.Push(30); stack.Push(40); stack.Push(50); stack.Display(); stack.Peek(); stack.Pop(); stack.IsEmpty(); queue.Enqueue(12); queue.Enqueue(24); queue.Enqueue(36); queue.Enqueue(48); queue.Dequeue(); queue.Display(); Console.ReadKey(); }
private string InsertParens(string input) { LinkedListStack <string> stack = new LinkedListStack <string>(); foreach (var i in input) { if (i.Equals(')')) { string expression = ")"; for (var j = 0; j < 3; j++) { expression = stack.Pop() + expression; } expression = "(" + expression; stack.Push(expression); } else { stack.Push(i.ToString()); } } return(stack.First()); }
public void PopFromEmptyStackThrowsAnException() { var sut = new LinkedListStack <int>(); Assert.Throws <ArgumentException>(() => sut.Pop()); }
/// <summary> /// Splits messages into various lists of in and out message destined for different nodes. /// </summary> /// <param name="messages"></param> /// <returns></returns> internal NodeWithMessagesCollection DistributeMessages(IList <RelayMessage> messages) { NodeWithMessagesCollection distribution = new NodeWithMessagesCollection(); RelayMessage message; Node node; for (int i = 0; i < messages.Count; i++) { if (messages[i] != null) { message = messages[i]; RelayMessage interZoneMessage = null; LinkedListStack <Node> nodesForMessage = GetNodesForMessage(message); LinkedListStack <Node> nodesForInterZoneMessage = null; if (nodesForMessage.Count > 0) { message.AddAddressToHistory(MyIpAddress); } message.RelayTTL--; #region Identify InterZone Messages if (message.IsTwoWayMessage == false) { message.ResultOutcome = RelayOutcome.Queued; //will be queued, if sync will not get overwritten // Identify nodes in foreign zones int nodeCount = nodesForMessage.Count; for (int nodeIndex = 0; nodeIndex < nodeCount; nodeIndex++) { nodesForMessage.Pop(out node); if (_myNodeDefinition != null && _myZone != node.Zone) { // Message needs to cross Zone bounderies if (interZoneMessage == null) { interZoneMessage = RelayMessage.CreateInterZoneMessageFrom(message); nodesForInterZoneMessage = new LinkedListStack <Node>(); } nodesForInterZoneMessage.Push(node); } else { nodesForMessage.Push(node); } } } #endregion if (nodesForMessage.Count > 0) { DebugWriter.WriteDebugInfo(message, nodesForMessage); distribution.Add(message, nodesForMessage); } if (nodesForInterZoneMessage != null && nodesForInterZoneMessage.Count > 0) { DebugWriter.WriteDebugInfo(interZoneMessage, nodesForInterZoneMessage); distribution.Add(interZoneMessage, nodesForInterZoneMessage); } } } return(distribution); }
/// <summary> /// Performs processing on single message /// </summary> /// <exception cref="SyncRelayOperationException"> /// When the type of an object is defined with settings /// <see cref="MySpace.DataRelay.Common.Schemas.TypeSettings"></see> with /// SyncInMessages=true and /// ThrowOnSyncFailure=true /// failed "in" executions will throw this exception /// </exception> /// <param name="message">Message to be processed</param> public virtual void HandleMessage(RelayMessage message) { Node node; if (message.IsTwoWayMessage) { if (PrepareMessage(message).Pop(out node)) { node.HandleOutMessage(message); } else { message.SetError(RelayErrorType.NoNodesAvailable); } RetryHandleMessageOnError(message, node); } else { LinkedListStack <Node> nodes = PrepareMessage(message); SerializedRelayMessage serializedMessage = new SerializedRelayMessage(message); SerializedRelayMessage serializedMessageInterZone = null; bool messageHandled = true; // start with "true" so that we do not pop // if there are no items in "nodes" if (nodes.Count == 0) { message.SetError(RelayErrorType.NoNodesAvailable); } else { while (nodes.Pop(out node)) { TypeSetting typeSetting = NodeManager.Instance.Config.TypeSettings.TypeSettingCollection[message.TypeId]; bool typesettingThrowOnSyncFailure = false; bool typesettingSyncInMessages = false; if (null != typeSetting && !node.NodeCluster.MeInThisCluster) { typesettingSyncInMessages = typeSetting.SyncInMessages; typesettingThrowOnSyncFailure = typeSetting.ThrowOnSyncFailure; } if (_myNodeDefinition != null && _myZone != node.Zone) { // Message needs to cross Zone bounderies if (serializedMessageInterZone == null) { serializedMessageInterZone = new SerializedRelayMessage(RelayMessage.CreateInterZoneMessageFrom(message)); } if (message.ResultOutcome == null) { message.ResultOutcome = RelayOutcome.Queued; } node.HandleInMessage(serializedMessageInterZone); } else if (typesettingSyncInMessages) { messageHandled = node.HandleInMessageSync(message, true, typesettingThrowOnSyncFailure); } else { if (message.ResultOutcome == null) { message.ResultOutcome = RelayOutcome.Queued; } node.HandleInMessage(serializedMessage); } if (!messageHandled) { throw new SyncRelayOperationException(string.Format("Node {0} failed to process message {1}\r\n", node, message)); } } } } }
public T Pop() { return(_stack.Pop().Data); }