This document provides algorithms for creating, inserting, deleting, and traversing nodes in a one-way linked list. The creation algorithm uses a while loop to add nodes to the end of the list. The insertion algorithm adds a new node at a given location by linking it between the preceding and following nodes. The deletion algorithm removes a node from a given location by relinking the preceding node to the node after the one to delete. The traversal algorithm uses a while loop to output the information from each node by traversing from the first node to the last using the link pointers.
This document provides algorithms for creating, inserting, deleting, and traversing nodes in a one-way linked list. The creation algorithm uses a while loop to add nodes to the end of the list. The insertion algorithm adds a new node at a given location by linking it between the preceding and following nodes. The deletion algorithm removes a node from a given location by relinking the preceding node to the node after the one to delete. The traversal algorithm uses a while loop to output the information from each node by traversing from the first node to the last using the link pointers.
This document provides algorithms for creating, inserting, deleting, and traversing nodes in a one-way linked list. The creation algorithm uses a while loop to add nodes to the end of the list. The insertion algorithm adds a new node at a given location by linking it between the preceding and following nodes. The deletion algorithm removes a node from a given location by relinking the preceding node to the node after the one to delete. The traversal algorithm uses a while loop to output the information from each node by traversing from the first node to the last using the link pointers.
first -> info =item; first ->link =NULL; p = first; // accessing first node again = ‘y’; while (again == ‘y’) //creation of other nodes { new -> info = item; new -> link = NULL; p -> link = new; p = p -> link; cout<<”to create another node press ‘y’”; cin>>again; }
Insertion Algorithm for One-way linked list
This algorithm inserts a new node at a given location ‘Loc’. i.e. if Loc=1 then the new node will become first node, if Loc=2 then the new node will become 2nd node and so on. new -> info=item; cin>>Loc; if (Loc==1) { new -> link = first; first = new; } else { new -> link = Loc -> link; Loc -> link = new; }
Deletion Algorithm for One-way linked list
This algorithm deletes a node from a given location ‘Loc’. i.e. if Loc=1 then the first node will be deleted, if Loc=2 then the 2nd node will be deleted and so on. cin>>Loc; if (Loc==1) first = first ->link; else Locp -> link = Loc -> link; delete Loc; where ‘Locp’ is the preceding node of ‘Loc’.
Traversing Algorithm for One-way linked list
p = first; while (p != NULL) { cout<<p -> info<<endl; p = p -> link; }