![]() We can use the following steps to insert a new node at beginning of the single linked list. Inserting At Specific location in the list.In a single linked list, the insertion operation can be performed in three ways. Step 5 - Implement the main method by displaying operations menu and make suitable function calls in the main method to perform user selected operation.Step 4 - Define a Node pointer 'head' and set it to NULL.Step 3 - Define a Node structure with two members data and next.Step 2 - Declare all the user defined functions.Step 1 - Include all the header files which are used in the program.First, perform the following steps before implementing actual operations. The following operations are performed on a Single Linked Listīefore we implement actual operations, first we need to set up an empty list. So we will make our tail pointer equal to a new node.In a single linked list, the address of the first node is always stored in a reference node known as "front" (Some times it is also known as "head").Īlways next part (reference part) of the last node must be NULL. The tail pointer should always point to the last node.Means passing the address of a new node to the next pointer of a tail node. Linking the newly created node with tail node.The creation of a new node at the end of linked list has 2 steps: So we are going to create this newly created node next to a tail node. We know that the last node is called a tail. I have also told you before that if there is just one node (which we are going to create) in linked lists, then it is called both head and tail.Īnd if a linked list is created already, then we would insert this node at the end of the linked list. Do you remember that the head points to the first node? It means if the head is equal to NULL then we can conclude that the linked list is empty. Now, the function will have a very special case that we want to know what would happen if the linked list is still empty? We will have to check it. The next field of node would be declared as NULL as it would be the last node of linked list. We need a pointer of a node type (which we defined) and we will insert the value in its data field. ![]() The process of creating node is very simple. Now, we will write a function for the node creation. The constructer will make them NULL to avoid any garbage value. This class should have two important pointers, i.e. Now, we need a class which will contain the functions to handle the nodes. ![]() In C++, our code would look like this: struct node Our structure should have at least one variable for data section and a pointer for the next node. ![]() If there is just one node created then it is called both head and tail.Īs linked list consists of nodes, we need to declare a structure which defines a single node. No matter how many nodes are present in the linked list, the very first node is called head and the last node is called the tail. So this is the place where the link between nodes is established. The next field contains the address of the next node. There may be many variables presenting the data section of a node. The data field contains the data being stored in that specific node. The elements of a linked list are called the nodes. On the other hand, every node in a doubly-linked list contains some data, a link to the next node and a link to the previous node. In a singly-linked list, every element contains some data and a link to the next element. There are two types of linked list singly-linked list, and doubly-linked list. These items are connected using pointers in a sequential manner. A linked list is a data structure that can store an indefinite amount of items.
0 Comments
Leave a Reply. |