Code Fragment: LinkedStack3



template <typename Object>			// copy from stack ls
void LinkedStack<Object>::copyFrom(const LinkedStack& ls) {
  tp = NULL;
  NodePtr p      = ls.tp;			// p is current node in ls
  NodePtr prev   = NULL;
  while (p != NULL) {
    NodePtr v = new Node(p->element, NULL);	// create copy of p
    if (tp == NULL) tp = v;			// if first node
    else prev->next = v;			// else link after prev
    prev  = v;
    p     = p->next;
  }
  sz = ls.sz;
}