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 that could be used as internal storage for a Stack. Any help?
Asked
Active
Viewed 426 times
3 Answers
0
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.

Community
- 1
- 1

Mark Byers
- 811,555
- 193
- 1,581
- 1,452
-
I realize now that the question is tagged with C#. However, from the data structure point of view a Linked List is a List. – Woot4Moo Oct 20 '10 at 20:51
0
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.

Sauleil
- 2,573
- 1
- 24
- 27