BCX Console Demonstration Program S153.bas

  TYPE LIST
    Cnt AS integer
    b[32] AS CHAR
    nxt AS LIST PTR
    prv AS LIST PTR
  END TYPE
  
  CONST sp = 0
  
  DIM Head AS LIST PTR
  
  DIM a$, buffer$[40], i, j
  
  a$ = "this is a test"
  i = SPLIT(buffer$, a$, " ", sp)
  FOR j = 0 TO i - 1
    CALL AddString(buffer$[j])
  NEXT
  a$ = "the quick brown fox jumped over the lazy dog"
  i = SPLIT(buffer$, a$, " ", sp)
  FOR j = 0 TO i - 1
    CALL AddString(buffer$[j])
  NEXT
  a$ = "to error is human to really screw up requires a computer"
  i = SPLIT(buffer$, a$, " ", sp)
  FOR j = 0 TO i - 1
    CALL AddString(buffer$[j])
  NEXT
  a$ = "if a function takes up more space than can fit on a screen it has an error in it"
  i = SPLIT(buffer$, a$, " ", sp)
  FOR j = 0 TO i - 1
    CALL AddString(buffer$[j])
  NEXT
  
  CALL OutPutData()
  !fflush(stdout);
  getchar();
  
  SUB OutPutData
    IF Head THEN
      WHILE Head->prv
        Head = Head->prv
      WEND
      WHILE Head
        PRINT Head->b$; Head->Cnt
        Head = Head->nxt
      WEND
    END IF
  END SUB
  
  SUB AddString(bufstr$)
    DIM RAW PHead AS LIST PTR
    DIM RAW CHead AS LIST PTR
    DIM RAW NHead AS LIST PTR
  
    IF !(Head) THEN
      Head = MakeRecord()
      Head->b$ = bufstr$
      Head->Cnt = 1
      EXIT SUB
    END IF
  
    CHead = Head
    WHILE CHead->b$ < bufstr$
      IF CHead->nxt THEN
        CHead = CHead->nxt
      ELSE
        PHead = CHead
        CHead->nxt = MakeRecord()
        CHead = CHead->nxt
        CHead->b$ = bufstr$
        CHead->Cnt = 1
        CHead->prv = PHead
        EXIT SUB
      END IF
    WEND
  
    IF CHead->b$ = bufstr$ THEN
      CHead->Cnt++
      EXIT SUB
    END IF
  
    WHILE CHead->b$ > bufstr$
      IF CHead->prv THEN
        CHead = CHead->prv
      ELSE
        PHead = CHead
        CHead->prv = MakeRecord()
        CHead = CHead->prv
        CHead->b$ = bufstr$
        CHead->Cnt = 1
        CHead->nxt = PHead
        EXIT SUB
      END IF
    WEND
  
    IF CHead->b$ = bufstr$ THEN
      CHead->Cnt++
      EXIT SUB
    END IF
  
    NHead = MakeRecord()
    NHead->b$ = bufstr$
    NHead->Cnt = 1
  
    PHead = CHead->nxt
    CHead->nxt = NHead
    NHead->nxt = PHead
    NHead->prv = CHead
    PHead->prv = NHead
  
  END SUB
  
  FUNCTION MakeRecord() AS LIST PTR
  !   return (LIST*)(calloc(1, sizeof(LIST)));
  END FUNCTION