-1

My code is able to compile and run, however the process returns -1. I have drawn out various test cases manually and believes it works, so I am not sure which part of my logic is wrong.

I ask the user for a infix exp., and in2PreLL will

  1. reverse the infix exp.
  2. insert it into the front of Linked List inExpLL or Stack s based on the switch-case statements
  3. Finally, I reverse the entire inExpLL to get the prefix exp.

This code assumes:

  1. input string consists only positive integer numbers
  2. Only operators are + - * / ( )

I entered the input of (1+2)+(3-4), expecting a prefix exp. of ++12-34 when I draw out the process manually. However, when I run the code, it gives me Process returned -1 (0xFFFFFFFF)

EDIT: I have debugged my code and it works now! Thanks for the suggestions everyone!

7
  • Please provide a minimal reproducible example which demonstrates your observations, if necessary with sample input (but better not needing input, e.g. using hardcoded init values), the output you get (including error messages quoted in full, verbatim, as text and directly here) in contrast to the output you expect. Commented Oct 19, 2021 at 8:27
  • I have tried including the driver code as well as the sample input to demonstrate my logic. Hope my question is clearer @Yunnosch Commented Oct 19, 2021 at 8:41
  • 1
    Please read the concept description of minimal reproducible example and try to apply it. Usually a MRE only has one code part to copy for reproducing. Commented Oct 19, 2021 at 8:42
  • It does not compile: definition of insertNode() is missing. Commented Oct 19, 2021 at 9:31
  • @Meisner Hi! You're right, I missed the definition out, I have included it in, it can compile now but it still doesn't return the right output Commented Oct 19, 2021 at 11:20

1 Answer 1

1

Warnings are not to be ignored:

    ...
    default:   //operand
        insertNode(inExpLL, atoi(c), OPERAND);
    ...

My compiler croaks with

incompatible integer to pointer conversion passing 'char' to parameter of type 'const char *'

And indeed atoi(c) is plain wrong here. To convert a single digit to its numeric value, you should use c - '0'.

Even worse, your code uses the deprecated gets and uses it badly: it should at least be:

    gets(infix, stdin);

Finally: you really should learn how to use a debugger...


With the added code and my 2 fixes, the code gives :

 4  3  +  2  1  +  )  +  )

so still the closing paren to remove and the opposite order...

Sign up to request clarification or add additional context in comments.

2 Comments

thank you for your help, I have included the definition of insertNode which I missed out before. can i ask why is atoi wrong here though? I thought atoi is used to convert a string/char into an integer data type?
@newtocodingyikes: atoi parameter is expected to be a C string, that is a pointer to a null terminated char array. Not a single character.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.