-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
232 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
class CircularQueue { | ||
private: | ||
int front, rear, size; | ||
int *arr; | ||
public: | ||
CircularQueue(int s) { | ||
front = rear = -1; | ||
size = s; | ||
arr = new int[size]; | ||
} | ||
|
||
void enqueue(int value) { | ||
if ((front == 0 && rear == size - 1) || (rear == (front - 1) % (size - 1))) { | ||
cout << "Queue is full" << endl; | ||
return; | ||
} | ||
else if (front == -1) { | ||
front = rear = 0; | ||
arr[rear] = value; | ||
} | ||
else if (rear == size - 1 && front != 0) { | ||
rear = 0; | ||
arr[rear] = value; | ||
} | ||
else { | ||
int i; | ||
if (front <= rear) { | ||
for (i = rear; i >= front; i--) { | ||
if (value > arr[i]) { | ||
arr[i + 1] = arr[i]; | ||
} | ||
else { | ||
break; | ||
} | ||
} | ||
} | ||
else { | ||
for (i = rear; i >= 0; i--) { | ||
if (value > arr[i]) { | ||
arr[i + 1] = arr[i]; | ||
} | ||
else { | ||
break; | ||
} | ||
} | ||
for (i = size - 1; i >= front; i--) { | ||
if (value > arr[i]) { | ||
arr[i + 1] = arr[i]; | ||
} | ||
else { | ||
break; | ||
} | ||
} | ||
} | ||
arr[i + 1] = value; | ||
rear++; | ||
} | ||
} | ||
|
||
int dequeue() { | ||
if (front == -1) { | ||
cout << "Queue is empty" << endl; | ||
return -1; | ||
} | ||
int data = arr[front]; | ||
arr[front] = -1; | ||
if (front == rear) { | ||
front = rear = -1; | ||
} | ||
else if (front == size - 1) { | ||
front = 0; | ||
} | ||
else { | ||
front++; | ||
} | ||
return data; | ||
} | ||
|
||
void display() { | ||
if (front == -1) { | ||
cout << "Queue is empty" << endl; | ||
return; | ||
} | ||
cout << "Elements in the queue are: "; | ||
if (rear >= front) { | ||
for (int i = front; i <= rear; i++) { | ||
cout << arr[i] << " "; | ||
} | ||
} | ||
else { | ||
for (int i = front; i < size; i++) { | ||
cout << arr[i] << " "; | ||
} | ||
for (int i = 0; i <= rear; i++) { | ||
cout << arr[i] << " "; | ||
} | ||
} | ||
cout << endl; | ||
} | ||
}; | ||
|
||
int main() { | ||
int size, choice, value; | ||
cout << "Enter the size of the circular queue: "; | ||
cin >> size; | ||
CircularQueue cq(size); | ||
do { | ||
cout << "Enter your choice:\n1. Insertion\n2. Deletion\n3. Display\n4. Exit\n"; | ||
cin >> choice; | ||
switch (choice) { | ||
case 1: | ||
cout << "Enter the value to be inserted: "; | ||
cin >> value; | ||
cq.enqueue(value); | ||
break; | ||
case 2: | ||
cq.dequeue(); | ||
break; | ||
case 3: | ||
cq.display(); | ||
break; | ||
case 4: | ||
cout << "Exiting the program" << endl; | ||
break; | ||
default: | ||
cout << "Invalid choice" << endl; | ||
} | ||
} while (choice != 4); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// #include<iostream> | ||
// #include<queue> | ||
// using namespace std; | ||
|
||
// struct Process { | ||
// int id; | ||
// int burst_time; | ||
// }; | ||
|
||
// void findCompletionTime(queue<Process> q, int n, int quantum, int ct[]) { | ||
// int rem_bt[n]; | ||
// for (int i = 0; i < n; i++) | ||
// rem_bt[i] = q.front().burst_time; | ||
// int t = 0; | ||
// while (1) { | ||
// bool done = true; | ||
// for (int i = 0; i < n; i++) { | ||
// if (rem_bt[i] > 0) { | ||
// done = false; | ||
// if (rem_bt[i] > quantum) { | ||
// t += quantum; | ||
// rem_bt[i] -= quantum; | ||
// } | ||
// else { | ||
// t = t + rem_bt[i]; | ||
// ct[i] = t; | ||
// rem_bt[i] = 0; | ||
// } | ||
// } | ||
// } | ||
// if (done == true) | ||
// break; | ||
// } | ||
// } | ||
|
||
// int main() { | ||
// queue<Process> q; | ||
// int quantum; | ||
// cout << "Enter time quantum: "; | ||
// cin >> quantum; | ||
// int exec_time1, exec_time2, exec_time3; | ||
// cout << "Enter execution times for three programs: "; | ||
// cin >> exec_time1 >> exec_time2 >> exec_time3; | ||
// Process p1 = {1, exec_time1}; | ||
// Process p2 = {2, exec_time2}; | ||
// Process p3 = {3, exec_time3}; | ||
// q.push(p1); | ||
// q.push(p2); | ||
// q.push(p3); | ||
// int ct[3]; | ||
// findCompletionTime(q, q.size(), quantum, ct); | ||
// cout << "Completion times are: " << ct[0] << ", " << ct[1] << ", " << ct[2]; | ||
// return 0; | ||
// } | ||
|
||
#include<iostream> | ||
#include<queue> | ||
using namespace std; | ||
|
||
struct Process { | ||
int id; | ||
int burst_time; | ||
}; | ||
|
||
void findCompletionTime(queue<Process>& q, int n, int quantum, int ct[]) { | ||
int time = 0; | ||
while (!q.empty()) { | ||
Process proc = q.front(); | ||
q.pop(); | ||
if (proc.burst_time > quantum) { | ||
time += quantum; | ||
proc.burst_time -= quantum; | ||
q.push(proc); | ||
} else { | ||
time += proc.burst_time; | ||
ct[proc.id - 1] = time; | ||
} | ||
} | ||
} | ||
|
||
int main() { | ||
queue<Process> q; | ||
int quantum; | ||
cout << "Enter time quantum: "; | ||
cin >> quantum; | ||
int exec_time1, exec_time2, exec_time3; | ||
cout << "Enter execution times for three programs: "; | ||
cin >> exec_time1 >> exec_time2 >> exec_time3; | ||
Process p1 = {1, exec_time1}; | ||
Process p2 = {2, exec_time2}; | ||
Process p3 = {3, exec_time3}; | ||
q.push(p1); | ||
q.push(p2); | ||
q.push(p3); | ||
int ct[3] = {0}; | ||
findCompletionTime(q, q.size(), quantum, ct); | ||
cout << "Completion times are: " << ct[0] << ", " << ct[2] << ", " << ct[1]; | ||
return 0; | ||
} |