Skip to content

Commit 2d489b8

Browse files
author
Prateek Narang
committed
trees,dp,common codes etc
1 parent 4b7d8e2 commit 2d489b8

8 files changed

+111
-17
lines changed

Queue/1. Queue using Array.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@ void deQueue(Queue &Q){
3131
return ;
3232
}
3333
Q.front++;
34+
3435
if(Q.front==100){
3536
Q.front = 0;
3637
}
38+
3739
Q.currentSize--;
40+
3841
if(Q.currentSize==0){
3942
Q.front = 0;
4043
Q.rear = -1;

Queue/2. Reverse A Queue Using Recursion.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ deQueue(Q);
7474
enQueue(Q,5);
7575
recursiveReverse(Q);
7676
print(Q);
77-
7877
}
7978

8079

Binary file not shown.

Recursion/11. Generate all subsequences of a string.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void printSubsequences(char *s,char *temp,int start_index,int temp_index,int len
2626

2727

2828
int main(){
29-
char s[20]="hello";
29+
char s[20]="abc";
3030
char temp[20];
3131
int len = strlen(s);
3232
printSubsequences(s,temp,0,-1,len);
Binary file not shown.

Shortest Paths/Dijkshtra Adjaceny List and Heap (ELogV).cpp

+69-14
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,37 @@ adj[dest].push_back(n1);
3636
}
3737

3838

39+
/*
40+
void disjkstra2(int s,int V=9){
41+
bool visited[100]={0};
42+
memset(dist,INT_MAX,sizeof dist);
43+
44+
priority_queue<int,vector<int>,myComparison> q;
3945
46+
dist[s]=0;
47+
visited[s]=true;
48+
for(int i=0;i<V;i++){
49+
q.push(i);
50+
}
4051
52+
while(!q.empty()){
53+
int cur = q.top();
54+
q.pop();
55+
56+
visited[cur]=true;
57+
58+
for(int j=0;j<adj[cur].size();j++){
59+
int des = adj[cur][j].dest;
60+
int wt = adj[cur][j].wt;
61+
62+
if(dist[cur]+wt < dist[des]&&visited[des]==false){
63+
dist[des] = dist[cur]+wt;
64+
}
65+
}
66+
}
67+
68+
}
69+
*/
4170

4271

4372
void dijkstra(int s){
@@ -78,29 +107,55 @@ while(!q.empty()){
78107

79108
}
80109

110+
}
111+
/*
112+
void test_func(){
113+
int v1=15;
114+
int v2 = 16;
81115
82116
83-
}
117+
priority_queue<int,vector<int>,myComparison> q;
118+
dist[15] = 10;
119+
dist[16] = 16;
120+
121+
122+
q.push(v1);
123+
q.push(v2);
124+
125+
dist[15]=100;
126+
127+
cout<<"In test function ";
128+
while(!q.empty()){
129+
cout<<q.top()<<endl;
130+
q.pop();
131+
132+
}
84133
134+
}
135+
*/
85136
int main(){
86137
int V = 9;
87-
addEdge( 0, 1, 4);
138+
addEdge(0, 1, 4);
88139
addEdge(0, 7, 8);
89-
addEdge( 1, 2, 8);
90-
addEdge( 1, 7, 11);
91-
addEdge( 2, 3, 7);
92-
addEdge( 2, 8, 2);
93-
addEdge( 2, 5, 4);
140+
addEdge(1, 2, 8);
141+
addEdge(1, 7, 11);
142+
addEdge(2, 3, 7);
143+
addEdge(2, 8, 2);
144+
addEdge(2, 5, 4);
94145
addEdge(3, 4, 9);
95-
addEdge( 3, 5, 14);
96-
addEdge( 4, 5, 10);
97-
addEdge( 5, 6, 2);
98-
addEdge( 6, 7, 1);
99-
addEdge( 6, 8, 6);
100-
addEdge( 7, 8, 7);
101-
dijkstra(0);
146+
addEdge(3, 5, 14);
147+
addEdge(4, 5, 10);
148+
addEdge(5, 6, 2);
149+
addEdge(6, 7, 1);
150+
addEdge(6, 8, 6);
151+
addEdge(7, 8, 7);
152+
//dijkstra(0);
153+
102154
int i;
103155
for(i=0;i<V;i++)
104156
cout<<i<<" "<<dist[i]<<endl;
157+
158+
test_func();
159+
105160
return 0;
106161
}

Shortest Paths/Dijkshtra Adjaceny List and Heap.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include<queue>
55
#include<cstring>
66
#define INT_MAX 1000000000
7+
#include<functional>
78
using namespace std;
89

910

Original file line numberDiff line numberDiff line change
@@ -1,9 +1,45 @@
11
#include<iostream>
2-
#include<stack>
2+
#include "stack_using_node.h"
33
using namespace std;
44

5+
//----------------------------------------Recursive Implementation
6+
void insertAtBottom(stack_using_node<int> &s,int data){
7+
int temp;
8+
if(s.isEmpty()))
9+
{
10+
s.push(data);
11+
}
12+
else
13+
{
14+
int temp = s.pop();
15+
insertAtBottom(s,data);
16+
s.push(data);
17+
}
18+
}
19+
20+
21+
void reverseStack(stack_using_node<int> &s){
22+
if(!s.isEmpty()){
23+
24+
int top = s.pop();
25+
reverseStack(s);
26+
insertAtBottom(top);
27+
28+
}
29+
}
30+
//----------------------------------------------Non Recursive Implemenation
31+
532

633
int main(){
34+
stack_using_node<int> s;
35+
s.push(1);
36+
s.push(2);
37+
s.push(3);
738

39+
reverseStack(s);
840

41+
while(!s.isEmpty()){
42+
cout<<s.pop();
43+
}
44+
return 0;
945
}

0 commit comments

Comments
 (0)