Insertion in Doubly Linked List in C++
What is insertion in doubly linked in CPP programming?
Insertion in doubly linked in C++ in general words can be understood as insertion of a new node in a doubly linked list that is already created. We can perform three different types of insertion operations on singly linked list, these different types of insertion operations are:-
- Insertion at beginning of the doubly linked list.
- Inserting a new node at the specific position in doubly linked list.
- Insertion at end in doubly linked list.
Types of Insertion in Doubly Linked List
Insertion at Beginning
Insertion at specific position
Insertion at end
How to perform insertion at beginning in doubly linked list?
To perform insertion at beginning in doubly linked list we will use the following steps:-
- We will allocate space for the new node at the beginning of the list
- After that we will built a new node on the space created.
- Now we will insert data in the newly created node.
void insertNodeAtBeginning(int num) { struct node * newnode; if(stnode == NULL) { cout<<" No data found in the list\n"; } else { newnode = (struct node *)malloc(sizeof(struct node)); newnode->num = num; newnode->nextptr = stnode; newnode->preptr = NULL; stnode->preptr = newnode; stnode = newnode; } }
How to perform insertion at specific position in doubly linked list?
To perform insertion at specific position in doubly linked list we will use the following steps:-- We will traverse till the specific position in the list.
- We will insert a node here by changing linking the next pointer of the previous node to new node.
- Also we will linked the previous pointer of next node with the new node.
void insertSpecific(int num, int pos) { int i; struct node * newnode, *tmp; if(ennode == NULL) { cout<<" No data found in the list!\n"; } else { tmp = stnode; i=1; while(inextptr; i++; } if(tmp!=NULL) { newnode = (struct node *)malloc(sizeof(struct node)); newnode->num = num; newnode->nextptr = tmp->nextptr; newnode->preptr = tmp; if(tmp->nextptr != NULL) { tmp->nextptr->preptr = newnode; } tmp->nextptr = newnode; } else { cout<<" The position you entered, is invalid.\n"; } } }
How to perform insertion at end in singly linked list?
To perform insertion at end in singly linked list we will use the following steps:-
After creating a linked list with some data in it we will :-
- Use a pointer to traverse till the end of the list.
- After that we will create a new node after the last node.
- Insert the data that you want to enter by putting it in the node.
void insertNodeAtEnd(int num) { struct node * newnode; if(ennode == NULL) { cout<<" No data found in the list!\n"; } else { newnode = (struct node *)malloc(sizeof(struct node)); newnode->num = num; newnode->nextptr = NULL; newnode->preptr = ennode; ennode->nextptr = newnode; ennode = newnode; } }
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Doubly Linked List
- Introduction to Doubly Linked list in Data Structure
Click Here - Doubly Linked List in –
- Insertion in doubly linked list –
- Insertion at beginning in doubly linked list –
- Insertion at end in doubly linked list –
- Insertion at nth node in doubly linked list –
- Deletion in doubly linked list –
- Deletion from beginning in doubly linked list :
- Deletion from nth in doubly linked list :
- Deletion from end in doubly linked list :
- Insertion and Deletion in a doubly linked list :
- Insertion in the middle in a doubly linked list :
Doubly Linked List
- Introduction to Doubly Linked list in Data Structure
- Doubly Linked List in – C | C++ | Java
- Insertion in doubly linked list – C | C++ | Java
- Deletion in doubly linked list – C | C++ | Java
- Insertion and Deletion in doubly linked list – C | C++ | Java
- Insertion in the middle in a doubly linked list – C | C++ | Java
Login/Signup to comment