-
Notifications
You must be signed in to change notification settings - Fork 72
/
LinkedList.cpp
46 lines (40 loc) · 853 Bytes
/
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
#include<iostream>
using namespace std;
struct node {
int element;
node *next;
};
node *root = NULL;
void append(int elem) {
if(root==NULL){
root = new node();
root -> element = elem;
root -> next = NULL;
}
else{
node *current_node = root;
while(current_node->next != NULL){
current_node = current_node->next;
}
node *newnode = new node();
newnode -> element = elem;
newnode -> next = NULL;
current_node -> next = newnode;
}
}
void printNodes() {
node *current_node = root;
while(current_node != NULL){
printf("%d\n", current_node->element);
current_node = current_node->next;
}
}
int main() {
append(4);
append(1);
append(5);
append(2);
append(3);
printNodes();
return 0;
}