예제 #1
0
        // Constant time.
        public Node AppendToFront(Node head, int data)
        {
            Node n = new Node(data);
            n.next = head;

            return n;
        }
예제 #2
0
        // TODO: Destructor.
        public void Push(int data)
        {
            Node n = new Node(data);
            this.head = n.AppendToFront(this.head, data);

            return;
        }
예제 #3
0
        // 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;
        }
예제 #4
0
        // 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;
        }
예제 #5
0
        // 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;
        }
예제 #6
0
 public Stack()
 {
     this.head = null;
 }