add function is not working properly in Polynomial using linked list - linked-list

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;
}

Related

Quick sort throws a 'not in inclusive range' error but sometimes it works. (Dart)

I have implemented a quick sort algorithm in Dart which sorts a list of random integers in the range from 0 to 100. Most of the times, it throws a 'not in inclusive range' exception. Other times, it works. I don't understand what is wrong with my code or logic here.
import 'dart:math';
List<int> list = [];
void main() {
randomize();
quickSort(0, list.length - 1);
print(list);
}
void randomize() {
for (int i = 0; i < 100; ++i) {
list.add(Random().nextInt(100));
}
}
void quickSort(int l, int h) {
if (l < h) {
int mid = partition(l, h);
quickSort(l, mid - 1);
quickSort(mid + 1, h);
}
}
int partition(int l, int h) {
int pivot = l;
int i = l;
int j = h;
while (i < j) {
while (list[i] <= list[pivot]) {
++i;
}
while (list[j] > list[pivot]) {
--j;
}
if (i < j) {
int temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
int temp = list[pivot];
list[pivot] = list[j];
list[j] = temp;
return j;
}

Itrying to find the path of a node in a binary tree by a vector function but I am getting only 2 values from the starting node in output of the vector

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;
}

Round Robin Algorithm Using Circular Linked List

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;
}

Tarjan algorithm based on BFS

I have an implemented BFS algorithm and I want to find SCC with Tarjan's algorithm using my BFS (implemented with colors and time method). Here is the code: /////////////////////////////////////////////////////////////
void dfs_visit_color(int v, Graf *G, int *time)
{
time++;
G->d[v] = *time;
G->color[v] = GRI;
NodeT *p = G->t[v];
int w;
printf("%d ", v);
while (p != NULL)
{
w = p->val;
if (G->color[w] == ALB)
{
T[w] = v;
dfs_visit_color(w, G, time);
}
p = p->next;
}
G->color[v] = NEGRU;
time++;
G->f[v] = *time;
}
void dfs_color(Graf *G){
int time = 0, i;
for (i = 0; i < G->n; i++)
{
if (G->color[i] = ALB);
}
for (i = 0; i < G->n; i++)
{
if (G->color[i] == ALB)
{
dfs_visit_color(i, G, &time);
}
}
}

How to print all possible solutions for Longest Common subsequence

I want to print all the possible solutions to LCS problem.
The two strings abcbdab and bdcaba should print following 3 strings:
bdab,bcba,bcab.
C is the global matrix table which takes values according to algorithm and m, n are the length of the sequences a, b.
But The output is something unexpected.
#include<stdio.h>
#include<conio.h>
int co=0,m=0,n=0,c[10][10];
char a[10],b[10];
void main()
{
int i,j;
clrscr();
printf("Enter Two strings: ");
scanf("%s",a);
scanf("%s",b);
m=strlen(a);
n=strlen(b);
for(i=0;i<=m;i++)
{
for(j=0;j<=n;j++)
{ if(i==0 || j==0)
{
c[i][j]=0;
}
else if(a[i-1]==b[j-1])
{
c[i][j]=c[i-1][j-1]+1;
}
else if(c[i-1][j]>=c[i][j-1])
{
c[i][j]=c[i-1][j];
}
else
{
c[i][j]=c[i][j-1];
}
}
}
for(i=0;i<=m;i++)
{
for(j=0;j<=n;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
print(m,n);
getch();
}
print(int i,int j)
{
if(i==0 || j==0)
return 0;
else if(a[i-1]==b[j-1])
{
print(i-1,j-1);
if(co==c[m][n])
{
co=0;
printf("\n");
}
printf("%c",a[i-1]);
co++;
}
else if(c[i-1][j]==c[i][j-1])
{
print(i-1,j);
print(i,j-1);
}
else if(c[i][j-1]>=c[i-1][j])
print(i,j-1);
else
print(i-1,j);
return;
}
Here you can find a recursive approach of how to do this: Reading out all LCSs
Here is my code for this approach in Java:
private Set<String> lcs(int[][] dp, String fst, String snd, int i, int j) {
Set<String> lcss = new HashSet<>();
if (i == 0 || j == 0) {
lcss.add("");
} else if (fst.charAt(i - 1) == snd.charAt(j - 1)) {
for (String lcs : lcs(dp, fst, snd, i - 1, j - 1)) {
lcss.add(lcs + fst.charAt(i - 1));
}
} else {
if (dp[i - 1][j] >= dp[i][j - 1]) {
lcss.addAll(lcs(dp, fst, snd, i - 1, j));
}
if (dp[i][j - 1] >= dp[i - 1][j]) {
lcss.addAll(lcs(dp, fst, snd, i, j - 1));
}
}
return lcss;
}
Here is the Java code with comments explaining how to print all possible lcs.
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class LongestCommonSubsequence {
public static int[][] LCSmatrix(String X, String Y) {
//we ignore the top most row and left most column in this matrix
//so we add 1 and create a matrix with appropriate row and column size
int m = X.length() + 1, n = Y.length() + 1;
int[][] c = new int[m][n];
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
//since we added 1 to row size and column size,
// we substract 1 from i,j to find the char at that index
if (X.charAt(i - 1) == Y.charAt(j - 1)) {
c[i][j] = c[i - 1][j - 1] + 1;
} else if (c[i - 1][j] >= c[i][j - 1]) {
c[i][j] = c[i - 1][j];
} else {
c[i][j] = c[i][j - 1];
}
}
}
printMatrix(c);
return c;
}
public static void printMatrix(int[][] grid) {
for (int r = 0; r < grid.length; r++) {
for (int c = 0; c < grid[r].length; c++) {
System.out.print(grid[r][c] + " ");
}
System.out.println();
}
}
public static void allLCS(int[][] c, String X, String Y, int i, int j, Set<String> setLCS, String s) {
//return when either of the string length is 0
if (i == 0 || j == 0) {
setLCS.add(s);
return;
}
//if last characters are equal, they belong in lcs
if (X.charAt(i - 1) == Y.charAt(j - 1)) {
//prepend the char to lcs since, we are going backwards
s = X.charAt(i - 1) + s;
//continue finding lcs in substrings X.substring(0,i-1) and Y.substring(0,j-1)
allLCS(c, X, Y, i - 1, j - 1, setLCS, s);
} // if there is a tie in matrix cells, we backtrack in both ways,
// else one way, which ever is greater
else if (c[i - 1][j] == c[i][j - 1]) {
//continue finding lcs in substring X.substring(0,i-1)
allLCS(c, X, Y, i - 1, j, setLCS, s);
//continue finding lcs in substring Y.substring(0,j-1)
allLCS(c, X, Y, i, j - 1, setLCS, s);
} else if (c[i - 1][j] > c[i][j - 1]) {
allLCS(c, X, Y, i - 1, j, setLCS, s);
} else {
allLCS(c, X, Y, i, j - 1, setLCS, s);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(" Enter String X and Y : ");
String X = sc.next();
String Y = sc.next();
sc.close();
Set<String> set = new HashSet<String>();
allLCS(LCSmatrix(X, Y), X, Y, X.length(), Y.length(), set, "");
System.out.println(set.toString());
}
}
class Solution
{
public int function1(String s,String t,int n,int m,int dp[][]){
if(n==0 || m==0){
return 0;
}
if(dp[n][m]!=-1){
return dp[n][m];
}
if(s.charAt(n-1)==t.charAt(m-1)){
return dp[n][m]=1+function1(s,t,n-1,m-1,dp);
}
return dp[n][m]=Math.max(function1(s,t,n-1,m,dp),function1(s,t,n,m-1,dp));
}
public HashSet<String> function2(String s,String t,int n,int m,int dp[][],HashMap<String,HashSet<String>> map){
HashSet<String> temp=new HashSet<String>();
String key=n+"-"+m;
if(n==0 || m==0){
temp.add("");
return temp;
}
if(map.containsKey(key)){
return map.get(key);
}
if(s.charAt(n-1)==t.charAt(m-1)){
for(String tempstr:function2(s,t,n-1,m-1,dp,map)){
temp.add(tempstr+s.substring(n-1,n));
}
}
else{
if(dp[n-1][m]>=dp[n][m-1]){
temp.addAll(function2(s,t,n-1,m,dp,map));
}
if(dp[n-1][m]<=dp[n][m-1]){
temp.addAll(function2(s,t,n,m-1,dp,map));
}
}
map.put(key,temp);
return temp;
}
public List<String> all_longest_common_subsequences(String s, String t)
{
int n=s.length();
int m=t.length();
int dp[][]=new int[n+1][m+1];
for(int i=0;i<=n;i++){
for(int j=0;j<=m;j++){
dp[i][j]=-1;
}
}
function1(s,t,n,m,dp);
HashMap<String,HashSet<String>> map=new HashMap<String,HashSet<String>>();
ArrayList<String> ans=new ArrayList<String>(function2(s,t,n,m,dp,map));
Collections.sort(ans);
return ans;
}
}
Your source code is not printing the lcs. It is actually calculating the length of lcs. Source code given by you is totally wrong. First try to print one lcs. Then extend that solution to print all the lcs. For your help given below is working java solution.
static int arr[][];
static void lcs(String s1, String s2) {
for (int i = 1; i <= s1.length(); i++) {
for (int j = 1; j <= s2.length(); j++) {
if (s1.charAt(i - 1) == s2.charAt(j - 1))
arr[i][j] = arr[i - 1][j - 1] + 1;
else
arr[i][j] = Math.max(arr[i - 1][j], arr[i][j - 1]);
}
}
}
static Set<String> lcs(String s1, String s2, int len1, int len2) {
if (len1 == 0 || len2 == 0) {
Set<String> set = new HashSet<String>();
set.add("");
return set;
}
if (s1.charAt(len1 - 1) == s2.charAt(len2 - 1)) {
Set<String> set = lcs(s1, s2, len1 - 1, len2 - 1);
Set<String> set1 = new HashSet<>();
for (String temp : set) {
temp = temp + s1.charAt(len1 - 1);
set1.add(temp);
}
return set1;
} else {
Set<String> set = new HashSet<>();
Set<String> set1 = new HashSet<>();
if (arr[len1 - 1][len2] >= arr[len1][len2 - 1]) {
set = lcs(s1, s2, len1 - 1, len2);
}
if (arr[len1][len2 - 1] >= arr[len1 - 1][len2]) {
set1 = lcs(s1, s2, len1, len2 - 1);
}
for (String temp : set) {
set1.add(temp);
}
//System.out.println("In lcs" + set1);
return set1;
}
}
public static void main(String[] args) {
String s1 = "abcbdab";
String s2 = "bdcaba ";
arr = new int[s1.length() + 1][s2.length() + 1];
lcs(s1, s2);
System.out.println(lcs(s1, s2, s1.length(), s2.length()));
}
If last character of strings are equal then they must be in lcs. If they are not equal lcs will be either constructed from upper side of matrix or left side of matrix depending upon which value is greater. If both the value is equal then lcs will be constructed from both the side. So keep constructing the lcs until you have constructed all the lcs and store them in a set.

Resources