// Constant time. public Node AppendToFront(Node head, int data) { Node n = new Node(data); n.next = head; return n; }
// TODO: Destructor. public void Push(int data) { Node n = new Node(data); this.head = n.AppendToFront(this.head, data); return; }
// Linear time since we're not tracking the tail element. public void AppendToTail(int data) { Node currNode = this; Node endNode = new Node(data); while (currNode.next != null) { currNode = currNode.next; } currNode.next = endNode; }
// Linear time. public Node Find(Node head, int data) { while (head.next != null && head.data != data) { head = head.next; } if (head.data != data) { return null; } return head; }
// Worst case, linear time. public Node Delete(Node head, int data) { Node n = head; if (n.data == data) { return head.next; } while (n.next != null) { if (n.next.data == data) { n.next = n.next.next; return head; } n = n.next; } return head; }
public Stack() { this.head = null; }