-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.cpp
More file actions
151 lines (113 loc) · 3.19 KB
/
Copy pathNode.cpp
File metadata and controls
151 lines (113 loc) · 3.19 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <stdio.h>
#include <stdlib.h>
typedef struct Node_ Node;
Node* Create(int data_to_fill);
int Print(Node AnyNode);
int Delete(Node* AddNode);
int Link(Node* next, Node* prev, Node* AddNode);
int Swap(Node* AddNode1, Node* AddNode2);
Node* Search(Node* AddNode, Node* AnyAddNode);
int Idx(Node* AddNode, Node* AnyAddNode);
struct Node_ {
unsigned long long data;
Node* next;
Node* prev;
};
/*int main() {
Node New_Node1 = *(Create(1));
Node New_Node2 = *(Create(2));
Node New_Node3 = *(Create(3));
//Link(&New_Node3, &New_Node1, &New_Node2);
//Delete(&New_Node2);
//PrintNode(New_Node2);
//Swap(&New_Node2, &New_Node3);
//printf("%d", ((New_Node1.next) -> data));
return 0;
}*/
int Print(Node AnyNode) {
printf("%llu\n", AnyNode.data);
return 0;
}
Node* Create(unsigned long long data_to_fill) {
Node* New_node = (Node*) calloc (1, sizeof(Node));
New_node -> data = data_to_fill;
New_node -> next = NULL;
New_node -> prev = NULL;
return New_node;
}
int Link(Node* next, Node* prev, Node* AddNode) {
if (AddNode == NULL){
printf("Node doesn't exist. Code 1");
return 1;
}
AddNode -> next = next;
AddNode -> prev = prev;
if (next != NULL)
next -> prev = AddNode;
if(prev !=NULL)
prev -> next = AddNode;
return 0;
}
int Delete(Node* AddNode) {
if (AddNode == NULL){
printf("Node doesn't exist. Code 1");
return 1;
}
AddNode -> next -> prev = AddNode -> prev;
AddNode -> prev -> next = AddNode -> next;
AddNode -> prev = NULL;
AddNode -> next = NULL;
free(AddNode);
return 0;
}
int Swap(Node* AddNode1, Node* AddNode2) {
if ((AddNode1 == NULL) || (AddNode2 == NULL)){
printf("Node doesn't exist. Code 1");
return 1;
}
if ((AddNode1 -> prev -> next) != NULL)
(AddNode1 -> prev -> next) = AddNode2;
(AddNode1 -> next) = (AddNode2 -> next);
(AddNode2 -> next) = AddNode1;
if((AddNode2 -> next -> prev) != NULL)
(AddNode2 -> next -> prev) = AddNode2;
(AddNode2 -> prev) = (AddNode1 -> prev);
(AddNode1 -> prev) = AddNode2;
return 0;
}
Node* Search(Node* AddNode, Node* AnyAddNode) {
if ((AddNode == NULL) || (AnyAddNode == NULL)){
printf("Node doesn't exist. Code 1");
//return 1;
}
Node* buf = AnyAddNode;
while((buf -> prev) != NULL)
buf = buf -> prev;
while((buf -> data) != (AddNode -> data)){
buf = buf -> next;
if (buf == NULL) {
printf ("Node doesn't find");
break;
}
}
return buf;
}
int Idx(Node* AddNode, Node* AnyAddNode) {
if ((AddNode == NULL) || (AnyAddNode == NULL)){
printf("Node doesn't exist. Code 1");
return 1;
}
Node* buf = AnyAddNode;
int idx = 0;
while((buf -> prev) != NULL)
buf = buf -> prev;
while((buf -> data) != (AddNode -> data)){
buf = buf -> next;
++idx;
if (buf == NULL) {
printf ("Node doesn't find");
break;
}
}
return idx;
}