forked from sauravgpt2/hacktoberfest36_2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsertion_in_DoubleLinkedList.cpp
73 lines (73 loc) · 1.47 KB
/
Insertion_in_DoubleLinkedList.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// INSERTION IN BETWEEN
// INPUT THE FIRST VARIABLE WITH NUM TO BE INSERTED
// AND THE SECOND INPUT TO SPECIFY AFTER WHICH NUM
// THE INPUT NUM IS TO BE INSERTED
#include <iostream>
using namespace std;
struct node{
int data;
node *ptr,*pre;
};
node *head=NULL;
void insertathead(int a){
node *p=new node;
p->data=a;
p->pre=p->ptr=NULL;
if(head==NULL){
head=p;
}
else{
head->pre=p;
p->ptr=head;
head=p;
}
}
void insertioninbetween(int a,int b){
node *p=new node;
p->data=a;
p->pre=p->ptr=NULL;
if(head==NULL)
p=head;
else {
node *temp=head;
while(temp && temp->data!=b){
temp=temp->ptr;
}
if(temp==NULL){
cout<<"ELEMENT NOT FOUND\n";
temp=p=NULL;
}
else if(temp->ptr==NULL){
p->pre=temp;
temp->ptr=p;
}
else{
p->pre=temp;
p->ptr=temp->ptr;
temp->ptr->pre=p;
temp->ptr=p;
}
}
}
void displayfromhead(){
node *p=head;
while(p!=NULL){
cout<<p->data<<endl;
p=p->ptr;
}
}
int main()
{
insertathead(50);
insertathead(40);
insertathead(30);
insertathead(20);
insertathead(10);
cout<<"BEFORE INSERTION\n";
displayfromhead();
insertioninbetween(500,30);
insertioninbetween(100,20);
cout<<"AFTER INSERTION\n";
displayfromhead();
return 0;
}