forked from WaderManasi/Knowing-DataStructures-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
insert at given pos in doublyy linked list
- Loading branch information
1 parent
7ea32ae
commit 096f098
Showing
1 changed file
with
23 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
void addNode(Node *head, int pos, int data) | ||
{ | ||
Node* newn= new Node(data); | ||
if(head==NULL) head=newn; | ||
else | ||
{ | ||
Node* temp=head; | ||
for(int i=0;i<pos;i++) | ||
temp=temp->next; | ||
if(temp->next==NULL) | ||
{ | ||
temp->next=newn; | ||
newn->prev=temp; | ||
} | ||
else | ||
{ | ||
temp->next->prev=newn; | ||
newn->next=temp->next; | ||
newn->prev=temp; | ||
temp->next=newn; | ||
} | ||
} | ||
} |