Skip to content

Commit c5e1f24

Browse files
Mohit JainMohit Jain
Mohit Jain
authored and
Mohit Jain
committed
stacks and deques
1 parent fac0044 commit c5e1f24

14 files changed

+312
-20
lines changed

Diff for: LinkedList.cpp

+44-13
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ node* take_input_2(){
128128
}
129129
return head;
130130
}
131+
132+
131133
ostream& operator<<(ostream &os,node*head){
132134
print(head);
133135
return os;
@@ -170,9 +172,6 @@ node* recReverse(node*head){
170172
temp->next = head;
171173

172174
return shead;
173-
174-
175-
176175
}
177176
node* midpoint(node*head){
178177
if(head==NULL or head->next == NULL){
@@ -205,6 +204,7 @@ node* recReverse(node*head){
205204
return slow;
206205

207206
}
207+
208208
node* merge(node*a,node*b){
209209

210210
if(a==NULL) return b;
@@ -239,22 +239,51 @@ node* mergeSort(node *head) {
239239
return c;
240240

241241
}
242-
// Cycle detection and removal (Floyd's cycle)
243-
bool detectCycle(node* head) {
242+
// Cycle detection and removal (Floyd's cycle) It should complete
243+
bool detectCycle(node* &head) {
244244
node*slow = head;
245245
node*fast = head;
246246

247247
while(fast!=NULL && fast->next!=NULL){
248248
fast = fast->next->next;
249249
slow = slow->next;
250-
if(fast==slow) return true;
250+
if(fast==slow) {
251+
252+
removeCycle(slow,head);
253+
254+
return true; }
251255

252256
}
253257

254258
return false;
255259

256260
}
257261

262+
void removeCycle(node* slow,node* &head ) {
263+
264+
node* ptr1 = slow;
265+
266+
node* ptr2 = head;
267+
268+
node* prev = ptr1;
269+
270+
while(ptr2!= ptr1){
271+
272+
prev = ptr1;
273+
ptr1 = ptr1->next;
274+
ptr2 = ptr2->next;
275+
276+
277+
278+
279+
280+
}
281+
282+
prev->next = NULL;
283+
284+
285+
}
286+
258287

259288
int main() {
260289

@@ -265,13 +294,15 @@ int main() {
265294
freopen("output.txt", "w", stdout);
266295
#endif
267296

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;
297+
node *head = take_input_2();
298+
299+
// node*head = take_input_2();
300+
// node*head2 = take_input_2();
301+
// node *head,*head2;
302+
// cin>>head>>head2;
303+
// cout<<head;
304+
// cout<<head2;
305+
// cout<<head<<head2;
275306
// insertAtHead(head,3);
276307
// insertAtHead(head,2);
277308
// insertAtHead(head,1);

Diff for: LinkedListProblems.pdf

54.4 KB
Binary file not shown.

Diff for: List-Stl

82.3 KB
Binary file not shown.

Diff for: List-Stl.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <iostream>
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
6+
int main() {
7+
8+
#ifndef ONLINE_JUDGE
9+
// for getting input from input.txt
10+
freopen("input.txt", "r", stdin);
11+
// for writing output to output.txt
12+
freopen("output.txt", "w", stdout);
13+
#endif
14+
15+
list<int> l1{1,2,3,4,5,7,10};
16+
17+
list<string> l2{"mohit","kanika","jeevan"};
18+
19+
l2.push_back("pineapple");
20+
21+
l2.sort();
22+
l2.reverse();
23+
l2.push_back("dipit");
24+
l2.push_back("apple");
25+
26+
27+
for(string s:l2){
28+
cout<<s<<"-->";
29+
}
30+
// remove all the occurrences
31+
l2.remove("apple");
32+
auto it = l2.begin();
33+
it++;
34+
it++;
35+
l2.erase(it);
36+
l2.insert(it,"dfgd");
37+
return 0;
38+
39+
}

Diff for: balancedParanthesis.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
bool isValidExp(char *s){
6+
7+
stack <char> s;
8+
for(int i=0;s[i]!='\0';i++){
9+
10+
char ch = s[i];
11+
12+
if(ch == '('){
13+
s.push(ch);
14+
}
15+
else if(ch == ')'){
16+
if(s.empty() or s.top()!='('){
17+
return false;
18+
}
19+
s.pop();
20+
}
21+
}
22+
23+
return s.empty();
24+
}
25+
26+
int main() {
27+
28+
29+
#ifndef ONLINE_JUDGE
30+
// for getting input from input.txt
31+
freopen("input.txt", "r", stdin);
32+
// for writing output to output.txt
33+
freopen("output.txt", "w", stdout);
34+
#endif
35+
36+
37+
38+
39+
40+
return 0;
41+
}

Diff for: circularLinkedList

39.6 KB
Binary file not shown.

Diff for: circularLinkedList.cpp

+49-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class node{
1111
data = d;
1212
next = NULL;
1313
}
14-
};
14+
};
1515

1616
void insert(node*&head,int data){
1717

@@ -37,7 +37,7 @@ void insert(node*&head,int data){
3737

3838
}
3939
void print(node*head){
40-
node*teamp = head;
40+
node*temp = head;
4141
while(temp->next!= head){
4242
cout<<temp->data<<" ";
4343
temp = temp->next;
@@ -46,7 +46,54 @@ void print(node*head){
4646
return;
4747
}
4848
// deletion in circular Linked list
49+
node *getnode(node *head,int data) {
50+
node*temp = head;
51+
while(temp->next!= head) {
52+
if(temp->data == data) return temp;
53+
54+
temp = temp->next;
55+
}
56+
57+
if(temp->data == data) return temp;
58+
59+
return NULL;
60+
}
61+
void deleteNode(node*&head, int data){
62+
node *del = getnode(head,data);
63+
if(del == NULL) return;
64+
65+
if(head == del) head = head->next;
66+
node *temp = head;
67+
while(temp->next!= del){
68+
temp = temp->next;
69+
}
70+
temp->next = del->next;
71+
delete del;
72+
}
4973
int main() {
74+
75+
76+
77+
#ifndef ONLINE_JUDGE
78+
// for getting input from input.txt
79+
freopen("input.txt", "r", stdin);
80+
// for writing output to output.txt
81+
freopen("output.txt", "w", stdout);
82+
#endif
83+
84+
85+
node * head = NULL;
86+
87+
insert(head,10);
88+
insert(head,20);
89+
90+
insert(head,30);
91+
insert(head,40);
92+
print(head);
93+
94+
deleteNode(head,30);
95+
96+
print(head);
5097

5198

5299

Diff for: input.txt

-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +0,0 @@
1-
3
2-
1 1 1 1
3-
8 8 7 6
4-
8 8 8 6

Diff for: output.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1
1+
1 2 3 4 5

Diff for: reverseStack

74.1 KB
Binary file not shown.

Diff for: reverseStack.cpp

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
void transfer(stack<int> &s1,stack<int>&s2,int n){
6+
for(int i=0; i<n;i++){
7+
s2.push(s1.top());
8+
s1.pop();
9+
}
10+
}
11+
void reverseStack(stack<int> &s1){
12+
13+
stack<int> s2;
14+
15+
int n = s1.size();
16+
17+
for(int i=0; i<n;i++){
18+
19+
int x = s1.top();
20+
21+
s1.pop();
22+
23+
transfer(s1,s2,n-i-1);
24+
s1.push(x);
25+
26+
transfer(s2,s1,n-i-1);
27+
28+
}
29+
30+
31+
void insertAtBottom(stack<int> &s,int x){
32+
33+
if(s.empty()){
34+
s.push(x);
35+
return;
36+
}
37+
38+
int data = s.top();
39+
s.pop();
40+
insertAtBottom(s,x);
41+
s.push(data);
42+
}
43+
void recursiveReverseStack(){
44+
45+
if(s.empty()) return;
46+
47+
int x = s.top();
48+
s.pop();
49+
reverseStack(s);
50+
insertAtBottom(s,x);
51+
}
52+
53+
54+
int main() {
55+
56+
57+
#ifndef ONLINE_JUDGE
58+
// for getting input from input.txt
59+
freopen("input.txt", "r", stdin);
60+
// for writing output to output.txt
61+
freopen("output.txt", "w", stdout);
62+
#endif
63+
64+
stack<int> s;
65+
66+
s.push(1);
67+
s.push(2);
68+
s.push(3);
69+
s.push(4);
70+
s.push(5);
71+
72+
reverseStack(s);
73+
while(!s.empty()){
74+
cout<<s.top()<<" ";
75+
s.pop();
76+
}
77+
return 0;
78+
}

Diff for: stacknew

43.8 KB
Binary file not shown.

0 commit comments

Comments
 (0)