Search code examples
c#genericsdata-structureslinked-liststack

How can i implement a pop method for a generic stack without traversing the list


I have a generic stack that I will use in a console application. I need the stack to behave like a .Net stack but with extra functionality. Below is part of the code for the stack

class DynamicGenericStack<T>
{

    private class Node
    {
        public T element { get; private set; }
        public Node Next { get; set; }

        public Node(T element, Node prevNode)
        {
            this.element = element;
            Next = null;
            Previous = null;
            if (prevNode != null) //if this is not the first node
                prevNode.Next = this;
        }
    }

    private Node head, tail;

    public int Count { get; private set; } //Count

    public DynamicStack()
    {
        this.head = null;
        this.tail = null;
        this.Count = 0;
    } 



    public void Push(T item)
    {
        if (this.head == null)
            this.head = this.tail = new Node(item, null);
        else
            tail = new Node(item, tail);
        Count++;
    } Push

    public T Peek()
    {
        return tail.element;
    } Peek

    public T Pop()
    {
        if (Count > 0)
        {
            T item = tail.element;
            Node prev = null, current = head;
            while (current != tail)
            {
                prev = current;
                current = current.Next;
            }
            tail = prev;
            Count--;
            return item;

        }
        return default(T);
    }

}

How can I implement the pop method above to remove the tail node without having to traverse the entire list as I did in the above code using the while loop? Is there any way in which track the tail node and remove it directly?


Solution

  • A single linked list is good enough for a stack, and only a head reference is needed.

    To push onto the stack:

        create new_node
        new_node.next = head;
        head = new_node;
        count++;
    

    To pop from the stack:

        if(count == 0)
            return null;
        tmp_node = head;
        head = head.next;
        count--;
        return tmp_node.element;