Thread: Passing linked list by value.

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,343
    Quote Originally Posted by amohammed63
    Here how I can implement it, I know it's wrong but that's how can I do it all.
    Wait. This is an extremely simple version of the complex problem that you're trying to solve. If you cannot implement the simple version, then you have no hope of implementing the complex version, so you really need to work on this first.

    Let's try again. Get it to work, and post your code. Your code should have an output statement so you can check the result. You need to implement multiply too, and use both add and multiply in the main function, in the same way as you did in your actual code.

    Don't assign a test value ("to check if the data is changed or not") in the add function, but rather put the print statements into the main function, and say what was your test input, expected output, and actual output.

    In other words, I want to have a look -- at a very simplified level -- into what you are doing. This way it becomes possible to understand what you're doing wrong, and what you might be doing right.
    Last edited by laserlight; 04-07-2021 at 04:31 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User
    Join Date
    Apr 2020
    Posts
    30
    Quote Originally Posted by laserlight View Post
    Wait. This is an extremely simple version of the complex problem that you're trying to solve. If you cannot implement the simple version, then you have no hope of implementing the complex version, so you really need to work on this first.

    Let's try again. Get it to work, and post your code. Your code should have an output statement so you can check the result. You need to implement multiply too, and use both add and multiply in the main function, in the same way as you did in your actual code.

    Don't assign a test value ("to check if the data is changed or not") in the add function, but rather put the print statements into the main function, and say what was your test input, expected output, and actual output.

    In other words, I want to have a look -- at a very simplified level -- into what you are doing. This way it becomes possible to understand what you're doing wrong, and what you might be doing right.
    I'm a beginner, I was searching for videos to show what I'm looking for and I didn't found anything. I have two simple implementations of single linked list but both do the same behavior. I came here to ask for help in understanding how can I do it not to get a code. I was trying since half an hour, but I didn't get it working as it should.

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,343
    Quote Originally Posted by amohammed63
    I came here to ask for help in understanding how can I do it not to get a code. I was trying since half an hour, but I didn't get it working as it should.
    So what I am suggesting is this: attempt a simpler version of what you're trying to solve, and show us how you do it. Forget about linked lists for now, and focus on the notion of implementing addition and multiplication for a number type and using them together. If you can do that correctly, then you can think about how to do the same thing, but on a more complex scale with linked lists involved.

    The thing is that right now unless you're willing to show your full code, and unless someone is really so kind as to dissect your full code for you, it is very hard to help you. Take a look at what you wrote:
    Quote Originally Posted by amohammed63
    When I perform addition, the result is correct. Once I perform multiplication after addition, the output is wrong.
    Looking at this statement, I cannot really say anything useful. I can tell you that obviously you need to debug, but you already know that...

    Quote Originally Posted by amohammed63
    I was following the linked list data using debugger I noticed that some of the data are lost. And, in addition I'm using variable1 and variable2 which are pointers to the original equations, and I'm stuck in that point and I don't know how to skip it.
    So some of the data is lost. Obviously there's a bug there. But how am I to help you debug? I don't know your actual code, only incomplete snippets, I don't know what's being lost etc. Futile to help you. That's why I'm suggesting that you attempt a simplified version of your problem and show us. If you refuse to do that, and you refuse to show your actual code and provide all the details, then good luck. Your best bet is to seek help with your instructors instead, and anyway that's what you're paying them for.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Registered User
    Join Date
    Apr 2020
    Posts
    30
    Code:
    #include<stdio.h>#include<stdlib.h>
    
    typedef struct number
    {
        int value;
    } number;
    
    number *add(number *x, number *y);
    number *multiply(number *x, number *y);
    int main() {
        number x, y;
    x.value = 5;
    y.value = 5;
    number *result1, *result2;
    result1 = add(&x, &y);
    result2 = multiply(&x, &y);
    printf("%d + %d = %d\n", x.value, y.value, result1->value);
    printf("%d * %d = %d", x.value, y.value, result2->value);
        return 0;
    }
    number *add(number *x, number *y){
        number *temp = malloc((sizeof (*temp)));
    (*temp).value = x->value + y->value;
        return temp;
    
    }
    number *multiply(number *x, number *y){
        number *temp = malloc((sizeof (*temp)));
    (*temp).value = x->value * y->value;
        return temp;
    }
    Here's a full code of what you requested previously. I can implement this, and I understand it but I don't know how to deal with linked lists. I can show you the code but I need to put it privately on Drive, if it's fine I can do it. Also, regarding to the bugs. If it's called a bug, I said it before, during operations I delete each node I perform operation on until the end of each linked list. So that makes the data loss.

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,343
    Quote Originally Posted by amohammed63
    Here's a full code of what you requested previously. I can implement this, and I understand it but I don't know how to deal with linked lists.
    That's great! If we apply what you wrote to your more complex problem with linked lists though, what you're doing is equivalent to doing deep copying. Granted, in this simplified version it is shallow and isn't really copying, but essentially that's the equivalent when your linked lists are involved.

    What I thought you might be going for was something like this:
    Code:
    number *add(number *x, number *y) {
        x->value += y->value;
        return x;
    }
    
    number *multiply(number *x, number *y) {
        x->value *= y->value;
        return x;
    }
    So instead of allocating space for a new number, we just change the first argument. If you don't need the original value of the first argument, this would be more efficient.

    Quote Originally Posted by amohammed63
    I can show you the code but I need to put it privately on Drive
    No, you don't. You can put it here, and if someone copies it, you have proof right here that you wrote it. Other community members might be interested to help you too, and it effectively becomes a matter of public record that you sought help to do your own work rather than getting someone else to do it for you.

    Quote Originally Posted by amohammed63
    Also, regarding to the bugs. If it's called a bug, I said it before, during operations I delete each node I perform operation on until the end of each linked list. So that makes the data loss.
    Given what you showed me for the simplified example, I'm more convinced that you need to do deep copying.

    There's another thing to think about: get your code working, even if it is inefficient, and then improve it. Should you fail to improve it, at least you can submit your working code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,706
    @amohammed63, please use "copy as text" and/or "paste as text" options of your IDE / browser.
    Your posts are unformatted and unreadable with the mix of pastel colours.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #22
    Registered User
    Join Date
    Apr 2020
    Posts
    30
    Quote Originally Posted by laserlight View Post
    That's great! If we apply what you wrote to your more complex problem with linked lists though, what you're doing is equivalent to doing deep copying. Granted, in this simplified version it is shallow and isn't really copying, but essentially that's the equivalent when your linked lists are involved.

    What I thought you might be going for was something like this:
    Code:
    number *add(number *x, number *y) {
        x->value += y->value;
        return x;
    }
    
    number *multiply(number *x, number *y) {
        x->value *= y->value;
        return x;
    }
    So instead of allocating space for a new number, we just change the first argument. If you don't need the original value of the first argument, this would be more efficient.


    No, you don't. You can put it here, and if someone copies it, you have proof right here that you wrote it. Other community members might be interested to help you too, and it effectively becomes a matter of public record that you sought help to do your own work rather than getting someone else to do it for you.


    Given what you showed me for the simplified example, I'm more convinced that you need to do deep copying.

    There's another thing to think about: get your code working, even if it is inefficient, and then improve it. Should you fail to improve it, at least you can submit your working code.
    AAAAH, I'm really tired. In my project I have a menu and the user choices is unlimited so that deep copying does not work since that the copy of the linked list is also affected, if the user chooses addition for example, and then wants to repeat it once again, the program crashes.

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,343
    I don't see why that is so.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #24
    Registered User
    Join Date
    Apr 2020
    Posts
    30
    Here's my code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <math.h>
    
    
    //add 40
    //sub -30
    //multi 481950000
    typedef struct node {
        double coefficient, power;
        struct node *Previous, *Next;
    } *equationList;
    
    
    typedef equationList node;
    typedef equationList resultList;
    
    
    node MakeEmpty(node list);
    
    
    void insert(equationList list, node *P, double power, double coefficient);
    
    
    void DeleteList(equationList list);
    
    
    node delete(node list, node P);
    
    
    equationList readFile(char line[50]);
    
    
    void printToFile(FILE *outFile, resultList results[], int numOfResults);
    
    
    resultList addition(equationList equation[], int numOfEquations);
    
    
    resultList multiplication(equationList equation[], int numOfEquations);
    
    
    resultList subtraction(equationList equation[], int numOfEquations);
    
    
    void printEquations(equationList list[50], int count);
    
    
    node find(double X, equationList equation2);
    
    
    resultList findEquivalent(resultList result);
    
    
    double valueOfEquation(resultList result, float x);
    
    
    void printResult(resultList result);
    
    
    int isFirst(equationList list, node P);
    
    
    int isLast(equationList list, node P);
    
    
    int listWithOneNode(node list);
    
    
    void copy(equationList list, equationList list1, equationList list2, equationList list3){
        equationList P = list->Next, tempNode1 = NULL, tempNode2 = NULL, tempNode3 = NULL;
        tempNode1 = MakeEmpty(tempNode1);
        tempNode2 = MakeEmpty(tempNode2);
        tempNode3 = MakeEmpty(tempNode3);
        while (P != NULL){
            insert(list1, &tempNode1, P->power, P->coefficient);
            insert(list2, &tempNode2, P->power, P->coefficient);
            insert(list3, &tempNode3, P->power, P->coefficient);
            P = P->Next;
        }
    }
    
    
    int main() {
        FILE *inFile, *outFile;
        equationList list[9999], list1[9999] = {0}, list2[9999] = {0} , list3[9999] = {0};
        resultList results[50];
        char line[50] = {0};
        double valueOfResults[50] = {0};
        int option = 0, operation = 0, numOfLists = 0, numOfResults = 0, flag1 = 0, flag2 = 0,
                flag3 = 0, flag4 = 0, temp = 0;
        float x = 0;
        inFile = fopen("C:\\Users\\alima\\CLionProjects\\P1_1190502_AliMari_4\\equations.txt", "r");
        outFile = fopen("C:\\Users\\alima\\CLionProjects\\P1_1190502_AliMari_4\\results.txt", "w");
        if (inFile == NULL) {
            printf("The file \"equations.txt\" is not found.\n");
        }
        while (1) {
            printf("----------------------\n");
            printf("Menu:\n");
            printf("1. Read the file\n");
            printf("2. Show equations\n");
            printf("3. Select operation\n");
            printf("4. Save results\n");
            printf("5. Exit\n");
            printf("----------------------\n");
            scanf("%d", &option);
            switch (option) {
                case 1:
                    if (flag1 == 1) {
                        printf("No need to read the file again.\n");
                        break;
                    }
                    for (int i = 0; !feof(inFile); i++) {
                        fgets(line, 50, inFile);
                        if (line[0] == '\n') {
                            i--;
                            continue;
                        }
                        list1[i] = MakeEmpty(list1[i]);
                        list2[i] = MakeEmpty(list2[i]);
                        list3[i] = MakeEmpty(list3[i]);
                        list[i] = readFile(line);
                        copy(list[i], list1[i], list2[i], list3[i]);
                        if (list[i] == NULL) {
                            i--;
                        }
                        numOfLists = i;
                    }
                    flag1 = 1;
                    printf("File read successfully.\n");
                    fclose(inFile);
                    break;
                case 2:
                    if (flag1 != 1) {
                        printf("You must read the file first.\n");
                        break;
                    }
                    printf("\n");
                    printEquations(list, numOfLists + 1);
                    printf("\n");
                    flag3 = 1;
                    break;
                case 3:
                    if (flag1 != 1) {
                        printf("You must read the file first.\n");
                        break;
                    }
                    if (flag3 != 1) {
                        printf("You didn't show the equations, show them firstly then select operations, write (1) to show them\n");
                        scanf("%d", &temp);
                        if (temp == 1) {
                            printEquations(list, numOfLists + 1);
                            printf("\n");
                        } else {
                            printf("Sorry! You can not select any operation, try it after showing the equations\n");
                            break;
                        }
                    }
                    printf("Select one of the three operations:\n");
                    printf("\t31. Add equations\n");
                    printf("\t32. Multiply equations\n");
                    printf("\t33. Subtract equations\n");
                    scanf("%d", &operation);
                    if (operation == 31) {
                        results[numOfResults] = addition(list1, numOfLists + 1);
                    } else if (operation == 32) {
                        results[numOfResults] = multiplication(list2, numOfLists + 1);
                    } else if (operation == 33) {
                        results[numOfResults] = subtraction(list3, numOfLists + 1);
                    } else {
                        printf("The selected operation is not available.\n");
                        break;
                    }
                    printf("The final equation: ");
                    printResult(results[numOfResults]);
                    printf("Do you want to add value to the variable x? (1) Yes, (0) No\n");
                    scanf("%d", &temp);
                    if (temp == 1) {
                        x = 0;
                        printf("Enter the value of x: ");
                        scanf("%f", &x);
                        printf("\n");
                        valueOfResults[numOfLists] = valueOfEquation(results[numOfResults], x);
                        printf("The value of the equation is: %.4f\n", valueOfResults[numOfLists]);
                    }
                    flag4 = 1;
                    numOfResults++;
                    break;
                case 4:
                    if (flag1 != 1) {
                        printf("You must read the file first.\n");
                        break;
                    }
                    if (flag4 != 1) {
                        printf("There are no results to be saved.\n");
                        break;
                    }
                    printToFile(outFile, results, numOfResults);
                    fclose(outFile);
                    printf("Results are printed to the file successfully!\n");
                    flag2 = 1;
                    break;
                case 5:
                    if (flag2 != 1) {
                        printf("Your results are not saved to the file, are you sure? (1) Yes, (0) No.\n");
                        scanf("%d", &temp);
                        if (temp == 1) {
                            fclose(outFile);
                        } else {
                            printf("You are leaving without saving the data, no data will be shown!\n");
                        }
                    }
    
    
                    exit(0);
                default:
                    printf("The selected item is not supported in the menu.\n");
            }
        }
    }
    
    
    node MakeEmpty(node list) {
        if (list != NULL)
            DeleteList(list);
        list = (struct node *) malloc(sizeof(struct node));
        list->Previous = NULL;
        list->Next = NULL;
        if (list == NULL)
            printf("Out of memory!\n");
        return list;
    }
    
    
    void insert(equationList list, node *P, double power, double coefficient) {
        node temp = NULL;
        temp = MakeEmpty(temp);
        node te = NULL;
        if (list->Next == NULL) {
            list->Next = temp;
            temp->Previous = list;
            temp->power = power;
            temp->coefficient = coefficient;
            *P = temp;
        } else {
            te = *P;
            te->Next = temp;
            temp->Previous = te;
            temp->power = power;
            temp->coefficient = coefficient;
            temp->Next = NULL;
            *P = temp;
        }
    
    
    }
    
    
    void DeleteList(equationList list) {
        equationList P, temp;
        if (list != NULL) {
            if (list->Next != NULL && list->Previous == NULL) {
                P = list->Next;
                P->Previous = NULL;
                list->Next = NULL;
                list->Previous = NULL;
                while (P != NULL) {
                    temp = P->Next;
                    P->Previous = NULL;
                    free(P);
                    P = temp;
                }
            }
        }
    }
    
    
    int isFirst(equationList list, node P) {
        if (list != NULL)
            return list == P;
        return 0;
    }
    
    
    int isLast(equationList list, node P) {
        node tempList = list, lastNode = NULL;
        while (tempList != NULL) {
            if (tempList->Next == NULL) {
                lastNode = tempList;
            }
            tempList = tempList->Next;
        }
        return lastNode == P;
    }
    
    
    int listWithOneNode(node list) {
        if (list != NULL)
            return list->Previous == NULL && list->Next == NULL;
        return 0;
    }
    
    
    node delete(node list, node P) {
        node previous, next, tempList = NULL;
        if (isFirst(list, P) && !listWithOneNode(list)) {
            tempList = P->Next;
            tempList->Previous = NULL;
            free(P);
            return tempList;
        } else if (isLast(list, P) && list != NULL) {
            if (list->Next == NULL) {
                list = NULL;
                return list;
            }
            previous = P->Previous;
            previous->Next = NULL;
            free(P);
            return list;
        } else if (listWithOneNode(list)) {
            tempList = NULL;
            return tempList;
        } else {
            if (list != NULL) {
                previous = P->Previous;
                next = P->Next;
                previous->Next = next;
                next->Previous = previous;
                free(P);
            }
            return list;
        }
    }
    
    
    void printToFile(FILE *outFile, resultList results[], int numOfResults) {
        equationList P;
        for (int i = 0; i < numOfResults; i++) {
            P = (results[i])->Next;
            fprintf(outFile, "Result of operation (%d): %.2fx^%.2f", (i + 1), P->coefficient, P->power);
            P = P->Next;
            while (P != NULL) {
                if (P->coefficient > 0) {
                    if (P->power == 0) {
                        fprintf(outFile, "+%.2f", P->coefficient);
                    } else if (P->power == 1) {
                        fprintf(outFile, "+%.2fx", P->coefficient);
                    } else {
                        fprintf(outFile, "+%.2fx^%.2f", P->coefficient, P->power);
                    }
                } else {
                    if (P->power == 0) {
                        fprintf(outFile, "%.2f", P->coefficient);
                    } else if (P->power == 1) {
                        fprintf(outFile, "%.2fx", P->coefficient);
                    } else {
                        fprintf(outFile, "%f.2x^%.2f", P->coefficient, P->power);
                    }
                }
                P = P->Next;
            }
            fprintf(outFile, "\n");
        }
    }
    
    
    void printResult(resultList result) {
        if (result != NULL) {
            equationList P = result->Next;
            printf("%.2fx^%.2f", P->coefficient, P->power);
            P = P->Next;
            while (P != NULL) {
                if (P->coefficient > 1) {
                    if (P->power == 0) {
                        printf("+%.2f", P->coefficient);
                    } else if (P->power == 1) {
                        printf("+%.2fx", P->coefficient);
                    } else {
                        printf("+%.2fx^%.2f", P->coefficient, P->power);
                    }
                } else if (P->coefficient == 1 || P->coefficient == -1) {
                    if (P->power == 0) {
                        if (P->coefficient == 1)
                            printf("+");
                        if (P->coefficient == -1)
                            printf("-");
                    } else if (P->power == 1) {
                        if (P->coefficient == 1)
                            printf("+x");
                        if (P->coefficient == -1)
                            printf("-x");
                    } else {
                        if (P->coefficient == 1)
                            printf("+x^%.2f", P->power);
                        if (P->coefficient == -1)
                            printf("-x^%.2f", P->power);
                    }
                } else {
                    if (P->power == 0) {
                        printf("%.2f", P->coefficient);
                    } else if (P->power == 1) {
                        printf("%.2fx", P->coefficient);
                    } else {
                        printf("%.2fx^%.2f", P->coefficient, P->power);
                    }
                }
                P = P->Next;
            }
            printf("\n");
        }
    }
    
    
    void printEquations(equationList list[50], int count) {
        equationList P;
        for (int i = 0; i < count; i++) {
            P = (list[i])->Next;
            printf("(%d) %.2fx^%.2f", (i + 1), P->coefficient, P->power);
            P = P->Next;
            while (P != NULL) {
                if (P->coefficient > 0) {
                    if (P->power == 0) {
                        printf("+%.2f", P->coefficient);
                    } else if (P->power == 1) {
                        printf("+%.2fx", P->coefficient);
                    } else {
                        printf("+%.2fx^%.2f", P->coefficient, P->power);
                    }
                } else {
                    if (P->power == 0) {
                        printf("%.2f", P->coefficient);
                    } else if (P->power == 1) {
                        printf("%.2fx", P->coefficient);
                    } else {
                        printf("%.2fx^%.2f", P->coefficient, P->power);
                    }
                }
                P = P->Next;
            }
            printf("\n");
        }
    }
    
    
    node find(double X, equationList equation2) {
        node P = equation2, temp = P;
        while (P != NULL) {
            if (P->power == X) {
                temp = P;
                break;
            }
            P = P->Next;
            temp = NULL;
        }
        P = temp;
        return P;
    }
    
    
    resultList findEquivalent(resultList result) {
        if (result != NULL && result->Next != NULL) {
            node variable1 = result->Next, P, temp;
            while (variable1->Next != NULL) {
                P = find(variable1->power, variable1->Next);
                while (P != NULL && P->Next != NULL) {
                    variable1->coefficient += P->coefficient;
                    temp = P;
                    P->Previous->Next = P->Next;
                    temp->Next->Previous = temp->Previous;
                    free(temp);
                    P = find(variable1->power, variable1->Next);
                }
                variable1 = variable1->Next;
            }
        }
        return result;
    }
    
    
    double valueOfEquation(resultList result, float x) {
        double value = 0;
        node P = result->Next;
        while (P != NULL) {
            value += P->coefficient * pow(x, P->power);
            P = P->Next;
        }
    
    
        return value;
    }
    
    
    equationList readFile(char line[50]) {
        equationList list = NULL, tempNode = NULL;
        list = MakeEmpty(list);
        tempNode = MakeEmpty(tempNode);
        char *tokens;
        double power = 0, coefficient = 0;
    
    
        char temp[50] = {0}, tempLine[50] = {0};
        //to delete all whitespaces in the equation
        for (int i = 0, j = 0; i < strlen(line); i++) {
            if (!isspace(line[i])) {
                tempLine[j++] = line[i];
                continue;
            }
        }
    
    
        for (int i = 0, j = 0; i < strlen(tempLine); i++) {
            if ((tempLine[i] == '-' || tempLine[i] == '+') && tempLine[i + 1] == 'x' && !isdigit(tempLine[i])) {
                temp[j++] = tempLine[i];
                temp[j++] = '1';
                temp[j++] = tempLine[++i];
                continue;
            }
            if (i == 0 && (tempLine[i] == 'x' || tempLine[i + 1] == 'x') && !isdigit(tempLine[i])) {
                if (tempLine[i] == 'x') {
                    temp[j++] = '1';
                    temp[j++] = tempLine[i];
                    continue;
                }
                if (tempLine[i + 1] == 'x') {
                    temp[j++] = temp[i];
                    temp[j++] = '1';
                    temp[j++] = temp[++i];
                    continue;
                }
            }
            temp[j++] = tempLine[i];
        }
        strcpy(tempLine, temp);
        for (int i = 0; i < strlen(tempLine); i++) {
            if (tempLine[i] == 'x') {
                continue;
            } else if (isdigit(tempLine[i])) {
                continue;
            } else if (tempLine[i] == '-' || tempLine[i] == '+') {
                continue;
            } else if (tempLine[i] == '^' && tempLine[i - 1] == 'x') {
                continue;
            } else {
                printf("Sorry! This equation (%s) is excluded from all operations because it has invalid character.\n",
                       tempLine);
                return NULL;
            }
        }
        //to add whitespace after each variable
        for (int i = 0, j = 0; i < strlen(tempLine);) {
            if (tempLine[i] == '+' || tempLine[i] == '-') {
                temp[j++] = tempLine[i++];
            }
            while (isdigit(tempLine[i])) {
                temp[j++] = tempLine[i++];
            }
            if (tempLine[i] == 'x') {
                if (i == 0 || tempLine[i - 1] == '+' || tempLine[i - 1] == '-') {
                    temp[j++] = '1';
                    temp[j++] = tempLine[i++];
                } else {
                    temp[j++] = tempLine[i++];
                }
            } else {
                temp[j++] = 'x';
                temp[j++] = '^';
                temp[j++] = '0';
                temp[j++] = ' ';
                continue;
            }
            if (tempLine[i] == '^') {
                temp[j++] = tempLine[i++];
                if (tempLine[i] == '-' || tempLine[i] == '+') {
                    temp[j++] = tempLine[i++];
                }
            } else {
                temp[j++] = '^';
                temp[j++] = '1';
                temp[j++] = ' ';
                continue;
            }
            while (isdigit(tempLine[i])) {
                temp[j++] = tempLine[i++];
            }
            temp[j++] = ' ';
        }
    
    
        tokens = strtok(temp, "x^ ");
        sscanf(tokens, "%lf", &coefficient);
    
    
        tokens = strtok(NULL, "x^ ");
        sscanf(tokens, "%lf", &power);
        insert(list, &tempNode, power, coefficient);
        //to split each variable and add its coefficient and power to the linked list
        while (tokens != NULL) {
            tokens = strtok(NULL, "x^ ");
            if (tokens == NULL) {
                break;
            }
            sscanf(tokens, "  %lf  ", &coefficient);
            tokens = strtok(NULL, "x^ ");
            if (tokens != NULL) {
                sscanf(tokens, " %lf ", &power);
    
    
            } else {
                power = 0;
            }
            insert(list, &tempNode, power, coefficient);
        }
        return list;
    }
    
    
    resultList addition(equationList equation[], int numOfEquations) {
        if (numOfEquations == 1) {
            return equation[0];
        } else {
            node variable1, variable2, x1, x2, tempNode = NULL;
            resultList results[9999] = {0};
            int numOfResults = 0;
            tempNode = MakeEmpty(tempNode);
            for (int i = 0; i < numOfEquations; i += 2, numOfResults++) {
                if (i == numOfEquations - 1) {
                    results[numOfResults] = equation[i];
                    continue;
                }
                results[numOfResults] = MakeEmpty(results[numOfResults]);
                variable1 = equation[i]->Next;
                variable2 = equation[i + 1]->Next;
                variable1->Previous = NULL, variable2->Previous = NULL;
                while (variable1 != NULL || variable2 != NULL) {
                    if (variable1 != NULL)
                        x1 = find(variable1->power, variable2);
                    else
                        x1 = NULL; //IMPORTANTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
                    if (variable2 != NULL)
                        x2 = find(variable2->power, variable1);
                    else
                        x2 = NULL;
                    if (x1 == NULL && x2 == NULL) {
                        if (variable1 != NULL) {
                            insert(results[numOfResults], &tempNode, variable1->power, variable1->coefficient);
                        }
                        if (variable2 != NULL) {
                            insert(results[numOfResults], &tempNode, variable2->power, variable2->coefficient);
                        }
                        variable1 = delete(variable1, variable1);
                        variable2 = delete(variable2, variable2);
                        continue;
                    }
                    if (x2 != NULL && x1 == NULL) {
                        if (variable1 != NULL) {
                            insert(results[numOfResults], &tempNode, variable1->power, variable1->coefficient);
                        }
                        if (variable2 != NULL) {
                            insert(results[numOfResults], &tempNode, variable2->power,
                                   variable2->coefficient + x2->coefficient);
                        }
                        variable1 = delete(variable1, x2);
                        variable1 = delete(variable1, variable1);
                        variable2 = delete(variable2, variable2);
                        continue;
                    }
                    if (x2 == NULL && x1 != NULL) {
                        if (variable1 != NULL) {
                            insert(results[numOfResults], &tempNode, variable1->power,
                                   variable1->coefficient + x1->coefficient);
                        }
                        if (variable2 != NULL) {
                            insert(results[numOfResults], &tempNode, variable2->power, variable2->coefficient);
                        }
                        variable2 = delete(variable2, x1);
                        variable2 = delete(variable2, variable2);
                        variable1 = delete(variable1, variable1);
                        continue;
                    }
                    if (x1->power == x2->power) {
                        if (variable1 != NULL) {
                            insert(results[numOfResults], &tempNode, x1->power, x1->coefficient + x2->coefficient);
                        }
                        variable1 = delete(variable1, x2);
                        variable2 = delete(variable2, x1);
                        continue;
                    }
                    if (x2 != NULL && x1 != NULL) {
                        if (variable1 != NULL) {
                            insert(results[numOfResults], &tempNode, variable1->power,
                                   variable1->coefficient + x1->coefficient);
                        }
                        if (variable2 != NULL) {
                            insert(results[numOfResults], &tempNode, variable2->power,
                                   variable2->coefficient + x2->coefficient);
                        }
                        variable1 = delete(variable1, x2);
                        variable2 = delete(variable2, x1);
                        variable2 = delete(variable2, variable2);
                        variable1 = delete(variable1, variable1);
                        continue;
                    }
                }
    
    
            }
            return addition(results, numOfResults);
        }
    }
    
    
    resultList multiplication(equationList equation[], int numOfEquations) {
        if (numOfEquations == 1) {
            return equation[0];
        } else {
            resultList results[50] = {0};
            int numOfResults = 0;
            node equation1 = NULL, equation2 = NULL, P1 = NULL, P2 = NULL, tempNode = NULL;
            for (int i = 0; i < numOfEquations; i += 2, numOfResults++) {
                if (i == numOfEquations - 1) {
                    results[numOfResults] = equation[i];
                    continue;
                }
                tempNode = MakeEmpty(tempNode);
                equation1 = equation[i];
                equation2 = equation[i + 1];
                results[numOfResults] = MakeEmpty(results[numOfResults]);
                P1 = equation1->Next;
                P2 = equation2->Next;
                while (P1 != NULL) {
                    while (P2 != NULL) {
                        insert(results[numOfResults], &tempNode, P1->power + P2->power, P1->coefficient * P2->coefficient);
                        P2 = P2->Next;
                    }
                    P2 = equation[i + 1]->Next;
                    P1 = P1->Next;
                }
                results[numOfResults] = findEquivalent(results[numOfResults]);
            }
            return multiplication(results, numOfResults);
        }
    }
    
    
    resultList subtraction(equationList equation[], int numOfEquations) {
        if (numOfEquations == 1) {
            return equation[0];
        } else {
            node variable1, variable2, x1, x2, tempNode = NULL;
            resultList results[50] = {0};
            int numOfResults = 0, flag = 1;
            tempNode = MakeEmpty(tempNode);
            for (int i = 0; i < numOfEquations; i += 2, numOfResults++) {
                if (i == numOfEquations - 1) {
                    results[numOfResults] = MakeEmpty(results[numOfResults]);
                    insert(results[numOfResults], &tempNode, (equation[i]->Next)->power, -(equation[i]->Next)->coefficient);
                    continue;
                }
                if (i != 0){
                    flag = -1;
                }
                results[numOfResults] = MakeEmpty(results[numOfResults]);
                variable1 = equation[i]->Next;
                variable2 = equation[i + 1]->Next;
                variable1->Previous = NULL, variable2->Previous = NULL;
                while (variable1 != NULL || variable2 != NULL) {
                    if (variable1 != NULL)
                        x1 = find(variable1->power, variable2);
                    if (variable2 != NULL)
                        x2 = find(variable2->power, variable1);
                    else
                        x2 = NULL;
                    if (x1 == NULL && x2 == NULL) {
                        if (variable1 != NULL) {
                            insert(results[numOfResults], &tempNode, variable1->power, flag * variable1->coefficient);
                        }
                        if (variable2 != NULL) {
                            insert(results[numOfResults], &tempNode, variable2->power, -variable2->coefficient);
                        }
                        variable1 = delete(variable1, variable1);
                        variable2 = delete(variable2, variable2);
                        continue;
                    }
                    if (x2 != NULL && x1 == NULL) {
                        if (variable1 != NULL) {
                            insert(results[numOfResults], &tempNode, variable1->power, flag * variable1->coefficient);
                        }
                        if (variable2 != NULL) {
                            insert(results[numOfResults], &tempNode, variable2->power,
                                   (flag * x2->coefficient) - variable2->coefficient);
                        }
                        variable1 = delete(variable1, x2);
                        variable1 = delete(variable1, variable1);
                        variable2 = delete(variable2, variable2);
                        continue;
                    }
                    if (x2 == NULL && x1 != NULL) {
                        if (variable1 != NULL) {
                            insert(results[numOfResults], &tempNode, variable1->power,
                                   (flag * variable1->coefficient) - x1->coefficient);
                        }
                        if (variable2 != NULL) {
                            insert(results[numOfResults], &tempNode, variable2->power, -variable2->coefficient);
                        }
                        variable2 = delete(variable2, x1);
                        variable2 = delete(variable2, variable2);
                        variable1 = delete(variable1, variable1);
                        continue;
                    }
                    if (x1->power == x2->power) {
                        if (variable1 != NULL) {
                            insert(results[numOfResults], &tempNode, x1->power, -x1->coefficient + (flag * x2->coefficient));
                        }
                        variable1 = delete(variable1, x2);
                        variable2 = delete(variable2, x1);
                        continue;
                    }
                    if (x2 != NULL && x1 != NULL) {
                        if (variable1 != NULL) {
                            insert(results[numOfResults], &tempNode, variable1->power,
                                   (flag * variable1->coefficient) - x1->coefficient);
                        }
                        if (variable2 != NULL) {
                            insert(results[numOfResults], &tempNode, variable2->power,
                                   -variable2->coefficient + (flag * x2->coefficient));
                        }
                        variable1 = delete(variable1, x2);
                        variable2 = delete(variable2, x1);
                        variable2 = delete(variable2, variable2);
                        variable1 = delete(variable1, variable1);
                        continue;
                    }
                }
    
    
            }
            return addition(results, numOfResults);
        }
    }

  10. #25
    Registered User
    Join Date
    Apr 2020
    Posts
    30
    Quote Originally Posted by Salem View Post
    @amohammed63, please use "copy as text" and/or "paste as text" options of your IDE / browser.
    Your posts are unformatted and unreadable with the mix of pastel colours.
    Sorry for that, I posted my full code following your instructions.

  11. #26
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,706
    Do you have a short example of the input data file?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #27
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,706
    Code:
    foo.c: In function ‘subtraction’:
    foo.c:776:20: warning: ‘x1’ may be used uninitialized in this function [-Wmaybe-uninitialized]
                     if (x1 == NULL && x2 == NULL) {
    You make a point of setting x2 to NULL, but x1 is left dangling.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #28
    Registered User
    Join Date
    Apr 2020
    Posts
    30
    Thank you @Salem and @laserlight for helping, I solved the problem but editing the find function, it works on copying the data of the original linked list and performs all operation on the copy and returns the copy not the original list, due to this solution, the original data is saved.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-08-2014, 02:52 PM
  2. passing 3D array to a linked list.
    By satty in forum C Programming
    Replies: 8
    Last Post: 08-21-2010, 09:36 AM
  3. Passing a linked list to a function
    By esmeco in forum C Programming
    Replies: 6
    Last Post: 06-09-2010, 01:58 AM
  4. Passing an array to linked list
    By bar338 in forum C Programming
    Replies: 7
    Last Post: 04-08-2009, 09:15 PM
  5. Passing an array to a linked list
    By henry_kay in forum C Programming
    Replies: 7
    Last Post: 10-04-2007, 02:21 AM

Tags for this Thread