Advertisement
WONGDEEM

Untitled

Mar 14th, 2021
923
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void expressionLL(char* infix, LinkedList *inExpLL) {
  2.   //init variables
  3.   LinkedList *s = malloc(sizeof(LinkedList));
  4.   char tempInt[99]; // to store the string of numbers
  5.   int index = 0;
  6.  
  7.   while (*infix != '\0') {
  8.     ListNode *temp = malloc(sizeof(ListNode));
  9.  
  10.     //handles insertion of ListNode into the linked list
  11.     if (inExpLL->head == NULL) inExpLL->head = temp;
  12.     else {
  13.       ListNode *currNode = inExpLL->head;
  14.       while (currNode->next != NULL) currNode = currNode->next;
  15.       currNode->next = temp;
  16.     }
  17.  
  18.     //checks where the number ends
  19.     while (*infix>=48) {
  20.       tempInt[index] = *infix;
  21.       infix++;
  22.       index++;
  23.     }
  24.     tempInt[index] = '\0';
  25.  
  26.     //checks if tempInt is populated with numbers. If not, then insert a node that is an operator
  27.     temp->item = (tempInt[0] == '\0') ? *infix : atoi(tempInt);
  28.     temp->type = (tempInt[0] == '\0') ? OPT : OPERAND;
  29.  
  30.     //if temp's type is OPERAND, insert the current char from infix bc it will be an operator
  31.     if (*infix != '\0' && temp->type != OPT) {
  32.       ListNode *tempOpt = malloc(sizeof(ListNode));
  33.       tempOpt->item = *infix;
  34.       tempOpt->type = OPT;
  35.       temp->next = tempOpt;
  36.     }
  37.     infix++;
  38.  
  39.     //resets vars for next loop
  40.     tempInt[0] = '\0';
  41.     index = 0;
  42.   }
  43. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement