 Hey guys, what's up? This is Umar Khan and I welcome you to a new section here on this course on advanced C++ And in this section, we are going to be covering a very interesting topic that is multi-map Now the content covered in this section is listed right here We are going to be starting on with the introduction to multi-map Then we are going to be seeing about the find and erase function Then we are going to be covering the three type of emplace functions that are applied on multi-map And we are also going to be covering many other operations on multi-map like find, erase and emplace and those operations basically are listed right here on your screens that include count that include begin and See begin see and see our begin see our end Moreover, we are going to be also covering these operations that include size swap finding the maximum size lower bound upper bound insertion and Equal range so all these operations basically apply on multi-map and all these are separate functions For which we are going to see how they are going to be applied on multi-map What is their syntax and how we will use them in our C++ code So there is so much for you guys to learn from this section So I guess you stick to this section and grab all those concepts So that's it with this tutorial in the next tutorial. We will start on with the introduction to multi-map So thank you so much guys for watching and I'll see you guys in the next tutorial Hey guys, what's up? This is Umar Khan and I welcome you to the introduction tutorial of multi-map in C++ Now the C++ a standard library multi-map class is used for the storage and retrieval of data from a collection in which each element is a pair that has both a data value and a sort key The value of the key does not need to be unique and is used to order the data automatically The value of an element in a multi-map, but not its associated key value may be changed directly instead key values associated with old elements must be deleted and new key values associated with new elements are inserted Now this is the syntax you have to use while using multi-map First we have template keyword then we have the class key then we have the class type Then the class trait equal to less key then we have class allocator equal to allocator pair constant key type and then finally we have a class multi-map Now if you talk about the parameters here, the first one is this key Now this key is basically the data type that is to be stored in the multi-map The second one is this type over here That is basically the element data type that is to be stored in the multi-map The third one is about the traits that is the type that provides a function object That can compare two element values as sort keys to determine their relative order in the multi-map Now the binary predicate which you can see right here right in front of traits that is less key it is basically the default value And finally we have this allocator This is basically the type that represents the stored allocator object that encapsulates details about the maps allocation and the allocation of memory Now the argument that is right in front of it This is basically optional and the default value is at the locator pair constant key type And finally we have the class multi-map in which we are going to be using our functions Which we studied in the very first tutorial that was the overview of this section in which we discuss a lot of operations and in which we discuss basically the functions So all those functions are going to be implemented in this class multi-map So now to talk about the standard multi-map library Now the C++ standard library multiple class is basically first it is an associated container which is a variable size container that supports the efficient retrieval of elements values based on an associated key value Secondly the standard library multiple multi-map class is reversible Now why it is reversible because it actually provides bi-directional iterators to access its element Third this is sorted because its elements are ordered by key values within the container in accordance with a specified comparison function Third they are multiple because its element do not need to have a unique key so that one key value may have many element data values associated with it Next it is a pair associative container because its element data values are distinct from its key values Third it is a template class because the functionality it provides is generic and so independent of the specific type of data contained as elements or keys The data types to be used for elements and keys are instead specified as parameters in the class template along with the comparison function and allocator Now the iterator provided by the map class is a bi-directional iterator but the class member functions insert and multi-map have versions that take as template parameters a weaker input iterator whose functionality requirements are more minimal than those guaranteed by the class of bi-directional iterators The different iterator concepts form a family related by the refinements in their functionality Now each iterator concept has its own set of requirements and the algorithms that work with them must limit their assumptions to the requirements that are provided by that type of iterator Now it may be assumed that an input iterator may be dereference to refer to some object and that it may be incremented to the next iterator in the sequence Now this is a minimal set of functionality but it is enough to be able to talk meaningful about a range of iterator in the contents of the class member functions Now the next thing that occur is about the choice of the container type Now the choice of the container type should be based in general on the type of searching and inserting required by the application Associative containers are optimized for the operation of lookup, insertion and removal The member functions that explicitly support these operations are efficient performing them in a time that is an average proportional to the logarithmic of the number of elements in the container inserting elements invalidates no iterators and removing elements invalidates only those iterator that had specifically been pointed at the removed elements Now the next thing that should be discussed is that the multiple map should be associative container Now it should be an associative container of choice when the condition associating the values with their keys are satisfied by the application A model for this type of structure is an ordered list of keywords with associated string values providing say the definitions where the words were not always uniquely defined If instead the keywords were uniquely defined so that keys were unique then a map would be the container of choice While if on the other hand just the list of words were being stored then a set would be the correct container Now if multiple occurrences of the words were allowed then a multi-set would be appropriate container structure Now the multi-map basically orders the sequence it controls by calling a stored function object of type key compare This stored object is a comparison function that may be accessed by calling the member function that is the key compare function In general the elements need to be merely less than comparable to establish this order so that given any two elements it may be determined either that they are equivalent or that one is less than the other Now this results in an ordering between the non-equivalent elements On a more technical note the comparison function is a binary predicate that indicates a strict weak ordering in the standard mathematical sense Now a binary predicate function that is let's say fxy that receive into argument it is a function object that has two argument objects x and y and a return value of true or false and now an ordering imposed on a set is a strict weak ordering If the binary predicate is irreflexive, anti-symmetric and transitive and if equivalence is transitive where two objects x and y are defined to be equivalent when both fxy and fyx are basically false Now if the stronger condition of equality between keys replaces that of equivalence then the ordering becomes total and the keys matched will be indistressable from each other Now if you just move ahead and talk about the members of the multi-map then we have four kind of members the first one is the constructor and you know what is a constructor the next one are the type definition that basically include some disk type names which we are going to be discussing right in this tutorial after this slide and then we have the member function that includes some function that can be performed on multi-map and then finally we have the operators so starting on with the constructor you know that we only have one constructor that is the multi-map constructor and this constructor basically constructs a multi-map that is empty or that is a copy of all or part of some other multi-map then we have the type definitions and in the type definitions we have a lot of things the first one is the allocator type that is a type that represents the allocator class for the multi-map object the next one is the constant iterator that is a type that provides a bi-directional iterator that can read a constant element in the multi-map the next one is the constant pointer that is a type that provides a pointer to a constant element in a multi-map The next one as you can see is constant reference that is basically a type that provides a reference to a constant element stored in a multi-map for reading and performing constant operations. The next one is the constant reverse iterator that is a type that provides a bi-directional iterator that can read any constant element in the multi-map. Now the next one is the difference type that is a signed integer type that can be used to represent the number of elements of a multi-map in a range between elements that are pointed to by some iterator. The next one is an iterator that is a type that provides the difference between two iterators that refer to elements within the same multi-map. The next one is the key compare that is a type that provides a function object that can compare a two sort keys to determine that a relative order of the two elements in the multi-map. Then we have the key type that is a type that describes the sort key object that constitutes each element of the multi-map. Then we have the mapped type that is a type that represents the data type stored in a multi-map. Then the next one is a pointer that is a type that provides a pointer to a constant element in a multi-map. Then we have a reference that is a type that provides a reference to an element stored in a multi-map. Then we have the reverse iterator that is a type that provides a bi-directional iterator that can read or modify an element in a reversed multi-map as you can see by its name also. The next one is the size type that is an unsigned integer type that provides a pointer to a constant element in a multi-map. And then we have the final one that is the value type that is a type that provides a function object that can compare two elements a sort keys to determine their relative order in the multi-map. So that's it with the type definition. The next thing is the member function. So as I told you these are basically the operations that can be performed on an multi-map. So these include first begin that basically returns an iterator that is addressing the first element in the multi-map. Then we have the see begin that returns a constant iterator addressing the first element in the multi-map. The difference between the first two is that the first one only returns an iterator and the second one returns a constant iterator. The next one is the see end that returns a constant iterator that addresses the location succeeding last element in a multi-map. Then we have this clear that erases all the elements of a multi-map. Then we have the count that returns the member of elements in the multi-map whose key matches a parameter specified key. Then we have this see are begin that returns a constant iterator addressing the first element in a reversed multi-map. So basically the begin the see begin and see are begin there are basically the very same thing but the begin is to return the first element the see begin is going to return the first constant element and the see are begin is going to return a constant iterator addressing but it is going to address the reverse multi-map. So the next one is CRN which is the very same as and but the difference is that it returns a constant iterator that addresses the location succeeding the last element in a reversed multi-map. Then we have in place and it inserts an element constructor constructed into a place in a multi-map. Then from the very in place we have the second function that is the in place hint that inserts an element constructed in place into a multi-map but with a placement hit and that is what is the difference between a place and in place hint. The next one is the empty that test if a multi-map is empty or not and the result returned by this function is either true or false. The next one is the end that returns an integer that addresses sorry not an integer it returns an iterator that addresses the location succeeding the last element in a multi-map. Then we have this equal range that points the range of elements where the key of the element matches a specified value. Then we have this erase function that removes an element or a range of elements in a multi-map from specified positions or remove elements that match a specified key. Then we have this find function that returns an iterator addressing the first location of element in a multi-map that has a key equivalent to a specified key. Then we have this get allocator that returns a copy of the allocator object that is used to construct the multi-map. Now we are not finished with the member functions. The next member function is the insert that inserts an element or a range of elements into a multi-map. The next one is the key compare and it retrieves a key copy of the comparison object that is used to order keys in a multi-map. Then we have the lower bound in upper bound and in the lower bound it basically returns an iterator to the first element in a multi-map that with a key that is equal to or greater than a specified key. Then we have the maximum size that returns the maximum length of the multi-map. Then we have the R begin that returns an iterator addressing the first element in a reversed multi-map and the difference between CR begin and R begin is only that it returned the last element of the reversed multi-map and that one returned the last element of the returned multi-map but that value is a constant value. The next one is the R end and that has the very same difference with CR end that it returns only an iterator that addresses the location succeeding the last element but in a reversed multi-map. So the next one is the size it returns the number of elements in the multi-map and then we have the swap that exchanges the element of two multi-maps then we have the upper bound that returns an iterator to the first element in a multi-map that with a key that is greater than a specified key and finally we have this last one that is the value compare the member function returns a function object that determines the order of elements in a multi-map by comparing their key values. So that's it with member functions the final thing is the operators. So in the operator we only have this one operator that is operator equal to that replaces the elements of a multi-map with a copy of another multi-map. So in this case we are going to have two multi-maps that are going to actually replace their elements. So you're going to see all of this stuff in the future tutorials we are going to be covering all of these in separate tutorials. So finally talking about the requirements of a multi-map the first one is that you have to include a map library in the header file then namespace std is going to be used as usual because standard library map has to include the namespace std and finally we are going to have a pair of key and value that are stored in a multi-map as objects of type pair. Now the pair class requires the header utility which is automatically included by the first header that we include that is map. So in the future tutorial you are going to see all the stuff we have only described in this tutorial. So you don't need to worry about the lot of functions we have discussed a lot of itradars we have discussed a lot of type definitions we have discussed all of these are going to be covered in detail in the future tutorial. So that's it with this tutorial thank you so much guys for watching and I'll see you guys in the next tutorial. Hey guys what's up this is Umar Khan and I welcome you to another tutorial on multi-map. In this tutorial we are going to be covering the find and erase functions that are applied on multi-map. So starting on with the find function so basically the multi-map find is a built-in function in C++ standard library which returns an iterator or a constant iterator that refers to the position where the key is present in the multi-map. Now if the key is not present in the multi-map container it returns an iterator or a constant iterator which refers to the multi-map end. So if you talk about the syntax the syntax is right on your screens it is the iterator multi-map name dot find and then you have to apply the key. So the function except one mandatory parameter which is the key and that specifies the key that is to be searched in the multi-map container. Now if you talk about the return value we have already discussed the return value for this function. Fine so basically this function is going to return an iterator or a constant iterator which refers to the position where the key is present in the multi-map. Now if the key is not present in the multi-map container then it returns an iterator or a constant iterator which refers to the multi-map dot end. Now if this function is going to return an iterator that is a normal value then the first syntax that is right here is going to be used whereas if it is going to return a constant value then this second one is going to be used and you are going to use this syntax. So let me give you an example that is a C++ program for the illustration of multi-map find function. So we will just move on to our editor and here we are. Now what we are going to do is that in the main function what we are going to do is that we are going to initialize a container first because if you just focus on the previous tutorials in which I have been talking I've already been talking about the word container and basically what is a container? A container is a place where the pairs containing the key and the element are actually stored in. So first you are going to initialize a container that is going to be initialized using multi-map and then you are going to specify the two types for example if this is the key this is the element so and then you are going to initialize an object for this as well so let's say my object is mp then the next thing you need to do is that you have to use the insert function and why you are going to use the insert function because now you have actually created a structure and actually you have created a container now you need to insert elements in random order so to insert elements in random order what you are going to do is that you are going to use this object mp.insert and the function the purpose of this insert function is to insert elements then these round brackets and in the round brackets you are going to use these curly braces and first you are going to define the position which is the key where the element is to be stored and the second thing you have to put this comma and thirdly you have to insert the element which you want to insert at this key location this key basically identifies the location the position where the element is going to be stored so for example I want to just store 10 so I call it right here so if you want to insert more values so if you write in mp.insert and let's say the key is 1 this time and let's say the element is 20 then we have let's say one more element mp.insert now let's say the key is 3 this time and you just insert 13 to it so now what you are going to do is that if you want to actually print out the elements then you have to actually use the find function for that purpose so how find function is basically going to be used you are going to write in for loop and what this for loop is going to do is that it's going to iterate to through this structure in which these elements are actually inserted so we are going to use an auto an iterator that is going to equal to mp.find which means that it is going to look in to this structure that is created using this object so in the find function you are going to specify the location from which you want to start on so I want to start on with 1 then you have to write in the condition that while itr is not equal to mp.n which means that it is going to keep iterating until the very end and finally you have to write in itr plus plus and then what you're going to do is that you're going to write in cout itr first alright so this is going to print out the element for you and then what you have to do is that you have to write in cout itr second so this is going to actually print out the key as well as the element for you now before I run this I have to add the library right here that is hash include std that is the standard library c plus plus dot edge and you have to write in bits this dot and slash and then stdc plus plus dot edge so now if I just run this code then you are going to see what's actually going on in here so as you can see on this output that at element 1 we have this 20 over here that is this stuff over here then at position 2 we have 10 and then at position 3 we have 30 so if we just amend it a bit and do not use this and deliver here and instead we just add a few spaces over here then it is going to be a very good look and then you are going to understand this as you can see that we have a position 1 we have 20 then at position 2 we have 10 and then in position 3 we have 30 so actually this part over here that is cout itr first it is actually going to print out this very first element that is the key for anything over here then this second one that is the cout itr second this is actually going to print out this data part actually the element that is stored at that key position so this loop actually is going to start on with the very first key basically this find function has this very single argument that is the key so it is going to look on for this key and it is going to find that key it is going to start iterating from there and it is going to iterate till the very last is reached and in this case the very last is this one that is 3 and this is going to actually keep iterating it and then this itr first is going to print out the key and the itr second is going to print out the element for us so what if we have let's say mp.insert and let's say we use this one again and print out 40 so if you now just run it you are going to see that it says at 1 we have 20 and then at 1 we have 40 as well and then at 2 we have 10 and then at 3 we have 30 so why is it because in a map for example if you just consider a normal map of your city then at two locations we have certain things so this is going to actually point out the coordinates actually so that's why you can do this stuff as well you can insert two elements at a specified location and then when you are searching for those element it is going to print out both the elements at that location so as you can see that we have two elements at key 1 that both have the key equal to 1 so when we look for those elements using the find function it printed out both the elements for us so I guess that is clear so moving ahead we've already covered an example so the next topic that is the part of this tutorial is the multi map erase function and it is also a built-in function in C++ standard library which is used to erase elements from the container now it can be used to erase keys elements at any specified position or a given range so first let's see the syntax how to remove a key so this is basically a simple syntax that is multi map name then you have to write an erase function and then you have to specify the position at which you want to actually erase an element so the return value for this is going to be the function that does not return anything because if you erase an element it does not have to return editing it simply raises all the elements with the specified key so let me just give you an example of this so we'll move on to a compiler and we already have a container present in which we have four elements so we will just use this very same container and we are going to delete the elements of those containers so right after this insertion what you're going to do is that you're going to use the very same object MP and you have to write an erase function and then you have to as shown in the slides you have to specify the position or the location or the key which you want to get deleted so what you're going to do is that let's say we want to delete the key number two then this column over here and you are going to simply run this code and as you can see that the key to and its data part as well is missing from the scene because when you delete the key the elements at that position are going to be automatically deleted now what if since we have two elements at the key one so if we just specify this key one over here now one thing that is to be changed over here is actually this because since we were starting on from one but now since one has been erased from the scene so you have to start on with two so now if you just run this code you can see that it printed out 210 and 330 now let me just show you what is going to happen if you specify one over here you can see that nothing was printed because the very starting location was actually not found and it does not iterate until the end and it just ended up right at the very first iteration since one was actually missing from the scenes so this is how it actually works so the next part we are going to cover is the syntax for deleting a specified a range so the syntax for this is right here on your screen that is multi map name dot erase then first you have to specify the starting position and then you have to specify the ending position and it is going to delete any elements present between that range so let's let me give you an example for this as well so here we are at our compiler so what you're going to do is that first you have to actually find those two ranges so you're going to write in auto let's say x is the name x equals to mp dot find for example we want to start on with two and then you have to write in the ending range as well so let's say I want to end it at three so now in this mp dot erase what you have to do is that you have to write in the starting range that it's x and then the ending range that is y so it is going to delete all the elements from two till three so if I just run this code so as you can see that two is actually missing now you can see that three is actually present because what it do is that it start on with this it delete any element with range two but it delete any elements from two to three but not with three for example if I just make it equal to four over here and now run this code then you are going to see that three is now also missing so the range that is included right over here that is the ending range it is not going to delete the elements of that range it's going to delete elements of ranges before that and until and unless this range and also it is going to delete the elements that has the key to but not the elements with key four so I hope this is clear so this is how basically you can actually delete elements within a specified range as well as you can delete elements that has a specific key so I guess that's it with this tutorial in the next tutorial we will cover some more functions so thank you so much guys for watching and I'll see you guys in the next tutorial hey guys what's up this is Umar Khan and I welcome you to another tutorial on multi map in this tutorial we are going to be covering the in place function and basically in place function has three types the first one is the map in place then we have the multi map in place and then we have a multi map in place hint so we are going to be covering all of these three in this tutorial so starting on with the first one that is the map in place the map in place is a built-in function in c++ standard library which inserts the key and its element in the map container it effectively increases the container size by one if the same key is in place more than once the map stores the first element only as the map is a container which does not store multiple keys of the same value so if you talk about the syntax of the map in place this is the syntax for that that is the map name dot in place then you have to place the key then you have to place the element so the function except two mandate three parameters the key basically as specifies the key to be inserted in the multi map container and the element specifies the element to the key which is to be inserted in the map container both has the very same logic as it was in the previous tutorial then if you talk about the return value the function has to return nothing so to give you a better understanding of this we will just go on to our compiler and see an example in which we will see the illustration of map in place function in our compiler so here we are so in this what we are going to do first is that we already have two libraries in right here first we are going to initialize our container so in the previous case our container resolution initialized using the multi map name now it is going to be initialized using map then it's going to be in which means that the key is going to be an integer and the element is also going to be an integer value and then you have to write in an object let's say it is mp then using that object what you have to do is that you have to now use the in place function and as I told you what the purpose of this function is that it is to insert the key and its element in the map container so we are going to use in place and now we are going to first write in the key let's say the key is 2 and the element is 10 and then let's say we have mp.inplace the key is 1 and the element is 20 then let's say we have mp.inplace and let's say the key is 3 this time and the element is 30 and then let's say we also have a duplicate element the same key and let's say the element is having a key 1 and the element is 40 and then let's say we have one more final element that has the key 4 and the element is let's say 50 all right so we have actually placed these five elements in the container and now what we need to do with these is that as in the previous tutorial we are going to print them out so we are going to use auto itr and it is equal to map.begin and then we are going to have itr is not equal to mp.in so it is going to start on from the end sorry it is going to start on from the very beginning and it is going to iterate till the very end and then we are going to write in itr plus plus now you must be thinking that why I have not inserted one over here now I have not inserted one over here because in the previous case we were using the find function so find function was to find the first element but mp.begin means that it is automatically going to start on from the very first element so we do not have to specify anything in these bracket and it is going to iterate till the very end because of this condition right in here all right let me just make it a bit more lookable all right so that seems to be perfectly all right now what we have to do simply is that we have to print out the element so first we are going to write in itr first and then we will add a bit of space and then we are going to see out itr second and an an aligned space so we have this code right in over here and we missed a bracket here so let's just run this code so you can see that it printed out the elements but one thing you might have noticed here that the duplicate is actually messings from the scene and that what is the basic difference between the insert and the in place function you can see that we have actually two elements with the key one that is 120 and then at one we also have 40 so it is basically going to take in the first entry that is 120 over here and it is going to relicate or it is going to actually to simplify it is going to ignore any other value that comes with the very same key so in this case it entered 120 into its database means 20 was inserted at the key one so when the next element came with the very same key it actually ignored that and it actually kept 120 in its container so I hope it is clear so let's move ahead so the next one is the in place now it is going to be used with the multi map so the multi map in place hint is a built-in function in c++ standard library which inserts the key and its element in the multi map container with a given hint it effectively increases the container size by one as multi map is the container that stores multiple keys with the same values the hint provided does not affect the position to be entered it only increases the speed of insertion as it points to the position from where the search for the ordering is to be started it inserts in the same order which is followed by the container it works similar to multi map in place function but it is at times faster than if it the user provides position accurately so to talk about the syntax of this you can see right on your screen it says multi map name then we have to write in in place hint then it accepts two argument the first one is the position key the sorry three arguments the first one is the position the second one is the key and the third one is the element if you talk about the key it specifies the key to be inserted in the multi map container as the very same in the previous cases then the element is also the very same it specify the element to the key which is to be inserted in the multi map container now comes the position it basically specifies the position from where the search operation for the ordering is to be started hence making the insertion faster and if you talk about the return value the function is to return nothing so let's just see a quick example of how this is going to be implemented in your c++ code so right here so right here if you talk this function that is the print function for auto ptr mp.begin this is going to be the very same since it is going to simply print out the element the difference is going to come right over here so we will just remove this from here and in here what we are going to do is we are going to write in mp.inplace underscore hint and now the first argument is going to be the position actually so we are going to let's say insert it at the beginning so we are going to write in mp.begin so which means that this element is going to be inserted at the very beginning then we have to specify the key let's say the key is two then you have to specify the element let's say the element is 10 and then you have to specify the score then let's say we have one more element in place hint you have to write an mp.begin again and then let's say the key is one and the element is 20 then let's say we have one more in place underscore hint let's also insert it at the beginning and let's say the key is repeated here for example let's say the key is two this time and the element is 30 and then let's say we have one more in place hint function in which we are going to write an in place mp.begin and then let's say the key one is now repeated and then you have let's say 40 over here and let's just create one more in place hint and in here let's start on from the beginning and let's say the key two is now repeated or let's just repeat one again and let's insert 50 so that you can just see what is going to be the difference in that so before we run it we have to change this to multi map since it is multi map in place hint not map in place hint so if I just run this code now you are going to see right in here that each and every element was printed 150 140 then 120 then 230 and then 210 was printed so it does not remove the duplicates but what did that it actually knows where to insert each and every element so I hope it's clear so if we do it with the very same map in place sorry from the map and then we have to use this in place hints so as you can see right over here that we have this 120 that is this over here and then we have two at ten which means this one right here so basically these three were ignored because since these were entered at the beginning but they were actually not accepted so basically if you talk about this the first one and the second one it is going to be a very fast process since these keys know where we are going to be inserted since we have specified that location so they are going to insert themselves but when this very first repetition occurs that is two at 30 it is going to be a very slow process because it is going to insert itself at the beginning but this system is not going to let it insert so it is going to start searching that whether a key with my key is present in the container or not so when it starts searching it found that 210 is already present so 230 was not allowed in sector for the fourth line that is mp.begin 1.40 it is again a very slow process since it is not again allowed but for this mp.begin 150 it is going to be a fast process since in the previous case now we know that we have an element with this key already so it is not going to allow any element with key one so any repetition now if occurs with key one or key two after this part after this part any line that has the same key it is going to reject it because it has rejected it once so that's how in place hint is actually going to help us out in that process now in the previous case if you have in place over here and you don't specify the location here and then you keep repeating the keys then every repetition is going to be a slow process but now in this case when mp.begin for two this one is called it now knows that element with key two now exists so it is not going to allow the processor to slow down for any other repetition with key two after this and then when this line was executed it now knows that element with key one exists so any repetition of this key one one now is going to occur for example this one occurred so this is going to be a fast process it is going to reject it as soon as it this request for insertion of 50 at key one occurs so it is going to simply reject it and it's going to be a fast process so that's how in place hint is actually going to help us out so moving ahead to the last one that is the multi map in place so basically multi map in place is a built-in function in c++ standard library which inserts the key and its elements in the multi map container it effectively increases the container size by one as multi map is the container that stores multiple keys with the same values now this is going to be the syntax for that it is going to be multi map name dot in place key and the element so which means that we are going to be expecting two mandate three pedometers that is the key and the element and it has the very same thing key specifies the key to be inserted in elements specify the elements to the key which is to be inserted in the multi map container and the return type again it is going to return nothing so let's just see a very quick example for how this is going to be done so here are right here we are so we are going to actually remove this hint from here or let's just record it so we are going to write an mp dot in place so now one more thing that is going to also change is that it is going to be multi map now so since this is a multi map now so we are going to write in multi map over here so now we are going to be using this mp dot in place I have to specify the key and then this element then you have to write an mp dot in place one comma 30 let's make it equal to 20 and you have to write an mp dot in place 2 comma 40 let's say repetition occurs and then let's say we have mp dot in place 3 comma 15 and then let's say we have another repetition for the key 2 and that want to insert let's say 13 so the print function is going to be the very same let's just run this code and you can see that it has actually printed each and every element out so in the map in place and in the multi map in place this is the very basic difference the map in place is going to reject any kind of elements that are duplicates but the multi map in place it is going to keep each and every element rather it is an iteration but one thing it is going to do is that it's going to order them the element with key one are going to be right here then the elements with key two are right here and then the element with key three is placed at the very end so this is basically the difference between these three so I guess we have covered an example of this so I guess that's it with this tutorial thank you so much guys for watching I'll see you guys in the next tutorial hey guys what's up this is Umar Khan now welcome you to another tutorial on multi map in C++ and in this tutorial we are going to be covering the count function so basically the multi map count is a built-in function in C++ standard library which returns the number of time a key is present in the multi map container if you talk about the syntax it has a very simple syntax that is multi map name dot count and then you have to specify the key for which you want to find the count so the function except only one parameter that is the key which specifies the key whose count in multi map container is to be returned and if you talk about the return value of this function the function is going to return something and what it's going to return it is going to return the number of times a key is present in the multi map container so let me give you a quick example of how this is going to be all done so we will move on to our compiler so right in here what we are going to do is that we are going to code a C++ function count that is going to actually counted the number of occurrence of a specific key in a container so the first thing you need to do is that you have to create a container that is of the type int int and then you have to specify this object and then let's insert certain elements you know this is how the insert function works first you have to specify the key let's say the key is one and the element is say 10 then you have to insert more elements into it and let's say the key is 2 and the element is 12 then you have to call and insert again and let's say the key is 3 this time and the element is 41 and let's just copy this from here paste it here paste it here and this has to be mp not mmp and let's change the elements only let's make it equal to 14 let's make it equal to 16 let's make it equal to 43 let's make it equal to 19 let's make it equal to something 100 and let's make it equal to 44 and let's just add mp.insert with the key 2 and let's add 200 to it so right here as you can see that we have a lot of elements with key one we have one two three elements and then with key two you have one two and three and four elements and then with key three you have one two and three elements now you can count them easily but what if you have a complete multi-map in which you have hundreds and thousands of grades so you cannot count the occurrence of a single key so that's where the count function is going to come in and it's going to help you out so for example you want to see the occurrences of this key 2 in this multi-map so what you're going to do is that you're going to see out 2 exists and in here you are going to call in mp.count and then you have to specify the key so for 2 you are going to specify 2 here and then you are going to specify times so this is going to tell you see out 2 exists this is going to generate the count which means it is going to generate 4 so it is going to print out 2 exists 4 times so if you just run this code all right as you can see right here that it says 2 exists 4 times now you want to find it for another key let's say 1 and just run the very same code but let's just make it equal to 1 now run it so now you can see that it says 1 exists 3 times so you can find the occurrence of any key for example the last key that is the 3 you can find the occurrences of this 3 over here run this code and it is going to tell you the occurrences of 3 in the map so it says 3 exists 3 times so you can find the occurrence of any other key so this is really helpful when you have a multi-map and not a map because a map does not have any occurrences a map only has one key at one time as you have seen in the previous example when you were using the in place function so when in place was used with map you see that the occurrences were actually not repeated again which means elements with a single key were repeated only the first time for the second time if you try to insert any element or in place any element with the same key it was not letting you it was just taking in the first occurrence of that key so actually map only has one occurrence of a single key but multi-map can have multiple elements with the very same key so that is where the count function is going to come in and is going to tell you the count and occurrence of a specific key in a multi-map so I hope it is clear so that's it with this tutorial thank you so much guys for watching and I'll see you guys in the next tutorial hey guys what's up this is Umar Khan and I welcome you to another tutorial on multi-map in C++ in this tutorial we are going to be covering four function the first one is C begin second one is C end then we are going to discuss C R begin and C R end finally so let's start on with the first one that is C begin so multi-map C begin is a built-in function in C++ a standard library which returns a constant iterator referring to the first element in the multi-map container now since multi-map container contain the elements in an ordered way the C begin function will point to an element that will come first according to the containers starting criteria now the basic difference between begin and C begin is that begin represents all the elements starting from the beginning and the C begin actually returns a constant iterator and it refers to the first element in the multi-map container so if you talk about the syntax you can see it right on your screen that is multi-map name dot C begin which means that it does not accept any parameter and then we have the return value this function is going to return a constant iterator that is going to refer to the first element in the multi-map container so let's see a quick example for how this is going to work so here we are the first thing we need to do is that we have to create a container multi-map then integer integer is going to be the types then going to create the object and then we are going to actually insert elements using mp.insert 2 into 20 is the first element then we have mp.insert 1 comma 10 is the second element then we have mp.insert 3 comma 15 is the third element then let's say we have mp.insert and then we have a repetition of 2 that is 30 and then we have a final element mp.insert and let's say it is a repetition of the key one so let it be 14 so this is our multi-map created now what we need to do is that we have to create an iterator first so first we are going to create an iterator and this iterator is going to equal to mp.cbegin all right so now what we are going to do is that using this iterator we are going to actually print out the first element so how our first element is going to be printed we are going to write in c out the first element is first we are going to bring out this part that is the key part so we are going to write in id er slash and then we are going to write in first then we are going to add some spaces and then we are going to write in id er second and finally an inline space so as I told you before that basically this iterator that is containing mp.cbegin it is going to actually return the very first element so when you write an itr first you don't have to specify anything else it is automatically going to return the first element for you so now to print out the remaining elements what you're going to do is that you are going to use the for loop and you are going to write in for auto it er it let's make it equal to it is going to be a change element we are going to write in mp.cbegin and as I told you that cbegin automatically contains the very first element so we are going to write in it is not equal to mp.cn and what is this actually I'll explain in a moment and then we are going to make this it plus plus and then in a body we are going to do the very same thing we need to just it first and then we have to add some spaces and then it second and finally an inline space so this is going to actually print out each and everything for us let's just add a few line spaces and I guess that's good enough let's just run this code so as you can see right here that it says the first element is one is the key and 10 is the element so as I told you that it is going to sort out the container first and then in accordance with that sorting it is going to print out the element so you can see the sorted list right here 110, 114, 220, 230 and 315 so in here the first element is 110 and as I told you that cbegin is actually going to return the very first element of our list so this is how this actually works so let's move on so the next one is cn, cn also is a built-in function in the c++ standard library of multi-map that returns a constant iterator pointing to the theoretical element that follows last element in the multi-map now since multi-map container contains the elements in an ordered way cn will point to that that follows the last element according to the container sorting criteria if you talk about the syntax of this this is the very same but the only difference is that it contains n not n it is going to contain cn actually so it does not accept any parameter and if you talk about the return value it is going to rate in a constant iterator that is pointing to the theoretical element that follows the last element in the multi-map and if you talk about the example of this so we have this itr equal to mp.cn cbegin we are going to just remove this and now let me just explain this cn over here so as I explained you in the explanation before that it is going to actually return a constant iterator that is pointing to to the theoretical element that follows the last element in the multi-map so it is going to iterate this very old stuff till the very last element which means that this it is going this mp.cn is going to tell this it that it is going to equal to so if you just for example you have the very same statement that was this statement and I make it equal to cn over here and then it is not going to return the complete thing it is going to just return this stuff the itr first which means that it is going to actually return the count so if I just run this code you're going to see what's going on in here so as you can see that it says the last element is 5 which means that this it is not equal to mp.cn mp.cn has actually told this loop that you have to iterate for five times which means that there are five elements in this multi-map so this is basically the purpose of cn that is going to return a constant iterator that is going to point to the theoretical element that follows the last element in the multi-map so I hope it is clear so let's move on to the next part that is the cr-begin now multi-map cr-begin is also a built-in function in c++ standard library which returns a constant reverse iterator that is referring to the last element in the multi-map container since multi-map container contain the elements in an ordered way therefore cr-begin will point to that element that will come last according to the container sorting criteria so if you talk about the syntax of this you can see it right here it does not have any kind of arguments the return value is going to be a constant reverse iterator referring to the last element in the multi-map container and if you talk about the example of this we are going to use the very same example we already have these elements what we are going to do is that we are going to write in cr-begin so as I told you that it is going to return a constant reverse iterator so this very same thing is going to and we are going to also then now use this iter last not last it is going to be second and then these line spaces and moreover we are going to do in here is that we are going to write in cr-begin here and cr-end over here as well so if I just run this code now we are going to see what is the difference between c-begin and cr-begin so you can see that it says the last element is 315 as you can see right here the 315 that was the first element sorry the last element in the previous case of c-begin is now the very first element it does not have to be the last element it has to be the first element over here so as you can see right here it says the first element is 315 so now if you talk about the list right here you are also going to see a very major difference here you can see that the list is basically now reversed completely the first element is now at the last because cr-begin is going to return the constant reverse iterator it is going to build that in it is going to start on with 315 and it is going to keep iterating until 110 so it is going to actually reverse your multi-map list and then if you talk about the last one that we are going to discuss in this tutorial that is cr-end it is also a built-in function in the c++ standard library which returns a constant reverse iterator that is pointing to the theoretical element before the first element in the multi-map now now since the multi-map container contained the elements in an ordered way cr-end will point to the element theoretically before the first element according to the container sorting criteria and if you talk about the syntax for this you can see it right on your screen it does not accept any parameter and the function is going to return a constant reverse iterator that is pointing to the theoretical element before the first element in the multi-map and if you just talk about an example we already have covered it it is this mp.crn this now is going to actually return a constant reverse iterator that is going to point towards a theoretical element before the first element which in case of c begin was before the first sorry before the last element but now in this case it is going to be the before the first element in the multi-map so i hope you have understood all of these four so that's it with this tutorial as well thank you so much guys for watching and i'll see you guys in the next tutorial hey guys what's up this is marhan and i welcome you to another tutorial on multi-map in c++ in this tutorial we are going to be covering three major functions that the first one is size the second one is swap and then finally we are going to find the maximum size so starting on with the very first one of this tutorial that is size so multi-map size is a built-in function in c++ standard library which returns the elements in the multi-map container simple enough as you can see from its name as well so this is the syntax for this it does not accept any parameters and it returns the number of elements in the multi-map container that it contains so i guess i don't have to tell you anything about it let's see an example for how this is going to be done so let's create first a container and the container is going to be int int and this is the object for that and then we are going to insert elements into the multi-map to 10 and let's just copy this from here and let's say we have this five elements let's just change the keys and let it be two and let's just change the elements as well all right so we have a container now and we have five elements inserted into that container as well now if you want to find the size of that container what you have to do is that you have to use the build in function from the standard library that is size so what you're going to do is that you're going to see out multi-map mp has you're going to write an mp.size and as i told you it is going to not contain any parameters so we are going to move on and multi-map has this is going to generate the number so multi-map mp has that number of elements good enough so return zero and let's just run this code all right so you can see right here that it says multi-map mp has five number of elements so this is a very simple function and this is what is the purpose of this size function to actually find the number of elements for us so let's move on so the next topic which we are going to cover is the swap the multi-map swap is also a build-in function is c plus plus standard library which actually swap to a multi-map containers so the contents of multi-map one are in multi-map two and context of multi-map two are in multi-map one after the swap function is called so if you talk about the syntax it is a complex type of syntax that has two multi-maps the first one is multi-map one dot name and it has the swap function and in the parameters we are going to specify multi-map two dot name so basically this function except one parameter which is to be swapped with the multi-map one dot name so this multi-map two underscore name is going to be swapped with multi-map one underscore name and if you talk about the return value this function is going to return nothing so let's just see an example for how this is going to work so here we are we already have one container that is multi-map with mp let's just remove this stuff from here all right so we already have one container present what we need to do is that we have to swap it with another container so what we are going to do is that we are going to create one more container and what we are going to write and we are going to write in mp one over here so when now this mp dot insert is called it is going to insert element into the objects of this multi-map so if you just copy this from here and paste it down here and just change the name of the object mp one mp one mp one dot insert so basically this statement has created two containers for us the first one with the object mp the second one with the object mp one so when mp dot insert is called elements are inserted into this multi-map and when mp one dot insert is called elements are inserted into this multi-map right over here so now what we are going to do is that we are going to actually swap the elements of these two so first to see how this is going to work on let's first have a cout statement elements before the swap function is called it's a two line spaces and now what we are going to do is that we are going to create the simple cout functions that were created in the previous as well so what we are going to do is that we are going to create a loop we are going to write in for auto id er equals to mp dot begin and we are going to specify the condition is not equal to mp dot end then what is going to do is that it's going to plus plus it er and in here what is going to print out it's going to print out cout it er first then some space and then the element the element is printed using it er a second then and and land space so after it has printed elements before the swap function and we are going to see out elements of container mp good enough now we are going to see out elements of container mp1 and before that what we are going to do is that we are also going to add a line space here so now we are going to create this very same functions we are going to just copy it from here paste it right here and we are going to write in mp1 dot begin mp1 dot end and it is going to work perfectly so this is basically print out the elements for us so let's just run to see if it works so you can see right here that elements before the swap function is called elements of container mpr right here and elements of container mp1 are right here as you can see right at the top here as well as you can see that mp dot insert has this 112 then 114 then 210 and then this 313 and then for mp1 it starts on with 112 of 114 then 210 215 and then 313 so now we have these two containers we have printed the elements before the swap function as well now what we need to do is that we need to now perform the swap function between these two containers so right down here what we are going to do is that we are going to write in mp dot swap and what we are going to write in this is we are going to write in mp1 which means that elements in the container mp are going to be swapped with mp1 which means now if you just print the elements of both the containers the elements that were previously in this container mp are going to appear in this container mp1 and the elements that were previously in container mp1 are going to appear in this container mp so what we are going to do is that we are going to simply copy all these statements from here and we are going to just paste them here so elements of container mp here and elements of container mp1 here and what we are going to do is we are going to see out elements after the swap function is called. So if you just run this code and let me just maximize it and these are the elements of container mp these are these are the elements of container mp and these are mp1 sorry and these are the elements before the swap function. Now if you consider this first one that is the elements of container mp and then you consider the elements of container mp again after the swap function is called you're going to see that they are completely different and they are actually the elements that are the container mp1 elements and if you consider this mp over here and after the swap function you consider the mp1 they are the same which means that mp is now mp1 and mp1 is now mp. So this is how basically the swap function works. So I hope it is clear so let's move on to the next one that is the maximum size. Now this is also a building function in C++ standard library which returns the maximum number of elements in a multi-map container can hold. So if you talk about the syntax of this it is a very simple syntax as you can see on your screen that is the multi-map name then we have the max size function and the function does not accept any parameters and if you talk about the return value it is going to return the maximum number of elements a list container can hold. So let's see an example of how this is going to work so let's just remove this code from here and all we need to do is that we have to contain we have to create a container so we have this container we have two containers one with mp1 and then one with mp so what we are going to do with this is that we are going to actually find its maximum size so maximum size is as I told you is the maximum number of elements a container can hold so we are going to see out elements in container mpr and then we are going to call in mp.max underscore size and then we are going to to find the size of the second container we are going to write in elements in container mpr mp1.max size and finally in nline space and moreover we are going to add one here and we are also going to add max number of elements and max number of elements here as well in container mp1r this and number of elements in mp1r this so let's just write return zero and run this code so as you can see that it says maximum number of elements in mpr 461168601842738790 and they are the very same for this container as well so both these container can hold the very same number of elements so this is the maximum number of elements you can actually insert into the container mp and mp1 so I guess that's clear so that's it with this tutorial thank you so much guys for watching and I'll see you guys in the next tutorial. Hey guys what's up this is Umar Khan and I welcome you to another tutorial on multi map in C++ in this tutorial we are going to be covering the lower and upper bound in multi map C++ so we are going to start on with the first one that is the lower bound the multi map lower bound is a built-in function in C++ standard library which returns an I traitor that is pointing to the key in the container which is equivalent to the key that is passed in the parameter now in case the key is not present in the multi map container the function returns an I traitor that is pointing to the immediate next element which is just greater than the key now if the key passed in the parameter exceeds the maximum key in the container then the I traitor return points to key plus one and the element is equal to zero so if you talk about the syntax you can see it right on your screens it is multi map underscore a name dot lower bound and then the key so this function accepts a single mandatory parameter key which specifies the element whose lower bound is to be written now if you talk about the return value the function returns an I traitor that is pointing to the key in the container which is equivalent to the key passed in the parameter in case the key is not present in the multi map container the function returns an I traitor pointing to the immediate next element which is just greater than the key whereas if the key passed in the parameter exceeds the maximum key in the container then the I traitor return points to the key plus one and the element is equal to zero so let's just see an example for how lower bound is going to be implemented using our code so we will just move on to our editor and here the main function the first thing we need to do is that we have to can create a container multi map container that has been in which means the key is going to be an integer as well as the element is also going to be an integer so the object is mp so now using that mp we are going to call in the insert function and we are going to insert certain elements so this is the first element that is 23 with the key one then let's say using the key two we have 10 and then mp.insert again and then let's say we have using key one we have let's say 30 over here then using mp.insert and this time the key is let's say three we have 22 and the final element is let's say using key one again and it is let's say 30 let's make it equal to 32 because we already have a 30 all right so now what we need to do is that we need to check certain conditions for example the first condition is that we have to check if a certain element is present then for example let's just take this two and we know that this two is present in this container so what we are going to do is that we are going to write in auto it equal to mp.lower underscore bound and we are going to pass this two over here so now what we are going to do is that we are going to see out the lower bound of two is we are going to just end it over here and we are going to see out a pointer to this it dot first this has to end right here it dot first and we are going to add a space before inserting the element and we are going to add id again and we are going to write in dot second and finally and l now let's just run this code so as you can see that the two is present so it says the lower bound of two is to 10 so this is actually the lower which means that with key two the lowest element present is this one that is the two 10 now the second case is when a certain element is not present so in this case you can see that four nothing with the key four is present so if we just pass in four over here write four over here and run this code now you can see that it says the lower bound of four is the key is five since it was an argument as passed and it says zero which means that no element was found because the key four actually does not exist so it does not know what is the lower bound of that key which is actually not present so the next is that next case is when certain range is exceeded for example so you can see that we have one two one three one until here and we do not have a certain element that exceeds that so what we are going to do is that for example let's just specify four over here and just add three over here which means that it is in this range it is in this range one two and three in between it four and then one but we do not have the element that is exceeding in the previous case when we used five over here for sorry even it was three over here and we added four over here so that was out of the range of this multi map so now we are in this range of multi map but this element actually does not exist so see what happens now you can see that it says the lower bound of three is 422 now no element was present with the key three but when this was entered with the lower with the lower bound of three first one two one and this element where it inserted and now we know that this element is going to be inserted because it has the key three and then finally this element has to be inserted at the very end so this was actually the lower bound of this key three but now if certain for example exceeds the limit as in the previous case when we used four and we had three over here it is going to give zero because now it does not have a lower bound since the limit has now been exceeded so this is how actually lower bound in C plus plus multi map works so the next one is the multi map upper bound the multi map upper bound is a building function in C plus plus standard library which returns an iterator that is pointing to the immediate next element which is greater than the key now if the key passed in the parameter exceeds the maximum key in the container then the iterator return points to key plus one and the element is equal to zero and if you talk about the syntax it can be seen right here it is multi map one underscore name dot upper bound and then the key so this function accepts a single mandatory parameter key which specifies the element whose upper bound is returned whereas if you talk about the return value as I told you earlier that the function is going to return an iterator pointing to the immediate next element which is just greater than the key so if the key passed in the parameter exceeds the maximum key in the container then the iterator would return point to key plus one and the element is equal to zero so let's just see an example of upper bound as well so we will have this very same example all we need to change is this function over here make it equal to upper bound make it equal to upper bound here as well and now we are also going to discuss certain scenarios now when an element is present now when an element is not present and when the range is exceeded so the first case is when an element is present so let's just add two over here two over here so now it is going to give us the upper bound so let's just run this code so as you can see that it says the upper bound of two is four two two which means that the upper bound of two means that the last element after the two which means that the element that is going to be at the last of the one having the key two is this 210 and after that we only have this element now if it was something like three over here and something like four over here and now if you just run this code you are going to feel the difference now you can see that it now says the upper bound of two is three two two because the last element with the key two is this and you know that after that the element that is going to be inserted is going to be this so basically upper bound means the element with the key last which means that if you specify this key to be two which means that any elements with key two the last element of that that is this in this case the element after that is going to be printed by the upper bound and when you are talking about the lower bound then the lowest element with the key the element before that element is going to give you the lower bound so now in this case it is going to print out 322 because this is the element that is going to be printed after the last element with key two so now the second case is when a certain element is not present so for example we have this one over here which means that three is not now present and it is in this range so if you just write in three over here three over here as well and run this code you're going to see that it says the upper bound of three is four three two which means that before this four three two any element that is at its top is going to be actually that so this is since the last element and you know that everything is with one to one so after the three this four is only going to appear so the next case is when you actually exceeds the limit so for example you write in just five over here and now run this code so you're going to see that it says upper bound of five is five zero as you know that no element with this key exists so no key after this element is going to exist so the upper bound is zero so these are the two functions the lower bound and lower upper bound and these are the three scenarios that are related with each of these so we already have covered an example with a bound so that's it with this tutorial thank you so much guys for watching and I'll see you guys in the next tutorial hey guys what's up this is Sumerhan and I welcome you to another tutorial on multi map in c++ in this tutorial we are going to be covering a single topic that is equal range so the multi map equal range is basically a built-in function in c++ a standard library which returns an iterator of pairs the pairs basically refers to the bound of a range that includes all the elements in the container which have a key equivalent to the argument that is passed into this function equal range so if there are no matches with the key the range return is of length zero with both iterators pointing to the first element that has a key considered to go after the key according to the container's internal comparison object so if you talk about so if you talk about the syntax this is the syntax for doing it iterator multi map underscore name dot equal range then the key so only one argument that specifies the element whose range is in the container that is to be returned whereas if you talk about the return value as I told you before it is going to return an iterator of pairs and that pair refers to the bound of a range that includes all the elements in the container which have a key equivalent to the key that is passed in as argument now if there are no matches I told you the length is going to be zero with both iterators pointing to the first element that has a key considered to go after the key that is passed in as an argument according to the container's internal comparison object so let's just see an example for how this is going to be quoted so in simple words if explain this basically equal range is going to for example if you just pass in the key as one then it is going to return all the elements in that container that have the key one and if you pass in two as an argument then it is going to return all the elements that has a key two so how this is going to be done let's just call it so the first thing you know that we need to do is that you have to create a container it has to be in this and then this mp and then using this object we are going to insert elements so let's say the first element is 1 comma 20 then we have mp.insert in which the second element is let's say 2 comma 20 or let's make it equal to 22 since 20 is already there so let's make another one and make it equal to 1 and let's make it into 10 and let's just insert one more element and let's say the key of it is again 1 now let's make it equal to 15 so we have these four elements in which three have the key one and one has the key two so now how we are going to use the equal range function we are actually going to store it in an attriter as I told you before we are going to write in auto it equal to mp.equal underscore range and we are going to specify the key let's say for the first time we want to find the occurrences of this one so we are going to write in this one is an argument then we are going to print on all the elements so we are going to use a follow-up for that and we are going to write in for auto itr equals to and then we are going to use this object it dot first the condition is going to be itr is not equal to it dot second and finally the closing condition is going to be plus plus itr now in this loop what we are going to print out is the very same thing we are going to print out itr first then a few spaces and finally itr second and an online space so right above here we are going to see out elements with the key equal to one good enough so let's just run this code so as you can see that all the elements that have the key one are actually printed elements with key equal to one or 120 110 and then 115 so if you just for example change this to two and now write in here two and specify this as two as well and now run this code you're going to see the elements that have the key that is equal to two so you can see that two elements have been returned and you can see that we have two elements that is 215 and this 222 so this is a very simple function whose purpose is only to return the elements that equal the range that is specified in as our argument into this function so I guess that's clear so that's it with this tutorial as well thank you so much guys for watching and I'll see you guys later hey guys what's up this is Umair Khan and I welcome you to a new section we are going to start on from this tutorial that is the linked list in this tutorial we are going to be covering only the overview of that section and I'll be showing you the content which we are going to be studying in this section so moving ahead now this is the content which we are going to be covering in this section the first one is the introduction of linked list then we will be studying the difference between linked list and our arrays then we will be studying the types of linked list then we will be studying the operations that can be performed on linked list and we will be seeing a lot of examples now in the introduction section we will be covering the definition of linked list we will be studying the purpose of linked list and we will be seeing the difference between linked list and arrays then moving to the next section moving to the next type of this section, we are going to be studying the types of linked list in which we will be covering three types of linked list, singly linked list, doubly linked list and circular linked list. And we will also be studying some operations on linked list that can be performed on any three kind of linked list may be simply doubly or circular. That includes adding elements to a linked list, adding elements to an already created linked list, including elements from a linked list, accessing elements of an array, additional techniques, and finally a lot of examples. So there is so much for you guys to learn in this section. So I hope you stay tuned and learn all of those concepts. So thank you so much guys for watching and I'll see you guys in the next tutorial in which we will formally start on with this section. Hey guys, what's up? This is Umar Khan and I welcome you to this new tutorial. In this tutorial, we will cover the introduction of linked list. So now what is a linked list and what is the purpose of a linked list? Now a linked list is a sequence of data structures which are connected to each other through links. Each link always has a data pod to store data and that what is the purpose of a linked list that is used to store data in it. Now each link is connected to another link and that's why it is called a linked list means a list of links in which each link has a data part as well as each link has a reference or you may say a pointer that points to the next link in the list. And that's how all this scenario is connected to each other and that's why it is called a linked list. In C++, a linked list is formed through a structure in which you have to define the structure of the linked list which you will be using. Now you will cover the types of linked list and the structure formation of each of those types in the future tutorials. For now, you need to just understand the definition and purpose of a linked list. Now for example, if you consider this diagram over here, you can clearly understand what I've been telling you up till now. Don't worry about the hat for now, just remember that it is from where the linked list gets started. Now linked list just contain a linked element called first, you can see that each link has a data field and a link field that is called most often next. Now each link is linked with its next link using its next link. And last node next points to null which indicates that the linked list has now ended. For example, as you can see over here that this is the starting position probably the nodes are going to be started. This is the first node and that here we declare a pointer name as head that points to the first node and then the first node contain a data path as well as a next part this part actually which can contain the address of node number one which is right here is actually called the next part of this complete node right here. So this address of node one is stored in the debt sorry the address part of this first node and then we have this node one that also has a data path and a next part. Now its next part is going to contain the address of the last node in case we have three nodes. In case we would have a node two here, then it must have contained the address of that node and then that node would have contained the address of last node. So this is actually a basic form of structure that how multiple nodes in a linked list work. So you don't need to worry about this we will be covering this for every kind of linked list we will be going through. So for now you just need to understand what is the purpose of a linked list and why do we have to use linked list. So I guess that's it with this tutorial. Thank you so much guys for watching and I'll see you guys in the next tutorial. Hey guys what's up this is Omar Khan and I welcome you to a new tutorial on linked list in C++. Now in this tutorial we will see the comparison of arrays and linked list and we will see also some of the major differences between these two. So let's start on with the comparison between arrays and linked list based on certain properties which you can see right on your screen. So to compare these basically array is a consistent set of fixed number of data items whereas if you talk about linked list it is an ordered set comprising of variable number of data items and if you talk about the size then array has a size that is specified during the declaration whereas in linked list you don't need to specify the size it can grow and shrink during its execution. Now the next one is the storage allocation if you talk about the storage allocation of an array then an element location is allocated during the compile time whereas if you talk about linked list then element position is assigned during the runtime. If you talk about the order of elements then in an array elements are stored consecutively whereas in linked list they are stored randomly. If you talk about accessing elements then in an array direct or random access is used where you specify the array index or subscript whereas if you talk about linked list then we use the sequential access method in which you traverse starting from the first node in the list by the pointer. You will see these all this concept what is a pointer you don't need to worry you just need to know the difference between these two. Moving ahead the next one is the insertion and deletion of elements. In an array if you want to insert or delete elements then it is relatively slow as shifting is required whereas in linked list it is very easier it is faster and it is very efficient. If you talk about searching then arrays use binary search and linear search whereas a linked list use linear search only whereas if you talk about the memory required then an array required less memory whereas a linked list require more memory whereas if you talk about memory utilization then array is ineffective in memory utilization whereas a linked list is a very efficient efficient in memory utilization. Now to talk about the key differences between arrays and linked list. Now the first one is that an array is the data structure containing a collection of similar data type elements whereas in linked list it is considered as non primitive data structure which contain a collection of unordered linked elements that are known usually as nodes. If you talk about the second difference then in an array the elements belong to indexes that is if you want to get into the fourth element you have to write the variable name with its index or location within the square brackets whereas in linked list so you have to start from the head and work your way through until you get to the fourth element. While accessing an element in an array it is fast whereas linked list being linear are quite slow. Operations like insertion and deletion in array consumes a lot of time whereas performance of these operation in linked list is quite fast whereas if you talk more then the requirement of memory is less due to actual data being stored within the index in the array as against there is a need for more memory in linked list due to storage of additional next and previous referencing element in addition memory utilization is inefficient in the array conversely memory utilization is efficient in the array as I told you before so these are some of the key differences there I guess there was no similarity except that both are used to store data the rest are all differences so these were some of the key differences between these two in the next tutorials you will be seeing how why do we use linked list and the advantages of using a linked list why we prefer to use linked list and the advantages of using an array of why we use arrays there are scenarios where you have to use an array and there are scenarios where you have to go on with linked list so I guess that's it with this tutorial thank you so much guys for watching and I'll see you guys in the next tutorial. Hey guys what's up this is Umar Khan now welcome you to a new tutorial on linked list in C++ in this tutorial we will see the different types of linked list we have in data structures of C++ so here we have the three types of linked list that we have in data structures of C++ that include singly linked list, doubly linked list and the circular linked list so let's start on with the singly linked list a singly linked list as by its name you can guess that it can be traversed only in one direction a singly linked list if you talk about its structure it contains two part the first part is the data part and the second one is the address part this data part right over here is used to store data and this address part is used to contain the address of the next node to form a link of lists now this is a structure of multiple nodes I've already covered this in the introduction to linked list now this is the head node this is the starting pointer which we usually create that is used to point towards the first node now this is our first node right here that contains the data part and a next part as well now a next part as I told you earlier contain the address of node one which is right here then in the node one we have data part and then since in this structure we have only three nodes so this is going to contain the address of the last node which is right here the last node also contain a data part and then this has this data part since this is the last node so this pointer points to null which indicates that a singly linked list has ended now moving to our doubly linked list as you can see from its name that this is a linked list that can be traversed in both direction if you talk about its structure it contain three parts two address parts and a single data part now if you consider the structure of multiple nodes the concept will be much more clear as in singly linked list doubly linked list also has a head part that points towards the first node now this had points completely towards this node not stored this section only it points towards the complete node then since this is the first node so its back pointer will point to null which indicates that this is a starting node then it will have a data part that will contain any kind of data you want to store in it then its next part is going to contain the address of node one so this is not one right here node one again has three parts now this part that is the previous part of this complete node it contain the address of the head node which is actually the node which is previous of it so using this back pointer at this this complete structure of doubly linked list can be traversed in the backward direction as you can see in back here in the singly linked list that we don't have a back pointer to traverse it in the backward direction we only have a forward pointer to traverse it in the forward direction whereas now in doubly linked list we have both the pointers as this back pointer as well as well as this forward pointer so this is going to connect it backward and this pointer is going to connect it forward as you can see right here that this contained the address of last node whereas this previous contain the address of node one which is the previous node of this last node right here then a data part and this pointer again points to null which indicates that a doubly linked list has ended so moving towards the last kind of linked list we have in data structure that is the circular linked list now a circular linked list can be both singly linked list or a doubly linked list it is only one difference that the last node in singly and doubly linked list which point to null is now pointed towards the first node let me just show you now this is the structure of a singly linked list as I told you that the circular linked list can be both singly linked list as well as doubly linked list so these are the two structures that can be formed and then if you consider the circular linked list structure in case of singly linked list then it is the very same as of singly linked list except that in singly linked list this pointer points to null and in circular linked list now this pointer points towards the first node and the next of the next node which mean that this pointer contain the address of this node now so this makes it a circle which rotates rotates rotate until some condition is met and this condition you are going to see in the future tutorials don't need to get confused about it now and now this is a structure of a circular linked list in case of a doubly linked list so it is the very same as a doubly linked list except the very same difference as in singly linked list that this pointer now points towards the head next node so I hope that you have understood all the three types of structures in the next tutorial we will be doing the coding part of all of these one by one so stay tuned for the next tutorial thank you so much for now hey guys what's up this is Umair Khan now welcome you to a new tutorial on linked list in C++ in this tutorial we will be seeing the structure of a singly linked list now this is how a structure of a singly linked list actually look likes and this we have to use the building word named as struct then we have to define the name of the structure we are going to use and this can be any name you can call it node you can call it by your name and you can call it anything now this contain two part as we are talking about singly linked list so as you know that singly linked list contain only two part a data part and an address part so in this we are going to initialize only two parts that is the data part and an address part and then using this structure we define using this keyword struct we are going to use it in our program so how is all this stuff done let's code it so I will move on to my compiler def C++ and here I am right now so the first thing as you know that I need to write in hash include ios stream then I'm going to write in using namespace std and here is my main now you need to understand one thing right here that the structure you are going to define is going to be outside this main at the top right here so I'm going to write in struct I'm going to name it as note and then these curly braces right here and this column is going to be put in by default by your compiler so in here I'm going to write in in data which means that my data part is going to store integers and I'm going to write in note steric next now what does this mean this means that for this structure named as note I have created a pointer which is named as next now this is not a compulsion this is not a compulsion to name these as I have named it you can name it anything but a good programming technique or practice is that you have to name variables according to the job they do so that it become easy for everyone to read your code so I've named the data part as data and the next part as next and this is going this pointer is going to be initialized using the name of the structure you specify right here which is again not a compulsion you can name it anything so now towards the main part now since you have got the structure so now you need to use this structure and add data parts and create pointers towards the next parts as well so the first thing you need to do is that you have to create one more pointer let's say P and now I'm going to write in P equals to new node now what does this P do that it is going to create a new node and what kind of node is going to create is going to create a node that is of this structure which we have defined right here which means that now a data part and the next part is going to be created by default so now I am going to give values to this data part as well as this next part using this pointer which is the pointer P that is at this time pointing towards the node I have just created using it so I'm going to write in P data equals to let's say 10 now this means that the data part is going to contain 10 always remember that this is always going to be an arrow which means that P data part contains 10 now the next thing I need to do is that I need to initialize this next part so I'm going to write in P next equals to null since for this time this is the last node since this is the first node so at this time it is going to be the last node the connection is going to be established when I create a new node and then I'm going to create a connection between the two nodes so let me just create a second node and show you but first let me just output this so that you can see that what's going on right here so I'm going to write in C now I have output actually P arrow data which means that I have outputted the data part of this node so if I just run this code you can see right here all right I have some arrow here it says that new all right this needs to be small letters now it will run perfectly now you can see this 10 right over here so at this time if I write in C out the next part and output this code you can see that it contains zero since I have written it right here that its next part can is now pointing towards null if I just remove it from here and add it before I initialize it to null and just run this code you can see that I have the address right here that contains the address of actually this part so since I have now initialized it to null right using this statement that P next part now points to null so if it is placed here it makes it equals to zero as you have already seen so let's create a new node so the first thing we need to create a node is that we need to create a pointer for that node star of Q and then I'm going to write in Q equals to new node and then the very same thing that I need to initialize the data part let's make it equal to 20 this time and I'm going to initialize the next part as well that is going to again point to null now now I have two nodes right here the first one is this node that is created using the pointer P and it's data but contain 10 and it's next pointing to null and then I have this node that is created using the pointer Q and it's data but contain 20 and it's next is pointing to null now actually for example I want this P to be the first node of the linked list and this Q to be the second node of the linked list which means that I need to connect this Q right here this node Q at the end of this node P which means that this is going to be the last node now so its next is again going to be pointing to null so I don't need to do anything with the next of the Q all I need to do is that I need to update the node of the next node of next sorry the next part of this node P so that it points to this node Q so to establish the connection let me just write in here connection to establish a connection I need to write in P next is going to equal Q now what does this mean that this means that the P next is now pointing to any node that is created using this pointer Q so if I just run it I guess I don't need to run it now let me just first output something and then show you so for example now if I write in see out P next data comma here this lesson sign here now as you can see that I have outputted the P next data now what does this mean now as you know since I have established a connection between P and Q right here so now P next is pointing towards Q so this P next statement means Q and I have outputted the data of Q so let me just comment these two statements out here so that this output does not confuse you so now let me just try this code and you can see right here that I have this 20 on my output screen now why have this 20 on my output screen because I have outputted the data part of the Q and I have used P next for that purpose so I hope that you now know how to create a node how to create a structure and how to establish a connection between two nodes so I guess that's it with this tutorial because the purpose of this tutorial was to let you know about the structure of a singly linked list and how to establish connection between two nodes so thank you so much guys for watching and I'll see you guys in the next tutorial hey guys what's up this is Umair Khan and I welcome you to another tutorial on linked list in C++ now as you know the structure of a singly linked list from the previous tutorial now in this tutorial what we are going to do is that we are going to study insertion in sync list we are going to see how to insert elements in a singly linked list and how to display the new linked list that have been created after inserting elements into a singly linked list and moreover we are going to see the different types of schemes in which we are going to insert element into a singly linked for example we can insert elements into a singly linked list at the start we can insert elements at the end and so on goes for many other cases which we are going to be studying in this tutorial so wasting no more time let's move on to fc++ so before we do anything in this main function we need to define the structure above this main function so we are going to write instruct and we are going to name it the very same name we did in the previous tutorials and it is going to be the same the data part is going to store integer values and we are going to steric next which means that it's going to contain a next part as well so now one thing we need to introduce right here is the concept of objects and classes and we need a class which will contain the function to handle all the nodes this class should have two important pointers that are going to be the head pointer and the tail pointer and we will have a constructor of that class that is going to initialize these head and tail pointers to null to avoid any kind of garbage values so these this class is going to be added to actually encapsulate each and every function we are going to add so we are going to create a class name let's name it singly since you are dealing with singly lists and in this class we are going to define private and public members so first we are going to initialize the private members that are going to be the node node with a capital N that is going to be the tail and the head pointer now in the public area we are going to have our constructor function that is the singly and in this we are going to make the head equal to null as well as the tail equal to null now this is just used here to avoid any kind of garbage values let's just add a call and write here to complete the syntax now we are going to create a function that is going to be the create function and it is going to receive one argument that is the value or you may say the data part that is going to be received here and it is going to be inserted into the data part of whatever node we create so before doing anything let's first create a node and then we will check for whether this node is going to be the first node and how its connection is going to be established and whether it's going to be a second node or third node or the fourth node and how its connection is going to be established with the previously established nodes so let's just create a new node using a temporary pointer so now we have created a node and now we can just initialize values to its data part as well as its next part so we are going to write in temp data equals to the value that we are going to receive in this function right here and we are going to write in temp next equal to null so now we are going to check in two conditions that whether head is pointing towards null or not now in case we are creating the very first node the head is going to be pointing towards null and the condition that is if head equal equals to null this condition is going to be true and a set of condition in this block is going to be executed right here now why this if head equal equal to null is going to be true and when it is going to be equal to true this is going to be equal to true when we are creating the first node why because for the first node this constructor is going to be called since an object of this class is going to be created and this white create is going to be called using that object so without creating that object we can call this function so when we create an object we are calling the constructor function head equals to null tail equals to null and this condition is going to be executed now for the second node this constructor is not going to be called and in this head equal equal to null what we are going to do we are going to change this head pointing and this no head pointer does no more points towards null so for the second node this condition is automatically going to be false since let me just code it then it's going to be much simple for you we are going to write in head equals to temp tail equals to temp and temp equals to null now we have pointed the head towards the newly created node using the that was created using this temporary pointer the tail also pointing towards that newly created node and the temp equals to null so when now the second node is created and this condition is checked this head does not equal to null this now point towards some node so in the else condition that is going to be executed for any node created after the first node what condition is going to be executed is that it's going to make the next equal to temp and then tail equals to temp now what is the purpose of this basically now as you know that in the case of first node we have pointed this tail pointer towards the temp now in case of the second node this node is created the temp is pointing towards the newly created node now now the temp next first we initialize it to null now to establish a connection between this second node created right here and the first node that was created using these same things but the pointers were initialized as this so now in case of the second node you know that the tail is pointing towards the first node as you have written here temp equals tail equals to temp now if you write in tail next equal to temp which means that you are establishing a connection between the first node and the second node since you are making the tail next which is pointing towards the first node this tail is pointing towards the first node we have written it's next is going to be equal to temp and now temp is pointing at this time this temp is pointing towards the newly created node that is the second node not the first node so after establishing a connection we are going to make tail equals to temp now this is a very interesting and important concept to grasp right here why we have to make move this pointer tail towards the second node now now this is moved forward so in case when we now we are creating the third node for example then this tail pointer is going to help us to establish a connection between the third node and the second node for example we created the third node so now this tail pointer is pointing towards the second node and this same condition is going to be executed that's tail next which means the second node next is going to point towards temp and now temp is actually going to be the newly created node for the third node not the second node and then similarly this tail is going to point towards the tail towards the next node so always remember that the tail after establishing a connection between the previous node and the newly created node move forward towards the latest created node so that it can be used in the future for establishing a connection if there are any new nodes created so let's move on to the main function now and in the main function i'm going to write in singly and the object is going to be s1 and we are going to write in singly right here so now using this object s1 we are going to call the function name create and we are going to pass in the value that is going to be the two for example now this function is called two is passed a new node is created its data part is going to contain two this condition is true since it is the first node the head is going to be equal to null then the head is now going to go towards the newly created node the tail is also going to point towards an added the temp is going to be equal to null and in case i create a second node that can stay contained three now so this condition is going to be false this condition is going to be executed the tail of which is pointing towards this node is going to actually establish a connection with this node which has a temp pointer pointing towards it so after establishing a connection the tail pointer is going to now point towards this and in case i create one more node pass let's say four to it so now the tail pointer since it is pointing here and the temp is now pointing here so we are going to write in temp sorry the tail next point towards temp that is going to establish a connection between these two nodes right here and then finally i'm going to make temp tail equals to temp which means that the tail is now pointing towards this newly created node so to display the output of all this stuff let's create a display function the function is going to be named as display and in this display function we are going to use a temporary pointer that is going to equal to a new node we are going to make this pointer point towards head because we are going to start from the head move towards that and print out anything that comes in our way so we are going to initialize a while loop and we are going to write in while temp is not equal to null what it's going to do is that it's going to see out temp data part and we are going to then write in temp equals to temp next so now what this is going to do is that at the first stage the temp is pointing towards the head node which is the starting node now we are going to initialize a condition that while temp is not equal to none which means that the data part sorry the next part is not equal to null which is only going to be null in case of the last node so we are going to see out any kind of data and we are also going to move the next point sorry the temp pointer towards the next node for example in for first for the head node then for the first node then for the second node then for the third node and so on until and unless the singly linked list ends so it is going to print all the elements for us so let's just call this function as well using the very same pointer s1.display and let's just run this code so let's just save it replace it write there need to be no space between this all right so you can see right here that we have this two three and four printed on our screen so i hope it is clear now this was the case when insertion in the linked list was done at the end now there are two other cases in which insertion can be done the first one is that insertion can be done at the start of a singly linked list insertion of a new node in this case is quite simple it is just a two-step algorithm which is performed to insert a node at the start of a singly linked list the new node should be connected to the first node which means the head this can be achieved by putting the address of the head in the next field of the new node the new node should be considered as a head it can be achieved by declaring head equals to a new node so this is a very simple code let's just write the code for how this is going to be done so for this we will just create a function down here and let's just name this function to be something that match what it's going to do is that let's say insert start and it is also going to have an argument that is going to be the int value that is going to be the data part actually it's going to be received from where the function is going to be called so the first thing when this is going to be called is that we need to for sure create a node using the temporary variable and this is going to equal to new node and just insert data into the node that is going to equal to value that is received from the calling area and then we are going to write in temp next equal to head and the head is going to equal to temp now what does these two lines mean these two lines are just simple the first one is to create a node second one is to initialize the data part of it now this line comes this says that the temp next should be pointing towards the head because we have a previously created head and now this node that is created using this function insert start is going to be the new head so at first the temp pointer is pointing towards the newly created node we said that it's next is going to point towards head which is now going to be the second node so then we are going to move this head pointer back towards the newly created node that is going to be the head node now so we write in as head equals to temp but before moving this head pointer back we need to establish a connection between the previous head and the newly created head so to create a connection between the previous head and the newly created head this line is doing that temp next equal to head and after establishing a connection we are going to move that head pointer back from the previously created head towards the newly created head so now the head is pointing towards the newly created node and the previously created head is no longer the head it is the first node and the newly created node using this line is now the head and the head pointer is also pointing towards that newly created node that was created using this temp pointer so this was quite simple let's just add a function before the display function and it was like this and we have to insert a value let's insert for example one run this code all right this is problem in this display function and error and yes a colon need to be added here as it says that expected a colon before s1 so let's just run it now and you can see right here that the one is now the head node and the data part of one is displayed before any other node so let me just change this to seven so that you may understand that it's not about the data but it's about the node you can see that this seven is at the top so i hope it's clear so the next part is how to insert a node at a particular position so the insertion of a new node at a particular position is a bit tricky to understand in this case we don't disturb the head and the tail node rather a new node is created between the two consecutive nodes so these two nodes should be accessible by our code we can call one node as for example let's say using a pointer named current and the other as the previous and the new node is placed between them so you have to perform two step for that purpose you have to pass the address of the new node in the next field of the previous node and you have to pass the address of the current node in the next field of the new node so we will access these nodes access these so we will access these nodes by asking the user at which position he wants to insert the new node now we will start a loop to reach those specific nodes we will initialize our current node by the head and move through the link list at the end we would find the two consecutive nodes and we will add the node we want to add a between those two consecutive nodes so let's just code it it's going to be a bit tricky so i hope you understand it at the first time if you don't go through the video again and try to grasp it so we will use the function let's say name the function as white insert position which means that we can insert it at any position and this is going to take in two variables the first one is going to be the position and the second one is going to be the value this position is going to consider the position at which we want to add our node and this value is again going to be for sure the data part so in this the first thing we need to do is that we need to create in two pointers the first pointer is going to be the previous pointer and the next one is going to be the current pointer that is also going to equal to new node so now we are going to create a new node using the temporary variable or you may say temporary pointer and that is going to equal to new node so now we are going to write in current equals to head which means that we are going to start looking from the head so we have pointed the current node towards the head as well and till where we are going to look we are going to use a for loop for that we are going to start from the head and we are going to use a loop till the position which we want to find or you may say insert of a new node so we are going to use a for loop and we are going to write in i equal to one which means it is going to start from one it is going to iterate till i is less than position which means one node less than the position which we want to insert and we are going to write in i plus plus for the iteration of the loop so right in here we are going to write in previous equals to current and the current equals to current next now what does this actually means the node which we want to insert its previous node is pointed this time by both the nodes sorry both the pointers that are the previous pointer and the current pointer but after this the newly created node the position which we want to create our node its previous node is pointed by the previous pointer and its next node is pointed by the current pointer now let me just show you this with the help of a simple diagram so for example these are the two nodes now for example this is the position at which we want to insert our node and this is our newly created node so all i want to say is that now the previous pointer now the previous node is pointing right here and the current node is pointing right here after this statement that is previous equals to current is executed both the pointers are pointing right here but after the second line is executed that says current equal to current next this pointer is removed from here and now it is pointing right here and this is actually the newly created node so let me just remove it from here and let's code the next two lines now after that what we want to do is that we will just write in temp data part equals to the value that was passed in and we are going to write in previous next equals to temp and the temp next equals to current now what does this means let me just show it to you right here again so when we write in temp data equals to value which means that we now have some data right here and the temp pointer is pointing towards this newly created node so now we want to establish a connection such that this previous next points right here and then this temp next point towards this next node this current pointing node so that we have this node inserted between these two nodes so for that we write previous next equals to temp which means that it's this next is now pointing right towards this node that is the newly created node and its next is pointing towards the node at which we have this current pointer right here so you can see that we have actually establish a connection right as here that this previous node is now pointing towards this temp and this temp is now pointing towards this current so we have established a connection and this node is now inserted in between the two consecutive nodes or the whatever position we want so let me just show you this with the help of now by calling this the name is insert position so we're going to write an s1.insert position so the first thing we need to specify is going to be the position for example let's say we want to insert after the second position and we want to insert let's say eight so let's just set up colon and run this code so you can see right here that at the second location we have this eight inserted right here so i hope it is now clear to you guys for example let me just change it as well then let's just say at the fourth location we want to add so you can see that at one two three four the fourth location we have this eight right here so i hope now you know how to insert a node at the start or make it ahead how to insert a node at the end of a singly linked list and how to add a node in between two consecutive node or you may say at any position in a singly linked list so this was the concept of this tutorial to teach you how to insert nodes in a singly linked list so i hope it was all clear if it is not go through the tutorial again make your own diagrams and use the code to make pointing from one node towards the next mode on a separate piece of paper and then you will be easily able to understand all these concepts so that's it with this tutorial thank you so much guys for watching i'll see you guys in the next tutorial hey guys what's up this is Umair Khan now welcome you to another tutorial on linked list in C++ in this tutorial we will learn about the deletion of elements or node you may want to delete from an already created singly linked list now deletion can be done in two ways from a singly linked list one you are given a key you want to delete and second deleting a key at a specified location means either by data or by location we will constitute all this stuff in a single function so the next time we can just call this function to do any kind of stuff related to deletion so let's discuss the first one delete a node from a singly linked list you need to follow certain steps first you have to find the previous of the node to be deleted second you have to change the next of the previous node and the third you have to free the memory for the node to be deleted so that no garbage value is generated all right so this is what we are going to be studying in this deleting element from a singly linked list so let's just when go to our compiler now and code all this stuff so here is my def c++ and this is the stuff from the previous tutorial so i just waste no time encoding this so i've already created this stuff that says this is a singly class it's a constructor and this is a create function from the previous tutorial and this is the display function from the previous tutorial i have removed some stuff from this code that we covered in the previous tutorial so that it doesn't get bulky and messy so this was i just done that so that no time can be wasted in creating that singly linked list we have to just go for the deletion purpose so we have to now initialize a function right here after this let's just do it before the display function so let's just name this function as delete and it is going to be avoid functions is going to return nothing and let's name it as delete node and now this function is going to receive in two arguments the first one is going to be the reference to the head of the list and the second one is going to be the key that deletes the first occurrence of the key in the list so we are going to just write in struct node head reference and then we are going to receive in the key now since we have we have specified here you must be in a bit of confusion that why i have written here struct node this is because i've written here struct node so that i am specifically talking about the structure of anything that is created by this node and why i have created this double pointer here this is because i am giving a reference that is pointer to pointer to the head of the list here so this is simply the key that is going to delete the first occurrence of whatever we pass into this so whatever this key is reached this is going to delete the first occurrence of that element so right here in this function what we are going to do is that we need to first store the head node so to store the head node we are going to write in struct node temp equal to head reference and a previous pointer now what is the purpose of this previous pointer you will just come to name a bit and this is also going to be pointer yep so this is a pointer here not f it's h e a t all right so the next thing we need to do is that we have to check if the head node itself holds the key to be deleted for example if a singly link list contain only one node then obviously this is going to be the head node so we have to check if the node to be deleted is the head node itself or not or else we want to delete any other node so the condition if a head node itself is to be deleted is going to be checked by the same condition so it is going to check if temp is not equal to null and temp data equal equals to the key which is passed right in here so what it's going to do is that it's going to say head reference equals to temp next free temp and return now what is actually going on here this is actually going to change the head for us it is going to make the head the very next node for example you are delete for example you let me just show you this right here so for example you have something like a structure of nodes like here you have one node here one node here and this is one more node here it doesn't seem pretty good in my drawing here but these are for example let's say three nodes you want so the key you passed in for example you passed in the key to this node at which the head is pointing at this time so before deleting this node what you have to do is that you want to move this pointer right towards this node which is the first node in this case so this is the first node all right so this first node needs this head right now because this is now going to be the new head since this head is going to be deleted since the key we passed in is of the head node so to delete this we have to first pass in this head pointer right towards the first node which is then going to be the head node so what this is going to do is that it's going to make the head reference towards temp next which means the temp next is this since temp at this time is pointing right in here since temp is created temp is going to point towards the very first node as we have right in here that node not steric temp equal to head reference and the previous pointer is also pointing right here so at this time we have three pointers pointing in here so if this is the head node is to be deleted this previous don't need to move anywhere this is simply to be deleted the head node is to be moved here this connection is to be diminished and the singly linked list goes on the very same way no other additions or no other subtractions or no other connections have to be established so now what does this do this is actually going to since this temp is pointing in here so this is actually this line is actually going to free the memory of this head so that so it has two advantages basically to free the memory first it does not generate any kind of garbage values and secondly it free the memory for any other kind of future addition for example you want to add anything so the memory gets free for that purpose and it is going to simply return so move ahead now the next thing you need to do is that you need to search for the key to be deleted we have to keep track of the previous node as if we need to change previous next so what i'm talking actually here is let me just code this and then i'll explain to you so we have to write in if temp does not equal to null and the data part and the data part of the temp is not equal to the key so what we are going to do in this case is that we are going to write in previous equals to temp and temp equals to temp next now what does these two lines specify is here these two lines is are going to search for the key that is to be deleted they are going to keep the track of the previous node as if you want to change what i'm actually talking here for example let's say we have one fourth node right here and this node also exists so we have these four nodes right here for example this is the node we want to delete so to delete this node we actually need some kind of pointer right here in this case the name of that pointer is previous so we need this previous pointer right in here now what is the purpose of pointing this previous pointer right in here because when we delete this node and this node you know it has a connection with this node so in when we delete this node we need a connection to be established between this node and this node so to establish that connection we need this pointer since this connection is going to be diminished the memory of this node is going to be freed and we need a connection right from here to here to make it a singly linked list and make this node and any other node after that part of everything behind that so i hope it's clear so let's move ahead so this line actually is going to keep moving the temp until and unless we find our node to be deleted and this is going to make the node point towards the previous node now we want to check if the key was not present in the linked list for example we entered a tree something that wasn't there for example there were three nodes and we want to delete for example the fifth key so what we are going to do for that we are also going to do something for that as well we are going to write in temp equal equal to null because if you don't do this stuff right here it is going to generate errors and we are going to write in return simple and moreover we also need to unlink the node from the linked list as well so we are going to write in previous next is going to be equal to temp next good enough and finally we have to free the memory all right so good enough so when the head node was to be deleted its memory was freed right up here but when something was found right in here so what does this do that is this node is actually going to establish the connection as i told you right here let me just move down all right so what i'm talking right here is that in the in the previous diagram right here which is so messy right here so i explained that we have we need a pointer that is to point towards the previous node we want to delete so we have this set of nodes this is the node we want to delete so this memory is going to be freed and we need a connection that or a pointer you may say that is the name has the name previous and this previous is going to point in right here and this memory is going to be freed now these lines are actually doing that step previous next which means the next that is this part right here it is going to point towards the temp next and since temp is pointing right here at the key which is which was actually passed into the function right up here and wherever this key is whatever this number is the temp is going to point towards that node because of this while loop because we are going to move keep moving the temp until and unless the key is found and that temp pointer is now pointing towards the node or you may say the key of the node which we want to delete so in this case this is the node we want to delete so this temp is pointing here the previous is pointing towards the previous node of this node sorry the previous node of this node which we want to delete so this is pointing right in here so now we want a connection that says previous next equals to temp next now this is actually the temp next in this case Since this is temp so this is going to be temp and next is so difficult to write in here so this is going to be temp next so we need a connection that says temp previous next equals to temp next so this connection is going to be established and after this connection is established we don't need this stuff right here so we are going to just write in free temp which means the memory of this node is going to be freed for us so I guess that's 100% of how to delete this so I guess it's time to run it now so before the display of a function we are going to write in s1. and it was delete node so right in the brackets of this delete node what we are going to pass in we need actually a head reference address and the key which we want to delete so to pass the head reference we have to write in here and head as you know if you have if you know the concept of pointers which I guess you know if you don't you need to go through it so if you know the concepts of head you know that whenever we are expecting a pointer to be received we have to pass in the address of whatever this thing this thing right here is pointing at so sorry this thing talking about this head reference so this since this head reference is a pointer so we need to pass in the address of that head so we are telling him that you have to point towards the address and we are giving him the location where he needs to point in so for example we want to delete this node right in here so the key is going to be four in this case since the data it contains is four so I'm going to just run this code all right it says head was not declared in this code yes we need to do one more thing I just forget to do that now as you can see right here that the place where we have initialized this tail and head we have made it private it does not have to be private anymore you have to just remove it from here and place it over here so that it becomes a global initialization and these two pointers of this structure node are now global let's just remove this stuff from here and now let's just run this code and you see it run perfectly and here are my output you see that the four is missing from the scenes let's just change the key set to something else and let's just make it equal to let's say three and you can see right here that three is missing from the scenes and if you just enter do right here and now run this code you can see now that two is missing from the scenes so I hope it is clear that how this node is actually deleted you have to actually pass in the address to where this head reference is going to point and that is the head node in this case you have to pass in the key the data element which you want to delete then you have to initialize two pointer head reference and previous that are going to start in from the very first node you have to initialize and temp point that is also going to start from the very start is going to check if the node to be deleted is head if it is the head this condition is going to be executed if it is not then it is going to look for the node it want to delete it is going to just point some pointer towards it temp is going to point towards the node which is to be deleted previous is going to be pointing towards the node previous to the node which is to be deleted and then we have to establish the connections between the previous node to be deleted and the next of the node which is to be deleted so the middle node which is to be deleted is free from the scenes using this statement so that no garbage value is generated and then this display function you know from the previous tutorial that how it get generated your output message so I guess that's it with this function so the next part of the tutorial says that how to delete an element for example you want to delete an entire length list so in this case you see that we can only delete one element if you want to delete another element you have to call this function again and again so for example you are given the task to delete an entire length list for example in this case we have a link list right here that says two three four and since this two is removed so this two is no more the part of it so we have a link list that contains three and four so what if we want to delete this entire link list completely in just by calling a function once so for that we have to do some more coding so let's get to it so we will initialize a function name delete complete since it's going to completely delete a function and it is also going to receive a pointer that says no starts at head reference and it is not going to receive any other for example it is it does not need a key right here since why does it not need a key this is because we don't want to delete a specific element the key was paused in just to tell the compiler or just tell your single link list that I want this element to be removed from your series of lists so in this case I don't want a specific element to be deleted I want to delete the entire length list so I don't need a key as an argument right here in this delete complete function so what I'm going to do is that first I need to dereference the head reference to get the real head so for that I will write a note pointer that is the current pointer make it equal to the head reference I am going to initialize one more pointer that is the next pointer right here now what I'm going to do is that I am going to loop around the entire link list and delete each and every entry that comes in my way so I'm going to write in while current is not equal to null which is this pointer that is at this time pointing towards the head node so I say that until and unless it does not point to null which is only the case when the last node is needed since is the next of the last node is going to points towards null so I'm going to write in next equal to current next and I'm going to free in the current and I'm going to write in current equals to next and finally off outside this while loop what I need to do is that I need to dereference the head reference to affect the real head back in the color area so I'm going to write in head reference equals to null so what basically is going on in here let me just explain it to you here I need to just remove all the stuff from here let's just create a new one all right so we have this screen right now so let's just let me just show you how what actually is going on in for example we have this series of nodes right in here one two three and four nodes I have these four nodes so I want to delete for example this entire length list so what I've actually done here is that I have created a head reference and using that head reference I actually have access the very first node and I have pointed some pointers towards it the first pointer is the head that is by default here now I have done is that I have used this current pointer to point in towards this first node at which the head is pointing right now and then I have used one more pointer that is the next pointer point towards this node right here now I says until and unless current is not equal to null which is only the case when this node is reached since this is basically pointing towards null so this loop is going to iterate until and unless this node is reached so now what this next this line is doing is that it says that next equals to current next so this is the current next so this means that this is going to be removed from here and the next pointer is going to come right here now it says free current free current means that this node is going to be deleted so this node is deleted from the scenes and then it says current equals to not current equal to next means that current is to be now pointing towards the area at which the next is pointing so current also comes here so when current comes here it checks the condition that while current is not equal to null since this is not equal to null because it is pointing right in here and it is the address of this node so this condition is false and this loop is again going to be executed in the next it is going to make next equal to current next which means the next is now going to move here be removed from here and the next set free current is going to remove this node and current is then going to move right here with current here it is going to again check the condition since it is again not true since this this next contains the address of this node so this loop is again going to be executed next is going to come here the second line is going to remove this node from here then finally current is going to come here and finally this condition is going to become true that says while current is not equal to null since in this case now current is not equal to null it is going to just this loop is just going to terminate but before terminating what it's going to do is it's going to also delete this node so let's just run this so I will call it as s1.delete complete and I'm going to write in het so just run it and you can see that display function calling this display function gives me nothing because everything has been deleted let me just remove this now run it still you can see that I got nothing because the complete link list has been deleted as I showed you that how it deleted the complete link list so I hope that is much pretty clear because I explained it in a very expressive and detailed way so I guess that's it with the deletion you now know how to delete a single element from a single link list and how to delete a complete link list given in the first case a key was given and in the second case no key was given why it was not given I've already explained that we don't need a key in this delete complete function since we are not talking about deleting a key we are talking about deleting an entire link list so I guess that's it with this tutorial thank you so much guys for watching and I'll see you guys in the next tutorial hey guys what's up this is Umar Khan and I welcome you to another tutorial on link list in C++ in this tutorial we are going to be covering some more concepts about a singly link list that contains finding length of a singly link list in which we will see that how many elements exist in a singly link list we are going to count those and just print out that output and then we are going to search for a specific element in a singly link list and we are going to count the occurrences of a specific element in a singly link list so it is a very interesting tutorial so let's just get to the coding since I don't need to I guess talk too much about this so this is the code from our previous tutorials and I just add this so that no kind of time is wasted in coding this create function and again and again and calling this display function again so we already have a link list that contains three elements that contains two three and four so all we are going to do is for the first part of the tutorial we are going to just count these elements we are going to count that we have to generate actually an output three because at this time the singly link list contained three elements so we to find the length we have to generate this output three so how it is done let's just get to it so we are going to just create a function just above the display function right here and the function name is going to be let's say something like int count seems to be a good name and the return type of this function is int since it is going since we are going to return actually the output if you don't want to return an output and just print it out right here in this function you can make it as white but I actually want to return it to the place from where it is called that is right here in the main function and just output it right there then so if you don't want to do it and you want to output the count value right in here this function so you don't have to make it the return type as into you can just use it as white but since I am returning it as I told you I am using this return type as in so we are going to just we just need a reference of the head node so we are going to write in head you can also write in any name you want for example head reference in the previous case but let's just make it to head so that typing becomes easy because I'm too lazy in typing so the next thing we need to do is that we need a variable name as counting equal to zero so since we are going to start in from here and you know that whenever you are generating a sum of two numbers you have to just you have to first initialize account or a sum variable equals to zero so the second thing we need right here is that we need one more pointer let's name it as for example current and it is also going to point towards the head node so while pointing these two pointers towards the head node the first one is the head node and the second one is the current node what we want to do is that we want to actually use a while loop in which we says while the current is not equal to null and you know when this condition is used you know that this condition is to always use when we want to go through the entire length list and you want this loop to ends until and unless the last node is reached so while current is not equal to null this loop is going to keep terminating and what it's going to do is that it's going to make counting equals to counting plus one and missing from both all right counting equals to counting plus one now the second thing we need to do is that we need to keep moving this pointer so we are going to write in current equals to current next and you now know that we have actually a real around the complete length list and since we have also counted it so I think our job is done all we need to do now is that we need to to return the whatever value is stored in this counting variable now so all we need now to do is that we need to just call it we don't need to call the display function in this all right so we are going to write in whatever the output let's just output it later we are going to write in some variable and let's say count not sound it's going to be count and it is going to equal to the name of the function that is counting what was the name of the function the name of the function was count so let's just change this variable to counts and make it equal to count and we need to pass in the reference of the head and then we need to just see out counts so when we run this code all right it says error it says count was not declared in scope all right we have to write an s1.count actually you have to call this count function using the object of the class so so the error now says that no matching function to call to singly node let's just remove this stuff from here let's just remove it from here as well let's just remove it from completely here as well and we don't need to struct over here as well since we are in that class we are talking about that structure so we are not in any other part of the code or not in any other program so we don't need this struct over here this is a good programming technique since that's why I've been talking about it so much from the last tutorial and I've been using the double reference pointer as well but if you don't use that it's going to be completely okay and your code is going to run completely okay but this is a good programming technique and I just explained that so that you may understand all that concept as well so if you just remove this from here the struct from here and for sure this is going to be removed from here since it is now expecting no argument so if you just run it now it's going to run perfectly fine and it is going to generate that tree for us which was needed in this case now why I've just removed you can see right here this function was called just right here the counting was initialized to zero since we are talking about the structure node so a new pointer of that structure was initialized and was now pointing towards the head node we have this y loop that make that pointer start from the very start make it and make it run until and unless it becomes equal to null counting was equal to counting plus one and current equals to current next so this is going to just generate the output or plus we just return that whatever was stored in this counting variable in this case it was actually the answer we required that was the length of that singly linked list so it just returned it and we just printed it out so I hope it is clear so now moving towards the second part that says searching for a specific element in a singly linked list so to search for any element we need a function whose return type is pulled because if that element is found it is going to return true if that element is not found it is going to return false so let's name that function as searching and it is going to receive the variable it is going to search for for example we want to search if two exist it is going to receive two for example we want to search if three exist in a singly linked list it's going to receive three and so on for any kind of variable so we are going to initialize a pointer here as well that is going to be named as current and it is going to start searching from the very first node that is the head node so we are going to now initialize a while loop you are now very familiar with how a while loop works when you want to search from the start towards the end and do anything with a singly linked list so this is the condition for that and in this we are going to specify a while loop sorry the if condition and what is this if condition is going to check it is going to check if the current key equal equals to x what it's going to do is that it's going to return true and outside this what it's going to do is that it's going to make current equals to current next this condition right here is going to be executed if for the first time for example this condition this node man and the first element in the singly linked list was not that element we were looking for so this is going to be executed it is going to move towards the next node and then this if condition is going to search in the next node if that element is found or not at the end if this while loop is terminated and that element we are looking for is not found then what it's going to do is that it's going to return false good enough so let's just now call this function let's just remove this counts from here and in here we are going to write in bool let's name it as x and it is going to equal to the name of the function that is named as searching yes the name of the function is searching and we are going to pass and let's say three to this function and then we are going to see out this x all right so good enough all right yes one thing right here this has to be data since we are not using the word key we are using the word data over here so we are going to search for if the current data equal equal to x which is actually the data we have passed in here so these data's are going to be compared if this returned a true or it is true then it's going to enter in this block and it is going to return true and that true is going to be saved in this boolex all right one more thing we need to write an s1 dot searching here and I guess that's good enough for this code to run so let's just run it all right so it says one which means true so since three exist right here as you can see that we have this tree over here so it returned a true for example let's just enter value seven as you know that seven does not exist here so in this case if we just run this code it is going to return a zero which means false so I hope it is clear so let's move on towards the third part of our tutorial that says counting occurrences of a specific element in a simply linked list so all we need to do for that is that we need to amend this searching function right here we have to just change its return type to int also at the calling part we have to change it to int and then what we are going to do here in this searching function is that we are going to initialize accounting variable that is going to start from zero and we are going to pass in the element which we want to search and for example we want to see that how many times two has occurred in this simply linked list so we are going to pass in that two over here the counting is going to be equal to zero the current is going to go into null while current is not equal to data what it's going to do is that if current data equal equal to x it's not going to return true what it's going to do is that it's going to say that counting equals to counting plus one good enough and n missing here yep good enough so it's going to say counting equals to counting plus one and after adding the counting it's what it's going to do is that it's going to make current equal to current next and we don't need this return false over here and I guess that's perfectly all right at the end what we need to do is that we need to return the counting since that it return type is integer not white so if you just run this but let's just change this to two and now run this code so it says one since two is here only once let's just let's just change this to two first now as you can see that two occurs two times so if we now run this code you can see that we have this two over here which means that two occurred twice so what is what's happening actually here that this two was passed in here which means the data which we want to search is two so the counting variable variable was initialized to zero a pointer was pointed towards the head for example let's just let me just show it to you here for example these were some of the nodes it doesn't seems to be like node but please consider them to be nodes so for example these were the three nodes and for example this contains two this contains two this contain let's say three and let's say this contain five so for example we want to search for the occurrences of this two in this link list so we have passed this two right in here the counting is zero the count in this case is equal to zero now when we says that current is not equal to null which means that the current is pointing right here we says that current data equal equals to x and we have this x right here which we passed in to this function that is two so since this matches so this count was incremented to one next we what we did that we move this current pointer this current pointer came right here this while loop again checks the condition since it does not exist since only this part points to null so it again compared the data the data again compared perfectly and this one was again incremented right here the current pointer was then moved right here due to this condition right here and when this matches again it is not matched so it is not increment the current pointer moves right here it matches again the match was not again found and finally the while loop ends and it returned the counting and since the count at this stage was equal to two so the output we got was two so i hope it is clear so that's and our discussion about finding length of singly linked list searching for a specific element in a singly linked list and counting the occurrences of a specific element in this case we discussed about the occurrences of two in a singly linked list so that's what was the objective of this tutorial so i guess that's it with this tutorial i hope you got everything thank you so much for watching and i'll see you guys in the next tutorial hey guys what's up this is umar han and i welcome you to another tutorial on linked list in c++ in this tutorial what we are going to be studying is about removing duplicates from a singly linked list we are going to study how to remove duplicate from a sorted singly linked list and then we are going to see how to remove duplicates from an unsorted singly linked list so starting on with removing duplicates from sorted singly linked list let's just move on to our compiler so here we have def c++ we have already a code right here so let's just add some duplicates so this is a duplicate now if we just run this code we have called the display function already and you can see that we have a duplication of 2 to right here so all we need to do is that we need to remove this two from here to make this code worthy or you may say to make this tutorial worthy so we are going to initialize our function that is going to be named as removing of duplicates and this is going to receive nothing all right so the first thing we need to do is that we need uh some pointers right here we need two pointers the first pointer is going to be named as current and it is going to point towards the head because you know that in order to find duplicates we need to take an element and then make this element compare with each and every element other element and compare in the singly linked list so that if we detect any duplicate we can then remove it so this pointer is just going to do our job this is going to do our job of comparing a single element to each and every other element present in the singly linked list then we are going to have one more pointer and this pointer purpose is to store the next pointer of a node to be deleted so that we can then install some connections so it is going to be named as next of next good enough so what is the purpose of this pointer the purpose of this pointer is to point towards the next pointer of a node to be deleted you as you if you remember we have uh in the previous tutorial a node pointer named as prab that says previous and that holds actually the that points actually towards the previous node of the node to be deleted but in this case we need a pointer that points towards the node next to the node to be deleted so this is actually that pointer that is going to point next to the node which is going to be deleted so the first thing we need to check is whether a node contains even contains something or not so for that we are just writing if current at the very stage we launch our code equals to null then what it's going to do is that it's going to simply return good enough let's just make it equal to a tab so that it more look likes the code of this block let's just add some line spaces now we need to now traverse the last till sorry we need to traverse the list till the last node now so for that we need a while loop and in this while loop what we are going to do is that we are going to write it while the next of the current is not equal to null now you know what does this means and what we are going to do in this while loop is that we are going to have a comparison between the current node and the next node so to compare the current node and the next node what we are going to do in that we are going to write in a current data equals equals to the current next data now you know what does this mean you can see that if we have for example let's say three nodes right here and we say that this points towards null this contains a two this contains again a two and this contain let's say a four so what we want to do is that we want to say that a current pointer is pointing right here and we says that it's data if it is equal to let me just move it a bit here it's the current next data and in this case the current next is this and we are actually saying the current next data so we are actually talking about these two and when we say current data we are talking about these two so when we write in a current data equals to current next data we are actually talking about these two data types and actually we are comparing these two data elements so what this condition is going to be true what is going to do is that it's going to actually perform a sequence of steps that are very important what it's going to do is that it says next of next pointer make equal to current next next now why what is the purpose of this actually let me just first spell it correct now what is the purpose of this actually we say that if you found a comparison between these two data data which in this case we have since this container two and this container two and what we are going to do is that we are going to take in this pointer that is next of next and where is this next of next going to point this next of next is basically going to point right here next let's name it as n o n which means next of next now why is this n o n pointing right here this is because if this is a duplicate which we found from this if condition and if this is true this means they both are duplicates of each other which means we want to remove one of them now since the function says removing of duplicates which means we have to remove the duplicates so this means if they are the duplicates of each other which means that we have to remove this so in order to remove this we need a pointer right here so that we may establish a connection like this and after establishing the connection we can just remove this from the story all right so to establish a connection we just pointed a pointer name next up next here and since the current is pointing here so it says current next which is this in this case and then it's next which is right here so that's why this n o n next up next is pointing in right towards this node so after pointing it right towards this node what we what we are going to do is that we are going to write in free current next and finally what we are going to do is that we are going to write in current next equals to next or next now what does these two lines means actually now after establishing the connection between these two which has been established first actually we have created a pointer right here then we say it's free current next which is this right this here so this is removed from the scenes and then it says current next which is actually this it is equal to next of next which means that it is pointing right in here now the next thing we need to do is that we need to specify an else condition else condition right here and what is this else condition is going to have it says that current equals to current next now what does this mean actually and what does this mean actually let me just explain right here now for example this was a mismatch and it says here is a three and then here is again a four and we have no kind of duplicates in this singly linked list so in case this is now this two is matched with this then this two is matched with this and in case we don't find any kind of duplicates let me just move down here to make it more clear so in case we don't find any kind of duplicates in these three nodes then all we have to do is that we need to then move towards this node now now the current is going to point towards this node which is this statement that says current equals to current next since current was at first pointing right here so we just remove current from here make it move here and then any data that's contained here for example a tree is here we now need to compare this tree with the remaining rest of the singly linked list we don't have to compare it with this since we have found from our previous iteration of the pi loop that these two does not match so now we have to compare this three with any remaining linked list for example we have some nodes some more nodes right here so this tree is going to compare to the nodes after this node not before this node because the previous iterations have told us about those scenarios in which we have found that the previous nodes are not a match of this node so i hope it is clear so let's just call this function simply before the display function for sure or let's just do one more thing we are going to see out and print out here before removing duplicates we are going to just add some spaces right here and then we are going to see out in quotation marks for sure after removing duplicates we are going to again call in s1.display and let's just run this code now all right a call and missing right here let's just run it now and hope it works yes it ran perfectly but it does not remove the duplicates some problem i guess so i guess we have got some problem yes we need to call this function before calling the display function so i'm going to write in s1. and the name of the function was i guess removing of duplicates yes it was so let's just put a call in right here and i guess we are good to go so let's just run this code again all right it again did not remove i guess some more problems we need to check it out right here so he says current pointing towards head all right yes if the current equal equals to null then it's going to return else it's going to execute this so i guess this was a problem let's just run this code now and yes it ran perfectly the duplicated two has been removed from the seeds so i guess that's clear so now moving towards the second part of this tutorials that deal with removing duplicates from an unsorted link list we are going to create a new function right after this function that is let's say let's name it as remove remove only so this is going to receive in nothing as well and in this function we are going to define some pointers as well we are going to define let's say two pointers the first one is ptr1 then we are going to define ptr2 and then we are going to define a pointer that is the duplicate pointer that is going to point towards anything that is duplicate so we are going to first write in that ptr1 equals to head that is the starting point and now we are going to compare the picked elements with the rest of the elements so for that we are going to use a while loop and in that while loop we are going to write in if ptr2 next is not equal to null then what it's going to do is that it is going to check using if condition that if a duplicate is formed then it's going to delete it and if it is not then it is going to simply move forward so if a duplicate is fine out then it's going to delete it and how it is going to find a duplicate is going to find a duplicate using any condition using that if ptr1 data equal equals to ptr2 next not next data since we are talking about data and comparing data is right here so if a duplicate is found what it's going to do is it's going to start it in in ptr2 next ptr2 next is then going to equal to ptr2 next next and then we are going to simply delete the duplicate it's also going to be duplicate all right so after this we are going to specify an else condition and then else condition we are going to write in ptr2 equals to ptr2 next and outside everything we are going to write in ptr1 equals to ptr1 next so so this is a very small code let me just explain it with the the help of this so we have let's suppose one two three and four nodes and for example these are unsorted nodes and contain unsorted data so the first thing is that we have this ptr1 equals to head which means that ptr1 and head they are both pointing towards this node at the very start when everything begins so the condition which we are going to check using by loop is that if ptr2 next is not equal to null so ptr2 next is not going to be null we missed this over here all right so in this if ptr2 is not equal to null which is only going to be equal to null at the very end so what we are going to do is that we are going to see that if ptr1 data which is this part of the data is not equal to ptr2 next data so ptr2 next data so until and unless ptr2 next is not equal to null this is going to be executed and yes the location of ptr2 here I missed something yes right here ptr2 is going to equal to ptr1 which means that ptr2 at the beginning is going to point at the very same location where ptr1 is pointing so in this case ptr2 is also pointing right here so the condition is that ptr2 next is not equal to null which means that it is going to be null when this is checked which is equal to null so at this time both these three pointers are pointing at this very first node so the condition is that if ptr1 data that is this part of the data is not equal equal to the ptr2 next data and ptr2 next is this loop right here so we are talking about this data right here so it is going to compare these two data's for example these two data's become equal and what it's going to do is that it's going to make duplicate equals to ptr2 next which means that ptr2 next at this point is this so duplicate is going to point right here so now duplicate is pointing right here in case a duplicate is found which is only possible when this condition becomes true when this condition becomes true duplicate is equal to ptr2 next which is right here then we are going to write in ptr2 next which is right this is going to equal to ptr2 next next which means that now ptr2 is going to be right here which is the next of next of ptr2 all right so ptr2 is now right here and we are going to delete the duplicate so and this duplicate is deleted which means that now ptr2 using this line that says ptr2 next is now pointing towards ptr2 next next which means it is pointing right here now so this connection was finished this node was deleted and we have this connection right here which means this is no more the part of the scenario so if this condition is not true it is going to make ptr2 equal to ptr2 next which means ptr2 is going to come right here and then it's going to compare with the nodes after this node not before this node as I told you about the condition before it is going to be like this and after all this stuff what it's going to do is that it's going to make ptr1 equal to ptr1 next so ptr1 is also going to move towards the next node so and how this is this is how it is going to delete any kind of stuff so we are going to write in let's call it using remove and you see that we have this to remove from here and in case since this is we are talking about a random data so for example we have this five over here then for example let's say a seven over here and let's say a five over here we run this code now it is going to remove this five from here at the last so I hope it's clear now here one thing that is to be shown to you is very important for example we have this unsorted data that is 575 and we call the removing of duplicate function yes we have an s over here yes we have so if you call this function now with this irrandom data that's not the sorted data and unsorted data you can see right here that the duplicate was not removed because this function as I explained before was not about the data that is unsorted this function remove is about unsorted data and we run this remove and then call the display function you can see that the duplicate is now missing so I hope you now understand the difference between how to remove duplicate from an sorted array and how to remove that from an unsorted sorry not array linked list so how to remove a duplicate from sorted singly linked list and how to remove duplicate from an unsorted singly linked list so I guess that's it for this tutorial thank you so much guys for watching and I'll see you guys in the next tutorial hey guys what's up this is Umar Khan and I welcome you to another tutorial on linked list in C++ in this tutorial we are going to be covering two concepts the first one is how to reverse a singly linked list and the second one is how to segregate even and odd in a singly linked list so starting on with reversing a singly linked list so let's move on to our editor now as you know that we already have a created linked list that contains three elements that says five seven and five let me just sort them out so that it looks like two three and four now let me just run it to see if it runs perfectly yes it ran perfectly so we have this sorted linked list and now what we are going to do in this tutorial first part is that we are going to reverse this singly linked list and it's going to look like four three two so that its elements has been reversed so let's go at this function so we are going to code a function right above this display function and that is going to be named as reverse and now in this reverse function what we are going to do is that this is not going to be here this is going to be here yes that seems perfectly well so now in this we are going to first have a pointer that is going to start from the head so you know how to start a pointer from the head you have to write in the name of the structure a steric for a pointer name the pointer and make it equal to wherever you want to start it from so I want to start it from head so I've written as node steric current equals to head means this current pointer is now pointing towards head so now we need two more pointers the first one is going to be the previous pointer and one more is going to be the after pointer so we are going to name them as node steric let's name them as before and that is going to equal to null and one more pointer is going to be after and that is going to be equal to null as well so now we are going to have a while loop and what this while loop do you already know it is going to start from the very start and keep looking until and unless the entire linked list has ended so when current is not equal to null and since current is pointing towards the head current null only becomes null when the last node is reached so now what we have to do at the very first step is that we have to store the next value so we are going to write an after equals to current next so the next of the current or you may say that after is pointing towards the location where current next is so current is at this time pointing towards the head node so the node after that after is pointing towards that node now so now what we have to do is that we have to reverse the previous node pointer no sorry the current nodes pointer we have to reverse it so to reverse it we are going to write in current next equals to before all right so the next thing we need to do is that we need to move the positions of the pointer one ahead so this statement is actually going to reverse the first two elements now to move the pointers ahead so that the next elements are also reverse you have to keep moving the pointers so the before pointer is going to equal to current and the current is going to equal to after and after this while loop we are going to write in head equals to before so let me just show you this with the help of a diagram now so that any kind of ambiguity is removed so right here we have a diagram uh oh let's just create a new one all right so first we have created a pointer for example let's say we have this node that contains two then we have this node that contains three then we have this node that contains four so now when we write node stare current equals to head which means that head and the current pointer are pointing right here and since we have said that before equals to null and after equals to now which means that two pointer that are the before and after pointer are pointing towards null all right we say is when current is not equal to null which is you know null only right here so we say is that after equals to current next and since current is pointing here so which means that after is going to now point right here and current next which is right there is going to equal to before which means that the current next is going to point towards the before which is at this time equals to null but the current next equals to before means that the current next is also going to point right here all right so then we have written before equals to current which means that the before is going to point towards the location where current is pointing and the current is going to point towards the location where after is pointing and then head equals to before means that head is going to point towards this before now which means that this is now going to be the head for the first time this is executed but for the second time when it is executed the head is going to move right here this is going to be the second one and then this is going to be the third one so the sequence is going to be like this this is going to be the first one this is going to be the second one and this is going to be the third one so the sequence that was two three four is now going to be four, three and two. So let me just run this code now but before running it up to call it for sure. I'm going to call it using the object s1.reverse and then the display function is going to be called. So let's run this code and you can see right here that my length has been reversed. Now it says four three two instead of two three four. So I hope it is clear. So moving on to the second concept of this tutorial that is how to segregate even and odd in singly linked list. So for that let's go on to our editor again. Moreover if the input is something like let's say eight twelve then then let's say five four one six then in this case the output is going to be something like eight then twelve then ten then four then six then five then one. So you can see that the order have been again the same. Eight twelve ten can came first and then when the scrambling occurred four and six as in the sequence appeared in the modified linked list and then five and one that with the odd numbers were added at the end in the sequence they appear in the first linked list. So now two more things. If all the numbers are even then you do not have to change them. For example if you have two four and six then the output is again going to be two four six even if they are scrambled like four two and eight and let's say six then the output is going to be the very same as four two eight six as the order is very important in this case and secondly if we have odd numbers like one three five seven then the output is again going to be same one three five and seven even though if they are scrambled as I told you in the previous case again that three one five seven is going to be the output. So I hope it is clear. So let me just remove all this from here. So now now we have a certain methods too far. The method we are going to adopt is that we have to get pointer to the last note of the list and then we have to traverse the list starting from the head note and move that odd values nodes from their current position to the end of the list and that's what is the concept we are going to be studying in this. So we are going to use specific algorithms. We have to get pointed towards the last node move all the nodes that are odd towards the end and for that we have to consider all odd nodes before the first even node and move them to the end change the head pointed to point to the first even node and to consider all odd nodes after the first even node and then move them to the end. So for that we are going to make a function. Let me just remove this reverse function from here and also remove its calling from here. That's it's just one dot reverse and let me just create some more nodes. So at the end the idea will be much more clear. So we have to create let me just copy this pasted again pasted again pasted again. I guess that's enough. So let's just put in a six here and then a 12 here and then a nine here and then a 15 here and then let's say a 14 here. So you can see that they are scrambled much scrambled and let's make it equal to 20 and let me get equal to 41. So you can see that they have been scrambled very much now and you will see that how this sequence is going to be followed and how the odd numbers are going to be moved at the very end and even numbers are going to be moved at the very start but the sequence is going to be followed. So for that we are going to create a function name as separate even odd or you can name it as segregate even odd that looks then much more familiar. This also looks much more familiar since we are going to separate even and odd. So for that we are going to have some pointers that are going to start from the head. So as I told you about the algorithm that we want a pointer that is pointing towards the very last node. So for first case we are going to first create pointers we are going to create a pointer and then we will create a pointer named as previous and it is going to be equal to null and then we need one more pointer that is the current pointer and that is also going to point towards the head. So in this case this node that is named as the end node and is pointed towards the head we are going to move it towards the very end. So this is then going to help us sort out our odd numbers at the very end. So the next thing we need to do is that we need to move this pointer as I told you about the algorithm before at the very end. So for that we need a while condition to traverse. So it says that while and next is not equal to null what it's going to do is that it's going to say and equals to and next. Good enough. So it is going to just move this pointer named and towards the very end and then when we need one more pointer. So for that we are going to write in node steric new and and that is again going to equal to end and since using this while pointer the end is already pointing towards the last node. So this new end is also then going to be pointing towards the last node since it is equal to an end now. So now we have to consider all the odd nodes before the first even node and move them towards the very end. So for that we need a while condition and the while condition says that while current data modulus 2 is not equal to 0 which is the condition for checking odd numbers you know and the current moreover is not equal to and which means that the last node is not yet reached. Then what it's going to do is that it's going to make the new and next equals to current. The current is going to move one step forward using current equals to current next. The new end next next is going to equal to null which means that this is the new and null I will explain this with help of a diagram but first let me just code it completely and then the new end is going to equal to new and next. So what basically is going on in here I just scroll it down so let's suppose we have these certain nodes right here for example we have these four nodes so you can see right here that we have this end pointing right here the head pointing right here and the current as well pointing right here. So we say is that while n next is not equal to null n is going to equal to n next which means that n is going to move right here which is the last node in this case since this is pointing towards null and then we have one more pointer that is the new and all right so new and and are both pointing towards the last node now so we says that while current data which is actually this data is not equal to null for is not equal to zero which means it is odd number for example let's say three and current is not equal to null it is not equal to null both conditions are true so what it's going to do is it is going to make new and next new and next you know is pointing towards null at this time it is going to now point towards current which means that this connection is going to be established like this since as I told you that we in case of odd we have to move that node towards the last so new and next is going to equal to current the current is going to move right here using the statement current equal to current next and the new and next the new and which is right here it's next and it's next which is right this node where current is pointing at this time is going to point towards null all right so then we have the statement new and equals to new and new and next which means that new and is now going to point right here so let's move forward so the second condition we need to do is that we need to check for the occurrence of the very first even number so for that we are going to write in if current data mod 2 equal equals to 0 then what it's going to do is that it's going to create a pointer that is the head pointer and the head is now going to point towards current which means since I told you that the first even number since we are following the sequence here so the first even number is going to be the very first node of the newly modified singly linked list so in that case the first even number is going to be the first node so the head is going to point towards the very first time this conditions become true so the next thing we need to do is that we need to know current points to the first even node so we are going to write in while current is not equal to and what it's going to do is that it's going to check an if condition and what is that if condition it is that if current data whatever the data is it's modulus with 2 equal equals to 0 then what it's going to do is that it's going to make previous equals to current and the current node points towards current next or if this condition is false then what it's going to do is it's going to check an else condition that is going to break the link between the previous and current and how this is going to be done it is like previous next is going to equal to the next of the current node and then we are going to make the next of the current null as well so for that we are going to write in current next equals to null and then we are going to move the current to the end so for that we are going to write in new and next is going to equal to current and then we are going to make current as the new and of the list so for that we are going to write in new and equals to current and finally we are going to update the current pointer to the next of the moved node so for that we are going to write in current equals to previous next and some final things we need to do in this code is that we need to write the else condition for this if that was current data mod 2 equal equals to 0 so it's else condition is going to be right after this and this else condition says that previous equals to current since the current has now moved forward so the previous is also going to move right there so that when the current moves one step more forward this previous points to the previous node of where this current node is pointing so one more thing we need to check using the if condition is that what if there are more than one odd nodes and end of the original list is odd then we have to move this node to the end to maintain the same order of odd numbers in the modified singly linked list so for that we are going to write in if condition that says if new and is not equal to and and one more condition that is and data modulus with 2 is not equal to 0 that is going to check if the original end of original list is odd or not and if it is then we have to move this node to end to maintain the same order of odd numbers in the modified list so for that we are going to write in previous next is going to equal to the end next and the end next is going to point towards null and the new and next is going to point towards and and finally we are going to write in return which is going to just give us of our output so before this display if you just now call this as one dot separate you or not call in here and call this function right display does not need to have any kind of input value let's just call it now and you can see that everything is so perfect 26 and 12 are at the very start that are the even numbers and are sorted in the sequence they appear like here where we pass them the 20 then the 6 then this 12 and if you talk about the odd numbers they are very much in the sequence but placed at the very end that it says 3 first then comes 41 then comes 9 then comes 15 so let me just give you a quick recap of things from here using this diagram we have to just change this to a new diagram all right so for example oh all right so we have for example this current data mod 2 equal equal to 0 for example for example we have this series of nodes and for example for the first node this line all right so for the first node let's just suppose that above conditions that was that this was an odd number and was moved at the end how it was done you already know that from the previous diagram and the previous code so for now for example this data is now even data for example this is just 2 here and this condition becomes true so now the head is going to point towards current and in the previous diagram you have seen that the current most move right here so which means that head is also now pointing here due to this condition that says head equals to current and it says that one current is not equal to null what it's going to do is that if the current data modulus 2 equal equals to 0 then it's going to make previous equals to current which means previous is going to point towards the very same location right here and the current is going to move one step ahead as I told you right behind somewhere here right here that why do we have to specify this else condition this is because we have to move the current pointer so in order to move the current pointer we also need to move the previous pointer so that it point always at the node and just before where the current is pointing so that's why this previous was pointed right here so when this current most moved here I moved here the previous is pointing towards this node now so in the else condition which says that why if current data modulus 2 is not equal equal to 0 what it's going to do is that it's going to write in previous next equals to current next which means that previous at this point was pointing right here at the very start it's next was pointing towards the current next which is right here and the current next was pointing towards null and the new end next was pointing towards current and the new end is now pointing towards the very same location after establishing the connection, it is going to point towards the current node, and the current is going to point towards the next of the previous node. So this is how all the connections are going to be established. And then finally, using this, we have to check if more than, if there are more than one odd nodes and the end of the original list is odd, then we have to move this node to end to maintain same order of odd nodes in the modified list. So this is what is going to check that condition. So when we call this function, using these set of outputs, the sequence was same, but even numbers were sorted at the very start and odd numbers at the end. So I hope it's clear. So that's it with this tutorial. Thank you so much, guys, for watching. And I'll see you guys in the next tutorial. Hey, guys, what's up? This is Omarhan and I welcome you to another tutorial on linked list and C++. In this tutorial, we are going to be moving towards doubly linked list. And we are going to be studying about the structure of a doubly linked list in this tutorial, but before moving on to a compiler and talking anything about the structure of a doubly linked list, I want to tell you something about the advantages and disadvantages of a doubly linked list over a singly linked list. So first talking about the advantages of a doubly linked list over a singly linked list, the first advantage is that a doubly linked list can be traversed both in the forward and the backward direction. The second advantage is that the delete operation in doubly linked list is a bit more efficient if pointer to the node to be deleted is given. And the third advantage is that we can quickly insert a new node before a given node, whereas in singly linked list to delete a node pointer to the previous node is needed to get this previous node. Sometime the list is to be traversed, whereas in doubly linked list you don't need to get into all that stuff. You can just get the previous node using the previous pointer. Now talking about the disadvantages of a doubly linked list over a singly linked list. The first disadvantage and the main disadvantage is that every node of doubly linked list requires an extra space for a previous pointer. The second disadvantage is that all operation requires an extra pointer previous to be maintained. For example, in insertion, we need to modify previous pointers together with the next pointer. For example, in a singly linked list, we have to only maintain the next pointer and make connections according to that pointer. But now in doubly linked list, you have to take care of both the pointers and make connections at both ends at the forward end and at the backward end as well. That is a very kinky joke to do in case you are dealing with some complex type of linked list. So you will see all of that stuff in the future tutorials. But for this tutorial, the main focus is to tell you about the structure of a doubly linked list. So this is how a structure of a doubly linked list look like. We have the struct keyword as we have in singly linked list. Then we have the name of the structure that can be anything as I told you before. Then we have the data part. Then we have the pointer to the next node. And then we have the pointer to the previous node. This data part is between these two. First we have the pointer to the previous node, then we have this data part, and then we have this pointer to the next node. So how this is implemented. Let's move on to our compiler now. So this is the main function in this main before this main function. What we need to do is that we need to define the struct keyword name of a structure. And now we need three things. The first is not steric previous. That is the previous pointer. Then we have the data part. And then we have no steric next. That is the next pointer, which is going to create a structure something like this. That is going to be having three segments. The first one is the previous. This is then the data segment. And finally we have this next pointer. And it is the very same as in case of single link list. But the addition in this double link list is this pointer right here that is going to help us to traverse in the backward direction. And this pointer next is as usual going to help us to traverse in the forward direction. So this is basically how the structure is made. Now in order, for example, you want to add some node. So we will create, let's say a simple pointer, not with a plus sign. It is going to be with a steric sign. And we are going to write in P equals to new node. And when you make calling P equals to new node, a structure of whatever is mentioned here is going to be created. And in this case, a double link list structure is going to be created. And then we are going to write in P. Data part is going to be equal to, let's say 10. Then we will have the previous equal to null. The next equal to null. Since this is the first node. So this is how our first node is created now in order to create a second node. We are going to write in another pointer. But first, let's create that pointer node steric Q. So we have one more pointer named as Q. And in this pointer, using this pointer, what we are going to do, we are going to create a new node of the very same structure. Then we are going to write in Q. Data equals to let's say 20. Now, since this is going to be the second node. This is the first node. So we have to now establish connection. So we are going to write its next actually is going to point towards null since this is now the last node. But its previous is going to point towards P. And what is P? P is actually the first node created. And one more thing we need to do is that we need to update the next of the P. So we are going to write in P next equals to Q. And I guess that's enough. So if we just run this code now. It ran perfectly and nothing was seen here on the output screen since we see out nothing. If you want to see out something, for example, I'm going to just write in C out. Let's say P, not P data. P next data. And just run this code. It is going to give me the data what is stored in the node of Q since I have written P next data. Now, this was actually something traversing the forward direction. For example, if we want to traverse in the backward direction, I'm going to write in Q previous. And Q previous in this case is this P and I have outputted its data. So this means 10 is going to be generated. Let's see how you can see that 10 was generated. So this is how we can traverse in the backward direction using the previous pointer and W link list. So I guess that's it with the structure of this W link list. So let's end with this tutorial as well. Thank you so much guys for watching and I'll see you guys in the next tutorial. Hey guys, what's up? This is Umar Khan and I welcome you to another tutorial on link list in C++. In this tutorial, we are going to be continuing with W link list and we are going to see how to insert elements in a W link list. Now insertion in a W link list can be done in all these four ways. You can add at the front of a W link list. You can add after a given note. You can add at the end of a W link list and you can add before a given note. So starting on with adding a note at the front of a W link list. Let's move on to a compiler. All right. So we already have a structure and before that structure, we have to create a class as we did in our singly link list column right here. And we have to define functions right in this class and then using the objects that are going to be created right here. And let's name this class as W since we are talking about W link list. So the object is going to be WS1 equals to W. Good enough. So talking about the first one is how to add a note at the front, which means make the newly created note the head of the already created W link list. So for that, let me just show you how this is going to be done. But before we are going to study that, let's first see how to create actually nodes in a W link list. So we are going to have a create function. And in this create function, we are going to receive the data. And in here, what we are going to do is that we are going to create a node. First, we are going to create a pointer P. Let's say we are going to write in P equals to new node. The data part of P is going to equal to whatever data is received right here. Let's just name it to something different variables so that it doesn't mix with this data. So let's make it equal to let's say integer or something like that. So it doesn't mix up. So we are going to write in integer over here. Then we are going to make the next of this node equal to null. And then we are going to specify in a condition that if this P, not this P, we are going to first let's create a constructor function that is going to be a public constructor. And in that public, the name of the constructor is going to be doubly. And in this constructor, what we are going to do is that we are going to make a head node. Nodes direct head, and that is going to equal to null. So in here, we are going to write in if head equal equals to null, which means that the node that is created is the very first node, then what it's going to do is that it's going to make the previous equals to null the next equal to null as well. And what is going to do more is that it's going to make head equals to P, which means that head is pointing to wherever this P is pointing. And in this case, P is pointing towards the newly created node. So in the else condition, what we are going to do is that when head is not equal to null, which means that the node created is not the first node, then what it's going to do is that it's going to make P data equals to whatever integer was received, or it was already done here. We need to just look up to the pointers right here. So the pointers that is next is always going to be equal to the previous node. And this previous is going to point towards something that is just the node before this P. So how to do it? We have to create a new pointer right here, node steric, Q and V. In here, we have to write in Q equals to P. One more thing we have to do that in here we are going to write in P previous equals to Q. And then Q next equals to P. And then finally we have to write in Q equals to P. Now what is actually going on in here? Let me just explain it to you here. So as you can see right above here at the top, if I just show you, it starts from right here. And you can see that we have two pointers. First, we have created a node at which P is pointing. So then we have a data part integer, then we have its next pointing towards null. And its previous also has to be points toward null and that is done right here. And then we says that if head equals equals to null, this statement I guess need to be removed from here. Yes, this has to be removed from here because we have already mentioned it here and here. So this doesn't seem to suit here. So the data part is equal to whatever the value was received. Then we say if head equals to null, which is for the very first node, then what it's going to do is that it's going to make the previous and the next equal equal to null. Moreover, head is going to point right here and Q is also going to point right here. Now for the second node which was created right here, this condition was not fulfilled and we said that P was pointing right here and we say that its next is pointing towards null. Now we say that its previous is going to point towards Q which is right here, which means that this is going to point right here and we says that Q next is going to point towards P. We have to establish a two-way connection from P to Q and then Q to P and then we says that Q next equal to P and then we says Q equals to P which means that Q is now going to come here after establishing the connection. So when the third node is created right here and P is pointing towards this newly created node, so this condition, this set of condition is going to be executed. The next is going to be equal to null. The previous of P is going to point right here because Q has now been moved here and the Q next is going to point straight towards this newly created node and then Q is finally going to come here after establishing the connections. So I guess that is clear. So how many nodes you created doesn't matter. You can create as many nodes as you want just by calling this create function. So let's just call this function using the object s1.create and let's just pass in 4 for the first time then pass in let's say 5 for the second time then pass in let's say 7 for the third time and just execute it. All right, the error message is that hell was not declared in this scope. Yes, we haven't declared it. We need to declare it right here. So we are going to write a node, star a cat, now run this code and you can see that it ran perfectly. We have not yet defined our display function that's why nothing was outputted and besides that it ran perfectly. So let's just create the display function now. So this is our display function and in this display function all we need is a pointer node steric let's say let's name it as display or dis and it is going to equal to head which means that it's going to start iterating from the head node and we are going to write in while dis is not equal to null which means until and unless the last node is created it's going to keep iterating and how it is going to iterate it it's going to see out dis data and moreover what it's going to do is that it's going to make dis equals to dis so it's going to keep moving this dis and the first line is going to keep printing data for us. So if I just now call this function right here s1. and this is my function I call it here run this code and you can see that I have this four, five and seven displayed the very same values I passed in to the create function and those were then inserted in the dumpling list and that were displayed using this s1.display function so now let's move towards insertion now how to insert elements the first method as I told you was how to add a node at the front which means for example you want to add a node right here at the top right here so what we need to do is that since dis head is no more pointing here since dis p is no more pointing here dis head is pointing here but we have to remove it since dis is now going to be the new head so the first thing we need to do is that we need to move over head here but before that we need a pointer here let's say q or just name it anything for example let's just name it as no so this pointer is actually because we have to move this head from here and when we move this head from here nothing is going to point here then how are we going to how are we supposed to establish this and this connection so for establishing these two connections this pointer here so then we can write in head next is going to equal to whatever this pointer is and this whatever pointer it is going to be is previous is going to be equal to head so that this is the newly created node then so just about this display function let's create a function named add before or let's say add new head that seems to be much more relevant so we are going to create a new pointer node steric let's name it as new pointer and this is going to equal to head so now what we are going to do is that we are going to just create a new node v equals to new node we are going to just specify the data that is going to equal to whatever integer was received here let's just make this right like this so now we have specified the data now we need to establish the connections so to establish a connection we now have this node pointing towards head so first we need to move our head back so we are going to write in head equals to b which means that head is now pointing towards the newly created node which we want to make the new head and then we are going to write in head next is going to point towards the new pointer and the new pointer previous is going to point towards head or you can just use p here instead of both these heads you have written here because p is also pointing towards the very same area at this time so let's just call our function before the display function so we are going to write in s1 new head function and in this new head function let's just pass in 2 on this code write it says p equals to new node is a problem so it says p was not declared in this code yes it was not we have to write in node p equals to new node let's just run it now and now it's going to be run perfectly and this 2 as you can see is added above everything which means that this 2 is now the data of our head node so I hope it is clear so the second part of the tutorial says that how to add at the very end so we are going to create a function at the end and it is also going to receive the data and how to add data at the end we have to simply update our pointers so we need a new pointer so we need a new pointer that is going to be named as ending let's call it as end node and it is going to point towards head we are going to move this pointer using a while loop towards the very end and how this is done you already know this we are going to write in while end node is equal to null what you are going to do you are going to just keep moving forward so end node is equal to end node next which means that this is just going to move towards the end and after this move towards the end what we are going to do is that we are going to create one more pointer and the new pointer is going to be the previous node and it is going to equal to end node which means that end node and previous node are pointing towards the very last node now so now we can just create our new node node star of p equals to new node so this is our newly created node we are going to specify the data that is going to equal to whatever integer was received and then we are going to establish the connections so we are going to write in end node next is going to equal to p and the previous of p is going to equal to end node and finally we are going to write in end node equals to p because we don't need that here anymore I guess we don't need this pointer here as well our job can be done just using this end node pointer so if you just call this function s1 dot at the end and pass in let's say 9 call the display function sticking some time write it doesn't generate it and you put some problem I guess so the reason it didn't generate any output is I guess yes we have not specified it's next and I guess we don't need to move this end pointer right here as well if you just remove it nothing is going to happen but if you keep it here nothing is going to happen again so we are going to write in p next equals to null since this is the last node so it's next is going to be null so if you just run it now yep it ran perfectly it says 2 4 5 7 9 that is our required output that 9 was added or you may say append it at the very end so we have covered this and the last one so moving now towards the second and forward after a given node and before a given node how to add a node after a given node and how to add a node before a given node so let's move on to a compiler so let me just explain this concept from here so let me just create a new one alright so here we are so for example you have this node then you have this node and for example this contains 2 this contains 4 and this contains 6 so for example let's say you have to create a node right here that has let's say that element of 5 so if you want to insert this 5 over here what you need to do is that you know that we have a connection from here to here and here to here but now what we want is that we want to actually just remove this connection and this connection and what we want is that we want to establish connection right from here to here here to here then from here to right here and then from here to here so these are some of the connections we want so how this is going to be implemented now let's see so right after this at the end function we are going to create a function named as after a node since we are going to add something after a node and we are going to receive an integer value as well as we are going to receive a key the key is going to tell us that after which node actually we want to insert some data alright so what it's going to do is that it's going to check for the node so for first condition we are going to write in node let's say the node name is w and it is going to equal to head which means that it's going to point towards head then we are going to start a while loop and we are going to write in while the w data equal equals to key or you may say while w data is not equal to key we want this loop to keep executing what we want to do is that we want to make w equals to w next so when we find the key after which we want to insert data this while loop is going to make that for us and then w is going to be pointing at the node after which we want to insert some node so now what we want here is we want one more pointer node steric let's name it as e and where we want to point it actually we want to point w here and then we want to point e right here so how e is going to be pointed here we are going to write in that e equals to w next since for now w is pointing at the node after which we want to insert a new node and now e is going to point towards the next of w which means the node before which we want to insert a new node so after this we can just create a node since now we have pointers pointing both at this node and this node as well so now we can just create this node so we are going to write in node steric p equals to new node we are going to specify the data for it and it is going to equal to integer we are going to now establish connections so we are going to write in that w next is going to equal to p and p previous is going to equal to w secondly we want to establish a connection with e so we are going to write in p next is going to equal to q and sorry not q it is e and the previous of e is going to point towards p so actually what we have done here let me just show it to you here actually e is pointing here w is pointing here and p is pointing here so we have established a connection such that if you start from here w next equals to p which means this connection is established and then when we say p previous equals to w this connection is established and for the last two lines p next equals to e which means that p next is now pointing here and e previous equals to p which means this connection is established so this is how always sorry connections are established and we already have data in this. So I guess that's going to work perfectly. So all we need now to do is that we need to call this function. So we're going to write an S1 dot and we are just going to call this function after a node. And since we are inserting, let's say we want to insert, let's say six, and we want to insert it after this. So we're going to write in five comma six. So if you just run this code, you can see right here that five has been inserted right after this six. So this is how you can insert a node after a node. So towards the last concept that says you have to insert a node just before a node. So how this is going to be done. So we are going to create a function for that that says before node. We are also going to specify the key for this as well. And we are going to specify the data part as well. So this concept is going to be very much similar to this concept right here. If you just scroll it down, just remove these things that seems to be a bit irritating. All right, so for example, we have this set of nodes, we have this node, this node, this node, and this node. And for example, we want to add something before this node, which means that we want to add a node right here. As you know, that we have a connection from here to here and then from here to here. But what we want now is that we want a connection to establish from here to here and then from here to here and then from here to here and then from here to here so that a node is established between these two so for this let's just code it and it is going to be very same like this so if we can just make a few amendments in it and just copy this I will just show you just copy this is to try it here and you can see that W is pointing towards head perfectly alright notice pointing towards E alright and it says W is W data is not equal to key perfectly our W is pointing towards that node now what we want is that we want in this in the previous case what we want that we want let me just create a new one I want a pointer at this node and this node if I want to insert a node something here but now in this case since I have to insert a node before and not after for example if I want to insert a node here then now I want is a pointer here and a pointer here so for that I want a pointer at the very data before which I want to insert data this is actually the W is going to since the key is going to be that key at which W is going to be pointing so now I want not the next pointer I want the before pointer so I'm going to write in W previous only and this is I guess going to do the job for me except that I have to just change this connection now E is pointing towards the previous node if you can see right here that we have W pointing here E pointing here and this is our newly created node that is created using P is pointing right here now all we need to do is that E next is going to point towards P P previous is going to points towards E P next is going to points to our W and W previous is going to points towards P so in this case we need to just reverse all of these which means that you have to just remove it from here and we are going to write in W previous is going to points toward P and you can see right here and then W previous as you can see that it's pointing towards P and P next is going to points towards W so we are going to write in P next is going to points towards W and for the second part we are going to write in E next is going to point towards P and final connection if you can see from here is that P previous is going to points toward E so we are going to write in P previous is going to points towards E so I guess that's good enough so in here before the display function we are going to write in S1 dot and it is before node function so we are going to specify the node for example we want to just insert it before 9 so we are going to write in 9 and we are going to write in let's say we want to insert something like 8 so we are going to just run it and you can see right here that we have this 8 before this 9 not after this 9 as in case of 6 you have seen that 5 or 6 was printed after this 5 and in this case it was printed before 9 so this was the difference between the two and I hope you have cleared it much because I've explained to you using the code I've explained to you using these diagrams and how these connections were established using the diagrams as well so I hope you have understood all these concepts you know how to create how to just create a simple function to insert elements into a W linked list how to display those elements from a W linked list how to then insert elements at the print of a W linked list after a given node before a given node and at the end of a W linked list so I guess that's it with this tutorial thank you so much guys for watching and I'll see you guys in the next tutorial hey guys what's up this is Umar Khan and I welcome you to another tutorial linked list in C++ in this tutorial we are going to be continuing with W linked list and in this tutorial what we are going to study is that we are going to see how to delete elements from a W linked list so in the previous tutorial you know how to create a W linked list how to insert elements in a W linked list at any location you want so in this tutorial we are going to see how to delete those elements we created in the previous tutorial so without wasting any more time let's move on to our compiler and as usual to save your time we already have a W linked list created which was created in the previous tutorial so just before just above the display function we are going to create a delete function delete node since delete is a reserve keyword so the first thing we need to do is that you have to specify the node reference using a double pointer and you know what is the purpose of it this is actually going to point towards head you don't do it as I told you before you don't have to it and the second thing we need to do is that we need to take in the position which we want to delete so we are going to write in node stare right now the base case of this is going to be that if the reference we passed in that is the head reference is equal to null or the second condition that can be true is that tell equal equal to null so if this these conditions are going to be true what it's going to do is that it is going to simply return that the second thing we need to check is that if the node to be deleted is the head node or not if it is then what's going to be executed let's see we are going to check if the steric reference that is the head reference equal equals to tell which means that the key which we want to delete and this reference that is pointing towards the head at this time both are equals which means pointing towards the same location then what it's going to do is that it's going to write in head reference equals to Dell next which means that we are going to move the head reference one step ahead which means that the head is going to be the new node now since the first head is deleted so the third thing we need to do here is that we need to change the next only if the node to be deleted is not the last node in case of any node that is not the last node we have to change the next so how the next is going to be changed we are going to first check if it is the last node or not so we are going to write in if Dell next is not equal to null which means that it is not the last node then what it's going to do is that it's going to write in Dell next previous is going to equal to Dell previous not enough and the final condition it needs to check is that if Dell previous is not equal to null then what it's going to do is that it's going to make the reference sorry the Dell previous next equals to Dell next and at the very end after checking all these conditions whatever condition was met what it's going to do is that it is going to free the memory of the Dell so that it don't generate any kind of garbage values and then we are going to simply return and this condition I guess I didn't told you about this this is going to change the previous and it is only going to change the previous if the node to be deleted is not the first node since in case of the first node it's going to points towards null so that doesn't have to be changed in case of any node other than the previous node sorry the first node this has to be changed so if you just now call this right before this display function so we are going to write in as one dot and this is the function we have to call and we you know that we have to pass in two arguments the first one is going to be head and the second one is the node we want to for example you want to delete the head node and we write in head over here and just run this code alright it says had was not declared in the scope so we have to do one thing have to just remove it from here and make it a global variable here so if you just run it now it's going to run perfectly yeah it does you'll see that the head was removed since as you can see right here that four was the head in this case so this four was deleted in this case and if you can see the output again right here this is five seven which means the head was now deleted so if you want to delete for example something else you have to call this function again that says delete node that pass in the arguments the first argument is going to be head and for example the next node we want to delete for example the head next a colon right after this so I guess that's good it just right now you can see the seven is missing from the scene because when this statement was executed this four was no more here and this was our head then so this was actually the head next in that case so when we write in head next to be deleted it deleted this seven so I guess that's good so to show you just hear that that this five is the head just remove it you know that this is now the newly created head after this statement is executed so when we execute this this five is going to be now deleted if you just run it now you can see that seven is right here five is deleted from the scenes so as previous output let's just close it as well all right so good enough so this is how the addition is done so let me just give you a diagrammatic look of what is going on in here and then we can just close on with this tutorial so for example you have this node and you have this node and then you have this node in this case we have I guess four right here and we have five right here it is a doubly linked as we have this previous pointers over here as well all right so at least we have seven over here so the first condition is just to check our errors since you know that this is the head reference head reference is going to points towards the location we pass in and in this case you can just scroll down here and you can see that we have passed in the head which mean that this head reference is pointing towards head actually just open it again you can see that we have this head as well as head reference pointing towards this four which means what it actually saying that if head reference equal equal to null which means that no node is present in this doubly linked list or del equal equal to null which means that the location you want to be doesn't exist then what it's going to do is it's going to just simply return other than generating errors which it is supposed to do but this condition is stopping it from generating errors and it is just returning it the second condition you need to check is that if head reference equal equal to del which is the condition when you want to delete the head so these two are going to be equal because why they are going to be equal because these two things are going to be the very same since we have passed in the address of head here and we are again referencing to head here because the node to be deleted is the head node so these are going to be equal and what it's going to do is it's going to make head reference point right here the third condition says that if del next is not equal to null del next since this is del next for example the node to be deleted is not the head node so which means that del next is not equal to null so in this case del next is not equal to null which means that this condition is not true is true actually it is not equal to null which means this condition is true so what it's going to do is that for example you want to delete this node then this condition over here is going to be executed and it says that del next which means that del next is its previous is going to points towards the del previous alright so del next del is since pointing right here so it's next previous which is right here it is going to equal to del previous which means that it is going to equal to this so this node is going to be deleted then and then for final condition if del previous is not equal to null which is only case for the first one first when this is equal to null so for these two this condition is going to be true so if this condition is going to it says that del previous next is going to equal to del next which means that it is going to just finish over the connection for any of the nodes we are talking about and finally what we are going to do is that we are going to free the memory since del contains del is pointing towards the node we want to delete so it is going to just free the memory and return so in the first case we deleted the head node right here and in the second case we also deleted this head node as well right here so I guess that's there this is how we can delete elements from a wlink list so that's it with this tutorial as well. Thank you so much guys for watching and I'll see you guys in the next tutorial. Hey guys what's up this is Umair Khan and I welcome you to another tutorial on linked list in C++. In this tutorial what we are going to be studying is that how to sort elements uh uh. Hey guys what's up this is Umair Khan and I welcome you to another tutorial on linked list in C++. In this tutorial what we are going to be studying is that how to remove duplicates from a doubly linked list. You have seen this very same scenario in case of singly linked list and you know how to remove duplicates from a singly linked list. Removing duplicates from a doubly linked list is not much different from removing duplicates from a singly linked list. So we will just give it a quick look that how to remove duplicates from a doubly linked list. So this tutorial consists of two parts. The first part talks about how to remove duplicates from a sorted doubly linked list. And the second part talks about how to remove duplicates from an unsorted doubly linked list as we talked about how to remove duplicates from a sorted and unsorted singly linked list. So let's start on with sorted singly linked list, sorry sorted doubly linked list. So we will just move on to our compiler. And in our compiler, we are going to create a function that say let's say duplicates. And in this duplicate function, what we are going to do is first thing we need to check that if our entire linked list is empty, then we have no, for example, we're writing it had equal equal to null, which means that all linked list is empty. So we are going to simply return. Alright, so now we have checked that if our doubly linked list is empty or not, if it is not, then what we are going to do we are going to remove duplicates from this doubly linked list. So how it is going to be done. So first thing we need to do is that we need a pointer named as current, and it is going to point towards the head. Then we need one more pointer that is named as next. Alright, now we need to traverse the list till the last node. So we are going to use a while loop for that purpose. And we are going to write in while current next is not equal to null, or you can simply write in while current is not equal to null. What it's going to do is that it's going to compare current node with the next node. So to compare a current node with the next node, what it's going to do is that it's going to write in if the data in the current node is equal equal to the data in the next of current, then what it's going to do is that it's going to delete the node pointed to by the current next. So we are going to call in the delete node function, which is right here. And you know how this is actually working. If you don't know, then I suggest you go to the previous tutorial because the previous tutorial was how to delete a node from a W linked list. So we are going to call in the delete node function. And what are the arguments that are going to be passing the first argument is going to be the head from where it is going to start looking. And which node it is going to delete, it is going to be the current next. Since if a match is found, which means that the current data and the current next data are pointing, or you may say, store the same data, which means, for example, they both contain a two in them. So we have to delete one of them. So for example, we want to delete the next one. So we are going to pass in current text, which means that the current next node is going to be deleted, which is a duplicate in this case. So in the else condition, what we are going to do, we are going to simply move to the next node if some match is not found. So we are going to write in else current equals to current next, good enough. So if you just move down here, and let me just first run it simply, you can see that we have this 457 over here. If we just call in S1 dot duplicates, run this now, you can see that my output is same because no duplicate found. And now if I just create a duplicate right here, and let's say it's four, since we are talking about sorted link list. So this has to be in a sorted form. So we have written 457 over here, then call the duplicate, then call the display function. So if I just run it now, you can see that it should have been two fours, but since this duplicate function was called one four was missing from the C to show you this, let me just remove it, run this code now. And you can see that I have this two fours right here, which means that the duplicate data was showed. But if I just put in this S1 dot duplicate back, run this code. Now you can see that one of the four is missing, which was a duplicate in this case. So that's how you have to remove duplicates from a sorted doubly link list. So now let's talk about how to remove duplicates from an unsorted doubly link list. So for that, we will create a function double duplicate or let's call it unduplicated, which means unsorted duplicate. So here are the first condition as usual that need to be checked is that if had not if had equal equals to null, which means that the doubly link list contain no elements. And one more condition which you can check right here is that if the doubly link list contain only one node, which is possible only if the head head next is pointing towards null. And you know that the last node next only points towards null. So if the head next is pointing towards null, which means that it is the last node and since it is about the head, so which means it is also the first node, which means that it contain only one element. So if it contain only one element, then the chance of duplicate is impossible. Because in link list, each node contain only one data element. So in case of one data element, the chance of duplicate is 0%. So we can just check these two conditions that if no element exists, or if only one element exists, then we can simply return it, which means we want to do nothing with it. So the second part now, now we have to initialize pointers as usual. The first pointer is let's say PTR1. And then we have one more pointer. It is PTR2, steric with it as well. And a colon finally, semicolon. All right. So now what we need to do is that we need to pick elements one by one, then we have to compare the pick elements with the rest of the elements. If we find a duplicate, then we have to delete it. And in that procedure, what we have to do is that we have to store the pointer to the node next to the PTR2 or any pointer we create instead of this right here, or you may if you use this one. And the second thing we need to do is that we need to just delete the pointer in which we have stored our duplicate. And then we have to update that pointer. So let's just code it. So we have these two pointers PTR1 and PTR2. Now we have to use a follow. In this follow, what we are going to do is that we are going to write in PTR1 equal to head, which means it is going to start from head. And it is going to iterate until and unless PTR1 equal equal to null. And what it's going to do is that it's going to make PTR1 equals to PTR1 next. So you can just do it using your while loop as well. But I introduce the concept of follow here so that you can also understand and see that this type of job can also be done using this follow. So this was just for the concept of understanding, you can just use your while loop as well and specify this condition and you can just update these things before and you can specify this condition as well. And this condition has to be in the while loop then. All right, so in this what we are going to do first is that we are going to write in PTR2 is going to equal to PTR1 next, which means that wherever PTR1 is pointing PTR2 is going to be pointing towards the next of that node. For example, if we have these two nodes, all right, this doesn't seem to be a node at all. So let's just remove it and create a new node. All right, so we have these two nodes. If PTR1 is pointing here, this is the first pointer, then it is sure that PTR2 is going to be pointed right here. Since we have written PTR2 equal to PTR1 next. Now we need to compare the picked elements with the rest of the elements. So for that, we have to specify a while loop. And in that while loop, we are going to write in PTR2 is not equal to null, which means that it is going to compare it with each and every element. So we are going to write in now we are going to check for the duplicates using an if condition now. The if condition is going to say that if PTR1 data equal equals to PTR2 data, as you can see right here, that one is pointing here, then two is pointing right towards the next node. So we can simply compare these two data by saying that if PTR1 data equal equals to PTR2 data, if they are the same, then this is going to return true. And what we are going to execute, if this is true, we are going to write in node next, which was already initialized previously, I guess. No, it wasn't. So let's just initialize it not steric next, not it next. And it is going to equal to PTR2 next, which means that now we have a new pointer named as next pointing right where where two is pointing in case we have a duplicate. Now we have actually store the pointer to the node next to the pointer. I'm sorry, next is not pointing here. Next is actually pointing towards the node. Next is not pointing here. Next is pointing right at the node after the two, which means next is pointing towards the third node. This is the first node. This is the second node. And this is our third node. So now next is pointing towards the next of the second node. As you can see right here, node next equals to PTR2 next. Alright, so now we have store the pointer to the node next to PTR2. Now what we need to do is that we need to delete node pointed to by PTR2. So how are we going to delete it? We are going to call the delete node function, which is right at the top. We are going to specify head right here, which means we are going to start looking from the head and we are going to write in PTR2. Alright, so PTR2 is going to be deleted now. And now what we have to do is that we have to update PTR2. So we are going to write in PTR2 equals to next. So which means that this contains a duplicated data. This node was deleted. This two pointer pointing number two PTR2 is now updated and is moved right here and which means that next and two are now pointing towards the same node. So this is done. So in case if we have more nodes like this and this, and we have some more duplicates right here, then we can just move next here, delete any kind of duplicated node. For example, this is also a duplication. So this is also going to be removed. And then two is going to move right here. And for example, this is the final node, and this is not any kind of duplicate. Then next is going to move here. And then two is also going to move here, but this will remain. This is going to remain and this is going to be remain. And these duplicates are going to be deleted. Alright, now towards the else condition, that is going to be specified right after this if condition. And what is going to check since the if condition was checking that the data was equal, so what if the data was not equal, it is going to simply update the PTR2 pointer as well and how it is going to update it, it is going to move it one step ahead. So in case these two, the node number one and node number two did not match and did not contain duplicate elements, then what it's going to do is that PTR2 is going to be removed from here and PTR2 is going to point right here. And now this node data is going to be compared with the rest of the nodes. So I hope it is clear. So let's just now call this function. Let's just remove this. Don't call this. And let's just answer the data. Let's call it 8. Let's call it 9. And let's just copy this from here. Paste it twice. And 11 over here. And you can see that we have these repetition of nine and it is an unsorted data. So we have called the display function, but before the display function, we need to call in the unduplicated function and just run our code. Alright, so we have an error that says expected a colon before PTR2. Yep, right here. We have a colon missing. Let's just run it now. Alright, you can see that nine is now missing from the scenes, which means that the duplicate is now removed. Alright, so this is how you can just remove duplicates from a W linked list, either it be a sorted or unsorted W linked list. So I guess that's it with this tutorial. Thank you so much guys for watching. I'll see you guys in the next tutorial. Hey guys, what's up? This is Umar Khan and I welcome you to another tutorial on linked list in C++. In this tutorial, we are going to be studying a very simple concept that is how to find a length of a W linked list. As you have seen in the case of single linked list that all we need is a counter variable. Then we need a pointer that starts from the very first node, move towards this last node and count each and every node on its way. So the same is going to be with W linked list. So let's just move on to our editor. And as you know that to save some time, we have already a W linked list created that contains four nodes, which means that our counter variable when return its output is going to generate a alright. So what we need to do is that just above this display function, we have to create a function named counting. And the first thing we need to do is that we need to initialize a counter equal to zero. Alright, now we need a pointer of the structure node. Let's name it as P and we want to point it towards the head. Alright, now all we need is a while loop. And we are going to write a condition that while P is not equal to null, which means that P is going to keep iterating until and unless the last node is reached. And how we are going to move this pointer towards the last you know that we are going to write in first we are going to just initialize or increment of a counter, we are going to write in counter equals to counter plus one. And after doing what we want, we are going to write in P equals to P not data, it's going to be P next. So I guess that's it. So what after it go on with this entire loop, we have our counter completely filled with the number of nodes. So all we have to do now is that we are going to write in see out counter and line space, line space need to be added here as well. See out. Alright, good enough. So let's just call this function now. Right below this display function, we are going to call in s one dot. And the function name is counting good enough, call it. And let's just run over code. So you can see right here that these are our nodes, a two line space. And then we have this count variable that is equal to four. So I guess that's clear. So this was a very simple tutorial to cover. So that's it with this tutorial, I guess. Thank you so much guys for watching. And I'll see you guys in the next tutorial. Hey, guys, what's up, this is Amir Khan and I welcome you to another tutorial on linked list in C++. In this tutorial, we are going to be covering how to reverse elements of a W linked list. So for that, let's move on to my editor. And we have already this link doubly linked list created that contains the element for eight, five and nine. So when we reverse it, it's going to be nine, five, eight, four. So without wasting any more time, just above this display function, we will create a new function that is going to be named as reverse since we are going to reverse elements. And in the body of that, we need two pointers. Now, if you know about how to swap elements, for example, let me just show you here, for example, if you have two variables a, and then you have another variable b, what if you want to swap the values of these, for example, this contains one, and b contains two. So if you want to swap the values of these tools with each other so that a becomes two ends b equal to one, you know that you have to take a new variable named as temp, you have to store one of the values either it be one, or it be to the value of a or b respectively, in this temp variable. So we are going to write in temp equal to, let's say a. So at this time, temp and a has the same value that says one. So now we can write in a equals to two, which means that now temp contains one, but now a contains two and b also contains two. So when we write in b equals to temp, which means that now a contains two, whereas b contain temps and since temp was equal to a, which means temp contain one, so b equals to one, which means that the values have been swapped. So in case of notes, we also need that temp not variable. This time we are going to be initializing it as a pointer. So we need a pointer temp. So at this time, it is going to point towards null. And then we need a note named as current. And what is the purpose of this note, it is going to move us from the starting note towards the end node and swap each and every entry on our way. So now we are going to use a while loop. And this while loop is going to use that current pointer. And the condition is going to be the same as usual. Let's say this current is not equal to null. So now this is where the temp pointer is going to come into play. So we are going to write in temp equals to current previous, then we are going to write in current previous is equal to current next, then we are going to write in current next equals to temp. And finally, current equals to current previous. So now you might be confused that what is going to happen with node one, whose previous is pointing towards null. Let me just show you here. So let's just scroll it down to the full size. Alright, so here we are. So for example, we have this node number one. Then we have this node number two. Then we have this node number three. And then this is our final load. That is node number four. And it's next is pointing towards null. And this is the head node. That is pointing towards this node. And it's previous is pointing towards null. So the first condition when it's this while loop actually start, it says that current is not equal to null, which is true. But in this case, we are talking about the head node. So when it says that temp equals to current previous, which means that temp will store null at this time, temp stores null. And then it says that current previous equals to current next. So this means that this equals to this. And then it says this equals to this, because the next condition is current next equal to temp, which is current previous. So this means that it is pointing here, and it is pointing right here, doesn't make any sense. And then it says that current equals to current previous. So current is pointing here. And current previous is right here, which means that for the first time, it doesn't mean anything. But when it reached for the second, this node right here, when temp is pointing here, sorry, not temp, current is pointing here. Then we write in temp equals to current previous, which means that now temp is pointing here. Since at this time, the previous of current is this node. And then when the second condition is executed, that says current previous equals to current next, which means that current previous points towards this node. Sorry, not this node, the next node, this node. So then the next condition is that the current next is equal to temp, which means that the current next is now pointing towards this node. So then it says that current equals to current previous, which means that current is going to move backward. Now why it is going to move backward? Because this, this over here as has actually moved over here, because of these two connections made, and this has moved forward. So now we need a current here. So that at next stage, the current is moved right here. And then these two can be actually reversed. Because at the end, what we want is that we want this note at the very end, and this note right here at the very start, because this is what actually reversing a link list means. Alright, so one final thing we need here is to just go on with our head property now. Now we have to check before changing the head, we have to check for the cases like empty list and list with only one node. So we have to now change our head and check a condition for that as well. So we are going to write in if temp is not equal to null, then what it's going to do is that it's going to make the head equals to temp previous. Now why the head is has been initialized to temp previous because at the last node when this condition is executed, the temp is going to be just at the second node. The temp will be just at the second node in the swept up list. So when we write in that head equals to temp previous, and we means that we are going to make this to be the head, which is actually exactly what we want. So this is going to initialize our head. So I guess this will run over code. So let's just call it using our s1.reverse colon and just run code. Compile it just run it. All right, you can see right here that we have our linked list reverse perfectly 9584 was exactly what we wanted. So I guess that's good. So you have seen that we have just reverse over linked list. So that's it with this tutorial as well. Thank you so much guys for watching and I'll see you guys in the next tutorial. Hey guys, what's up? This is Omar Khan and I welcome you to another tutorial on linked list in C++. In this tutorial, we are going to be starting on with our circular linked list. And I'm going to tell you about circular linked list. So now what is a circular linked list? Now circular linked list is a little more complicated data structure. In the circular linked list, we can insert elements anywhere in the list whereas in the array, we cannot insert elements anywhere in the list because it is in the contiguous memory. In the circular linked list, the previous element store the address of the next element and the last element stores the address of the starting element. The elements point to each other in a circular way, which forms a circular chain. The circular linked list has a dynamic size, which means the memory can be allocated when it is required, not like arrays. So in this tutorial, basically, we are going to be talking about the applications of a circular linked list. And we are going to see the applications, sorry, the implementations of a circular linked list. So starting on with the applications of circular linked list. The real life application where the circular linked list is used is our personal computers, where multiple applications are running, all the running applications are kept in a circular linked list. And operating system gives a fixed time slot to all for running the operating system keeps on iterating over the linked list until all the applications are completed. Just suppose that if we use the concept of single linked list or doubly linked list in case of real life applications like personal computer, in which you have multiple tasks running, then it is going to not work fine because at the first time at the first iteration, it is going to run completely fine. But in the next iteration, since we don't have a pointer from the last node towards the second first node, and the last node points towards null, which means the ending has reached. So it is not going to give the second chance to any program or any thread or any process you may say in a time slice. So circular linked list come into play in that regard. Another example of the applications of circular linked list can be multiplayer games. All the players are kept in a circular linked list. And the pointer keeps on moving forward as a player changes or chance its ends. Now the circular linked list can also be used to create circular queues. In a queue, we have to keep two pointers. The first one is the front pointer and then the rear pointer at the front and rear in memory, all the time, whereas in a circular linked list, only one pointer is required. So to conclude my argument on the applications of circular linked list, I will say that circular linked list is used at places where you want something to run in a same fashion in the same manner again and again. As in case of circular linked list, you have a pointer from the last node towards the first node, and that pointer that points from the last node towards the first node is going to keep you moving along the linked list again and again. So now moving on towards the implementation of a circular linked list. Implementing a circular linked list is quite easy and you're almost familiar to the linked list implementation from this singly linked list and the doubly linked list with only one basic difference that in circular linked list, the last node will have its next point to the head of the list. I've already explained that in the previous tutorials and I guess in the introduction tutorials as well. So in linear linked list, the last node, as you know, holds a null pointer but in this case, it is going to hold the next pointer and that next is going to point towards the head node. So without wasting any more time, let's move on. So let's go on to our editor and we already have a basic format of a C++ program. Now all we need is to actually just create a class right here. And let's call the class as circular. Since in this case, we are talking about circular linked lists. And we are going to have public members that has the data bot. Let's just create a structure right at the top as well. So the structure name is going to be note. And you know that circular linked list can be both single linked list and doubly linked list. So in this case, I'm going to create it as a single linked list. So we will have only two things that is the data part and the next part. So let's just remove this from here since already we have created it at the top here. So now next thing we need is that we need to make a constructor function that is going to be named as circular. And in this constructor function, what we are going to do is that we are going to actually initialize the values for these that is going to be equal to zero and the next is going to equal to null. If you just don't do it, it doesn't matter. But this is actually a bit important because for the first time, when you initialize an object of this class, actually this constructor function is going to be called and the next is going to be initialized to zero. So that no garbage values are generated at the first call. So let's now see that how elements can be inserted into a circular linked list. So for that we will create a function named as create. And in this we are going to receive the value which we are going to enter into the data part actually. And in this create function, the first thing we need to do is that we need to create a pointer. And using that pointer, we need to create a new node of the structure. Alright, with the new node, what we want to do is that we want to insert data. And that is going to equal to whatever value is received in the arguments. And secondly, what we want is that we want it's next to be equal to null. Now in this way, our first note will be created. But since we were talking about circular linked list, so where does actually the circular linked list concept comes. So here first, we are going to have a condition if condition that is going to say that it had equal equals to null, which means that this is the first note created. So what it's going to do is that it is going to perform actually this statement right here. Alright, so for this case, what we want to do is that we need to first have a constructor function, which is right here. And in this we are going to write in head equals to null. So when had equal equal to null, which means P next is now going to be equal to null. But one more thing we need to do here is that we need to make head equals to B, which means that when this constructor function is called, this head was equal to null, this condition was true. So whenever this first node was created, head was going to point towards that node. And it's next was equal to null. Now in the else condition, we are going to code for any remaining nodes other than this node, which means that had is not equal to null. So when had is not equal to null, the data part has already been inserted. Now what we want to do is that node has been created or data part has been done. Now what we want to do is that we want to do something with this next value. So what we are going to do with this next value, we are going to first initialize a pointer for that purpose as well. So we will have a pointer Q of the structure note. And here we are going to write in Q equals to B, which means that Q is also pointing at the very same location P is pointing. So now in the else condition, what we are going to do is that we are going to write in that B next is going to equal to Q, not Q, it is going to equal to had actually, because this is a circular linked list. And in this case, this is the last node created. So it's next is going to point towards the head node. And moreover, we want to establish a connection between the first node and the second node as well. So we know that Q is pointing at that node at this time. So we are going to write in Q next equals to P. And then we are going to write in Q equals to P. So I'll set. So now if I just show you what just happened here. So you can see right here. So you can see right here that we have actually a note. For example, just consider that we have these three nodes in our structure. Now you can see that P points towards the first note is data bars was inserted with any value right here. Its data part was inserted with any value here and its data part was inserted with some value right here, not here, the data is going to be right here. In the first lot. All right, then it says that it had equal equal to null. Now for the first condition head is going to equal to null because of the constructor function. So at this time head was equal to null. So this statement was executed and it says that had equal to P, which means that head is now also going to point towards this note. Then P next was equal to null because this is the first note. And then we said the Q equals to P, which means that we have introduced a new pointer named as Q, and we have pointed it towards our very first note. And the logic of introducing this new pointer Q here is the very same as we introduced in the singly linked list concept, where we added this pointer just to establish connection between the previous node and the newly created node, which will be this in the next case. So when the second node is created, this condition was false because head is no more pointing towards null, it is pointing towards the previously created node that was had in that case. So this condition was false and this else statement was to be executed. And in this else statement, it said that P next equal to head, which means that this next was pointing right towards the node where head is pointing. All right, so it starts pointing here. And it said that Q next equal to P, this is actually Q. So Q next is now pointing towards this node, where P is pointing at this time. Because this P this node was created using this pointer. So this was just removed from here and moved right here. And then we said that Q equals to B, which means that Q was also moved here. So you can see right here that this is a circular link list. Now, the last note next is actually pointing towards heads. For example, you create a third node, P is going to come right here. Then P next is going to point towards head right here. And this Q next, which was previously pointing towards the head is no more the last node. So it is going to start pointing towards P, which is right here. And then we have to move this pointer Q in order a new node is created, it is going to help us with the connections setup. So I guess that is clear. So if we just I guess now run this code, it's going to run perfectly. So the first thing we need to do is that we need to create an object. Circular is the name, as one is the name of the object. And then again, circular calling at the end, and then using this object, we are going to call in our function that is named as create. Good enough. Yep, now, and now we're going to pass in some value to it. I guess we have the correct name. Circular it is. All right, so let's just run this code. Oops, I guess we have a bunch of errors right here. This is that how it was not declared in this code. Yes, has to work. This next does not need to be here. Just remove it. And yes, this head needs to be made public right here, or you may say global. So we are going to write a note steric head. So this head is no more going to generate any kind of error. And I guess that's good enough. Let's just run it again. Alright, it says that the constructor has to be private as well. Yes, constructor has to be public, because we have initialized it as private here. So we need to make it public. Because the constructor values are going to be used everywhere, you know, because you know that the head is used everywhere. This data part is used everywhere. Since we have initialized it at the top and make it as public value. So that's why we have commented it out from here, let's just move it completely from here. And it says that had equals to null. So this has to be public. So let's just run it now. And yes, you can see that it ran perfectly. If we pass in one more value, that is as we're not create, let's pass in five this time, run this code again. And yes, it ran perfectly, which means that we have both the segments correct. The first segment is this, that was about including the first note. And this is about including any note after the first note. So now let's just create our display function to display the values. And then you are going to see that how a circular linked list is going to keep iterating. So the function name is going to be display. All right, so now in this display, all right, so now in this display, all right, so now in this display function, we need a pointer, let's call it dis and it is going to equal to head, which means it is going to start from the head, we are going to use a while loop that while dis is not equal to null. What it's going to do is that is going to see out dis data part align space. And finally, what it's going to do is that it's going to increment dis to dis next means it's going to keep moving. All right, so I guess that's it. Let's call the display function right after this as one dot create five. All right, so this is our display function, a colon and run over code. All right, that's a very common mistake. It's going to be equal. Now run over code. And you can see that a loop has started that is printing. If you can just have a closer look, or if I just stop this program. All right, so you can see right here that we have the output as 35353535 and it was going to be an infinity loop, but I stopped it myself. And you can see if you just scroll up, it started from right here. And then it keep moving, moving, moving, moving, and until infinity. So this is how a circular linked list actually work. So if you just remember the applications of the circular linked list, I told you that it is actually going to repeat a sequence of tasks. For example, you have 10 tasks, you want to, for example, assign two seconds to each tasks at a time. So what circular linked list is going to do, it is going to give two seconds, each to every 10 tasks, and then it's going to come back to task number one, and again, give it two seconds. So this is what is the purpose of circular linked list. Now there is a criteria to stop and you have to use flags, you have to use if conditions, and you have to use loops to stop such kind of displays, so that you can just iterate it the times you want to iterate, because you don't want for sure that to assign something a time for infinity amount of time, you don't, for example, if a task take 10 seconds, and in a single slice, you are giving it five seconds, then you'd only need to give it the control two times, which means that the circular linked list need to carry it only two times, not for the third time, because that will be a wastage of a time slot, because the current program has completed its execution. So there are flags for that purpose. And that's how the CPU actually controls such kind of things. So that's just beyond the scope of this what we are speaking right now, because that's more into operating system topics, and away from C++. So I guess for part of my tutorial, that's it. Thank you so much guys for watching and I'll see you guys in the next tutorial. Hey guys, what's up? This is Umar Khan and I welcome you to another tutorial on linked list in C++. In this tutorial, we are going to be continuing with circular linked list. And in this tutorial, we are going to be studying about insertion in circular linked list. So we are going to be studying two kinds of insertions. The first one is insertion at the start of a circular linked list. And the second one is insertion at the end of a circular linked list. And at the end, and the third part of this tutorial, we are going to be searching for the elements we have added in a circular linked list, either at the start, or at the end, or they were already present there. So let's start on with the first part that says insertion at the start of a circular linked list. But before we start on with that, I've actually done some flag work right here. Let me just explain that flag work first. First thing I've done is that I have initialized a global counter named as count. And then whenever I call this create function, that is to actually add a new element, this count was incremented to plus one. At start, this count was equal to zero, is t to be zero here. And then whenever this create function was called this count was incremented to one. Now in the while loop, I have actually changed this condition right here. That says while n is less than count. Now what does this mean? This means that it is going to iterate from n equal to zero, which I have written right here. And it is going to iterate till this count variable. And what is discount discount is actually the number of elements added, and how it is the number of elements added, because count is going to be incremented each time this create function is going to be called. And count at the start was zero right here. So whenever this count is called this count is incremented and this while loop is going to execute only the number of times this count has a value because it's going to start from zero, and it is going to iterate towards the number of elements present in the circular linked list. So if I just run this code now, you can see right here that I have only this three and five cents. This loop was iterated only two times, because the number of elements in this case was two right here. So I just added it so that the loop condition would be solved. And we could see a output. So now let's move on to the first part of this tutorial. That says insertion at the start of a circular linked list. So before inserting any kind of element, let me just show you here with the help of a diagram. So we already have two nodes. This is our first node, this is our second node, the first node has a data element tree. And then our second node has a data element of five. Now the first part of our tutorial says that we have to add a node just before this first node. For example, I want to add a two right here. So now what I want actually is that I want a connection from here to here, then from here to here, and then from here to right here. But in case of the previous nodes, you know that before this node was inserted, we have a connection from here to here, and then from here to right here. So we need to just remove these connections. Actually, we have to remove this connection from here. We have to the new connections that are going to be added in this case is this connection. And this connection is already added. So we have to just add this connection. And we have to actually remove this connection from here. So how this is going to be done. Let's just code this. So let's create a function for this. And let's name it as add at front. It is going to receive the data part that is going to be inserted into the data part. And the first thing this function is going to contain is that count equals to count plus one, because this is something that is actually adding an element in our link list. So this has to be incremented discount variable has to be incremented by one as it was incremented right here when this create function was called because this create function was actually adding a node. And this add at front is also creating a node for us. So this count variable has to be incremented at the very start, this function is called. Now, we have to first create a new node. So I'm going to write a node Serif P equals to new node. So now we have a new node created at which this P is pointing. Now you know that we have a head pointer pointing towards the previous head. But in now this is going to be the new head because we are adding this at the very start. So what we are going to do is that first we need to move a pointer from the very start towards the very end node. So for that, let's create a pointer, named as and and make it equal to head, which means the previous head. And then using this value, what we are going to do is that while so where it is going to add it because we do not have a null condition now. So we need to move it to a location where and is while and point towards head. Or you may say that while it is not equal to head, since the last node is going to be pointing towards this head node. So we are going to iterate it until this node is reached. So we are going to write in and equals to and next. Why have I done it? Because you know that this node, that is the last node in this case, is pointing towards the head. So we have written a condition right here that while head is not equal to and and equals to and next, since and starts from here, that is going to keep by trading between any number of nodes between this and is going to keep by trading until and unless this next is not equal to head. So when it is, it is going to point right here, which means that the end pointer is going to point towards the last node. Good now. So now, now we have a pointer at the last node, we have a pointer at the newly created node. And we have a pointer at the head node that is the head pointer itself. So now what we need to do is that we need to actually create a connection between the newly created node and the previous head. So we are going to write in P equals to or not, not P, P next is going to equal to head. And since P next is pointing towards head, which means that a connection between P and head has been established successfully. So now after establishing the connection, what we need to do is that we need to move the head backward. So now head is pointing towards the newly created node, which is actually the new head now. So now the last thing we need to do is that now we have this connection right here. We have this connection right here, the head is pointing at this node. And finally, what we need now is that we want this node, which is the final node, we want this node to point right towards the newly created head, not this head, you have to diminish this connection and establish this connection. Now when this connection will be established, we are talking about this connection right here, that is this connection, then actually what is going to happen is that this connection right here is automatically going to be diminished. So we are going to write in that and next equals to you can write in head or you can write in P. So since head and P both are pointing at the node that is the newly created head. So I guess that's good enough. So now let's just run this code. But before running this code, we have to call this function as well. So let's call it as s one dot. And this is the name of the function. Let's pass it to colon and let's just run this code. Or we've just got garbage values. And this is exactly what we don't need. And these garbage values I guess generated because we have not initialized our integer value. Yes, we have not assigned anything to the data part. So the data part is going to equal to integer. And now I guess it's going to run without generating any run. Yes, garbage values are finished. And you can see that two has been added at the very start. But still we are getting an issue that this two is being repeated. They should not be the case the output should have been 235. I guess we need to do this job with a global variable not with this. So let's just remove this and stuff from here. And make it even more simpler. Let's just initialize a pointer right here. That is named as last. And every time the node is created, the last pointer is pointing towards it, which means that last equals to the newly created pointer P. Let's just copy it from here, paste it right here, which means that now at the last node, we already have a pointer that is last equals to P, which is also global in this case. So now in this add, add front function, all we can do is that we can use that pointer, and we can just replace this as last next equals to head. And I guess that's going to solve a problem. And yes, it does. The output is now exactly what we want 235 respectively. So now what does this last was here doing? Actually, first, this last pointer is global. Second, whenever a node is created using the pointer, this last is also going to be pointing towards that node, which means that last is always going to be pointing at the last node. So right here, we have last equals to be in case of the head node. And then in case of any other node, this last is also going to be pointing towards the newly created node, which is the last node in this case, because create actually add the nodes at the very end. And then we have this add at the front function. And in that, we can use this last pointer. So this is how a new node can be inserted in a circular linked list. And that can be made the head. And the last node is then going to point towards that node. So I guess that it's clear. So the second part of the tutorial says that we have to add an element at the very end of a circular linked list. So adding an element at the end of a circular linked list is very similar to this create function, because when this create function is called an node is passed to this. So adding an element at the end is very similar to this create function right here. Because as you know that when we call this create function, whatever data we pass in, that is inserted into a node and that node is inserted automatically at the end. So in case of the head node, these conditions are going to be executed. But in case of any other node, these else conditions are going to be executed. Let me just diagrammatically shows you because I don't need to code it because this is already coded right here. So for example, we have this node, and then we have this one more node. And then we have for example, two here and then three here. And let's suppose that this is actually the new node we want to create. And it contains an element five. So, so now to talk about the connections. Actually, this node next is pointing right here. Let's suppose this is the head node. And our last pointer, it's been named as last, is pointing right here. And at this time, B is also pointing here, since this node had yet not been created. And the next of the last is pointing right here. So now when this new node is created, the connections are going to be modified. The head is going to be remaining at the very same position. Now what is going to change? Let me just use a different color. Let me use black one. This is our pointer P that is pointing towards the newly created node. And now last is also pointing right here. But before pointing last here, we need to use this last or you may say the variable Q, that is also pointing at this time right here, to establish some connections. Now the Q next is going to point right here, which was previously pointing at this head node. So this means that this connection has been rejected. And this connection has been established. Moreover, we want this P next to point right here at this node. Now, why does we want this to point at this node? Because the circular linked list last node, which is five now in this case, is going to point towards the head, which is right here that contain this element two. So this is the connection that is going to be established. So I guess that's clear. So now moving towards the last part of the tutorial that is about searching a specific element. So for that, let's create a function. And let's name it as searching. And this function is going to receive the data element we want to search. And it is going to have a pointer. Let's name the pointer as s e r, that stands for something like search. And it is going to point towards head node. So the condition here is going to be while s e r is less than count. What it's going to do is going to check an if condition that if the data element we are looking for data equal equals to the element in the key, then what it's going to do is that it's going to see out that element, element found, and it's going to return. Else what it's going to do is that it's going to write in s e r equals to search next. Good enough. So it's going to keep by trading until and unless the count and the count is actually the number of data elements present in this link list. And what it's going to do is that it's going to compare the data element with the key. And if that element is found, it's going to see out element found, and it's going to just return from here. Else what it's going to do is that it's going to move towards the next node. So let's just call this function. And this is our function right here. So let's say we pass in the key six. And I guess we don't need this display function anymore. Let's just call it. All right, it says that this is something not allowed, which means that we also need to use int n concept right here. See, I don't need to be here. It needs to be n, just like the case we used up here somewhere in the by loop. I guess I deleted that. So let's just run it now. And you can see right here that it's actually executing executing and since it is not finding that element, that's why it is not generating an output. But if you pass in something that is in the link list, execute it now, you can see that it says element found, which means three was found. Now, in case of six, you see that this program was not terminating. You see right here, this was because of the very previous condition when we use this very condition in the by loop. This has to be done in the very same manner as it was done right here, using this last pointer. So you can use it. And then I guess it's going to terminate. So you can apply that concept right here yourself. So you may be able to create a better understanding of that last pointer as well. So if you pass in something that is in the link list, as I passed in three, let's pass in five this time, run it. And you can see that it said element found. And now you must be thinking that why does this loop ended? This was because of this return statement. So I guess that's it with this tutorial. So now you guys knows how to insert elements into a circular link list at the start, at the end, and how to search for a specific element. So thank you so much guys for watching. And I'll see you guys in the next tutorial. Hey guys, what's up? This is Umar Khan and I welcome you to another tutorial on link list in C++. In this tutorial, we are going to be continuing with the concept of circular link list. And in this tutorial, what we are going to do is that we are going to see that how to delete an element from a circular link list. Now an element from a circular link list, that is to be deleted can be the head element, or it can be an element that is stored in the last node, or it can be any element in between the head and the last node. So we are going to be seeing all these three methods that how to delete elements from the start, how to delete an element from the end, and how to delete an element in between of that. So let's start on. So let's move on to our compiler. Alright, so right here we are. So let's create a function right above this display function right here. And the name of the function is going to be deleting. And here we are going to receive the key, which is going to tell us that which element we actually want to delete. Alright, so the first thing we need here is that we need a node. Let's name it as stem. And it is going to equal to head, which means it's going to point towards the head node. Then we need one more pointer, that is point to Q. And we want to point it towards head as well. Now the condition for the while loop here is going to be the very same. That is while n is less than count. And up here we are going to initialize this variable n, that is going to equal to zero. And the next thing we need to do is that we need to check that if temp data is equal equal to whatever the key that was passed into this function as an argument, then what we are going to do is that we are going to establish connections. Now this Q over here is actually going to help us to establish those connections. Now how it is going to help us, this is going to help us because we know that this is not the head node. So in case this is not the head node, what we want is because in this case, we are only dealing with the elements that are in between not the head node, not the last node, the elements in between the head and the last nodes are going to be checked using these conditions. So in the else condition, we are going to write in Q equals to temp, and then we are going to write in temp equals to temp next. Now what this is going to help us in the first iteration is that now Q is going to point towards the previous node and temp is going to point towards the node we want to check. Let me just show you. Alright, so you can have let's say some nodes. Now the code for which I am writing is that I want to delete this node. For example, this contains three, this contains two, and this contains five. Now we know that we have some connections like this. Moreover, this is pointing here and this is pointing here. So the new connections are going to be something like this because we want two to point towards five, and this connection is going to be the very same. We want actually this connection to be diminished because this node is going to be deleted and this connection to be diminished as well. When this connection between two and five is established, these two connections are going to be diminished by themselves. Now, what these Q and temp pointer is doing, because at the start we initialized both of them to right here towards the first node Q is pointing right here and temp is also pointing right here. So when for the first node, we know that we have a head right here. So in this case, we are not dealing with heads because if we pass in head to this function, this is going to generate garbage values because there are quite connections for the head node are not mentioned here. In the next part of this tutorial, we are going to be talking how to delete the head node. But in this case, we are dealing with elements in between the head and the last node. So we know that this is some node in between the head node and the last node, which is right here. So for the first time, this condition is going to be false because for the first time, this is going to be the head node, and this is not going to be the element which we want to delete. So it is going to move towards else. And what it's going to do is it's going to make Q equals to temp, which means right here, and then it's going to move temp one step forward, which means temp is now going to be pointing right here. So now I guess you understand the purpose of these two pointers. These two pointers were here because so that the temp which is checking actually the elements and its data right here and Q is going to point just at the previous node at which this temp pointer is doing its checkings. So now what is the purpose of that purpose of this is because this Q is then going to help us if a match is found here to establish a connection between this node at which Q is pointing and towards the next of the node we want to delete. So this temp is moved right here, which means right here. So for the second time, the condition is this condition is going to be equal to true. So now what we want is that we want the Q next to point towards actually the temp next, which is right here. And the rest connections are going to be the very same. So let's just do it. So we are going to write in Q next is going to equal to temp next. And then we are going to free the memory for the temp. And you are also going to decrement the counter variable as well. And then we are going to return. Now what is the purpose of decrementing this counter purpose is so that this display function since this is a global variable. So this is to be decremented because since when we were creating those elements, we have incremented the counter each time by one. So when we want to delete an element for example, if I just call in this function named as deleting and pass in let's say five. So this has to be decremented because now the number of elements are one less than the previously count variable value. As I told you that count actually store the value of the number of items in our circular linked list. Since now one element have been deleted. So the count variable has to be decremented as well. So let me just run it this code. So you can see that five is now missing from the scenes are right now towards the second condition. The second condition says that what if you want to delete the head node. So for that purpose I need to delete this if and else from here. And the vial condition is going to be the same. These are the brackets for the while loop. And in the while loop what we are going to do is that first we are going to write in if temp equal equals to head, which means that if temp is pointing towards head, then what it's going to do is that it's going to check that if the data in the head node, which is at this temp temp is also pointing there. So we want to check that if temp data equal equals to the key passed into this function as an argument, then what connections are going to be established? For example, we have this node, we have this node, and we have this node. So if you want to delete this node right here at which head is pointing, Q is pointing and temp is pointing as well. So if you want to delete this node, what we want is that we want actually this connection to be diminished, this connection to be diminished as well. And we want this connection to be established. And this connection is already established. We want this connection to be established and these two connections to be diminished. So for that purpose, what we want is that we want to move our head node right here. And then since we know from our previous tutorials, that a pointer named last is pointing right here at the last node. So what we can do then simply after moving this head over here that we can write in that last next equals to head, which means that this is now going to point right here. And this connection is also going to be diminished when we just write in that we have to free this memory. So when this memory is freed, this connection is automatically going to be diminished. So let's just code it. So if this match is found at the head node, then what is going to first do is that it's going to move our head towards the next node because when one head is diminished, then first node is then going to be the head. So the head is now moved one step ahead. And then we are going to write in last, next is going to equal to head. And finally, we are going to free the memory of our temp function. And then similarly, decrementing the counter and then followed by return statement. So if we just run it, no, we have not to run it, we have to just change the value. For example, this is the head in this case. So let's just run it now. So you can see right here that tree is missing from the scenes, which means that the head node has been deleted successfully. So now finally moving towards the last part of this tutorial that says how to delete a node if it is pointing towards the last node, which means if you want to delete one last node, then what is going, what are we going to do? So for that, we are going to first check if it is the last node or not. So we are going to write in if temp equal equals to last, which means that if temp is pointing towards this last node, which means that this is the last node, then what we are going to do is that we are going to first check that if the data in that node equals to the key, and if it is equal to the key, then what we are going to do is that we are going to make certain connections and how are these connections going to be established? Let me just show you right here. I'd use a different pen, but pen seems to be good. So this is our node, this is our second node, and this is our third node. So for example, you want to delete this node. So we know that a pointer name last is pointing right at this node. So what we want actually is that we want this connection to be diminished. We want this connection to be established simple enough. So as you know that when we were moving around, this temp is right here, and a pointer Q is right here, as I told you in the logic I explained right here, at how this Q pointer is actually moved. So this means that this Q is pointing just before the node at which this temp pointer is pointing. So temp at this time is pointing towards the last node, which means that Q is going to be pointing towards the second last node. So if this is the node we want to delete, which means that this is going to be then the last node. So what we are going to do is that we are going to just remove this last pointer from last pointer from here, we are going to move this last pointer right here. And then what we are going to do is that we are going to make last next equals to head, which is right here. So let's just do it. So it says that if temp data equals equals to the key, then what it's going to do is that it's going to make the last equals to Q, which means the last is now pointing towards Q. And then what we are going to do is that we are going to write in that last next is going to point towards the head. We are to free the memory of temp as well. Or before freeing the memory, you can also do one thing that you can write in that temp next equal equals to or just equal to now because we are actually initializing not checking. So we are going to write in that temp next equal to null, and then we are going to free the memory for our temp variable, then decrementing the count is very important right here. And then we are going to write in return. So now if we just pass in seven to this function, which is the last node in this case, run this code, or taking some time because it is moving towards the last node, I guess, or maybe I think some problem here. Let's just check it out. I guess we made it an infinite loop. That's why I kept checking. And I guess we missed something. Yes, I think I found the problem. Yes, we miss actually the else conditions. We are checking this for data. We are checking this for data using the temp pointer. But actually, we are not moving that temp pointer towards the next node. This temp pointer is pointing towards head, but nowhere in our code have we move it towards the next node. So this condition never becomes true. So I guess it keep iterating because this n was also not incremented anywhere in the program. The previous two parts in which we deleted the head and the between nodes, we also did not, I guess, incremented the end. But those programs exit because this if condition was true and this return was called so doesn't need to have this check this condition then it excited as soon as it saw this return statement. So we are going to write in first q equals to p as in the very previous case and why it is. So first we are going to write in q equals to p. And then we are going to write in temp equals to temp next. And finally, n plus plus a column right here. And I guess it's going to now work perfectly. All right, not be a stamp. Yep, a column here as well. Oh, please don't do this. Let's just have a second look. And temp is equal to Nost and then temp data has been checked q equals to temp. And I guess it should print perfectly. And yes, it does. It ran perfectly. Since I am not running a supercomputer. So that's why it was taking some time in the previous part, I guess, it got to hang up. So this is our output, which we exactly want seven is missing, which is the last node in this case. So I guess that's clear. So now I guess you know how to delete an element from a circular link list from any location. You can just combine this code in a single set of block. Let me just show you how. So for example, you have this conditions right here, if temp equal to last, you can specify conditions like using else if conditions, you can write in if temp is equal to head, you can then write the code in which you have to delete the head node. And then you can just write in the condition that the temp is not equal to head and temp is not equal to last. So if these two conditions both are true, which means that you are going to use and and operator, which means that both conditions have to be true to execute a set of a block. So you can write that condition and in that condition set of a block, what code you are going to write, you're going to write the code that wall that we covered at the very start, that is to delete the nodes that are in between the head and the last node. So in this way, if you just combine all that code and then you call in anything from here, it is going to be deleted. So I guess that's enough with this tutorial. So thank you so much guys for watching. And I'll see you guys in the next tutorial.