Singly linked sentinel list

Requirements

In this sub-project, you will implement two classes:

  1. Singly linked lists with a sentinel: Sentinel_list, and
  2. Singly linked nodes: Single_node.

A singly linked list with a sentinel and three nodes is shown in Figure 1. The empty singly linked list with a sentinel is shown in Figure 2, the node marked S being the sentinel node.

Figure 1. A singly linked list with a sentinel and three nodes.

Figure 2. An empty singly linked list with a sentinel.

Class Specifications

UML Class Diagram

Sentinel_list
- list_head:Single_node
- list_tail:Single_node
- list_size:Integer
+ create():Sentinel_list
+ create( in sl:Sentinel_list ):Sentinel_list
+ size():Integer
+ empty():Boolean
+ front():Type
+ back():Type
+ head():Single_node
+ tail():Single_node
+ count( in obj:Type ):Integer
+ swap( inout list:Sentinel_list )
+ =( in rhs:Sentinel_list ):Sentinel_list
+ push_front( in obj:Type )
+ push_back( in obj:Type )
+ pop_front():Type
+ erase( in obj:Type ):Integer
+ destroy()


Description

This class stores a finite list of n (zero or more) elements stored in singly linked nodes. If there are zero elements in the list, the list is said to be empty. Each element is stored in an instance of the Single_node<Type> class. At all times, the head pointer points to a sentinel node. If the list is empty, the tail pointer points to the sentinel node.

Member Variables

The three member variables are:

  • Two pointers to Single_node<Type> objects, referred to as the head pointer and tail pointer, respectively, and
  • An integer referred to as the list size which equals the number of elements in the list.

Member Functions

Constructors

Sentinel_list()

The constructor creates an instance of a Single_node<Type> (called the sentinel). The value stored in this node is not important, you can use the default value or whatever value you want. The next pointer of the sentinel should be nullptr. The head and tail pointers are set to point at the sentinel. The node count is set to 0. (O(1))

Destructor

The destructor must delete each of the nodes in the list including the sentinel. (O(n))

Copy Constructor

The copy constructor must create a new singly linked list with a copy of all of the nodes within the linked list with the elements stored in the same order. Once a copy is made, any change to the original linked list must not affect the copy. (O(n))

Accessors

This class has seven accessors:

int size() const;
Returns the number of items in the list. (O(1))
bool empty() const;
Returns true if the list is empty, false otherwise. (O(1))
Type front() const;
Retrieves the object stored in the node pointed to by the next pointer of the sentinel. This function throws a underflow if the list is empty. (O(1))
Type back() const;
Retrieves the object stored in the node pointed to by the tail pointer. This function throws a underflow if the list is empty. (O(1))
Single_node<Type> *head() const;
Returns the head pointer. (O(1))
Single_node<Type> *tail() const;
Returns the tail pointer. (O(1))
int count( Type const & ) const;
Returns the number of nodes in the linked list storing a value equal to the argument. (O(n))

Mutators

This class has six mutators:

void swap( Sentinel_list & );
The swap function swaps all the member variables of this linked list with those of the argument. (O(1))
Sentinel_list &operator=( Sentinel_list & );
The assignment operator makes a copy of the argument and then swaps the member variables of this sentinel list with those of the copy. (O(nlhs + nrhs))
void push_front( Type const & );
Creates a new Single_node<Type> storing the argument, the next pointer of which is set to the next pointer of the sentinel. The next pointer of the sentinel is set to the new node. If the list was originally empty, the tail pointer is set to point to the new node. (O(1))
void push_back( Type const & );
Similar to push_front, this places a new node at the back of the list. (O(1))
Type pop_front();
Delete the node at the front of the linked list and, as necessary, update the tail pointer and the next pointer of the sentinel. Return the object stored in the node being popped. Throw an underflow exception if the list is empty. (O(1))
int erase( Type const & );
Delete all nodes (other than the sentinals) in the linked list that contains the object equal to the argument (use == to to test for equality with the retrieved element). As necessary, update the tail pointer and the next pointer of any other node (including possibly the sentinel) within the list. Return the number of nodes that were deleted. (O(n))