• 15 hours ago
Transcript
00:00The next thing we are going to talk about is the size of my link list.
00:03That is how we track the size of a link list.
00:06If I have a link list which has 4 nodes inside,
00:10I can say that the size of this link list will be equal to 4.
00:16So, I am going to make a static variable.
00:19static int size
00:21which I will initialize with 1.
00:24And how will I change this size?
00:26As I have added a new node in the link list,
00:30then I will add the size of this plus plus.
00:33And when I removed the previous node from there,
00:37then I will do minus-minus.
00:39We are not going to study the operations of remove.
00:40So, what will I do?
00:42As I will add a node,
00:44I will add the size of this plus plus.
00:46And I will keep the value static.
00:48Because as the node does the size of this plus plus,
00:51the same size in the same size.
00:54So, what will I do?
00:56I will define a size property for your link list.
00:59What will I do?
01:00Simply, my link list class,
01:02where I have defined the head and tail,
01:05I will define a new variable,
01:06public static int size.
01:09And it will initialize the value by default Java as my 0.
01:13As I am going to add first.
01:16As I am going to create a new node,
01:18I will create the size of this plus plus.
01:22And as I am going to add last,
01:23as I am going to create a new node,
01:25I will create the size of this plus plus.
01:27And I will add in the base case.
01:29And after the base case,
01:32I will add in the base case.
01:33Because in the add first,
01:34it is already plus plus.
01:35I will not have to do this plus plus.
01:37So, I will create the size of this plus plus.
01:38As I am going to create a new node,
01:40I will create the size of this plus plus.
01:43Let's save this.
01:44And this was my link list.
01:46I have 5 elements.
01:48How did I have?
01:49First of all,
01:50I have 1.
01:52I mean,
01:52here is my link list.
01:54It should be something like this.
01:55I am doing 1,
01:552,
01:562,
01:579,
01:579,
01:593,
02:003,
02:024.
02:03So,
02:03I have put a link list.
02:05In fact,
02:06I will restructure it a little bit.
02:08How will I restructure it?
02:10Here I will pass 4,
02:12here I will pass 5.
02:14And 2,
02:14then I will insert 3.
02:16So,
02:17the link list will be something like this.
02:193,
02:202,
02:204,
02:202,
02:215.
02:22Now,
02:22what I have to do?
02:24I have to print the size of my link list.
02:27Let's do this.
02:28Save.
02:28I already know that I have size equal to 5.
02:34So,
02:34equal to 5,
02:35my size is printed.
02:36And my link list is printed.
02:381,
02:382,
02:383,
02:394,
02:395,
02:39ending with null.
02:41So,
02:41this way,
02:41the size property,
02:43we can use it in different cases.
02:46Or,
02:46when I know in my link list,
02:48how many elements are stored.
02:50So,
02:50I don't need to traverse it every time.
02:52Every time,
02:53I have a constant size.
02:54If I don't call the size parameter static type,
02:57I don't call it.
02:58Then,
02:58what do I have to do?
02:59Every time,
02:59I have to use a loop.
03:02This time,
03:03I don't have to use a loop.
03:04This time,
03:05constant time,
03:06my job got to print my size.
03:07Simply print it out.