Skip to content

Commit bf1b470

Browse files
solved on 19/12/23
1 parent b747715 commit bf1b470

File tree

2 files changed

+203
-0
lines changed

2 files changed

+203
-0
lines changed

CS_39_NstacksInAnArray.cpp

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
class NStack
5+
{
6+
int *arr;
7+
int *top;
8+
int *next; // To store next entry in all stacks (the index of below the top element in the main array of stack m - useful for pop operation) and free list
9+
int n; // Number of stacks
10+
int size; // Size of main array
11+
int freeSpot; // Index of free spot in main array
12+
public:
13+
// constructor
14+
NStack(int N, int S) : n(N), size(S)
15+
{
16+
freeSpot = 0; // Initially all spaces are free
17+
arr = new int[size]; // Main array of size S
18+
top = new int[n]; // Stores top element of each stack in the main array thus same size as number of stacks
19+
next = new int[size]; // Stores next entry in all stacks and free list thus same size as main array
20+
21+
// Initialize all stacks as empty
22+
for (int i = 0; i < n; i++)
23+
{
24+
top[i] = -1;
25+
}
26+
27+
// Initialize all spaces as free
28+
for (int i = 0; i < size - 1; i++)
29+
{
30+
next[i] = i + 1;
31+
}
32+
next[size - 1] = -1; // -1 is used to indicate end of free list, setting last element of next[] as -1 as it itself is the last free spot
33+
}
34+
35+
// destructor
36+
~NStack()
37+
{
38+
delete[] arr;
39+
delete[] top;
40+
delete[] next;
41+
}
42+
43+
// Pushes 'X' into the Mth stack. Returns true if it gets pushed into the stack, and false otherwise.
44+
// tc: O(1) sc: O(1)
45+
bool push(int x, int m)
46+
{
47+
if (freeSpot == -1)
48+
{
49+
cout << "Stack overflow";
50+
cout << ". Can't push " << x << " into stack " << m << endl;
51+
return false; // No free spot left in main array thus stack overflow
52+
}
53+
54+
int i = freeSpot; // 1. Store index of free spot
55+
freeSpot = next[i]; // 2. Update index of free spot to next free spot
56+
arr[i] = x; // 3. Put data in free spot
57+
next[i] = top[m - 1]; // 4. Update next of free spot to index of top element of stack m, m-1 because m is 1-indexed
58+
top[m - 1] = i; // 5. Update top of stack m to index of free spot, m-1 because m is 1-indexed
59+
cout << "Pushed " << x << " into stack " << m << endl;
60+
return true; // 6. Return true as data is pushed into stack
61+
}
62+
63+
// Pops top element from Mth Stack. Returns -1 if the stack is empty, otherwise returns the popped element.
64+
// tc: O(1) sc: O(1)
65+
int pop(int m)
66+
{
67+
if (top[m - 1] == -1)
68+
{
69+
cout << "Stack underflow";
70+
cout << ". Can't pop from stack " << m << endl;
71+
return -1; // Stack is empty thus stack underflow
72+
}
73+
74+
// reverse process of push
75+
int i = top[m - 1]; // 1. Store index of top element of stack m
76+
top[m - 1] = next[i]; // 2. Update top of stack m to index of next element of top element of stack m (the element below the top element in the main array of stack m)
77+
int popped = arr[i]; // 3. Store popped element
78+
next[i] = freeSpot; // 4. Update next of popped element to index of free spot
79+
freeSpot = i; // 5. Update index of free spot to index of popped element
80+
cout << "Popped " << popped << " from stack " << m << endl;
81+
return popped; // 6. Return popped element
82+
}
83+
};
84+
85+
int main()
86+
{
87+
int n = 3, size = 6;
88+
cout << "Number of stacks: " << n << endl;
89+
cout << "Size of main array: " << size << endl << endl;
90+
NStack stack(n, size);
91+
stack.push(100, 1);
92+
stack.push(200, 1);
93+
stack.push(2000, 2);
94+
stack.push(8, 3);
95+
stack.push(4, 3);
96+
stack.push(2, 3);
97+
stack.push(3000, 2);
98+
stack.push(1, 3);
99+
}

CS_40_NQueuesInAnArray.cpp

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
class NQueue
5+
{
6+
public:
7+
int n, k, freeSpot; // n is size of main array, k is number of queues, freeSpot is index of free spot in main array
8+
int *arr, *front, *rear, *next;
9+
// constructor
10+
NQueue(int n, int s) : n(s), k(n), freeSpot(0)
11+
{
12+
arr = new int[n];
13+
next = new int[n];
14+
front = new int[k];
15+
rear = new int[k];
16+
17+
// Initialize all queues as empty
18+
for (int i = 0; i < k; i++)
19+
{
20+
front[i] = rear[i] = -1;
21+
}
22+
// intialize all spaces as free
23+
for (int i = 0; i < n; i++)
24+
{
25+
next[i] = i + 1;
26+
}
27+
next[n - 1] = -1; // -1 is used to indicate end of free list, setting last element of next[] as -1 as it itself is the last free spot
28+
}
29+
30+
// destructor
31+
~NQueue()
32+
{
33+
delete[] arr;
34+
delete[] front;
35+
delete[] rear;
36+
delete[] next;
37+
}
38+
39+
// Enqueues 'X' into the Mth queue. Returns true if it gets pushed into the queue, and false otherwise.
40+
bool enqueue(int x, int m)
41+
{
42+
// overflow check
43+
if (freeSpot == -1)
44+
{
45+
cout << "Queue overflow";
46+
cout << ". Can't enqueue " << x << " into queue " << m << endl;
47+
return false; // No free spot left in main array thus queue overflow
48+
}
49+
50+
int i = freeSpot; // 1. Store index of free spot
51+
freeSpot = next[i]; // 2. Update index of free spot to next free spot
52+
// if first element is being enqueued
53+
if (front[m - 1] == -1)
54+
{
55+
front[m - 1] = i; // 3. Update front of queue m to index of free spot, m-1 because m is 1-indexed
56+
}
57+
else
58+
{
59+
// link the previous rear to the new rear (some elements may be in between which belong to other queues)
60+
next[rear[m - 1]] = i; // 3. Update next of rear of queue m to index of free spot, m-1 because m is 1-indexed
61+
}
62+
next[i] = -1; // 4. Update next of free spot to -1 to indicate end of queue
63+
rear[m - 1] = i; // 5. Update rear of queue m to index of free spot, m-1 because m is 1-indexed
64+
arr[i] = x; // 6. Put data in free spot
65+
cout << "Enqueued " << x << " into queue " << m << endl;
66+
return true; // 7. Return true as data is enqueued into queue
67+
}
68+
69+
// Dequeues top element from Mth queue. Returns -1 if the queue is empty, otherwise returns the popped element.
70+
int dequeue(int m)
71+
{
72+
// underflow check
73+
if (front[m - 1] == -1)
74+
{
75+
cout << "Queue underflow";
76+
cout << ". Can't dequeue from queue " << m << endl;
77+
return -1; // Queue is empty thus queue underflow
78+
}
79+
80+
// find index of front element of queue m to pop it
81+
int i = front[m - 1]; // 1. Store index of front element of queue m
82+
front[m - 1] = next[i]; // 2. Update front
83+
// update free spots
84+
next[i] = freeSpot; // 3. Update next of popped element to index of free spot
85+
freeSpot = i; // 4. Update index of free spot to index of popped element
86+
cout << "Dequeued " << arr[i] << " from queue " << m << endl;
87+
return arr[i]; // 5. Return popped element
88+
}
89+
};
90+
91+
int main()
92+
{
93+
int n = 3, size = 6;
94+
cout << "Number of queues: " << n << endl;
95+
cout << "Size of main array: " << size << endl << endl;
96+
NQueue q(n, size);
97+
q.enqueue(1, 1);
98+
q.enqueue(2, 1);
99+
q.enqueue(3, 2);
100+
q.enqueue(4, 2);
101+
q.enqueue(5, 3);
102+
q.enqueue(6, 3);
103+
return 0;
104+
}

0 commit comments

Comments
 (0)