Single Linked List Operations | Data Structures Tutorial | Mr. Srinivas

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome to nourish technologies this is cinemas in this session so we are going to discuss all the operations we can perform on single link at least in the last session we discussed it clearly so what is the node structure of a single linkedlist and how the memory we are allocating right to a particular node using malloc function or ml log function in a C language right C so first here what are the operations we can perform on single linkedlist single linkedlist operations operations first one append the element so nothing but insert are more clearly adding an element at the end of the list added and is the first one second one add at begin added begin suppose after five elements insertion right in the beginning of the list add first if you want to insert any element so that is also next one add it after a particular node actually nothing but in the middle right how to insert add at the end added the begin in the middle nothing but after a specified node how to insert the elements next one how to delete first node and how to delete a specified node how to delete a specified node next how to display elements how to find length next how to reverse the list and next one how to swap elements in the list swap two nodes or two elements in the list next how to sort the elements in the list how to sort the elements so all these operations we can perform on single linkedlist we will see one by one okay how can we perform all these operations right for example so in a new technologies like in Java right in a collections framework all these functions available directly predefined implementations are there so which are called a collection framework classes right but in C language so we have to write the logic manually I will explain all these things using a C language syntax and if you are good at a core Java completely logical programming and then you can try exactly how to develop how to implement the logic right manually instead of depending on predefined implemented classes right how can we write the logic that you can try later right so let me explain all these things right first one how to append the element to list okay see how to append elements to linkedlist right so function so we are writing append append and list also we are writing list how to declare root variable means first node type is a struct node struct node in a single linkedlist here it is int first one is a data second one link type is a struct node type struct node type here it is a node structure contains a two elements nothing but the two fields it will create first one is a data and next one is a Link field so first we are creating one variable of course a global variable just consider struct node star root then root gets memory allocation root gets memory allocation at some location at some location node gets memory allocation root now so as we are checking so what is the initial value and of course global variables are default value null only more clearly if you want to assign you can assign value is a null root value initially null that that is nothing but so what that is representing means right the linkedlist is not having elements at so now we are inserting the elements how to insert so first in the FN we are creating one temporary variable the temporary variable is also struct node star temper it is also user-defined data type variable only that is nothing but node structure type variable only we are declaring node structure type variable and next one we need to allocate the memory dynamically here it is Emma log and we are passing sizeof struct node struct node we are passing so we need to pass the size of the structure so what Emma log function will do means it will allocate the memory see how it will allocate the memory here it is dynamically dynamically a block memory will be allocated a block memory will be allocated at some location and before that remember this temporary will also get memory allocation local variable temp variable get memory allocation and root variable is a global variable observe here it is a root already created now local variable is a temporary variable so temp gets memory allocation next using malloc function we are allocating the memory to structure that but node type structure memory allocation here so it allocates the block the address is a two zero four six addresses two zero four six that we are collecting into local variable temp variable first and of course here here typecasting compulsory struct node star type struct node star type conversion so after typecasting we are collecting into temper so here it is two zero four six we are collecting into temp so it start pointing to this node so as of now node is ready node memory allocation is ready and next what we have to do we have to read the information how to read we are asking printf printf enter node data so what data you want to store inside the node that we are collecting enter node data sir how to scan sir percentage D because in teaser data now observe very clearly temp so node is having two fields so what is the data field and what is the link field that you cannot decide but address 2 0 4 6 next 1 2 0 4 8 consider 4 scanf function to read the information we have to provide the base address very very important C temp to data address once again very clearly whenever a pointer is pointing to the structure we need to use arrow operator to read the elements nothing but to process the elements so here it is a temporary variable is a pointer variable pointing to structure so we need to provide this address to Stan a function so temp 2 data location address so here temp 2 temp 2 data address this is reading information just consider if they give 10 the 10 will be collected into this location then next here I want to place null null value I want to place so here null value we are collecting into m2 link field into temp - link field directly we are storing the value null temp - link field value is a null now node is ready completely only node memory allocation is ready and what are the values what is the information we need to place inside the node is ready now we need to check where we have to connect this node right either at the starting position or at the end of the available linkedlist but as of now we are creating the first node in the linkedlist we are creating the first node in the linked list so we have to check with the root element for example if root element is not pointing to any other element the newly created element should be the root element see here we are checking the condition if root element is equals to null so what it is representing the root is not pointing to any node that means so whatever the node you have created newly that is comes under newly created element and that should be the first element so in this situation we are passing we are assigning the temp value to root we are assigning the temp value to root so what is the temporal you observe here clearly M value is a 2 0 4 6 and the root value temp value we are storing into root the root is already null now that we are replacing with a temp so it start pointing to the first node sir then what about a temporary available it is a local variable you declared locally inside append function so whenever append function execution has been completed then automatically that will be deleted right local variables will be deleted along with the frames destruction sir what is the frame destruction we know that methods and blocks execute in the stack memory in any programming language once the frame is going to destroy along with all the local variables will be deleted so this is only first case sir water water remaining cases what about a remaining cases else block else block logic in the else block remaining logic we have to write so remaining logic I will explain clearly okay see for example if you are inserting the first element right how to insert the element directly temp variable we are assigning to root is happy next suppose some of the elements are already present so then else block will execute how it executes si si for example already some elements are present in the list this is the first element this is a second element third element fourth element four elements are there for T it's null how to store all these things I will explain so here it is root element is pointing to root element is pointing to the first location first node and some of the nodes already connected now we are inserting one more element already node is ready right for example using a malloc function first we are declaring creating one temporary variable temporary variable gets memory allocation temporary variable next using a malach function we are allocating the memory to node suppose this is the node node for example at some location is a two zero one two two zero one two first the address we are collecting into temp variable this is temporary connection MP is pointing to this one next we are reading the elements using printf and scanf already we discussed just now all right how to read the elements right we are reading the element to temp to data address suppose value is of 50 and next m2 link will be null temp - link will be null reading is over but now final part is a connection part how to connect element at the end of the list okay now if block will be terminated if if root equals to null if root equals to null what we have written say temple you will be assigned to root or else now here it is we have to write the logic now in the previous case suppose if you are creating the first node in the linked list by the time route will be null only but now root is pointing to some other node is pointing to some location so what is that location 1 0 2 4 is the location right it is pointing to 1 0 to 4 location so that root not equals to null if condition has failed so that we have to connect this how how to connect first you have to travel from first location to last location first location to last location because newly connected element you have to insert at the end you have to insert at the end sir how can we connect observe here we are declaring one more temporary variable here it is struct struct node struct node pointer for example P P gets memory allocation at some location it is also pointer type only initially into P we are storing the value root value root value here it is root value is a 1 0 2 4 so 1 0 2 4 will be stored so temporarily it is pointing to this 1 by temporarily because it is a local variable temporary variable whenever a function execution has been completed along with the temporary variable P variable memory also will be deleted as it was a temporary variable nothing but a local variable now we need to send the control from first location to last location last location how to sensor very simple with the help of one while loop while p2 link not equals to null p2 link not equals to null just store the p2 link value into P why because we need to send the control from first location to last location so how can you decide that you and you means what the control sent to the last location means last location link field is a null field because no other connection no other node connection from the last node so that you should keep in keep it in mind because based on null pointer only we should travel from first location to last location first to p2 link we have to check what is the p-value is a 1 0 2 4 1 0 2 4 2 link this is data field and this is the link field suppose if this link field is a null field means what it is the last node because no other connection but now it is not the null field it is - 0 9 - so nothing but some other node it has already connected that you should understand so what is the value 2 0 9 - so not equals to null condition true come inside that p2 link value will be stored into P so what is that p2 link value p2 link value is a - 0 9 - that will be stored into P then it stop pointing to first node that P is not pointing to second node second node next while loop as long as the condition is a true it will repeat so what is the second node again p2 link not equals to null - 0 9 - 2 link seven zero five eight not equals to null so that seven zero five eight p2 link value will be stored into P it is not pointing to the second node and it is pointing to the third node seven zero five eight - link not equals to null yes of course 9:05 it so that value will be stored it stop pointing and it is start pointing to the last node now 9:05 - to link 9:05 - to link is a null null value not equals to null value condition false then why loop will be terminated at the end condition false because P the temporary variable is already pointing to the last location where we have to connect the newly created node so what is the newly created node is it 2:01 - is already in a temporary variable so connect now temp variable newly created node right here it is we are assigning to p2p is pointing to the last node ptoo link location so temp value to 0 1 2 will be stored here then it pointing to this one the 2 0 1 2 will be stored in two we do link this is connection ok first node connection is very easy but if you want to connect the remaining nodes first we have to travel from first location to the last location so how to travel from first location to last location so for that so we need to declare one local variable another temporary variable by which we can travel so once we reach the last location so then we have to connect the newly created node at the end of the list so this is the insertion part how to insert an element nothing but how to append an element in a linkedlist some more features of a linkedlist and so remaining operations on linkedlist will continue in the next session for more videos please subscribe to nourish 80 channel thank you
Info
Channel: Naresh i Technologies
Views: 684,579
Rating: 4.8857293 out of 5
Keywords: Data Structures, Srinivas, Naresh IT, Data Structures Tutorial Videos, Data Structures Overview, Single, Single Linked, Singly Linked List, Singly Linked List Operations, Singly Linked List Operations in Data Structure, What is a linked list, Doubly Linked List, linked list with an example C program, Data Structures Course, singly linked list in ds, linked list in c, linked list operations in data structure, linked list in data structure using c
Id: o1QaGUEi6ew
Channel Id: undefined
Length: 20min 33sec (1233 seconds)
Published: Mon Sep 26 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.