Hi,
Let me know if my comments aren't clear enough for you:
// The Stack class exists in the framework under System.Collecti ons
// (and System.Collecti ons.Generic in .NET 2.0) and provides a
// FILO (first-in-last-out) list of elements.
// I can't say for sure whether the managed stack is implemented as a
// singly linked-list, such as in this example, but the behavior is the same.
public class Stack
{
// first is a reference to the first Node in the linked list.
// It is a linked list because Stack doesn't contain a direct reference to any other Node.
// Stack has to use the link of the first Node to access the next Node, and so on.
private Node first = null ;
// Gets whether the Stack contains any nodes since the remainder of the link list
// depends on whether first has been assigned to a non-null value.
public bool Empty {
get {
return (first == null);
}
}
// Removes and returns the object on the top of the stack.
// The next object in the list, if one is available, is moved on top.
public object Pop() {
if (first == null)
// Nothing to be popped (first Node doesn't exist)
throw new Exception("Can' t Pop from an empty Stack.");
else {
// Grab the first Node
object temp = first.Value;
// assign the global variable, "first" to the next Node in the list
// which is only accessible using the link from the current "first" Node.
// Note, first.Next may very well be null, which indicates that the Stack
// is now empty (see Empty property)
first = first.Next;
// return the previous first Node (popped)
return temp;
}
}
// Adds the specified object to the top of the Stack (first Node in the linked list)
public void Push(object o) {
// assign the global variable, "first" a reference to a new Node that represents
// the specified object, making sure to link the current "first" Node to the
// new Node via a constructor parameter. The link is important because it
// defines the relationship between the new Node and the current "first" Node
// within the linked list. The relationship can be viewed as parent --child
// where the new "first" Node becomes the parent and the linked (previous) "first"
// Node becomes the child.
first = new Node(o, first);
}
// Nodes are used to encapsulate objects in the Stack's internal list. They provide
// the linking capability on which the Stack relies. Nodes and objects
// have a one-to-one correlation with each other. Nodes are used internally
// by the Stack class. Actually, it makes sense for this class to either be nested in the
// Stack class and marked private or to be left in-place and marked internal.
class Node
{
// holds the link (reference) to the next Node in the Stack's internal list.
// this field is the physical implementation of the linked list
public Node Next;
// holds a reference to the object that this Node encapsulates for the Stack
// The consumer of the Stack class doesn't need to know about Nodes, just
// the object references that are assigned to the Stack through the Push method
public object Value;
// Constructs a new instance of the Node class and does not link the Next node
// This constructor overload is pointless, in this example (and probably in this pattern).
public Node(object value): this(value, null) {}
// Constructs a new instance of the Node class with the specified object value and
// link (reference) to the next Node in the list. The presence of this constructor allows
// the Stack class to create the linked-list.
public Node(object value, Node next) {
Next = next;
Value = value;
}
}
}
--
Dave Sexton
<gw********@hot mail.comwrote in message news:11******** *************@c 28g2000cwb.goog legroups.com...
Hello I am studying the ECMA specs and was doing wonderfully until just
now. They have just broached the topic of AMM and given some example
code which seems to be demanding a greater understanding from the
reader than the previous examples.
I include the code following, would someone please be kind enough to
comment it as thouroughly as possible so I can understand exactly how
it is operating and what it is doing.
I have used wikipedia to explore nodes and linked lists. And my
understanding at the moment is that a node is a unit of data, and a
linked list is a fundamental data structure.
Most of this code goes over my head so please assume as little
knowledge as possible.
Many Thanks,
GM.
Code:
public class Stack
{
private Node first = null ;
public bool Empty {
get {
return (first == null);
}
}
public object Pop() {
if (first == null)
throw new Exception("Can' t Pop from an empty Stack.");
else {
object temp = first.Value;
first = first.Next;
return temp;
}
}
public void Push(object o) {
first = new Node(o, first);
}
class Node
{
public Node Next;
public object Value;
public Node(object value): this(value, null) {}
public Node(object value, Node next) {
Next = next;
Value = value;
}
}
}