with C++ Lecture 11
Operations at the Back of the List
1. Pushing and Popping at the Back
o Push and Pop Operations: While previous discussions
focused on operations at the front, operations at the back
provide flexibility. However, they are not always necessary but
can be implemented for enhanced functionality.
2. Push Back Operation
o Steps:
1. Create a new element and assign data to it.
2. Set the 'next' pointer of the new element to nullptr since
it will be the last element.
3. If the list is empty, set 'head' to point at the new
element.
4. Otherwise, traverse the list to find the last element and
set its 'next' pointer to the new element.
Example:
, 2. Pop Back Operation
o Steps:
1. Check if the list is not empty.
2. Traverse the list to find the last and next-to-last
elements.
3. Set the 'next' pointer of the next-to-last element to
nullptr.
4. Delete the last element.
Example:
3. Get Back Operation
o Steps:
1. Check if the list is not empty.
2. Traverse the list to find the last element and return its
data.