Repository navigation
Expand file tree
/
Copy pathdouble_linked_list.cpp
More file actions
94 lines (76 loc) · 1.81 KB
/
Copy pathdouble_linked_list.cpp
File metadata and controls
94 lines (76 loc) · 1.81 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
#include <iostream>
using namespace std;
class Node{
public:
int data;
Node* next;
Node* previous;
};
void Print_Forward(Node* head){
Node* temp = head;
while(temp != NULL){
cout << temp->data << " ";
temp = temp->next;
}
}
void Print_Backward(Node* tail){
Node* temp = tail;
while(temp != NULL){
cout << temp->data << " ";
temp = temp->previous;
}
}
void InsertAtHead(Node** head, int data){
// create a new node
Node* new_node = new Node();
new_node->data = data;
new_node->next = *head;
new_node->previous = NULL;
//update the tail to point to the new node
(*head)->previous = new_node;
// update the head to point to the new node
*head = new_node;
}
void insertAtTail(Node** tail, int data){
// create a new node
Node* new_node = new Node();
new_node->data = data;
new_node->next = NULL;
new_node->previous = *tail;
//update the tail to point to the new node
(*tail)->next = new_node;
// update the tail to point to the new node
*tail = new_node;
}
int main(){
Node* head;
Node* tail;
Node* first = new Node();
Node* second = new Node();
Node* third = new Node();
Node* fourth = new Node();
first->data = 1;
first->next = second;
first->previous = NULL;
second->data = 2;
second->next = third;
second->previous = first;
third->data = 3;
third->next = fourth;
third->previous = second;
fourth->data = 4;
fourth->next = NULL;
fourth->previous = third;
head = first;
tail = fourth;
InsertAtHead(&head, -10);
Print_Forward(head);
cout << endl;
insertAtTail(&tail, 10);
Print_Backward(tail);
delete first;
delete second;
delete third;
delete fourth;
return 0;
}