📄 list_linked.h
字号:
template <class Node_entry>struct Node
// Node declaration:
{ // data members
Node_entry entry;
Node<Node_entry> *next;
// constructors
Node( );
Node(Node_entry, Node<Node_entry> *link=NULL);
};
template <class List_entry>class List
// List declaration:
{ public:
// Specifications for the methods of the list ADT go here.
// The following methods replace compiler-generated defaults.
~List( );
List(const List<List_entry> ©);
void operator=(const List<List_entry> ©);
protected:
// Data members for the linked list implementation now follow.
int count;
Node<List_entry> *head;
// The following auxiliary function is used to locate list positions
Node<List_entry> *set position(int position) const;
};
template <class List_entry>
Node<List_entry> *List<List_entry>::set_position(int position) const
/* Pre: position is a valid position in the List ;0<=position<count .
Post: Returns a pointer to the Node in position . */
{
Node<List_entry> *q = head;
for (int i=0; i<position; i++) q=q->next;
return q;
}
template <class List_entry>
Error_code List<List_entry>::insert(int position, const List_entry &x)
/* Post: If the List is not full and 0<=position<n , where n is the number of
entries in theList , the function succeeds: Any entry formerly atposition and
all later entries have their position numbers increased by 1, andx is inserted at
position of theList .
Else: The function fails with a diagnostic error code. */
{
if (position < 0 || position > count)
return range error;
Node<List_entry> *new node, *previous, *following;
if (position > 0) {
previous = set position(position . 1);
following = previous->next;
}
else following = head;
new_node = new Node<List_entry>(x, following);
if (new_node == NULL)
return overflow;
if (position == 0)
head = new_node;
else
previous->next = new_node;
count++;
return success;
}
template <class List_entry>
Error_code List<List_entry> :: insert(int position, const List_entry &x)
/* Post: If the List is not full and 0<=position<=n , where n is the number of
entries in the List, the function succeeds: Any entry formerly at position and
all later entries have their position numbers increased by 1, andx is inserted at
position of the List.
Else: The function fails with a diagnostic error_code. */
{
if (position < 0 || position > count)
return range error;
Node<List_entry> *new node, *previous, *following;
if (position > 0) {
previous = set position(position . 1);
following = previous->next;
}
else following = head;
new_node = new Node<List_entry>(x, following);
if(new_node==NULL)return overflow;
if (position==0)head = new_node;
else
previous->next = new_node;
count++;
return success;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -