-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.cpp
65 lines (59 loc) · 1.31 KB
/
LinkedList.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
#include<bits/stdc++.h>
using namespace std;
struct node {
int val;
node * next;
};
node * root = NULL;
void append(int val) {
//if the linked list is empty then creat the first node//
if (root == NULL) {
root = new node();
root -> val = val;
root -> next = NULL;
}
//if the linked list already had element//
else {
node * current_node = root;
while (current_node -> next != NULL) {
current_node = current_node -> next;
}
node * newNode = new node();
newNode -> val = val;
newNode -> next = NULL;
current_node -> next = newNode;
}
}
void delet(int n) {
node * currentNode = root;
//if the deleted element in the first node, then change the address of the root node//
if (currentNode -> val == n) {
root = currentNode -> next;
}
else {
while (currentNode -> next != NULL) {
if (currentNode -> next -> val == n) {
currentNode -> next = currentNode -> next -> next;
}
else {
currentNode = currentNode -> next;
}
}
}
}
void print() {
node * currentNode = root;
while (currentNode -> next != NULL) {
cout << currentNode -> val;
currentNode = currentNode -> next;
}
cout << currentNode -> val;
}
int main() {
for (int i = 0; i <= 3; i++) {
append(i);
}
delet(3);
print();
return 0;
}