-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_lists.cpp
More file actions
60 lines (41 loc) · 957 Bytes
/
linked_lists.cpp
File metadata and controls
60 lines (41 loc) · 957 Bytes
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
#include<bits/stdc++.h>
class Node {
public:
int value;
Node* next;
Node(int n){
this -> value = n;
this -> next = nullptr;
}
};
void insertFront(Node** head, int value){
Node* newNode = new Node(value);
newNode -> next = *head;
*head = newNode;
}
void insertBack(Node** head, int value){
Node* newNode = new Node(value);
Node* cur = *head;
while(cur -> next != nullptr){
cur = cur -> next;
}
cur -> next = newNode;
}
using namespace std;
int main(){
Node* head = nullptr;
Node* second = nullptr;
Node* third = nullptr;
head = new Node(1);
second = new Node(2);
third = new Node(3);
head -> next = second;
second -> next = third;
insertBack(&head, 50);
Node* cur = head;
while(cur!= nullptr){
cout << cur -> value << " ";
cur = cur -> next;
}
return 0;
}