Introduction
In the previous posts, I’ve talked about some kinds of abstract data structures, which are some different version of list. In this post, I’ll introduce the List.
Definition
A List of elements of type T is a finite sequence of elements of T together with the following operations:
- Construct the list, leaving it empty.
- Determine whether the list is empty or not.
- Determine whether the list if full or not.
- Find the size of the list.
- Clear the list to make it empty.
- Insert an entry at a specified position of the list.
- Remove an entry from a specified position in the list.
- Retrieve the entry from a specified position in the list.
- Replace the entry at a specified position in the list.
- Traverse the list, performing a given operation on each entry.
Implementation
First I’d like to give the definition of List.(List.hpp) Here I specify the size of list is 100 in brief.
1 | template <class List_entry> |
List()
precondition: None.
postcondition: The List has been created and is initialized to be empty.
1 | template <class List_entry> |
~List()
precondition: None.
postcondition: Clear the List itself.
1 | template <class List_entry> |
List(const List< List_entry > ©)
precondition: None.
postcondition: The List is initialized as a copy of List copy.
1 | template <class List_entry> |
operator=(const List< List_entry > ©)
precondition: None.
postcondition: The List is reset as a copy of List copy.
1 | template <class List_entry> |
clear()
precondition: None.
postcondition: All List entries have been removed; the List is empty.
1 | template <class List_entry> |
empty()
precondition: None.
postcondition: The function returns true or false according to whether the List is empty or not.
1 | template <class List_entry> |
size()
precondition: None.
postcondition: The function returns the number of entries in the List.
1 | template <class List_entry> |
insert(int position, const List_entry &x)
precondition: None.
postcondition: 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, and x is inserted at position in the List. Else: The function fails with a diagnostic error code.
1 | template <class List_entry> |
remove(int position, List_entry &x)
precondition: None.
postcondition: If 0 ≤ position < n, where n is the number of entries in the List, the function succeeds: The entry at position is removed from the List, and all later entries have their position numbers decreased by 1. The parameter x records a copy of the entry formerly at position. Else: The function fails with a diagnostic error code.
1 | template <class List_entry> |
retrieve(int position, List_entry &x)
precondition: None.
postcondition: If 0 ≤ position < n, where n is the number of entries in the List, the function succeeds: The entry at position is copied to x; all List entries remain unchanged. Else: The function fails with a diagnostic error code.
1 | template <class List_entry> |
replace(int position, const List_entry &x)
precondition: None.
postcondition: If 0 ≤ position < n, where n is the number of entries in the List, the function succeeds: The entry at position is replaced by x; all other entries remain unchanged. Else: The function fails with a diagnostic error code.
1 | template <class List_entry> |
traverse(void (*visit)(List_entry &))
precondition: None.
postcondition: The action specified by function *visit has been performed on every entry of the List, beginning at position 0 and doing each in turn.
1 | template <class List_entry> |
So far, I’ve show you the List. List is an important data structure, which is the base of Stack and Queue. Consequently, use List expertly will be helpful for further learing. Thank you!