Singly linked List
A singly linked list is the simplest type of linked list in which every node contains some data and a pointer to the next node of the same data type.The node contains a pointer to the next node we mean that the node stores the address of the next node in sequence.A singly linked list allows traversal of data only in one way.
Traversing a singly linked list
Traversing a linked list means accessing the nodes of the list in order to perform some operation on them.A linked list always contains a pointer variable STRAT which stores the address of the first node of the list.The end of the list is marked by storing Null in the next field of the last node.For traversing the linked list,We also make use of the another pointer variable PTR,which will point to the node that is currently being accessed.
Searching a linked list
Searching linked list means to find a particular element in the linked list.A linked list consists of a node which is divided into parts-the DATA part and the next part where Data stores the relevant information and next stores the address of the next node in sequence.searching means finding whether a given value is present in the information part of the node or not.There are two variants of the searching algorithm.while one algorithm is used to search for a value in a sorted linked list,the other works for an unsorted linked list.
Algorithm:
In step 1,we initialize a pointer variable PTR with START.in step 2 a while loop is executed which will compare every nodes's DATA with val for which the search is being made.If search is successful,that is the val has been found,then the address of the node is stored in pos and control jumps to the last statement of the algorithm.If search is unsuccessful,pos is set to null,which indicates that the val is not present in the linked list.
Comments
Post a Comment