# Implementation - push\_front(), pop\_front()

## push\_front(data)

The push\_front function adds a node to the front of the linked list.  As with all insertion of nodes, the process can be generally described in the following manner:

1. create a new node with the appropriate data
2. link the new node up with the rest of the list

Let us start by considering how this would work in a general linked list.  The steps are:

Step 1: Create new node, next node is the current front of list, there is no previous node which should be set to a nullptr

![](https://3939692842-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LFrWzEqLSRHjU6HG9dw%2F-LL5MMY8nw67wXDhuFsd%2F-LL5gxikmq6ODLYTPyAO%2Fpushfront1.png?alt=media\&token=3ea7ada9-c97f-4fcb-b4e2-4f36fe7e767e)



Step 2:  Make the previous node of the current front of list point to the new node

![](https://3939692842-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LFrWzEqLSRHjU6HG9dw%2F-LLFdsDKIotqGCLoyHkm%2F-LLGlOuUxa_OrFwV2gs7%2Fpushfront2.png?alt=media\&token=c221721e-7880-4c2e-b306-1f8e29ee7d50)

Step 3: Make the front pointer point to the new node

![](https://3939692842-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LFrWzEqLSRHjU6HG9dw%2F-LL5MMY8nw67wXDhuFsd%2F-LL5hv8w06YZy66IDELP%2Fpushfront3.png?alt=media\&token=0d655cce-e1ee-47ff-a2f7-1ad2fd214f4c)

Putting this together in code:

```cpp
Node* nn = new Node(data,nullptr,front_);
front_->prev_=nn;
front_=nn;
```

### Does above always work?

Now lets consider if the following would work if we started off with a linked list that is empty.

Step 1:&#x20;

![](https://3939692842-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LFrWzEqLSRHjU6HG9dw%2F-LLFdsDKIotqGCLoyHkm%2F-LLGlGwSvvGgo9EhGekj%2Fpushfront4.png?alt=media\&token=87d89f7c-fa8e-4e48-95c6-0740c49bb13d)

Step one will run without problems

Step 2:

![](https://3939692842-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LFrWzEqLSRHjU6HG9dw%2F-LLFdsDKIotqGCLoyHkm%2F-LLGlt6pHyhUxQJBGVtr%2Fpushfront5.png?alt=media\&token=ff920c27-532c-45dd-9214-edf72e7e2566)

front\_ is nullptr, so front\_->prev\_ will crash.  Thus, it looks like we need to skip this step or do something different if we have an empty list

Step 3:&#x20;

![](https://3939692842-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LFrWzEqLSRHjU6HG9dw%2F-LLFdsDKIotqGCLoyHkm%2F-LLGn1MHsNee-3k17q0y%2Fpushfront6.png?alt=media\&token=d2cebfa1-2efe-41d2-9f22-436c323785de)

The above will work. However, if we were to simply skip step 2, our linked list would not be valid as back\_ would not be correctly set.  The node we just added is not only the first node in the linked list but also the last.  To make this work, we should add code to make back\_ point to nn.&#x20;

Putting all this together, The final function would look like the following:

```cpp
void push_front(const T& data){
    Node* nn = new Node(data,nullptr,front_);
    if(front_){
        front_->prev_=nn;
    }
    else{
        back_=nn;
    }
    front_=nn;
}
```

## pop\_front()

The pop\_front() function removes the first node from the linked list.  The following are the general steps to remove a node:

1. check to make sure that the list isn't empty (or that the node to be removed actually exist).&#x20;
2. unlink the node to be remove from the list (ensure other nodes are not lost in the process)
3. deallocate the memory for the node

Given the above steps, let us consider how this would work in the general case.  The steps for performing a pop\_front are:

Step 1: Check to make sure list isn't empty.  If it is do nothing. Otherwise continue to next steps

Step 2: Make a local pointer point to the first node in the list (hold this node so we don't lose it by accident)

![](https://3939692842-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LFrWzEqLSRHjU6HG9dw%2F-LLFdsDKIotqGCLoyHkm%2F-LLGnWbnJA9YcwaKdYjA%2Fpopfront1.png?alt=media\&token=5f5c854d-a82b-4bee-a3b2-67d5882f9eaa)

Step 3: Make the front pointer point to the second node

![](https://3939692842-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LFrWzEqLSRHjU6HG9dw%2F-LLFdsDKIotqGCLoyHkm%2F-LLGna-t-VkMTZKBMzLx%2Fpopfront2.png?alt=media\&token=e16618f7-3d57-45c4-99c6-075190597e91)

Step 4: Make the new front node's previous pointer a nullptr

![](https://3939692842-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LFrWzEqLSRHjU6HG9dw%2F-LLFdsDKIotqGCLoyHkm%2F-LLGnepgcUkdws2Ueguh%2Fpopfront3.png?alt=media\&token=41a0015f-384d-4fa7-a383-e3ed15ffff20)

Step 5: deallocate the node we are removing

![](https://3939692842-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LFrWzEqLSRHjU6HG9dw%2F-LLFdsDKIotqGCLoyHkm%2F-LLGnkBz4HLhKupdWtGG%2Fpopfront4.png?alt=media\&token=7a00f7ef-cc16-4838-a202-8861bc8d7b7d)

{% hint style="info" %}
_**delete p;**_ does not deallocate the pointer _**p**_ itself.   Rather it deallocates what the pointer points at.  Thus, _**delete rm**_; deallocates the node rm points at.&#x20;
{% endhint %}

The code snippet to do this is:

```cpp
if(front_){
    Node* rm = front_;
    front_=front_->next_;
    front_->prev_=nullptr;
    delete rm;
}
```

{% hint style="success" %}
A common misconception is that when you declare a pointer, you must use new.  Unless you are trying to create an instance of the object, you do not need to use new.  If you are using the pointer to simply refer to something that exist (like rm in the code above), there is no reason to use new and in fact, if you used new, you would end up with a memory leak.
{% endhint %}

### Does the above always work?

The snippet of code above works for the general linked list and empty linked lists.  However, does it always work?  Let us consider the following situation.  If we only had one node left in the list, would the above snippet still work?

If we perform the four steps inside the if statement from the above snippet, this is what we will see:

Step 2:

![](https://3939692842-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LFrWzEqLSRHjU6HG9dw%2F-LLFdsDKIotqGCLoyHkm%2F-LLGoCITmpS-b0YLx4IT%2Fpopfront5.png?alt=media\&token=14b18054-9b7a-4306-ac0a-3dc571ec1b35)

Above looks fine.

Step 3:

![](https://3939692842-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LFrWzEqLSRHjU6HG9dw%2F-LLFdsDKIotqGCLoyHkm%2F-LLGpSrPHL643u_5GDXo%2Fpopfront6.png?alt=media\&token=806969b8-808e-4305-a386-fdf19b8d6a13)

This also looks correct

Step 4:

![](https://3939692842-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LFrWzEqLSRHjU6HG9dw%2F-LLFdsDKIotqGCLoyHkm%2F-LLGpaX3ucD-Bh8N96Qj%2Fpopfront7.png?alt=media\&token=621cefb9-0efb-466d-ad11-31a726b3ffd4)

If we were to try to do the above at this point we would end up crashing the program as front\_ is currently nullptr.  This step will either need to be skipped for lists with just one node or something differnt will need to be done

Step 5:

![](https://3939692842-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LFrWzEqLSRHjU6HG9dw%2F-LLFdsDKIotqGCLoyHkm%2F-LLGppMskr6k95FibkNp%2Fpopfront8.png?alt=media\&token=27be65d2-8655-4850-8937-d7fffa78e11b)

The final line is to delete the node, which won't crash.  However, the list is not in a valid state.  We have a back\_ pointer that points to the memory location of the node that has now been deallocated.  This pointer is therefore invalid.  For lists with just one node, we must also adjust the back\_ pointer to a nullptr as the list is now empty

Putting all this together our pop\_front() function should look something like this:

```cpp
void pop_front(){
    if(front_){
        Node* rm = front_;
        front_=front_->next_;
        if(front_==nullptr){  //if only one node exists
            back_=nullptr;
        }
        else{
            front_->prev_=nullptr;
        }
        delete rm;
    }
}
```

