-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list.cpp
More file actions
178 lines (159 loc) · 5.1 KB
/
Copy pathlinked_list.cpp
File metadata and controls
178 lines (159 loc) · 5.1 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <bits/stdc++.h>
using namespace std;
#define rep(i, l, r) for (int i = l; i < r; i++)
#define rrep(i, r, l) for (int i = r-1; i >=0; i--)
#define ll long long
#define nline '\n'
#define prn cout << "NO" << nline
#define pry cout << "YES" << nline
#define vi vector<int>
#define vvi vector<vi>
#define eb emplace_back
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define inparr(v) \
for (auto &x : v) \
cin >> x;
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vpii vector<pii>
#define vvpii vector<vector<pii>>
#define mp make_pair
#define ff first
#define ss second
#define float long double
// #define int long long
//---- Debugger ---- //
#ifdef LOCAL
#define debarr(a,n) cout<<#a<<" : ";for(int i=0;i<n;i++) cerr<<a[i]<<" "; cerr<<nline;
#define debmat(mat,row,col) cout<<#mat<<" :\n";for(int i=0;i<row;i++) {for(int j=0;j<col;j++) cerr<<mat[i][j]<<" ";cerr<<nline;}
#define pr(...) dbs(#__VA_ARGS__, __VA_ARGS__)
template <class S, class T>ostream& operator <<(ostream& os, const pair<S, T>& p) {return os << "(" << p.first << ", " << p.second << ")";}
template <class T>ostream& operator <<(ostream& os, const vector<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T>ostream& operator <<(ostream& os, const unordered_set<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class S, class T>ostream& operator <<(ostream& os, const unordered_map<S, T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T>ostream& operator <<(ostream& os, const set<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T>ostream& operator <<(ostream& os, const multiset<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class S, class T>ostream& operator <<(ostream& os, const map<S, T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T> void dbs(string str, T t) {cerr << str << " : " << t << "\n";}
template <class T, class... S> void dbs(string str, T t, S... s) {int idx = str.find(','); cerr << str.substr(0, idx) << " : " << t << ","; dbs(str.substr(idx + 1), s...);}
template <class T> void prc(T a, T b) {cerr << "["; for (T i = a; i != b; ++i) {if (i != a) cerr << ", "; cerr << *i;} cerr << "]\n";}
#else
#define pr(...)
#endif
//----------------- //
const int MOD = 1e9 + 7;
const int INF = 1e9 + 1;
class Node {
public:
int data;
Node* next;
Node(int val) {
data = val;
next = nullptr;
}
};
class MyLinkedList {
private:
Node* head;
Node* tail;
public:
MyLinkedList() {
head = tail = nullptr;
}
int get(int index) {
int ans = -1;
Node* curr = head;
while(index-- && curr != nullptr) {
curr = curr->next;
}
if(curr != nullptr) ans = curr->data;
return ans;
}
void addAtHead(int val) {
Node* newNode = new Node(val);
if(head == nullptr) {
head = tail = newNode;
return;
}
newNode->next = head;
head = newNode;
}
void addAtTail(int val) {
Node* newNode = new Node(val);
if(head == nullptr) {
head = tail = newNode;
return;
}
tail->next = newNode;
newNode = tail;
}
// 1->2
void addAtIndex(int index, int val) {
Node* curr = head;
Node* prev = head;
Node* newNode = new Node(val);
while(index-- && curr != nullptr) {
prev = curr;
curr = curr->next;
}
if(index == 0) {
if(head == nullptr) head = newNode;
else if(curr == head) {
newNode->next = head;
head = newNode;
} else if(curr == nullptr) {
prev->next = newNode;
tail = newNode;
} else {
prev->next = newNode;
newNode->next = curr;
}
}
}
/*
The 5 Golden Rules of Raw Pointers
1. The "Empty Railyard" Rule (head == nullptr)
2. The Conductor Rule (Head Check)
3. The Caboose Rule (Tail Check)
4. The "Lone Car" Rule (head == tail)
5. The Connection Rule (New to Old, Old to New)
*/
void deleteAtIndex(int index) {
Node* curr = head;
Node* prev = head;
while(index-- && curr != nullptr) {
prev = curr;
curr = curr->next;
}
if(index == 0) {
if(head == nullptr) return;
if(curr == head) {
}
}
}
};
/**
* Your MyLinkedList object will be instantiated and called as such:
* MyLinkedList* obj = new MyLinkedList();
* int param_1 = obj->get(index);
* obj->addAtHead(val);
* obj->addAtTail(val);
* obj->addAtIndex(index,val);
* obj->deleteAtIndex(index);
*/
void solve() {
// LinkedList list;
// list.push_back(10);
// list.push_front(20);
// list.printLinkedList();
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while (t--)
solve();
}