Pages

Friday, January 31, 2014

RESOLVING: POINTER TO ARRAY AND ARRAY OF POINTER

HOW TO RESOLVE POINTER TO ARRAY OR ARRAY OF POINTER

int *ptr[100];
int *(ptr[100]);
int (*ptr[100]);
int (*ptr)[100];

ARRAY OF POINTERS

int *ptr[100];
int (*ptr[100]);
int *(ptr[100]);

POINTER TO ARRAY

int (*ptr)[100];

REVERSE SINGLY LINKED LIST

typedef struct _listElement {
    int nData;
    struct _listElement* pNext;
} LIST;

/* Reversing A Linked List Recurrsively */
void recurrsivereverse(LIST** pHead) {
    LIST* pCurrent = *pHead;
    LIST* pNext = NULL;
    
    if(pCurrent == NULL) {
        return;
    }
    pNext = pCurrent->pNext;
    recurrsivereverse(&pNext);
    if(pNext)
    {
        pCurrent->pNext->pNext = pCurrent;
        pCurrent->pNext = NULL;
        *pHead = pNext;
    }
    return;
}

/* Reversing A Linked List Iteratively */
void iterativereverse(LIST** pHead) {
    LIST* pCurrent = *pHead;
    LIST* pPrev = NULL;
    LIST* pNext = NULL;

    while(pCurrent) {
        pNext = pCurrent->pNext;
        pCurrent->pNext = pPrev;
        pPrev = pCurrent;
        pCurrent = pNext;
    }
    
    *pHead = pPrev;
    return;
}

Monday, January 27, 2014

INFIX TO PREFIX

GET THE INFIX EXPRESSION IN A BUFFER
INITIALIZE PRINT QUEUE
INITIALIZE OPERATOR STACK
REVERSE THE INFIX EXPRESSION BUFFER
WHILE BUFFER IS NOT EMPTY
   READ A TOKEN FROM THE BUFFER
   IF THE TOKEN IS A OPERAND
       ENQUEUE THE TOKEN IN PRINT QUEUE
   ELSE IF THE TOKEN IS A OPERATOR
       IF OPERATOR STACK EMPTY
          PUSH THE TOKEN IN THE OPERATOR STACK
       ELSE
          IF TOS (OPERATOR STACK) IS HIGHER PRIORITY OPERATOR THAN TOKEN
              ENQUEUE ( POP FROM OPERATOR STACK) IN PRINT QUEUE
              PUSH THE TOKEN IN THE OPERATOR STACK
          ELSE
              PUSH THE TOKEN IN THE OPERATOR STACK
          END IF
       END IF
   ELSE IF THE TOKEN IS ")"
       PUSH THE TOKEN IN THE OPERATOR STACK
   ELSE IF THE TOKEN IS "("
        IF OPERATOR STACK IS EMPTY
            INFIX EXPRESSION IS NOT VALID
        ELSE
            WHILE TOS (OPERATOR STACK) IS NOT ")" AND OPERATOR STACK NOT EMPTY
                ENQUEUE ( POP FROM OPERATOR STACK) INTO PRINT QUEUE
            END WHILE
            IF OPERATOR STACK EMPTY
                INFIX EXPRESSION IS NOT VALID
            ELSE
                POP FROM OPERATOR STACK /* POP ")" */
            END IF
        END IF
   END IF
END WHILE
WHILE OPERATOR STACK IS NOT EMPTY
    ENQUEUE ( POP FROM OPERATOR STACK) INTO PRINT QUEUE
END WHILE
REVERSE THE PRINT QUEUE

Sunday, January 26, 2014

INFIX TO POSTFIX

GET THE INFIX EXPRESSION IN A BUFFER
INITIALIZE PRINT QUEUE
INITIALIZE OPERATOR STACK
WHILE BUFFER IS NOT EMPTY
    READ A TOKEN FROM THE BUFFER
    IF TOKEN IS OPERAND
       ENQUEUE THE TOKEN IN PRINT QUEUE
    ELSE IF TOKEN IS A OPERATOR
        IF OPERATOR STACK IS EMPTY
            PUSH THE TOKEN IN THE OPERATOR STACK
        ELSE
            IF THE TOS (OPERATOR STACK) IS HIGHER PRIORITY OPERATOR THAN TOKEN
                ENQUEUE ( POP FROM OPERATOR STACK) INTO PRINT QUEUE
                PUSH THE TOKEN IN THE OPERATOR STACK
            ELSE
                PUSH THE TOKEN IN THE OPERATOR STACK
            END IF
        END IF
    ELSE IF TOKEN IS "("
        PUSH THE TOKEN IN THE OPERATOR STACK
    ELSE IF TOKEN IS ")"
        IF OPERATOR STACK IS EMPTY
            INFIX EXPRESSION IS NOT VALID
        ELSE
            WHILE TOS (OPERATOR STACK) IS NOT "(" AND OPERATOR STACK NOT EMPTY
                ENQUEUE ( POP FROM OPERATOR STACK) INTO PRINT QUEUE
            END WHILE
            IF OPERATOR STACK EMPTY
                INFIX EXPRESSION IS NOT VALID
            ELSE
                POP FROM OPERATOR STACK /* POP "(" */
            END IF
        END IF
    END IF
END WHILE
WHILE OPERATOR STACK IS NOT EMPTY
    ENQUEUE ( POP FROM OPERATOR STACK) INTO PRINT QUEUE
END WHILE