ArrayList

Algemene discussies en vragen omtrent Liberty BASIC programma's. Stuur zoveel mogelijk delen van listings (snippets) in als je hulp wilt.

Moderators: anthonio, Abcott

ArrayList

Berichtdoor bluatigro » vr aug 17, 2012 12:01 pm

n arraylist is een stack die FIFO / LIFO werkt
tevens kun je in t midden opslaan en weghalen

error :
- part$() werkt nog niet helemaal goed
- sort$() iden
Code: Selecteer alles
global cut$ , true , false
cut$ = "|"
true = not( false )
for i = 1 to 16
  number$ = push$( number$ , str$( i ) , false )
next i
print "numbers = " ; number$
print "numbers.size = " ; size( number$ )
number$ = remove$( number$ , "3" )
print "numbers.remove 3 = " ; number$
number$ = removeAt$( number$ , 4 )
print "numbers.removeAt 4 = " ; number$
number$ = removeAt$( number$ , -4 )
print "mumbers.removeAt -4 = " ; number$
number$ = insertAt$( number$ , "A" , 5 )
print "numbers.insert A @ 5 = " ; number$
number$ = insertAt$( number$ , "x" , -5 )
print "numbers.insert x @ -5 = " ; number$
number$ = insertSorted$( number$ , "13" , 1 , true )
print "numbers.insertSorted 13 1 true = " ; number$
q$ = top$( number$ )
print "numbers.top = " ; q$
print "numbers.element 5 = " ; bool$( element( number$ , "5" ) )
print "numbers.element 3 = " ; bool$( element( number$ , "3" ) )
print "numbers.reverse = " ; reverse$( number$ )
print "numbers.part 3 7 = " ; part$( number$ , 3 , 7 )
print "numbers.part -5 -2 = " ; part$( number$ , -5 , -2 )
print "numbers.suffle = " ; suffle$( number$ )
''print "numbers.sort 1 false = " ; sort$( number$ , 1 , false )



end
function bool$( x )
  if x then
    uit$ = "TRUE"
  else
    uit$ = "FALSE"
  end if
  bool$ = uit$
end function

function push$( stack$ , object$ , l )
  if object$ <> cut$ then
    if l <> 0 then
      push$ = object$ + cut$ + stack$
    else
      push$ = stack$ + object$ + cut$
      ''push$ = stack$ + cut$ + object$
    end if
  else
    push$ = stack$
  end if
end function

function pop$( stack$ )
''delete last object$
  i = instr( stack$ , cut$ )
  if stack$ = "" then pop$ = ""
''get right side of stack
  pop$ = mid$( stack$ , i + 1 , len( stack$ ) -i )
end function

function top$( stack$ )
''read last object$
  i = instr( stack$ , cut$ )
  if stack$ = "" then
    top$ = ""
  else
    top$ = mid$( stack$ , 1 , i - 1 ) '   <<<<<<<<
  end if
end function

function size( a$ )
  result = 0
  for i = 1 to len( a$ )
    if mid$( a$ , i , 1 ) = cut$ then result = result + 1
  next i
  size = result
end function

function insertAt$( a$ , item$ , i )
  p = 1
  result$ = ""
  max = size( a$ )
  if i < 0 then i = max + 2 - abs( i )
  while p < i
    result$ = push$( result$ , word$( a$ , p , cut$ ) , false )
    p = p + 1
  wend
  result$ = push$( result$ , item$ , false )
  while p <= max
    result$ = push$( result$ , word$( a$ , p , cut$ ) , false )
    p = p + 1
  wend
  insertAt$ = result$
end function

function insertSorted$( a$ , item$ , no , isStr )
  p = 1
  result$ = ""
  max = size( a$ )
  done = false
  while p <= max and not( done )
    m$ = word$( a$ , p , cut$ )
    mp$ = word$( m$ , no )
    ip$ = word$( item$ , no )
    result$ = push$( result$ , m$ , false )
    if isStr then
      if mp$ <= ip$ then
        result$ = push$( result$ , item$ , false )
        done = true
      end if
    else
      if val( mp$ ) <= val( ip$ ) then
        result$ = push$( result$ , item$ , false )
        done = true
      end if
    end if
    p = p + 1
  wend
  while p < max
    m$ = word$( a$ , p , cut$ )
    result$ = push$( result$ , m$ , false )
    p = p + 1
  wend
  insertSorted$ = result$
end function

function sort$( a$ , no , isStr )
  max = size( a$ )
  for i = 1 to max
    result$ = insertSorted$( result$ , word$( a$ , i , cut$ ) , no , isStr )
  next i
  sort$ = result$
end function

function remove$( a$ , item$ )
  p = 1
  result$ = ""
  max = size( a$ )
  while p <= max
    if word$( a$ , p , cut$ ) <> item$ then
      result$ = push$( result$ , word$( a$ , p , cut$ ) , false )
    end if
    p = p + 1
  wend
  remove$ = result$
end function

function removeAt$( a$ , i )
  p = 1
  result$ = ""
  max = size( a$ )
  if i < 0 then i = max + 1 - abs( i )
  while p <= max
    if p <> i then
      result$ = push$( result$ , word$( a$ , p , cut$ ) , false )
    end if
    p = p + 1
  wend
  removeAt$ = result$
end function

function reverse$( a$ )
  p = 1
  max = size( a$ )
  while p <= max
    result$ = push$( result$ , word$( a$ , p , cut$ ) , true )
    p = p + 1
  wend
  reverse$ = result$
end function

function part$( a$ , b , e )
  max = size( a$ )
  if b < 0 then b = max + 1 - abs( b )
  if e < 0 then e = max + 1 - abs( e )
  for i = b to e
    r$ = push$( r$ , word$( a$ , i , cut$ ) , false )
  next i
  part$ = r$
end function

function element( a$ , item$ )
  p = instr( cut$ + a$ , cut$ + item$ + cut$ )
  element = p > 0
end function

function suffle$( a$ )
  max = size( a$ )
  dice = int( rnd( 0 ) * max ) + 1
  seed$ = word$( a$ , dice , cut$ )
  for i = 2 to max
    dice = int( rnd( 0 ) * ( max - i ) ) + 1
    p$ = word$( a$ , dice , cut$ )
    uit$ = push$( uit$ , p$ , false )
    a$ = removeAt$( a$ , dice )
  next i
  suffle$ = uit$
end function
bluatigro
 
Berichten: 306
Geregistreerd: za sep 27, 2008 6:01 pm

Keer terug naar Liberty BASIC Code

Wie is er online

Gebruikers op dit forum: Geen geregistreerde gebruikers. en 1 gast

cron