开发者

Name three strategies for internal data storage when implementing a Stack ADT

开发者 https://www.devze.com 2023-01-21 09:51 出处:网络
This was a question my Data Structures teacher put on our recent test.I immediately thought of a List and an Array but I cannot for the life of me think of a third ADT t开发者_StackOverflow社区hat cou

This was a question my Data Structures teacher put on our recent test. I immediately thought of a List and an Array but I cannot for the life of me think of a third ADT t开发者_StackOverflow社区hat could be used as internal storage for a Stack. Any help?


List,Array,Tree,Graph


A linked list is a third option.

class MyStack<T>
{
    LinkedList<T> linkedList = new LinkedList<T>();

    public void Push(T t)
    {
        linkedList.AddFirst(t);
    }

    public T Pop()
    {
        T result = linkedList.First.Value;
        linkedList.RemoveFirst();
        return result;
    }
}

It's also possible (but not generally useful) to implement a stack using two queues.


I think there is only 2 possible way to implement a queue:

  • array
  • linked list

A third way would probably be a mix of the 2:

  • linked list of arrays.
0

精彩评论

暂无评论...
验证码 换一张
取 消