Insert linked list C++
C program to insert an element in a singly linked listPosted on 09th October 2017 A linked list is linear data structure which is made up of a set of nodes. In addition to the data, each node also contains a pointer to the next node in the list. If the next node pointer is NULL it means that node is the last node in the list. A linked list also uses a local variable called the head thats points to the top of the list. If the head pointer points to NULL then it means the list is empty. In this article we will discuss how to insert an element(node) in a linked with the help of a C program. Show There are three possibilities to insert/add an element to a linked list. Here are those three possiblities and the steps involved: Inserting a node to the top of a listThis is probably the easiest and fastest method of adding an element to a linked list. Here are the steps.
Inserting a node at the end of a listTo insert an element at the bottom of a list, you need to traverse up to the last element of the list and then,
Inserting before or after a nodeTo insert an element before or after a particular node you need to traverse up to that node(to insert after) or up to the previous node(to insert before)and then,
Here is a C Program to perform the following operations on a singly linked list.
This program also displays a menu for the users to make a selection. Here is the full source code. Source Code/******************************************** * C program to insert an element at any * * position in a linked list * ********************************************/ #includeSample Output************************************ * Linked list operations: * * 1. Insert at the top of list * * 2. Insert at bottom of list * * 3. Insert after an element * * 4. Insert before an element * * 5. Show all elements * * 6. Quit * ************************************ Choose an option [1-5] : 4 Enter a number to insert : 4 Before which number do you want to insert : 5 Number 4 is now inserted before 5 in the list Press any key to continue...
|