The Linked List ADT extends the notion of array by storing a sequence of objects. It uses an array to store the elements of linear list.
You will implement the singly linked list with the sentinel nodes.
A singly linked list with a sentinel and three nodes, the node marked S being the sentinel node. see image.
The empty singly linked list with a sentinel, the node marked S being the sentinel node. see image.
Runtime:
The run time of each member function is specified in parentheses at the end of the description.
UML Class Diagram
SinglyLinkedNode |
-element:Type -next_node:SinglyLinkedNode |
+SinglyLinkedNode(in_obj:Type = Type(), in n:Single_node = nullptr) +value(): Type +next(): SinglyLinkedNode |
Description
A class which stores an object and a pointer to the next node in a linked list.
Member Variables
The two member variables are:
Member Functions
Constructors
SinglyLinkedNode( const Type &, SinglyLinkedNode * )
This constructor takes two arguments: a constant reference to an Type (by default, a new instance of the type Type) and a pointer to a SinglyLinkedNode (by default nullptr). These are assigned to the member variables, respectively. (O(1))
Destructor
This class uses the default destructor.
Copy Constructor
This class uses the default copy constructor.
Accessors
This class has two accessors, one to get each of the two associated member variables:
Type value() const
SinglyLinkedNode* next() const
Mutators
This class has no member functions which modify the member variables.
Friends
The class SentinelList< Type> is a friend of this class.
UML Class Diagram
Sentinel List |
- list_head:SinglyLinkedNode - list_tail: SinglyLinkedNode - list_size: Integer |
+ SentinelList() + SentinelList(in ls:SentinelList): SentinelList + ~SentinelList() + size(): Integer + empty(): Boolean + front(): Type + back(): Type + head(): SinglyLinkedNode + tail(): SinglyLinkedNode + count(in obj:Type): Integer + swap(inout list:SentinelList) + push_front(in obj:Type) + push_back(in obj:Type) + pop_front(): Type + pop_back(): Type + erase(in obj:Type): Integer |
A skeleton for this class is provided in the source directory in the file SentinelList.h.
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 SinglyLinkedNode< Type> class, see SinglyLinkedNode. 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:
Member Functions
Constructors
SentinelList()
Destructor
~SentinelList()
Copy Constructor
SentinelList(const SentinelList &)
Accessors
This class has seven accessors:
int size() const
bool empty() const
Type front() const
Type back() const
SinglyLinkedNode< Type>* begin() const
SinglyLinkedNode< Type>* end() const
int count( const Type & ) const
Mutators
This class has five mutators:
void swap( SentinelList & )
void push_front( const Type & )
void push_back( Type const & )
Type pop_front();
int erase( const Type & )