📄 linkstack.h
字号:
struct Node
{ Node_entry entry; // data members
Node *next;
Node( ){ next=NULL; } // constructors
Node(Node_entry item, Node *add_on = NULL); // constructors
};
class Stack
{ public:
// Standard Stack methods
Stack( );
bool empty( ) const;
Error_code push(const Stack entry &item);
Error_code pop( );
Error_code top(Stack_entry &item) const;
// Safety features for linked structures
~Stack( );
Stack(const Stack &original);
void operator = (const Stack &original);
protected:
Node *top_node;
};
Stack::Stack( ) // Constructor
{ top_node = NULL; }
bool Stack::empty( )
{ return top_node==NULL; }
Error_code Stack::push(const Stack_entry &item)
/* Post: Stack_entry item is added to the top_of the Stack; returns success or
returns a code of overflow if dynamic memory is exhausted. */
{ Node *new_top = new Node(item, top_node);
if (new_top==NULL) return overflow;
top_node = new top;
return success;
}
Error_code Stack::top(Stack_entry &item)
{ if(top_node == NULL) return underflow;
item = top_node->data;
}
Error_code Stack::pop( )
/* Post: The top_of the Stack is removed. If the Stack is empty
the method returns underflow; otherwise it returns
success . */
{ Node *old_top = top_node;
if(top_node == NULL) return underflow;
top_node = old_top->next;
delete old_top;
return success;
}
Stack::~Stack( ) // Destructor
// Post: The Stack is cleared.
{ while (!empty( )) pop( ); }
void Stack :: operator=(const Stack &original)
// Post: The Stack is reset as a copy of Stack original .
{ Node *new_top, *new_copy, *original_node = original.top_node;
if (original_node == NULL) new_top = NULL ;
else // Duplicate the linked nodes
{ new_copy = new_top = new Node(original_node->entry);
while (original_node->next != NULL)
{ original_node = original_node->next ;
new_copy->next = new Node(original_node->entry);
new_copy = new_copy->next;
}
}
while (!empty( )) pop( ); // Clean out old Stack entries
top node = new top; // and replace them with new entries.
}
Stack::Stack(const Stack &original) // copy constructor
/* Post: The Stack is initialized as a copy of Stack original. */
{ Node *new_copy, *original_node = original.top_node;
if (original_node == NULL) top_node = NULL;
else // Duplicate the linked nodes.
{ top_node = new_copy = new Node(original_node->entry);
while (original_node->next!=NULL)
{ original_node = original_node->next;
new_copy->next=new Node(original_node->entry);
new_copy=new_copy->next;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -