How to insert a new node in a linked list in C++? (at the front, at the end, after a given node)

CodeBeauty
3 Mar 202130:31

Summary

TLDRThis video tutorial delves into the intricacies of linked lists, demonstrating three distinct methods for inserting nodes: at the front, at the end, and after a specific node. The presenter provides a clear, step-by-step guide through the process, complemented by C++ code examples. Viewers unfamiliar with linked lists are directed to an introductory video, while the current tutorial focuses on practical implementation, ensuring a comprehensive understanding of node insertion techniques.

Takeaways

  • 📘 The video discusses three methods of inserting elements into a linked list: at the front, at the end, and after a specific node.
  • 👨‍🏫 For those unfamiliar with linked lists, the creator suggests watching an earlier video for a foundational understanding and implementation guide using C++.
  • 🔗 The code for a basic linked list implementation in C++ is provided, including the creation of nodes and a 'printList' function to display list contents.
  • 🔑 The 'insertAtFront' function is explained, detailing the steps to add a new node before the current head of the list, which involves creating a new node and adjusting pointers.
  • 📌 To insert at the front, the new node's 'next' pointer is set to the current head, and then the head pointer is updated to the new node, effectively adding it to the front.
  • 🔍 The 'insertAtEnd' function involves checking if the list is empty and then finding the last node before inserting the new node after it, ensuring the list's integrity.
  • 🔄 The process of finding the last node in a non-empty list is demonstrated using a while loop that traverses to the node pointing to null.
  • 📝 The 'insertAfter' function requires a reference to a specific node and a value to insert after it, and it involves checking for null references and updating pointers to maintain the list structure.
  • 🔗 When inserting after a specific node, it's crucial to first set the new node's 'next' pointer to the subsequent node to avoid losing any elements in the list.
  • 🛠️ The video provides practical examples of invoking the 'insertAfter' function to demonstrate how elements are added to the list after a given node.
  • 👍 The video concludes with an invitation for viewers to like, share, and comment on the video, as well as to engage with the creator on social media.

Q & A

  • What are the three different ways to add an element into a linked list as discussed in the video?

    -The video discusses inserting a node at the front of a linked list, at the end of a linked list, and after a specific node within the list.

  • What is the first step in inserting a node at the front of a linked list?

    -The first step is to prepare a new node by allocating memory for it and assigning the new value to the node.

  • How does the 'insert at the front' function update the head of the linked list?

    -The function updates the head of the linked list by making the new node the head, and then making the old head the next node in the list.

  • What is the purpose of the 'print list' function in the script?

    -The 'print list' function is used to traverse and print out all the values stored in the linked list, starting from the head node.

  • What is the significance of the 'head' pointer in a linked list?

    -The 'head' pointer is significant as it points to the first node of the linked list, and is used to traverse the list.

  • How does the video script ensure that the newly added node at the front of the list doesn't lose connection with the rest of the list?

    -The script ensures this by first making the new node point to the current head (thus not losing the connection), and then updating the head pointer to the new node.

  • What are the steps involved in inserting a node at the end of a linked list?

    -The steps are: 1) Prepare a new node, 2) Check if the list is empty (if yes, make the new node the head), 3) Find the last node, and 4) Make the last node point to the new node.

  • Why is it necessary to check if the previous node is null in the 'insert after' function?

    -It is necessary to ensure that a valid node is provided after which the new node will be inserted, as inserting after a null node would not be meaningful.

  • How does the 'insert after' function handle the connection between the previous node and the node following it?

    -The function first stores the address of the node following the previous node in the new node, and then updates the 'next' pointer of the previous node to point to the new node.

  • What happens if the linked list is empty when trying to insert a node at the end?

    -If the list is empty, the new node becomes the head of the list, as well as the last and the only node in the list.

  • Can you explain the process of finding the last node in a linked list as described in the script?

    -The process involves starting at the head node and iteratively moving to the next node until a node is found that points to null, which indicates it is the last node in the list.

Outlines

00:00

📚 Introduction to Linked Lists and Insertion Methods

This paragraph introduces the topic of linked lists and outlines the video's purpose, which is to demonstrate three methods of inserting elements into a linked list: at the front, at the end, and after a specific node. The speaker also refers viewers to a previous video for a basic understanding of linked lists and their implementation in C++ programming language. The provided code snippet is a brief overview of a linked list implementation, including a 'Node' class, a 'printList' function, and a sample linked list with three nodes.

05:01

🔍 Detailed Explanation of Inserting at the Front of a Linked List

The speaker explains the process of inserting a node at the front of a linked list through a function named 'insertAtTheFront'. The function takes two parameters: a pointer to the head node and the value to be inserted. The steps include creating a new node, adjusting pointers to place the new node before the current head, and updating the head pointer. The speaker provides C++ code examples for each step and demonstrates the function's effectiveness by inserting elements with values of -1 and -2 at the front of the list.

10:02

📝 Steps for Inserting a Node at the End of a Linked List

This paragraph details the steps required to insert a node at the end of a linked list within the 'insertAtTheEnd' function. The process involves preparing a new node, checking if the list is empty to determine if the new node should become the head, finding the last node in the list, and then updating the last node's pointer to the new node. The speaker also provides C++ code snippets for each step, illustrating how to traverse the list to find the last node and how to add a new node to the end.

15:04

🔄 Implementing the End Insertion with C++ Code

The speaker implements the 'insertAtTheEnd' function in C++ and tests it by adding nodes with values of 4 and 5 to the end of the list. The function first checks if the list is empty and sets the new node as the head if true. If the list is not empty, it finds the last node using a while loop and updates the last node's 'next' pointer to the new node. The results of running the program confirm that the new nodes are correctly added to the end of the list.

20:05

🔄 Inserting a Node After a Specific Node in a Linked List

The paragraph explains how to insert a node after a specific node within the 'insertAfter' function. It requires two parameters: a pointer to the specific node after which the new node is to be inserted and the value for the new node. The steps include checking for a null previous node, preparing the new node, and then adjusting pointers to insert the new node in the correct position. The speaker emphasizes the importance of maintaining the connection to the subsequent node to avoid losing it and provides C++ code to demonstrate the process.

25:05

📝 Testing the 'insertAfter' Function with C++ Code

The speaker tests the 'insertAfter' function by inserting elements with values of -1 after the head node and -2 after the second node in the list. The function is demonstrated to work correctly, as shown by the updated list output after each insertion. The process involves checking for a null previous node, creating a new node with the specified value, and then reassigning pointers to insert the new node after the given node.

30:05

👋 Conclusion and Call to Action

In the concluding paragraph, the speaker invites viewers to like the video, share it with friends, and ask questions in the comments. They also encourage viewers to tag them on Instagram stories and provide links to their social media accounts in the video description. The speaker thanks the viewers for watching and looks forward to seeing them in the next video.

Mindmap

Keywords

💡Linked List

A linked list is a linear data structure where each element, called a node, contains a reference to the next node in the sequence. It is a dynamic data structure that allows for efficient insertion and deletion of elements without the need to resize the underlying array. In the video, the concept of linked lists is central, as the script discusses various methods of inserting nodes into different positions within a linked list.

💡Node

In the context of data structures, a node represents a single element in a linked list, consisting of data and a reference (or pointer) to the next node. The script explains that each node has two main components: the value it holds and a pointer to the next node, which is crucial for maintaining the sequence of the list.

💡Insertion

Insertion refers to the process of adding a new element or node to a data structure. The video script outlines three distinct methods of insertion in a linked list: at the front, at the end, and after a specific node. This process is fundamental to understanding how elements are dynamically added to a linked list.

💡Head

The head of a linked list is a reference to the first node in the list. It serves as the entry point for accessing all other nodes in the list. The script mentions the head multiple times, particularly when discussing the insertion of nodes at the front of the list, where the new node becomes the new head.

💡Pointer

In computer science, a pointer is a variable that stores the memory address of another variable. In the context of linked lists, pointers are used to link nodes together by storing the address of the next node. The script explains how pointers are used to establish connections between nodes and to navigate through the list.

💡Traversal

Traversal is the act of visiting each node in a data structure, such as a linked list, typically starting from the head node and following the pointers to the next nodes until the end of the list is reached. The script describes a 'print list' function that uses a while loop to traverse the list and print the value of each node.

💡Null

In programming, null is a special value that represents the absence of a value or a null reference. In the context of linked lists, a node's pointer is set to null to indicate the end of the list. The script uses null to signify the last node in the list, which does not point to any further nodes.

💡Function

In programming, a function is a block of code designed to perform a specific task, which can be called with parameters and may return a value. The script introduces several functions, such as 'insert at the front', 'insert at the end', and 'insert after', which encapsulate the logic for inserting nodes into different positions in a linked list.

💡Parameter

A parameter is a variable in a function that acts as an input for that function. The script describes functions with parameters such as 'head', a pointer to the head node of the list, and 'newValue', the value to be inserted into the list, which are essential for the functions to perform their tasks.

💡Memory Allocation

Memory allocation in programming refers to the process of reserving a certain amount of memory for use by a program. The script mentions the creation of new nodes using 'new', which is a way to allocate memory for a new object in many programming languages, including C++.

Highlights

Introduction of three methods to insert elements into a linked list.

Explanation of inserting a node at the front of a linked list.

Description of creating a 'node' class representing elements of a linked list.

Demonstration of linking nodes to form a linked list.

Implementation of a 'print list' function to traverse and display list elements.

Guide on how to prepare a new node for insertion.

Process of updating the head pointer to include a new node at the front.

Testing the 'insert at the front' function with example values.

Introduction of the 'insert at the end' function.

Steps to insert a node when the list is empty.

Traversal method to find the last node of a non-empty list.

Inserting a new node after the last node in the list.

Testing the 'insert at the end' function with additional elements.

Introduction of the 'insert after' function for specific node insertion.

Null check for the 'previous node' to ensure valid insertion.

Detailed steps to insert a node after a given node in the list.

Testing the 'insert after' function with specific node insertion.

Summary of the video's content on inserting nodes in various positions of a linked list.

Transcripts

play00:00

hi everyone and welcome to my channel in

play00:03

this video i want to talk about linked

play00:05

lists and i want to show you three

play00:07

different ways to add an element into a

play00:10

linked list which means to insert a node

play00:13

into a linked list so i want to show you

play00:16

how you can insert a node at the front

play00:19

of a linked list at the end of a linked

play00:21

list and then also how you can insert a

play00:24

node after a specific node so that is

play00:27

what i will be talking about in this

play00:29

video and then if you are not familiar

play00:31

with linked lists at all if you don't

play00:33

know how to implement your own linked

play00:34

list if you don't know what our main

play00:37

characteristic what characteristics what

play00:39

i mean advantages and disadvantages of

play00:41

linked lists i would recommend you to

play00:43

watch my first video related to this

play00:45

topic which i will link here and in the

play00:48

description as well so make sure to

play00:50

watch that video and in that video i

play00:52

showed you how you can implement your

play00:54

own linked list

play00:55

using c plus programming language which

play00:58

is this code here so if you don't

play01:00

understand some of this code here make

play01:02

sure to watch that video and then i will

play01:05

also paste this code in the comment and

play01:08

i will pin that comment so that you can

play01:10

use this code if you want

play01:13

so now i will just go very quickly over

play01:15

this code so that you are familiar with

play01:17

what it is doing but again if you need

play01:19

to understand what every single line of

play01:21

code is doing make sure to watch my

play01:23

first video related to linked lists

play01:26

so here i have created a class called

play01:29

node and that class represents one

play01:32

element of a linked list so one node and

play01:35

that node consists of two parts the

play01:38

first part is value of that node and

play01:40

then second part is a pointer to the

play01:43

next node so that is this class here and

play01:47

then here i have created three nodes

play01:49

actually i have created three pointers

play01:51

to three nodes so first second and third

play01:55

and this head this is actually just a

play01:57

name for the first node so it is head of

play01:59

a linked list which means that it is the

play02:01

first node of a linked list and then

play02:05

here and here and here i have assigned

play02:09

values to first and second and third

play02:13

node and then i have also linked

play02:15

elements of my list

play02:17

so here i have set that my head element

play02:19

is pointing to the second element and

play02:21

then my second element is pointing to

play02:23

the third element and then third

play02:26

considering that it is the last is

play02:28

pointing to null so that means that it

play02:30

is not pointing to anything which means

play02:32

that it is the last element of a linked

play02:34

list and i have also implemented a

play02:37

function called print list which

play02:39

receives head element which means the

play02:42

first element of this list here and if

play02:46

you look at the implementation of print

play02:48

list function you will notice that it

play02:51

uses while loop in order to traverse

play02:54

every single element of that list and in

play02:57

each iteration it is going to write out

play03:00

the value of that specific element so

play03:02

the value of that specific node and then

play03:05

it will move to the next node and it

play03:08

will continue doing that so it will

play03:09

continue moving to the next node until

play03:12

it comes to the node which is equal to

play03:14

null and at that point it will no longer

play03:17

enter into this while loop which means

play03:19

that we will leave this function here

play03:21

and after executing this function here

play03:24

we will successfully print out all of

play03:27

the values that are stored inside our

play03:30

linked list so if i run this program

play03:34

okay

play03:35

you will see that we have one two three

play03:38

which are the elements of our linked

play03:40

list

play03:42

so again if you need to understand this

play03:44

code in more detail make sure to watch

play03:46

my first video related to linked lists

play03:48

it will be in the description and now i

play03:51

want to show you three different ways on

play03:53

how you can insert an element into this

play03:56

list here so the first way to insert an

play04:00

element into a linked list will be to

play04:02

insert it at the front of a linked list

play04:05

so i will do that using a function so

play04:08

let's create that function and let's

play04:11

create it here

play04:12

so it will be of return type void and i

play04:16

will call it insert

play04:18

at the front

play04:21

like this

play04:23

okay

play04:24

and let's collapse this print list

play04:26

function so that i have more space to

play04:28

work now let's see what this insert at

play04:31

the front function is going to receive

play04:33

so what kind of parameters will i pass

play04:36

to this function here well

play04:38

this is how the invocation of this

play04:40

function is going to look like so i'm

play04:43

going to say insert

play04:45

at the front

play04:46

and then what i want to pass to this

play04:49

function here is going to be the address

play04:52

of this head node pointer so i will say

play04:55

please pass the address of my head

play04:58

and then i also want to pass the value

play05:00

that i want to add the value that i want

play05:03

to insert at the front of my list so

play05:06

let's say for example that will be minus

play05:08

1 because this list here

play05:12

so this list here as you can see from my

play05:14

class is storing integer values so here

play05:19

i am passing the address of my head node

play05:21

and then i am passing integer value that

play05:23

will be added at the front of my linked

play05:26

list now this here means that we need to

play05:29

receive these two parameters inside this

play05:32

function here so considering that here i

play05:35

am passing an address of a pointer that

play05:38

means that here i will need to receive a

play05:40

pointer to a pointer so i will need to

play05:43

receive a node pointer to a pointer

play05:46

which i will call head

play05:49

okay so that is the first parameter and

play05:51

then the second parameter is just an

play05:53

integer number so here i will say int

play05:57

and let's call that parameter new value

play06:01

like this

play06:02

okay so these are two parameters that

play06:05

this function needs to receive in order

play06:08

to insert a new node at the front of a

play06:11

linked list so there are three steps

play06:14

that this function here needs to perform

play06:16

in order to do that in order to insert

play06:19

an element at the front of a linked list

play06:21

so let me very quickly type out those

play06:24

three steps so i will write them here

play06:26

and then we will implement those three

play06:29

steps together

play06:31

so here are three steps that we need to

play06:33

implement in order to insert an element

play06:36

at the front of a linked list the first

play06:39

step is that we need to prepare a new

play06:42

node and i will show you how you can

play06:44

prepare a new node in a couple of

play06:46

moments and then this second step says

play06:49

that we need to put it in front of our

play06:51

current head which means that we need to

play06:53

put that new node in front of current

play06:56

first node of our linked list because we

play06:59

are trying to add it at the front of our

play07:02

linked list and then third step says

play07:05

move head of the linked list to point to

play07:08

new node why well because this newly

play07:12

added node is now going to be the head

play07:14

of our linked list and what previously

play07:16

was the head of our linked list is now

play07:19

going to become the second node of that

play07:21

list so let's implement these three

play07:24

steps and let me show you how you can

play07:26

describe these three steps using c plus

play07:29

code

play07:30

so

play07:31

in order to prepare a new node you will

play07:34

do the following you will create a node

play07:36

pointer which i will call

play07:39

new

play07:40

node

play07:41

like this and i will allocate memory for

play07:43

that note so i will say new node

play07:46

okay and then considering that we have

play07:49

passed a new value which means the value

play07:52

for this node here we have passed that

play07:54

as parameter to this function what i

play07:57

will do is i will say that my new node

play08:00

the value of that node will be equal to

play08:03

this value here

play08:05

okay so that is the code that we will

play08:08

type for this first step and with this

play08:11

code here we have prepared our new node

play08:14

now this second step says that we need

play08:17

to put it in front of our current head

play08:19

which means that we need to say now that

play08:22

our new node will point to our current

play08:25

head so

play08:26

i will say that my new node

play08:29

dot next will be

play08:32

equal to our head node pointer and as

play08:36

you can see here i am assigning

play08:38

assigning a headnote pointer because

play08:40

this here is pointer as well as you can

play08:43

see here so i am assigning a pointer to

play08:45

a pointer and with this line of code

play08:48

here i have said that now my new node is

play08:51

pointing to my current head of my linked

play08:53

list and then in this third step here

play08:56

what i need to do is i need to say to my

play08:58

linked list hey this newly added node is

play09:02

now going to be your head so i have

play09:04

added an element at the front of a

play09:06

linked list and that element is now

play09:08

going to be your head

play09:10

so i'm going to say now that my head

play09:12

pointer will be equal to my new node and

play09:16

again this here is a pointer and my new

play09:19

node is created as a pointer so this

play09:22

here will work okay so with this code

play09:25

here i have successfully inserted an

play09:28

element at the front of a linked list

play09:30

and in order to test this function here

play09:33

i will return to this part here and and

play09:36

as you can see i have already invoked

play09:38

this function so i have said insert at

play09:41

the front and then i have passed address

play09:44

of this head node pointer and i have

play09:46

passed an element which has the value of

play09:48

minus one so if i run my program now

play09:53

let's see what we will get well as you

play09:55

can see it indeed it has successfully

play09:58

inserted an element at the front of our

play10:00

linked list which previously was one two

play10:02

three and now we have minus one at the

play10:05

front so let's stop this program and

play10:07

let's try invoking this function one

play10:09

more time so i'm going to say now please

play10:12

insert minus one at the front of my list

play10:15

and then insert minus two at the front

play10:17

of that list so if i run my program

play10:20

again

play10:21

as you can see we will get minus 2 minus

play10:24

1 and then 1 2 3 which means that this

play10:28

function here has done its job so indeed

play10:31

it inserts an element at the front of a

play10:34

linked list

play10:36

so again how this function inserts an

play10:38

element at the front of our linked list

play10:41

well it prepares a new node here and

play10:44

then it says that that new node will

play10:46

point to the current first element of

play10:48

our linked list because we are trying to

play10:51

add a new node at the front and then

play10:54

after doing that we now need to say that

play10:56

at the head of our linked list will be

play10:58

that newly added node so if we had a

play11:01

list which was one two three in this

play11:04

first step here i have prepared a new

play11:07

node and then in this second step i have

play11:10

said that this new node is now pointing

play11:12

to my first node what previously was the

play11:15

first node of this list and then in this

play11:19

third step i now say to my linked list

play11:22

hey this is now your head it's no longer

play11:24

this element here but it is this element

play11:27

here so now my linked list looks like

play11:29

that first second third fourth so minus

play11:33

one one two three

play11:36

okay

play11:37

so let me collapse this function here

play11:40

and then i will also delete these two

play11:42

invocations because i want to show you

play11:45

how you can implement a function that is

play11:48

going to add an element at the end of a

play11:50

linked list so now i want to show you

play11:53

how you can insert a node at the end of

play11:56

a linked list so in order to do that i

play11:58

will implement another function which i

play12:01

will call

play12:02

insert

play12:04

at

play12:04

the end

play12:06

like this

play12:07

and this function is going to be very

play12:10

similar to this function here and it

play12:13

will receive the same parameters as this

play12:16

insert at the front function so i'm

play12:18

going to copy this parameter list and

play12:21

paste it

play12:22

here

play12:23

okay now this insert at the end function

play12:26

needs to perform four steps in order to

play12:29

insert a node at the end of a linked

play12:32

list and i will very quickly write those

play12:34

steps here and i will be back so that we

play12:36

can again implement those steps together

play12:41

so here are four steps that we need to

play12:43

implement in order to insert a node at

play12:46

the end of a linked list so the first

play12:49

step is that we need to prepare a new

play12:51

node that will be added at the end and

play12:54

then second step is a check so we need

play12:57

to make sure that this list is not empty

play13:00

because if it is empty that means that

play13:03

this node that we want to add at the end

play13:05

will actually be the first node and the

play13:08

last node and the only node in that list

play13:10

which means that that new node will

play13:13

become a head node of that list and then

play13:17

if this step here is not true so if this

play13:20

list already has elements we will

play13:23

proceed with these two steps so in this

play13:26

third step we will need to find the last

play13:28

node why well because in this fourth

play13:32

step we need to make that last node

play13:35

point to our new node that we want to

play13:37

add at the end so we will need to insert

play13:40

that new node after last node which

play13:43

means insert it at the end so

play13:46

let's implement this first step here

play13:49

let's prepare a new node so i'm going to

play13:52

say that i want to create a node pointer

play13:55

which i will call new node and i will

play13:58

allocate memory for my new node

play14:01

like this

play14:03

okay and then considering that i passed

play14:06

a value for my new node here i will use

play14:09

that value in order to assign it to the

play14:12

value of my new node so i will say that

play14:15

my new node value will be equal to new

play14:19

value

play14:20

okay

play14:20

and then another step is that

play14:22

considering that we want to add a node

play14:24

to the end of our linked list that means

play14:27

that that will be the last node so it

play14:30

will point to null so we can do that

play14:32

part here as well i can say that my new

play14:35

node dot next will be equal to null like

play14:39

this so with these three lines of code i

play14:43

have successfully prepared my new node

play14:45

that will be added at the end of my

play14:48

linked list

play14:49

now this second step here says that we

play14:52

need to check if our linked list is

play14:55

empty so that means that if

play14:58

our current head pointer is equal to

play15:02

null

play15:03

we will say that our current head

play15:06

pointer will become our new node which

play15:10

means that if this list here is empty so

play15:14

if it is null the head of our list will

play15:17

become this newly created node so it

play15:20

will become the first and the last and

play15:22

the only node of that list and if this

play15:26

here is the case we don't need to do

play15:28

anything else because we have

play15:29

successfully added that node to our list

play15:32

so here i can say return oh i can say

play15:37

return

play15:38

okay so with this code here i have

play15:41

checked if that list was empty and if it

play15:44

was i added a new node to my linked list

play15:48

and that new node had become the new

play15:51

head of that list and also the last

play15:53

element of that list and the only

play15:56

element of that list so that is what

play15:58

this code here is doing but in case that

play16:02

the list already contains elements so in

play16:04

case that it is not empty we need to

play16:07

perform these two steps in order to add

play16:10

a node at the end of that list so

play16:14

this third step says that we need to

play16:16

find the last node so how do you do that

play16:19

how do you find the last node of a

play16:21

linked list in this situation we have a

play16:24

list that has three nodes first second

play16:28

and third so how can i find this third

play16:31

element who knows where this third

play16:33

element is the answer is that the second

play16:36

node knows where the third node is why

play16:39

well because the second node is storing

play16:41

a pointer to the third node and then who

play16:44

knows where the second node is the

play16:46

answer is that the first node knows

play16:49

where the second node is why because the

play16:52

first node has a pointer to the second

play16:54

node okay so in order to access to the

play16:57

third node you need to traverse first

play17:01

second and then third so how will you

play17:04

access this last element how will you

play17:06

find this last element the answer is you

play17:09

will start at the first node and then

play17:11

you will ask hey are you pointing to any

play17:13

elements or are you pointing to null and

play17:15

then if this element here says i am

play17:18

pointing to a node you will move to that

play17:20

next node and then you will ask that

play17:23

next node hey are you pointing to any

play17:25

node or are you pointing to null and

play17:27

then this element here will say i'm

play17:30

actually pointing to a valid node so you

play17:32

will move to that next node which is

play17:34

third node and then you will ask this

play17:36

node as well hey are you pointing to any

play17:39

nodes or are you pointing to null but

play17:41

considering that this node here is the

play17:43

last one it will point to null so that

play17:46

is how you will know that you have come

play17:49

to the last element of your linked list

play17:52

so let's do that and let's implement

play17:54

this logic using a while loop so what i

play17:57

want to do first is i want to create a

play17:59

new

play18:00

node pointer which i will call last and

play18:03

initially i want to say that my last

play18:06

node will hold the same value as my head

play18:09

node pointer so i will start at the

play18:11

first node and then what i want to do is

play18:14

i want to move to the next and the next

play18:16

and the next and the next and so on

play18:18

until i come to the one that is pointing

play18:21

to null and in order to do that i will

play18:23

use while loop

play18:26

and i will say inside this while loop

play18:29

that it is going to iterate while our

play18:32

last

play18:34

next

play18:36

is different than no

play18:39

okay

play18:40

and in each iteration what i want to do

play18:42

is i want to say that my last will be

play18:45

equal to whatever my last

play18:48

next is pointing to so i will move my

play18:53

last element to the next and the next

play18:56

and the next until i come to the one

play18:59

which is pointing to null and at that

play19:01

point i will know that my last is

play19:04

storing the address of my last note

play19:08

so with this we have successfully found

play19:10

the last node of our linked list and

play19:13

then this fourth step here says that we

play19:16

need to insert a new node after our last

play19:19

node which means that now i need to say

play19:22

that my last node so my current last

play19:25

node dot next will now be equal to my

play19:30

new node

play19:31

like this

play19:33

okay so we have now successfully added a

play19:35

new node after our previously last node

play19:39

so in the list that has three elements

play19:41

first second and third i have

play19:44

successfully added this new node here so

play19:47

now what previously was last element is

play19:50

pointing to our new node

play19:53

okay so let's test this function i am

play19:56

going to invoke it here i'm going to say

play19:59

insert at the

play20:01

end like this and this function is going

play20:04

to receive the address of our head node

play20:08

and then it will also receive the value

play20:11

for the new node that we want to insert

play20:13

at the end so let's say that will be for

play20:16

example number four

play20:18

okay and if i run this program

play20:21

as you can see it says one two three

play20:24

four so this new element has

play20:26

successfully been added at the end of

play20:28

our linked list and if i try to add one

play20:33

more for example

play20:34

number five and run my program again as

play20:38

you can see now we have a list that has

play20:40

one two three four five elements

play20:43

okay so that means that this function

play20:46

here indeed is adding or inserting

play20:49

elements at the end of a linked list

play20:52

so how do we do that how do we insert a

play20:55

node at the end of a linked list well in

play20:58

the first step we need to prepare that

play21:00

node so i'm going to allocate the memory

play21:03

for that node and then i will say that

play21:05

the value of that node will be the value

play21:08

that i want to add at the end of my

play21:10

linked list which i have passed as this

play21:12

parameter here and then considering that

play21:15

this node will be the last node i can

play21:18

say that it will point to null and then

play21:21

in this second step i need to check if

play21:23

this list is empty because if it is

play21:25

empty that means that this new node will

play21:28

actually be the head node so it will be

play21:30

the first node and the last node and the

play21:32

only node so that is what this code here

play21:36

does but in case that this list is not

play21:39

empty we proceed with these two steps so

play21:42

in this third step we need to find the

play21:45

last node and we do that by moving to

play21:48

the next node until we come to the one

play21:51

which is equal to null and at that point

play21:54

we have the address of that last node

play21:56

stored inside this last node pointer and

play22:00

then in this fourth step considering

play22:02

that here we have successfully found our

play22:05

last node what i can do is i can say

play22:08

that now my last node will point to a

play22:11

new node that i want to add at the end

play22:13

which is this node here so that means

play22:16

that we have successfully added a new

play22:19

node to our linked list at the end of

play22:21

that list

play22:23

so let's collapse now this insert at the

play22:26

end and let's also delete these two

play22:29

invocations here because i want to show

play22:32

you third way to insert an element into

play22:35

a linked list so third way to add a node

play22:39

to a linked list which is going to be to

play22:41

insert it after a specific node so what

play22:45

i want to do is i want to create a third

play22:47

function

play22:48

also of return type

play22:50

void and i'm going to call it

play22:52

insert after

play22:55

okay

play22:56

now this insert after function will have

play22:59

to receive two parameters in order to be

play23:02

able to insert a node after a specific

play23:05

node the first parameter will be that

play23:08

specific node after which i want to add

play23:10

a new node so i will call that parameter

play23:14

previous or previous node and i will put

play23:16

it here i will say node pointer which i

play23:20

will call previous

play23:22

like this and then the second parameter

play23:25

will be the value for my new node so i

play23:28

will paste it here it's the same

play23:30

parameter as these two previous

play23:32

functions received and then this

play23:34

function also will have to perform three

play23:37

steps in order to insert a node after

play23:41

this node here so let me again write

play23:45

those steps here on the screen and then

play23:47

we will implement those steps and we

play23:50

will see how we can insert an element

play23:52

insert a node after a specific node

play23:56

so here are three steps that you need to

play23:58

implement in order to insert a node

play24:01

after a specific node the first step is

play24:04

that you just need to check if this

play24:06

previous node is null so you need to

play24:08

make sure that someone has not passed no

play24:11

value for your previous node okay and

play24:14

then this second step is that you need

play24:17

to prepare your new node and then the

play24:19

third step says that you need to insert

play24:21

new node after your previous element

play24:25

so how does this third step work

play24:28

well imagine the following situation you

play24:30

have node a and node b and your node a

play24:34

is pointing to your node b

play24:36

so how do you add a new node after your

play24:39

node a

play24:40

well the answer is that now your node a

play24:43

needs to point to that new node and then

play24:46

the new node will point to your element

play24:48

b okay so instead of your linked list

play24:51

going like this a b it will go like this

play24:55

a

play24:55

new node and then b

play24:58

okay so let's implement these three

play25:01

steps here let me show you how that

play25:02

works in c plus plus code

play25:05

the first step is to check if previous

play25:07

node is null so i will just say if

play25:10

previous

play25:12

is equal to null

play25:14

in that situation i will write out a

play25:16

message to my user and i will say that

play25:19

previous

play25:21

cannot be

play25:23

null

play25:24

like this and i will return from this

play25:27

function here which means that i will

play25:29

not continue the execution of this

play25:31

function here why well because here we

play25:34

want to receive a valid element and null

play25:36

is not valid element we want to receive

play25:39

a valid node after which we will add a

play25:42

new node and null is not a valid node so

play25:46

that is the first step and then the

play25:49

second step says that we need to prepare

play25:51

a new node so let's create a new node

play25:55

pointer and i will call it new node and

play25:58

here i will allocate memory for that

play26:01

node

play26:02

like this and then i will assign this

play26:06

new value as the value for my new node

play26:08

so i will say newnode dot

play26:12

value

play26:13

is going to be equal to new value the

play26:16

value that i want to add in my linked

play26:19

list and then this third step here says

play26:22

that i need to insert a new node after

play26:26

this previous node which means that now

play26:29

my previous node so now my node a needs

play26:32

to point to that new node and then that

play26:35

new node now needs to point to my node b

play26:38

so instead of my node a pointing to my

play26:40

node b now my node a will point to the

play26:42

new node and my new node will point to

play26:45

my node b but one very important thing

play26:48

to pay attention to is

play26:51

when will you break this connection

play26:53

between your node a and your node b so

play26:56

that you don't lose your node b so that

play26:58

you don't lose the address of your node

play27:00

b forever because the only one who knows

play27:04

where your node b is

play27:06

is your node a and if you do it the way

play27:08

that i just explained so if you first

play27:10

say that your node a will point to your

play27:13

new node at that moment you are deleting

play27:17

this connection in order to make this

play27:20

connection and you will lose the address

play27:23

of your node b forever if you do that so

play27:26

what you need to do first is you first

play27:29

need to say that your new node will

play27:31

store the address of your node b so your

play27:34

new node will point to whatever your

play27:36

node a is pointing to which is your node

play27:40

b and then after you have successfully

play27:42

stored the address of your node b inside

play27:46

this connection then you can break this

play27:48

connection so then you can say that your

play27:50

node a will point to your new node so

play27:54

let's see how we can implement that in c

play27:56

plus plus code

play27:58

so first you implement this connection

play28:01

so first you say that newnode dot next

play28:05

will be equal to whatever our previous

play28:09

node was pointing to so it will be equal

play28:12

to our previous next and then after you

play28:15

have successfully stored the address of

play28:17

your node b

play28:19

then you can overwrite this connection

play28:21

with disconnection so then you can say

play28:24

that your previous dot next will point

play28:28

to your new node like this okay so with

play28:32

this function here we have successfully

play28:34

inserted an element after a specific

play28:37

node so let's test this function here

play28:40

and i will invoke it here before my

play28:43

print list function i will say insert

play28:46

or actually insert after and what i want

play28:49

to do is i want to insert an element

play28:51

after this head node so i will say

play28:54

please insert after this element here so

play28:57

after one what i want to insert is i

play29:00

want to insert minus one

play29:02

like this

play29:03

and if i run my program

play29:06

let's see what will happen as you can

play29:08

see minus one has successfully been

play29:10

inserted after one so we have inserted

play29:14

an element after our head element now

play29:16

let's insert another element for example

play29:19

let's insert an element after

play29:22

this element here so after two i want to

play29:25

insert the value of -2 and if i run my

play29:29

program

play29:30

as you can see this is how our linked

play29:33

list looks like now so 1 minus 1 2 minus

play29:37

2 and then 3 which means that this

play29:41

function here indeed is inserting

play29:43

elements after a specific element so it

play29:46

is inserting nodes after previous node

play29:48

that is passed as a parameter here

play29:51

okay

play29:52

so i hope this video helped you

play29:54

understand how you can insert a node at

play29:57

the beginning of a linked list at the

play29:59

end of a linked list and then also after

play30:01

a specific element of your linked list

play30:05

there is something else that i wanted to

play30:06

say

play30:08

i don't know so give this video a thumbs

play30:10

up and share it with your friends and if

play30:12

you have any questions put those in the

play30:14

comments down below

play30:16

and tag me on your instagram stories i

play30:18

love watching your instagram stories and

play30:20

i will leave links to my other social

play30:22

media accounts in the description of

play30:24

this video and thank you very much for

play30:27

watching i will see you in my next video

play30:29

bye

Rate This

5.0 / 5 (0 votes)

Связанные теги
Linked ListsC++ ProgrammingData StructuresInsertion TechniquesFront InsertionEnd InsertionAfter Node InsertionCode TutorialProgramming TipsEducational ContentTechnical Learning
Вам нужно краткое изложение на английском?