-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdouble_linkedliist.cpp
124 lines (118 loc) · 1.98 KB
/
double_linkedliist.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include<iostream>
using namespace std;
class Node{
public:
Node* prev;
int data;
Node* next;
Node(int data){
this->prev=NULL;
this->data=data;
this->next=NULL;
}
};
class DLL{
public:
Node* head;
DLL(){
head=NULL;
}
bool isEmpty(){
return(head==NULL);
}
void addBeg(int ele){
Node* ptr=new Node(ele);
if(!isEmpty()){
head->prev=ptr;
ptr->next=head;
}
head=ptr;
}
void addEnd(int ele){
if(isEmpty()){
addBeg(ele);
return;
}
Node* ptr=new Node(ele);
Node* temp=head;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=ptr;
ptr->prev=temp;
}
void display(){
if(!isEmpty()){
Node* temp=head;
while(temp!=NULL){
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}
}
void delBeg(){
if(!isEmpty()){
Node* ptr=head;
if(head->next!=NULL){
head=head->next;
head->prev=NULL;
delete(ptr);
}else{
delete(ptr);
head=NULL;
}
}
}
void delEnd(){
if(!isEmpty()){
Node* ptr=head;
Node* temp=head->next;
if(temp==NULL){
delBeg();
return;
}
while(temp->next!=NULL){
temp=temp->next;
ptr=ptr->next;
}
ptr->next=NULL;
temp->prev=NULL;
delete(temp);
}
}
void displayreverse(){
Node* ptr=head;
while(ptr->next!=NULL){
ptr=ptr->next;
}
while(ptr!=NULL){
cout<<ptr->data<<" ";
ptr=ptr->prev;
}
}
void delElement(int ele){
if(isEmpty()){
return;
}
Node* ptr=head;
if(head->data==ele){
delBeg();
}else{
while(ptr!=NULL){
if((ptr->data)==ele){
Node* temp=ptr->prev;
temp->next=ptr->next;
if(ptr->next!=NULL){
ptr->next->prev=temp;
}
return;
}
ptr=ptr->next;
}
}
}
};
int main(void){
//menu driven is to be done..rest is done
}