Skip to content

Commit b240f36

Browse files
author
Mohit Jain
committed
Linked List
1 parent 5df139a commit b240f36

15 files changed

+566
-64
lines changed

DpIntro

Whitespace-only changes.

DpIntro.cpp

Whitespace-only changes.

LastIndex

33.3 KB
Binary file not shown.

LastIndex.cpp

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int lastIndex(int a[], int n, int m){
6+
7+
if (n == 0) return -1;
8+
9+
int lIndex = lastIndex(a+1,n-1,m);
10+
11+
if(lIndex == -1) {
12+
if(a[0] == m ) {
13+
return 0;
14+
}
15+
else return -1;
16+
}
17+
18+
return lIndex + 1;
19+
}
20+
21+
22+
int main() {
23+
24+
25+
#ifndef ONLINE_JUDGE
26+
// for getting input from input.txt
27+
freopen("input.txt", "r", stdin);
28+
// for writing output to output.txt
29+
freopen("output.txt", "w", stdout);
30+
#endif
31+
32+
int n;
33+
34+
cin>>n;
35+
36+
int a[n];
37+
38+
for(int i =0 ; i < n ; i++) {
39+
cin>>a[i];
40+
}
41+
42+
int m; cin >> m;
43+
cout<<lastIndex(a,n,m);
44+
45+
return 0;
46+
}

LinkedList

39.2 KB
Binary file not shown.

LinkedList.cpp

+283
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,283 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
class node{
6+
public:
7+
int data;
8+
node *next;
9+
10+
node(int d){
11+
data = d;
12+
next = NULL;
13+
}
14+
};
15+
16+
// Linked Class (OOP)
17+
18+
// class LinkedList {
19+
// node *head;
20+
// node *tail;
21+
22+
// public:
23+
// LinkedList(){
24+
25+
// }
26+
// void insert(int d)
27+
// }
28+
29+
void build() {
30+
31+
}
32+
33+
void insertAtHead(node*&head,int d) {
34+
if(head == NULL) {
35+
head = new node(d);
36+
return;
37+
}
38+
node *n = new node(d);
39+
n->next = head;
40+
head = n;
41+
42+
}
43+
int length(node*head){
44+
int cnt =0;
45+
while(head!= NULL) {
46+
// cout<<head->data<<"- >";
47+
head = head->next;
48+
cnt++;
49+
}
50+
return cnt;
51+
}
52+
void insertAtTail(node*&head,int data) {
53+
if(head == NULL){
54+
head = new node(data);
55+
return;
56+
}
57+
58+
node *tail = head;
59+
while(tail->next != NULL){
60+
tail = tail->next;
61+
}
62+
tail->next = new node(data);
63+
return;
64+
}
65+
void insertInMiddle(node*&head,int data,int p){
66+
if(head == NULL or p==0){
67+
insertAtHead(head,data);
68+
}
69+
else if(p>length(head)){
70+
insertAtTail(head,data);
71+
}
72+
else {
73+
int jump = 1;
74+
node *temp = head;
75+
76+
while(jump <= p-1) {
77+
temp = temp->next;
78+
jump++;
79+
}
80+
node * n = new node(data);
81+
n-> next = temp->next;
82+
temp->next = n;
83+
}
84+
}
85+
void print(node*head){
86+
while(head!= NULL) {
87+
cout<<head->data<<"- >";
88+
head = head->next;
89+
}
90+
}
91+
void deleteHead(node*&head){
92+
if(head == NULL){
93+
return;
94+
}
95+
node *temp = head->next;
96+
delete head;
97+
head = temp;
98+
99+
}
100+
bool search(node *head,int key){
101+
node*temp = head;
102+
103+
while(temp!= NULL){
104+
if(head->data == key) return true;
105+
106+
head = head-> next;
107+
}
108+
return false;
109+
}
110+
// recursively
111+
bool searchRecursive(node*head,int key) {
112+
if(head == NULL) return false;
113+
114+
if(head->data==key){
115+
return true;
116+
} else {
117+
return searchRecursive(head->next,key);
118+
}
119+
}
120+
121+
node* take_input_2(){
122+
int d;
123+
cin>>d;
124+
node*head = NULL;
125+
while(d!=-1){
126+
insertAtHead(head,d);
127+
cin>>d;
128+
}
129+
return head;
130+
}
131+
ostream& operator<<(ostream &os,node*head){
132+
print(head);
133+
return os;
134+
}
135+
136+
istream& operator>>(istream &is,node*&head){
137+
head = take_input_2();
138+
return is;
139+
}
140+
141+
void reverse(node*&head) {
142+
node*C = head;
143+
node* P = NULL;
144+
145+
node*N;
146+
147+
while(C!= NULL){
148+
149+
N = C->next;
150+
151+
C->next=P;
152+
153+
P = C;
154+
C = N;
155+
}
156+
head = P;
157+
}
158+
159+
// recursively reverse a linked list
160+
161+
node* recReverse(node*head){
162+
if(head->next == NULL or head==NULL){
163+
return head;
164+
}
165+
166+
node* shead = recReverse(head->next);
167+
168+
node *temp = head->next;
169+
head->next = NULL;
170+
temp->next = head;
171+
172+
return shead;
173+
174+
175+
176+
}
177+
node* midpoint(node*head){
178+
if(head==NULL or head->next == NULL){
179+
return head;
180+
}
181+
node*slow = head;
182+
node*fast = head-> next;
183+
184+
while(fast->next!=NULL and fast!=NULL) {
185+
fast = fast->next->next;
186+
slow = slow->next;
187+
}
188+
189+
return slow;
190+
}
191+
node* kthPositionFromEnd(node *head, int k){
192+
if(head ==NULL or head->next == NULL)
193+
return head;
194+
195+
node* slow = head;
196+
node* fast = head;
197+
while(k!=0){
198+
fast = fast->next;
199+
}
200+
while(fast!=NULL and fast->next!= NULL){
201+
slow = slow->next;
202+
fast = fast->next;
203+
}
204+
205+
return slow;
206+
207+
}
208+
node* merge(node*a,node*b){
209+
210+
if(a==NULL) return b;
211+
212+
if(b==NULL) return a;
213+
214+
node*c;
215+
if(a->data < b->data){
216+
c = a;
217+
c->next = merge(a->next,b);
218+
} else {
219+
c = b;
220+
c->next = merge(a,b->next);
221+
}
222+
return c;
223+
}
224+
// merge sort on linked list
225+
node* mergeSort(node *head) {
226+
if(head==NULL or head->next==NULL) return head;
227+
228+
node* mid = midpoint(head);
229+
node* b = mid->next;
230+
node* a = head;
231+
232+
mid->next = NULL;
233+
234+
a = mergeSort(a);
235+
b = mergeSort(b);
236+
237+
node* c = merge(a,b);
238+
239+
return c;
240+
241+
}
242+
// Cycle detection and removal (Floyd's cycle)
243+
bool detectCycle(node* head) {
244+
node*slow = head;
245+
node*fast = head;
246+
247+
while(fast!=NULL && fast->next!=NULL){
248+
fast = fast->next->next;
249+
slow = slow->next;
250+
if(fast==slow) return true;
251+
252+
}
253+
254+
return false;
255+
256+
}
257+
258+
259+
int main() {
260+
261+
#ifndef ONLINE_JUDGE
262+
// for getting input from input.txt
263+
freopen("input.txt", "r", stdin);
264+
// for writing output to output.txt
265+
freopen("output.txt", "w", stdout);
266+
#endif
267+
268+
node*head = take_input_2();
269+
node*head2 = take_input_2();
270+
node *head,*head2;
271+
cin>>head>>head2;
272+
cout<<head;
273+
cout<<head2;
274+
cout<<head<<head2;
275+
// insertAtHead(head,3);
276+
// insertAtHead(head,2);
277+
// insertAtHead(head,1);
278+
// insertAtHead(head,3);
279+
// insertInMiddle(head,5,2);
280+
281+
// print(head);
282+
283+
}

circularLinkedList.cpp

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
class node{
6+
public:
7+
int data;
8+
node *next;
9+
10+
node(int d){
11+
data = d;
12+
next = NULL;
13+
}
14+
};
15+
16+
void insert(node*&head,int data){
17+
18+
node* n = new node(data);
19+
20+
node*temp = head;
21+
22+
n->next = head;
23+
if(temp!= NULL){
24+
25+
while(temp->next!= head){
26+
27+
temp = temp->next;
28+
29+
}
30+
temp->next = n;
31+
32+
33+
}else {
34+
n->next = n;
35+
}
36+
head = n;
37+
38+
}
39+
void print(node*head){
40+
node*teamp = head;
41+
while(temp->next!= head){
42+
cout<<temp->data<<" ";
43+
temp = temp->next;
44+
}
45+
cout<<temp->data<<endl;
46+
return;
47+
}
48+
// deletion in circular Linked list
49+
int main() {
50+
51+
52+
53+
return 0;
54+
}
Binary file not shown.

input.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
4
1+
7
2+
86 -16 77 65 45 77 28
3+
77

nQueenBitmask

-5.03 KB
Binary file not shown.

0 commit comments

Comments
 (0)