/** * Removes the last node from the stack and returns it's data */ public String Pop() { // Check the stack isn't empty if (IsEmpty()) throw new InvalidOperationException(); // Store the popped data String poppedData = tail.Data; // Remove the popped node from the stack if (head == tail) { // The popped node was the only in the stack head = null; tail = null; } else { // The popped data was not the only in the list StringNode nodeWalker = head; while (nodeWalker != null) { if (nodeWalker.Next == tail) { nodeWalker.Next = null; tail = nodeWalker; } nodeWalker = nodeWalker.Next; } } return poppedData; }
public int Count() { int count = 0; StringNode nodeWalker = top; while (nodeWalker != null) { count++; nodeWalker = nodeWalker.Next; } return(count); }
public void Push(string newString) { StringNode newNode = new StringNode(newString); if (top == null) { top = newNode; } else { newNode.Next = top; top = newNode; } }
public String Pop() { try { //get the string from the last added node on stack String returnString = top.nodeString; //remove the top node from the stack top = top.Next; return(returnString); } //if nothing on the stack catch (NullReferenceException) { throw new NullReferenceException("no items in stack"); } }
// Constructor public Stack() { head = null; tail = null; }
/** * Adds a new node the the stack */ public void Push(String newNodeData) { if (IsEmpty()) { // If stack is empty StringNode newNode = new StringNode(newNodeData); head = newNode; tail = newNode; } else { // If stack has already got at least one node tail.Next = new StringNode(newNodeData); tail = tail.Next; } }
// Constructor public StringNode(String data) { this.data = data; next = null; }