-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdoubly_linked_list.cpp
More file actions
95 lines (83 loc) · 1.41 KB
/
doubly_linked_list.cpp
File metadata and controls
95 lines (83 loc) · 1.41 KB
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
#include <iostream>
using namespace std;
class Node{
public:
int x;
Node* next;
Node* prev;
Node(){
next = NULL;
prev = NULL;
x = 0;
}
Node(int k){
x = k;
}
};
class doubly_linked_list{
public:
Node* head;
doubly_linked_list(){
head = NULL;
};
void insert_at_beginning(int k){
Node* sub = new Node(k);
if(head == NULL){
sub->prev = head;
head = sub;
sub->next = NULL;
return;
}
Node* temp = head;
sub->next = temp;
sub->prev = head;
head = sub;
temp->prev = sub;
}
void insert_at_end(int k){
Node* sub = new Node(k);
if(head == NULL){
sub->prev = head;
head = sub;
sub->next = NULL;
return;
}
Node* temp = head;
while(temp->next!=NULL){
temp = temp->next;
}
temp->next = sub;
sub->prev = temp;
sub->next = NULL;
}
int len(){
int c = 0;
Node* temp = head;
while(temp!=NULL){
c++;
temp = temp->next;
}
return c;
}
void print(){
Node* h = head;
while(h != NULL){
cout << h->x << endl;
h = h->next;
}
}
};
int main(){
doubly_linked_list h;
int n;
cout << "enter the number of elements of the list" << endl;
cin >> n;
for(int i=0;i<n;i++){
int k;
cout << "enter the number" << endl;
cin >> k;
h.insert_at_end(k);
}
h.print();
cout << "len of the array is: " << h.len() << endl;
}