Having trouble on where and how to implement the linked list

This is a discussion on Having trouble on where and how to implement the linked list within the C Programming forums, part of the General Programming Boards category; Hello I have a problem set where i have to read in numbers from a file as strings, convert from ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    51

    Having trouble on where and how to implement the linked list

    Hello I have a problem set where i have to read in numbers from a file as strings, convert from strings to integers, and pass the integers into a linked list, where each integer is a node. I am unsure of how to go about doing this. Can anyone help me please? Thanks. This is what I have so far:

    Code:
    # include <stdio.h>
    # include <stdlib.h>
    
    # define MAX_INT_SIZE 10000
    
    typedef struct integer BigInt;
    struct integer
    {
        int digit;
        BigInt* next;
    };
    
    BigInt* makeBigInt(char* stringInt);
    void printBigInt(BigInt* p);
    BigInt* multiply(BigInt* p, BigInt* q);
    
    BigInt* makeBigInt(char* stringInt)
    {
        int size, i;
        size = strlen(stringInt);
        integer *head;
        for (i=0; i<size; i++)
        {
            /*FIRST NODE*/ = stringInt[size-i-1] - '0';
        }
    }
    
    void printBigInt(BigInt* p)
    {
    
    }
    
    int main()
    {
        FILE *ifp = fopen("bigint.txt", "r");
    
        if(!ifp)
        {
            printf("ERROR, ERROR! FILE IS NOT OPENING!");
        }
    
        int number_of_operations, i;
    
        char *integer1, *integer2;
        integer1 = malloc(sizeof(char*) *MAX_INT_SIZE);
        integer2 = malloc(sizeof(char*) *MAX_INT_SIZE);
    
        fscanf(ifp, "%d", &number_of_operations);
    
        for (i=0; i<number_of_operations; i++)
        {
            fscanf(ifp, "%s %s", integer1, integer2);
            //printf("Problem #%d: %s * %s = \n", i+1, integer1, integer2);
            struct integer* convertedInteger1 =  makeBigInt(integer1);
            struct integer* convertedInteger2 =  makeBigInt(integer2);
        }
    
        return 0;
    }
    Attached Images Attached Images

  2. #2
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Posts
    326
    What's with the MAX_INT_SIZE? The whole point of a linked list is you don't need to know how many items will be stored in it...

    Anyways, you need to read strings of digits from the file, putting each digit value into a linked list.

    First, write some functions that successfully scan the strings of digits from the file, and write them out manually, digit by digit. Only when you have this working, move to the linked list stuff.

    For linked list, you need a node for each digit, as well as an external "head" pointer that will point to the first node in the linked list. Set that to NULL to indicate an empty list. Then, change your old code that writes out digit strings, to instead add each digit t a linked list.

    The whole idea is to partition the code into small, easily understandable parts that do one thing. Then you integrate these parts to form the entire program.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    51
    Alright after stepping aside for a week, this is the code that I have so far:
    Code:
    # include <stdio.h>
    # include <stdlib.h>
    # include <string.h>
    
    typedef struct integer
    {
       int digit;
       struct integer *next;
    } BigInt;
    
    typedef struct Array
    {
        int *digits;
        int size;
    } stringArray;
    
    BigInt* makeBigInt(char* stringInt)
    {
        int i, length_of_string = strlen(stringInt);
    
        stringArray *p = malloc(sizeof(stringArray));
        p->size = length_of_string;
        p->digits = malloc(sizeof(int) * length_of_string);
    
        for (i=0; i<length_of_string; i++)
            p->digits[i] = stringInt[length_of_string - 1 - i] - '0';
    
        BigInt *head = NULL;
        BigInt *new_node = malloc(sizeof(BigInt));
    
        for(i=0; i<length_of_string; i++)
        {
            new_node->digit = p->digits[i];
            new_node->next = new_node->digit->next;
        }
    
        return head;
    }
    void printBigInt(BigInt* p)
    {
        int i;
    }
    
    int isZero(BigInt *p)
    {
    
    }
    
    BigInt* multiply(BigInt* p, BigInt* q)
    {
    
    }
    
    int main()
    {
        FILE *ifp = fopen("bigint.txt", "r");
    
        if(!ifp)
        {
            printf("The file cannot be opened!\n");
            return -1;
        }
    
        // buffer is what the temporary strings are read into...
        char buffer[10001];
        int i, number_of_operations;
        fscanf(ifp, "%d", &number_of_operations);
        printf("The number of operations is %d\n", number_of_operations);
    
        for(i=0; i< number_of_operations; i++)
        {
            // Scans the first integer into the temporary buffer.
            fscanf(ifp, "%s", buffer);
            printf("%s ", buffer);
    
            // Scans the second integer into the temporary buffer.
            fscanf(ifp, "%s", buffer);
            printf("%s\n", buffer);
    
        }
    
        fclose(ifp);
        return 0;
    }
    What I am trying to accomplish is reading in
    Code:
    27 10
    as strings, converting them into integers and then creating a linked list of
    Code:
    2 7 // and
    1 0
    in the MakeBigInt function, but I am getting an error that says
    Code:
    invalid type argument of '->' (have 'int')
    is there something wrong with my logic? A clue into what I can do to accomplish the MakeBigInt would be appreciated. Thanks for your help friends.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by C0__0D View Post
    in the MakeBigInt function, but I am getting an error that says
    Code:
    invalid type argument of '->' (have 'int')
    Always copy'n'paste the complete warnings/errors you get and tell us which line number in your post corresponds to the line number mentioned in the error message if the are not the same.
    Otherwise people who want to help can only guess which line is producing the error.

    I guess the culprit is line 34 in your code, right?
    What's the type of "new_node->digit"?

    Bye, Andreas

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    1
    Hi I am new to this site. I am in desperate need of some help! I am taking an intro to programming class.We are learning the if/then statements.

    Here is the question:
    Marcy's Department store is having a BoGoHo (Buy One, Get One Half Off) sale. The store manager wants a program that allows the salesclerk to enter the prices of two items. The program should both calculate and display the total amount the customer owes. The half off should always be taken on the item having the lowest price. For example, if the items cost $24.99 and $10, the half-off would be take on the $10 item.

    I basically need to program this to find out the total owed...

    so far here is what i have gotten which isnt very far

    #include <iostream>
    using namespace std;

    int main ()
    {
    // local variable declaration:
    double itemOne = 0.0;
    double itemTwo = 0.0;

    // check the boolean condition
    if( itemTwo < itemOne )
    cout<<"divide itemTwo into 2: ";
    cout<<"add quotient with itemOne: ";
    else itemOne = itemOne/2;
    cout<<"divide itemOne into 2: ";
    cout<<"add quotient with itemTwo: ";
    //end if

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    51
    Make your own post. And always put your post in CODE tags!

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    51
    Yes the error is on line 34. The type is of BigInt why am I getting this error, and is my logic correct at least? I believe it is my problem is I don't know how to test code like this. Sure you can debug with printf but how do I test and debug a function such as MakeBigInt?

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    @SassyProgrammer: Being desperate is not an excuse for failing to read and obey the instructions and rules of this site.
    You will be ignored for now until you have done so.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by C0__0D View Post
    Yes the error is on line 34. The type is of BigInt why am I getting this error
    "new_node" is a pointer to BigInt and "new_node->digit" is an int. Hence, "new_node->digit->next" tries to dereference an int which is wrong.

    I don't really understand what you want to achieve. Do you want a list with integers like
    Code:
    27 -> 10 -> 88 -> ...
    or an array where each element is a linked list of the digits of an integer like
    Code:
    arr[0]: 2 -> 7 // which order 27 or 72?
    arr[1]: 1 -> 0
    arr[2]: 8 -> 8
    ...
    In both cases you need a working implementation of a linked list. So I would concentrate on this part. Changing the type of the value you want to store in the list is rather trivial.

    Bye, Andreas

  10. #10
    Registered User
    Join Date
    Nov 2012
    Posts
    51
    I have to multiply linked lists that can contain up to 10,000 digits, which is too large for the unsigned int type, which is why they are being read in as strings. The steps that I am going to take for starters are:

    Code:
    1. Reading in the strings from the file (Which I have done.)
    2. Convert the strings to integers.
    3. store the integers into linked lists.
    I cannot change the prototype of the BigInt struct as it was given to us by the professor. So what I am trying to accomplish in the MakeBigInt function is converting the string to an integer and storing it into a linked list, so for example:

    Code:
    // Read in 27 10
    // Store 27 as...
    2 -> 7
    // store 10 as...
    1 -> 0
    Then I have to multiply them with a function I have to create but that's a different story :-) so I cannot change the struct and I thought the logic seemed correct, but obviously it isn't. Please note, as this is homework, I am not trying to cheat here. I just need some guidance as I am a noob to C.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    We answered this exact question for two other people, what seem like about a month ago, with the exact same requirements and same struct definition.
    I believe one of the examples was to multiple 111111111111 (not sure how many ones) by 4.
    Conincidence?!?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked list trouble...
    By JM1082 in forum C++ Programming
    Replies: 3
    Last Post: 11-27-2011, 04:02 AM
  2. Trouble with linked list
    By mikeman in forum C++ Programming
    Replies: 8
    Last Post: 01-21-2010, 11:10 PM
  3. Linked List(+reverse) trouble
    By killmequick in forum C Programming
    Replies: 8
    Last Post: 09-18-2008, 02:40 PM
  4. Linked list trouble
    By sand_man in forum C Programming
    Replies: 2
    Last Post: 02-08-2005, 01:14 PM
  5. Having trouble with Linked List
    By hkmixxa in forum C++ Programming
    Replies: 7
    Last Post: 08-09-2004, 04:25 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21