Use a circular singly linked list to implement Round Robin process scheduling algorithm in which
each process is provided a fixed time (quantum) to execute and is pre-empted after that time period
to allow the other process to execute. Assume a set of ‘n’ processes are ready for execution.
Read the time quantum and for each of the processes, read the total execution time.
Name the processes as ‘A’, ‘B’ and so on in sequence. Each node should contain the name
of the process, its total execution time and the remaining execution time. If a process
completes its execution, remove it from the list after displaying its name and the
completion time.
Input format:
First line contains the value of ‘n’, the number of processes
Second line contains the time quantum
The remaining lines contain the total execution time of the processes in order.
5
2
6
3
7
5
1
Output:
E 9
B 12
A 18
D 21
C 22
#include <iostream>
using namespace std;
class node
{
public:
char name;
int tm;
int rt;
node *next;
};
class rr
{
public:
node * Head = NULL;
int j = 65;
void insert (int n)
{
node *nn = new node;
nn->name = j++;
nn->tm = n;
nn->rt = nn->tm;
if (Head == NULL)
{
Head = nn;
Head->next = Head;
}
else
{
node *temp = Head;
while (temp->next != Head)
temp = temp->next;
nn->next = temp->next;
temp->next = nn;
}
}
void quantum (int t)
{
node *temp = Head;
int c = 0, i = 0;
while (Head != NULL)
{
{
temp->rt = temp->rt - t;
c = c + t;
if (temp->rt <= 0)
{
c = c + temp->rt;
cout << temp->name;
cout << c << endl;
del (temp->name);
if (temp->next == temp)
{
break;
}
}
temp = temp->next;
}
}
}
void del (char x)
{
node *p = NULL;
node *temp = Head;
if (Head->name == x)
{
while (temp->next != Head)
temp = temp->next;
p = Head;
temp->next = Head->next;
Head = Head->next;
delete p;
}
else
{
while (temp->name != x)
{
p = temp;
temp = temp->next;
}
p->next = temp->next;
delete temp;
}
}
};
int
main ()
{
rr robin;
int i, n, x, y, t;
cin >> y;
cin >> t;
for (i = 0; i < y; i++)
{
cin >> n;
robin.insert (n);
}
robin.quantum (t);
return 0;
}
Related
I am trying to find the path of a node in a binary tree by a
vector function but I am getting only 2 values from the of
starting of the node in output of the vector
like I am trying to find the path of the node 10 and my output
would be in the following way---(1 3 7 9 10),but I am getting
only (1 3) in the output
#include <bits/stdc++.h>
using namespace std;
struct node
{
int data;
struct node *left;
struct node *right;
node(int val)
{
data = val;
left = NULL;
right = NULL;
}
};
vector<int> path(node *root, int x)
{
static vector<int> v;
if (root == NULL)
{
return v;
}
v.push_back(root->data);
if (root->data == x)
{
v.push_back(x);
return v;
}
path(root->left, x);
path(root->right, x);
v.pop_back();
return v;
}
int main()
{
struct node *root = new node(1);
root->left = new node(2);
root->right = new node(3);
root->left->left = new node(4);
root->left->right = new node(5);
root->left->right->left = new node(11);
root->right->left = new node(6);
root->right->right = new node(7);
root->right->right->right = new node(9);
root->right->right->right->right = new node(10);
int x = 10;
vector<int> v = path(root, x);
cout << v.size();
cout << endl;
for (int i = 0; i < v.size(); i++)
{
cout << v[i] << " ";
}
return 0;
}
Below is a solution Network delay problem of leetcode. I have written a all test case success solution. But not able to analyse the time complexity. I believe its O(V^2 + E) where V is the number of nodes and E edges.
In this solution though I am adding all adjacents of each node every time, but not processing them further if there exists a min distance for that node already.
Leetcode question link https://leetcode.com/problems/network-delay-time
public int networkDelayTime(int[][] times, int n, int k) {
int[] distances = new int[n+1];
Arrays.fill(distances , -1);
if(n > 0){
List<List<int[]>> edges = new ArrayList<List<int[]>>();
for(int i = 0 ; i <= n ; i++){
edges.add(new ArrayList<int[]>());
}
for(int[] time : times){
edges.get(time[0]).add(new int[]{time[1] , time[2]});
}
Queue<Vertex> queue = new LinkedList<>();
queue.add(new Vertex(k , 0));
while(!queue.isEmpty()){
Vertex cx = queue.poll();
int index = cx.index;
int distance = cx.distance;
//process adjacents only if distance is updated
if(distances[index] == -1 || distances[index] > distance){
distances[index] = distance;
List<int[]> adjacents = edges.get(index);
for(int[] adjacent : adjacents){
queue.add(new Vertex(adjacent[0] , adjacent[1]+distance));
}
}
}
}
int sum = 0;
for(int i = 1 ; i <= n; i++){
int distance = distances[i];
if(distance == -1){
return -1;
}
sum = Math.max(sum , distance);
}
return sum;
}
public static class Vertex{
int index;
int distance;
public Vertex(int i , int d){
index = i;
distance = d;
}
}
You should use PriorityQueue instead of LinkedList
I can't find any error in this code, please can anyone help me out with this.
please provide the whole code by debugging the given code.
I'm having a problem in adding two polynomials otherwise everything is fine.
I wasted my whole week in figuring out this bug but can't be able to find it.
so I will be happy if anyone can help me, please
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct Node
{
int coeff;
int exp;
struct Node *next;
} *poly = NULL, *poly2 = NULL;
void create()
{
struct Node *t, *last = NULL;
int num, i;
printf("Enter number of terms");
scanf("%d", &num);
printf("Enter each term with coeff and exp\n");
for (i = 0; i < num; i++)
{
t = (struct Node *)malloc(sizeof(struct Node));
scanf("%d%d", &t->coeff, &t->exp);
t->next = NULL;
if (poly == NULL)
{
poly = last = t;
}
else
{
last->next = t;
last = t;
}
}
}
void create2()
{
struct Node *t, *last = NULL;
int num, i;
printf("Enter number of terms");
scanf("%d", &num);
printf("Enter each term with coeff and exp\n");
for (i = 0; i < num; i++)
{
t = (struct Node *)malloc(sizeof(struct Node));
scanf("%d%d", &t->coeff, &t->exp);
t->next = NULL;
if (poly2 == NULL)
{
poly2 = last = t;
}
else
{
last->next = t;
last = t;
}
}
}
void Display(struct Node *p)
{
while (p)
{
printf("%dx%d +", p->coeff, p->exp);
p = p->next;
}
printf("\n");
}
long Eval(struct Node *p, int x)
{
long val = 0;
while (p)
{
val += p->coeff * pow(x, p->exp);
p = p->next;
}
return val;
}
void add(struct Node *p, struct Node *q)
{
struct Node *sum = (struct Node *)malloc(sizeof(struct Node));
while (p && q)
{
if (p->exp > q->exp)
{
sum->exp = p->exp;
sum->coeff = p->coeff;
p = p->next;
if (p)
{
sum->next = (struct Node *)malloc(sizeof(struct Node));
sum = sum->next;
}
}
else if (p->exp < q->exp)
{
sum->exp = q->exp;
sum->coeff = q->coeff;
q = q->next;
if (q)
{
sum->next = (struct Node *)malloc(sizeof(struct Node));
sum = sum->next;
}
}
else
{
sum->exp = p->exp;
sum->coeff = p->coeff + q->coeff;
q = q->next;
p = p->next;
if (p && q)
{
sum->next = (struct Node *)malloc(sizeof(struct Node));
sum = sum->next;
}
}
}
while (p)
{
sum->next = (struct Node *)malloc(sizeof(struct Node));
sum = sum->next;
sum->exp = p->exp;
sum->coeff = p->coeff;
p = p->next;
}
while (q)
{
sum->next = (struct Node *)malloc(sizeof(struct Node));
sum = sum->next;
sum->exp = q->exp;
sum->coeff = q->coeff;
q = q->next;
}
sum->next = NULL;
Display(sum);
}
int main()
{
create();
create2();
Display(poly);
//printf("\n");
Display(poly2);
//printf("\n");
add(poly, poly2);
return 0;
}
I need to make a c program that will make a histogram of all the letters present in a phrase the user gives. When I run it, I does it but gives a "* stack smashing detected *: terminated". Where would this error be coming from? (for ease right now I set max to 3). In the future i'll have it find the max
Thank you
Andrew
#include <stdio.h>
#include <ctype.h>
#include <string.h>
static void ReadText(int histo[26],int max) {
char phrase[100];
int i;
char Letter;
char toArray;
// read in phrase
printf("Enter Phrase: "); // reads in phrase with spaces between words
scanf("%[^\n]",phrase);
// count the number of certain letters that occur
for(i = 0; i <= strlen(phrase);++i) {
Letter = phrase[i];
if(isalpha(Letter) != 0){
Letter = tolower(Letter);
toArray = Letter - 97;
histo[(int)toArray] = histo[(int)toArray] + 1;
}
}
}
static void DrawHist(int histo[26], int max){
int i;
int j;
int histo2[50];
for(i = 0; i <= 26; i++) {
histo2[i+i] = histo[i];
if(i < 25) {
histo2[i+i+1] = 0;
}
}
// (i = 1; i <= 50; i++) {
// printf("%d",histo2[i]);
//}
//printf("\n");
for(i=max;i>0;--i) {
for(j=0;j<=51;++j) {
if((j < 51) && (histo2[j] >= i)) {
printf("|");
}
else if((j < 51) && (histo2[j] < i)){
printf(" ");
}
else if(j == 51){
printf("\n");
}
}
}
printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-\n");
printf("A B C D E F G H I J K L M N O P Q R S T U V W X Y Z\n");
}
int main() {
int histo[26] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int max = 3;
//int i;
ReadText(histo,max);
//for(i = 0; i<26;++i) {
// printf("%d",histo[i]);
//}
DrawHist(histo,max);
return 0;
}
Alright, i have a hw assignment to check if an inserted string is a palindrome. The string must first be inserted into a stack, a queue, and then compared. I have the program up and running, for me that is. My teacher, when trying to grade it experience(d) a run time error. Also, getline portable is a requirement of the assignment and came with the file and instructions.
This is the note from the teacher:
Check if a line is a palindrome. Ignore spaces? y/n y (her running the code)
Input line to check
(where she gets the runtime error)
175 [main] csc240Summer2014PE8Student 10060 open_stackdumpfile: Dumping stack trace to csc240Summer2014PE8Student.exe.stackdump
#include <iostream>
#include <stack>
#include <queue>
#include <string>
using namespace std;
istream& getline_portable( istream& is, string& str ) {
istream& ris = std::getline(is,str);
if ( str.size() && str[str.size()-1] == '\r' )
str.resize(str.size()-1);
return ris;
}
int main()
{
stack<char> s;
queue<char> q;
char c, choice, b;
string str;
int i = 0;
int count = 0;
do
{
cout<<"Check if a line is a palindrome. Ignore spaces? y/n ";
cin >> b;
cin.ignore();
tolower(b);
cout<<"Input line to check\n";
getline_portable(cin,str);
for(int j = 0; j < str.size(); j++)
str[j] = tolower(str[j]);
if(b == 'n')
{
for(int j = 0; j < str.size(); j++)
{
c = str[j];
q.push(c);
s.push(c);
}
}
else if (b == 'y')
{
for(int j = 0; j < str.size() - count; j++)
{
c = str[j];
if(isspace(c))
{
}
else if(!isspace(c))
{
q.push(c);
s.push(c);
}
}
}
do
{
if(q.front() != s.top())
{
i = false;
break;
}
else
{
i = true;
s.pop();
q.pop();
}
}while(!q.empty() && !s.empty());
if (i == true)
cout << str << " is a pallindrom.\n";
else if (i == false)
cout << "Your input of " << str << " is not a pallindrome.\n";
cout << "Would you like to test another string? y/n ";
cin >> choice;
tolower(choice);
cin.ignore();
}while (choice == 'y');
cout << "Press enter to continue...";
cin.get();
return 0;
}