commit 50bb017a9fb5e5846ad3d49da68d79d74706e0e0
parent 4ba63e4e06a6b302fdad4197dc553cb5cd19a26a
Author: Amit Dutta <mail@amit.is-a.dev>
Date: Wed, 7 Oct 2026 23:24:46 +0530
Merge pull request #144 from notamitgamer/Fix
Fixing many algos.
Diffstat:
7 files changed, 70 insertions(+), 61 deletions(-)
diff --git a/semester_2/algorithms/algo_023.md b/semester_2/algorithms/algo_023.md
@@ -6,23 +6,26 @@
## Algorithm
```
-procedure insert(dl, val, key)
+Procedure insert(dl, val, key)
begin
ptr ← dl;
found ← 0;
- while(ptr)
- if(info(ptr) = key)
+
+ while (ptr ≠ NULL) do
+ if (INFO(ptr) = key) then
found ← 1;
break;
endif
ptr ← next(ptr);
- end while
- if(found)
+ endwhile
+
+ if (found = 1) then
nptr ← getNode();
- info(nptr) ← val;
+ INFO(nptr) ← val;
next(nptr) ← NULL;
prev(nptr) ← NULL;
- if(next(ptr) = NULL)
+
+ if (next(ptr) = NULL) then
prev(nptr) ← ptr;
next(ptr) ← nptr;
else
@@ -30,9 +33,11 @@ begin
prev(next(ptr)) ← nptr;
next(ptr) ← nptr;
prev(nptr) ← ptr;
- end if
+ endif
else
- write("key not found");
+ write("Key not found");
endif
+
+ return(dl);
end procedure
-```
-\ No newline at end of file
+```
diff --git a/semester_2/algorithms/algo_025.md b/semester_2/algorithms/algo_025.md
@@ -6,24 +6,23 @@
## Algorithm
```
-procedure delete_lastNode(dl)
+Procedure delete_lastNode(dl)
begin
- if(dl = NULL)
- write("List is empty. deletion not possible");
+ if (dl = NULL) then
+ write("List is empty, deletion not possible.");
else
ptr ← dl;
- if(next(ptr) = NULL)
- dl ← next(ptr);
+ if (next(ptr) = NULL) then
+ dl ← NULL;
delete(ptr);
else
- while(next(ptr) ≠ NULL)
+ while (next(ptr) ≠ NULL) do
ptr ← next(ptr);
- end while
- ptr ← prev(ptr);
- next(ptr) ← NULL;
+ endwhile
+ next(prev(ptr)) ← NULL;
delete(ptr);
endif
endif
return(dl);
end procedure
-```
-\ No newline at end of file
+```
diff --git a/semester_2/algorithms/algo_029.md b/semester_2/algorithms/algo_029.md
@@ -6,15 +6,15 @@
## Algorithm
```
-procedure pop(stack[], top, n)
+Procedure pop(stack[], top)
begin
- if(top = -1)
+ if (top = -1) then
write("Stack is empty, deletion not possible.");
- return;
+ return NULL;
else
x ← stack[top];
top ← top - 1;
return x;
- endif;
+ endif
end procedure
-```
-\ No newline at end of file
+```
diff --git a/semester_2/algorithms/algo_030.md b/semester_2/algorithms/algo_030.md
@@ -6,12 +6,17 @@
## Algorithm
```
-procedure push(Stack, val)
-begin
+Procedure push(stack, val)
+begin
ptr ← getNode();
- info(ptr) ← val;
+ if (ptr = NULL) then
+ write("Stack Overflow / Memory Allocation Failed");
+ return(stack);
+ endif
+
+ INFO(ptr) ← val;
next(ptr) ← stack;
stack ← ptr;
return(stack);
end procedure
-```
-\ No newline at end of file
+```
diff --git a/semester_2/algorithms/algo_031.md b/semester_2/algorithms/algo_031.md
@@ -6,15 +6,17 @@
## Algorithm
```
-procedure pop(stack, val)
+Procedure pop(stack, val)
begin
- if(stack = NULL)
- write("Empty");
- else
- ptr ← stack;
- stack ← next(ptr);
+ if (stack = NULL) then
+ write("Stack Underflow");
+ return(stack);
+ else
+ ptr ← stack;
+ val ← INFO(ptr);
+ stack ← next(ptr);
delete(ptr);
return(stack);
endif
end procedure
-```
-\ No newline at end of file
+```
diff --git a/semester_2/algorithms/algo_038.md b/semester_2/algorithms/algo_038.md
@@ -6,17 +6,20 @@
## Algorithm
```
-procedure enqueue(rear, Qsize, front, Q[], val)
+Procedure enqueue(Q[], front, rear, Qsize, val)
begin
- if((rear + 1) % Qsize = font)
+ if ((rear + 1) % Qsize = front) then
write("Queue full, insertion can't be done.");
else
- if(front = -1)
+ if (front = -1) then
front ← 0;
+ rear ← 0;
+ else
+ rear ← (rear + 1) % Qsize;
endif
- rear ← (rear + 1) % Qsize;
+
Q[rear] ← val;
- write("Inserted element", val);
+ write("Inserted element: ", val);
endif
end procedure
-```
-\ No newline at end of file
+```
diff --git a/semester_2/algorithms/algo_039.md b/semester_2/algorithms/algo_039.md
@@ -6,20 +6,22 @@
## Algorithm
```
-procedure dequeue(Q[], front, rear)
+Procedure dequeue(Q[], front, rear, Qsize)
begin
- if(front = -1)
- write("Queue empty.");
+ if (front = -1) then
+ write("Queue empty.")
else
- rear ← Q[front];
- write("Deleted element: ", val);
- --------------------------------
- | front ← (front + 1) % Qsize; |
- | if(front > rear) | Vice Versa
- | front ← -1; |
- | rear ← -1; |
- --------------------------------
+ val ← Q[front]
+ write("Deleted element: ", val)
+
+ // CORRECTED SECTION:
+ if (front = rear) then
+ front ← -1
+ rear ← -1
+ else
+ front ← (front + 1) % Qsize
endif
endif
end procedure
-```
-\ No newline at end of file
+
+```