Skip to content

Commit faf3d48

Browse files
authored
Merge branch 'master' into add_leetcode_pow_x_n
2 parents 1d0ecab + 90d7d81 commit faf3d48

File tree

24 files changed

+1052
-163
lines changed

24 files changed

+1052
-163
lines changed

.github/labeler.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Leetcode folder changes:
2+
- leetcode/**/*

.github/workflows/labeler.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
name: "Pull Request Labeler"
2+
on:
3+
- pull_request_target
4+
5+
jobs:
6+
triage:
7+
permissions:
8+
contents: read
9+
pull-requests: write
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/labeler@v4
13+
with:
14+
repo-token: "${{ secrets.GITHUB_TOKEN }}"
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# The objective of this GitHub Action is to update the leetcode DIRECTORY.md file (if needed)
2+
# when doing a git push
3+
name: leetcode_directory_writer
4+
on:
5+
push:
6+
paths:
7+
- "leetcode/src/**.c"
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v3
13+
with:
14+
fetch-depth: 0
15+
- uses: actions/setup-python@v4
16+
with:
17+
python-version: 3.x
18+
- name: Add python dependencies
19+
run: |
20+
pip install requests
21+
- name: Write leectode DIRECTORY.md
22+
run: |
23+
python3 scripts/leetcode_directory_md.py 2>&1 | tee leetcode/DIRECTORY.md
24+
git config --global user.name github-actions[bot]
25+
git config --global user.email 'github-actions@users.noreply.github.com'
26+
- name: Update LeetCode's directory
27+
run: |
28+
git add leetcode/DIRECTORY.md
29+
git commit -am "updating DIRECTORY.md" || true
30+
git push origin HEAD:$GITHUB_REF || true

DIRECTORY.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,15 @@
172172
* [1009](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/1009.c)
173173
* [101](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/101.c)
174174
* [1019](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/1019.c)
175+
* [1026](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/1026.c)
175176
* [104](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/104.c)
176177
* [108](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/108.c)
177178
* [1089](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/1089.c)
178179
* [109](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/109.c)
179180
* [11](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/11.c)
180181
* [110](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/110.c)
181182
* [112](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/112.c)
183+
* [1147](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/1147.c)
182184
* [118](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/118.c)
183185
* [1184](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/1184.c)
184186
* [1189](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/1189.c)
@@ -187,6 +189,7 @@
187189
* [1207](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/1207.c)
188190
* [121](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/121.c)
189191
* [125](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/125.c)
192+
* [1283](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/1283.c)
190193
* [13](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/13.c)
191194
* [136](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/136.c)
192195
* [14](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/14.c)
@@ -196,12 +199,15 @@
196199
* [153](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/153.c)
197200
* [160](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/160.c)
198201
* [1653](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/1653.c)
202+
* [1657](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/1657.c)
199203
* [169](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/169.c)
200204
* [1695](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/1695.c)
201205
* [1704](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/1704.c)
202206
* [173](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/173.c)
203207
* [1752](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/1752.c)
204208
* [1769](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/1769.c)
209+
* [1833](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/1833.c)
210+
* [1838](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/1838.c)
205211
* [189](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/189.c)
206212
* [190](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/190.c)
207213
* [191](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/191.c)
@@ -211,7 +217,9 @@
211217
* [2024](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/2024.c)
212218
* [203](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/203.c)
213219
* [206](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/206.c)
220+
* [2095](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/2095.c)
214221
* [21](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/21.c)
222+
* [2125](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/2125.c)
215223
* [2130](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/2130.c)
216224
* [215](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/215.c)
217225
* [217](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/217.c)
@@ -220,13 +228,16 @@
220228
* [2256](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/2256.c)
221229
* [226](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/226.c)
222230
* [2270](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/2270.c)
231+
* [2279](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/2279.c)
223232
* [230](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/230.c)
224233
* [2304](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/2304.c)
225234
* [231](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/231.c)
226235
* [234](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/234.c)
227236
* [236](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/236.c)
228237
* [24](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/24.c)
229238
* [242](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/242.c)
239+
* [2482](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/2482.c)
240+
* [2501](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/2501.c)
230241
* [26](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/26.c)
231242
* [268](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/268.c)
232243
* [27](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/27.c)
@@ -264,23 +275,27 @@
264275
* [66](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/66.c)
265276
* [669](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/669.c)
266277
* [674](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/674.c)
278+
* [684](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/684.c)
267279
* [7](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/7.c)
268280
* [700](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/700.c)
269281
* [701](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/701.c)
270282
* [704](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/704.c)
271283
* [709](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/709.c)
284+
* [75](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/75.c)
272285
* [771](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/771.c)
273286
* [79](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/79.c)
274287
* [8](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/8.c)
275288
* [807](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/807.c)
276289
* [82](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/82.c)
277290
* [83](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/83.c)
291+
* [841](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/841.c)
278292
* [852](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/852.c)
279293
* [876](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/876.c)
280294
* [9](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/9.c)
281295
* [901](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/901.c)
282296
* [905](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/905.c)
283297
* [917](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/917.c)
298+
* [931](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/931.c)
284299
* [938](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/938.c)
285300
* [94](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/94.c)
286301
* [965](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/965.c)

data_structures/linked_list/singly_link_list_deletion.c

Lines changed: 120 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,64 @@
44
when passed with a key of the node.
55
*/
66
#include <stdio.h>
7+
#include <stdlib.h>
8+
#include <assert.h>
79
struct node
810
{
911
int info;
1012
struct node *link;
1113
};
1214
struct node *start = NULL;
13-
///////////////////////////////////////////////////////////
14-
struct node *createnode() // function to create node
15+
//////////////////////////////////////////////////////////////////
16+
struct node *createnode() // function to create node
1517
{
1618
struct node *t;
1719
t = (struct node *)malloc(sizeof(struct node));
1820
return (t);
1921
}
20-
////////////////////////////////////////////////////////
21-
void insert() // function to insert at first location
22+
//////////////////////////////////////////////////////////////////
23+
int insert(int pos, int d)
2224
{
23-
struct node *p;
24-
p = createnode();
25-
printf("\nenter the number to insert");
26-
scanf("%d", &p->info);
27-
p->link = NULL;
28-
if (start == NULL)
25+
struct node *new;
26+
new = createnode();
27+
new->info = d;
28+
if (pos == 1)
2929
{
30-
start = p;
30+
new->link = NULL;
31+
if (start == NULL)
32+
{
33+
start = new;
34+
}
35+
else
36+
{
37+
new->link = start;
38+
start = new;
39+
}
3140
}
3241
else
3342
{
34-
p->link = start;
35-
start = p;
43+
struct node *pre = start;
44+
for (int i = 2; i < pos; i++)
45+
{
46+
if (pre == NULL)
47+
{
48+
break;
49+
}
50+
pre = pre->link;
51+
}
52+
if(pre==NULL)
53+
{
54+
printf("Position not found!");
55+
return 0;
56+
}
57+
new->link = pre->link;
58+
pre->link = new;
3659
}
37-
}
38-
///////////////////////////////////////////////////////////
39-
void deletion() // function to delete from first position
60+
return 0;
61+
}
62+
63+
///////////////////////////////////////////////////////////////////
64+
int deletion(int pos) // function to delete from any position
4065
{
4166
struct node *t;
4267
if (start == NULL)
@@ -45,14 +70,34 @@ void deletion() // function to delete from first position
4570
}
4671
else
4772
{
48-
struct node *p;
49-
p = start;
50-
start = start->link;
51-
free(p);
73+
if (pos == 1)
74+
{
75+
struct node *p;
76+
p = start;
77+
start = start->link;
78+
free(p);
79+
}
80+
else
81+
{
82+
struct node *prev = start;
83+
for (int i = 2; i < pos; i++)
84+
{
85+
if (prev == NULL)
86+
{
87+
printf("Position not found!");
88+
return 0;
89+
}
90+
prev = prev->link;
91+
}
92+
struct node *n = prev->link; // n points to required node to be deleted
93+
prev->link = n->link;
94+
free(n);
95+
}
5296
}
97+
return 0;
5398
}
54-
///////////////////////////////////////////////////////
55-
void viewlist() // function to display values
99+
///////////////////////////////////////////////////////////////////
100+
void viewlist() // function to display values
56101
{
57102
struct node *p;
58103
if (start == NULL)
@@ -69,32 +114,64 @@ void viewlist() // function to display values
69114
}
70115
}
71116
}
72-
//////////////////////////////////////////////////////////////////////
117+
//////////////////////////////////////////////////////////////////
118+
static void test()
119+
{
120+
insert(1, 39);
121+
assert(start->info == 39);
122+
insert(2, 10);
123+
insert(3, 11);
124+
deletion(1);
125+
assert(start->info != 39);
73126

127+
printf("Self-tests successfully passed!\n");
128+
}
129+
//////////////////////////////////////////////////////////////////
74130
int main()
75131
{
76-
int n;
77-
while (1)
132+
int n = 0, pos = 0, p = 0, num = 0, c = 0;
133+
printf("\n1.self test mode");
134+
printf("\n2.interactive mode");
135+
printf("\nenter your choice:");
136+
scanf("%d", &c);
137+
if (c == 1)
138+
{
139+
test();
140+
}
141+
else if (c == 2)
78142
{
79-
printf("\n1.add value at first location");
80-
printf("\n2.delete value from first location");
81-
printf("\n3.view value");
82-
printf("\nenter your choice");
83-
scanf("%d", &n);
84-
switch (n)
143+
while (1)
85144
{
86-
case 1:
87-
insert();
88-
break;
89-
case 2:
90-
deletion();
91-
break;
92-
case 3:
93-
viewlist();
94-
break;
95-
default:
96-
printf("\ninvalid choice");
145+
printf("\n1.add value at the given location");
146+
printf("\n2.delete value at the given location");
147+
printf("\n3.view list");
148+
printf("\nenter your choice :");
149+
scanf("%d", &n);
150+
switch (n)
151+
{
152+
case 1:
153+
printf("enter the position where the element is to be added :");
154+
scanf("%d", &p);
155+
printf("enter the element is to be added :");
156+
scanf("%d", &num);
157+
insert(p, num);
158+
break;
159+
case 2:
160+
printf("enter the position where the element is to be deleted :");
161+
scanf("%d", &pos);
162+
deletion(pos);
163+
break;
164+
case 3:
165+
viewlist();
166+
break;
167+
default:
168+
printf("\ninvalid choice");
169+
}
97170
}
98171
}
99-
return (0);
172+
else
173+
{
174+
printf("Invalid choice");
175+
}
176+
return 0;
100177
}

0 commit comments

Comments
 (0)