Snippets

Jack Stade Code for Linked Lists

Created by Jack Stade last modified
//add this to a function to add the ability to use linked lists
//this is probably riddled with bugs that I haven't fixed yet
//There is more functionality here than you probably need
//so feel free to take only the bits you want
//note that some functions have dependancies
//create an empty list
#define list_create
return {next:-1};

//create a list based on an array
#define list_create_from(vals)
var len = array_length_1d(vals);
var prev = -1;
for (var i = len-1;i>=0;i--) {
  var n = {next:prev, value:vals[i]};
}
return {next:prev};

//get an array based on a list
//dependant on list_length
#define list_get_all(list)
var len = list_length(list);
var a = array_create(len);
var last = list;
var p = 0;
while (last.next<=0) {
  last = last.next;
  a[p] = last;
}
return a;

//number of elements in a list
#define list_length(list)
var last = list;
len = 0;
while(is_array(last.next) && len<1000) {
  last = last.next;
  len++;
}
if (len>500) trace(len);
return len;

//add something to a list
#define list_add(list, val)
var last = list;
while(is_array(last.next)) {
  last = last.next;
}
last.next = {next:-1, value:val};
return list;

//adds something to a list at a specific position
#define list_add_pos(list, val, pos)
var last = list;
var n = list.next;
for (var i = 0;i<pos && next>=0;i++) {
  last = last.next;
  n = last.next;
}
last.next = {next:n, value:val};
return list;

//gets the position of an item in a list
#define list_find(list, item)
var last = list;
var place = -1;
var on = 0;
while(is_array(last.next)) {
  last = last.next;
  if (last.value == item) {
    place = on;
  }
  on++;
}
return place;

//removes an element from a list
#define list_remove(list, val)
var last = list;
var prev = list;
var go = 1;
while(is_array(last.next) && go) {
  prev = last;
  last = last.next;
  if (last.value == val) go = 0;
}
if (!go) {
  prev.next = last.next;
}
return list;

//removes the element at a specific position from a list
#define list_remove_pos(list, pos)
var last = list;
var prev = list;
var go = 1;
var p = 0;
while(is_array(last.next) && go) {
  prev = last;
  last = last.next;
  if (p == pos) go = 0;
  p++;
}
if (!go) {
  prev.next = last.next;
}
return list;

//gets the the value of a list at a certain position
#define list_get(list, pos)
var last = list;
var p = 0;
while(is_array(last.next) && p<pos) {
  last = last.next;
  p++;
}
if ("value" in last.next) {
  return last.next.value;
} else {
  return -1;
}

//gets the fist value of a list that satisfies a certain condition
#define list_get_first(list, check)
var last = list;
while(is_array(last.next) && !script_ref_call(check, last.next.value)) {
  last = last.next;
}
if ("value" in last.next) {
  return last.next.value;
} else {
  return -1;
}

//preforms an event for each element
//event is a script_ref that will get called with the value as an argument
#define list_do_all(list, event)
var last = list;
while(is_array(last.next)) {
  last = last.next;
  script_ref_call(event, last.value);
}

//removes each element from the list if a certain condition is met
#define list_remove_if(list, check)
var last = list;
var prev = list;
var go = 1;
while(is_array(last.next)) {
  prev = last;
  last = last.next;
  if (script_ref_call(check, last.value)) {
    prev.next = last.next;
    last = prev.next;
  }
}

//Appends a list to the end of another list
#define list_append(list, newList)
var last = list;
while (is_array(last.next)) {
  last = last.next;
}
last.next = newList.next;
return list;

//sorts a list with mergesort
//takes a script ref that should check whether the second argument is greater
//the array will be sorted so that the "largest" values are last in the array, etc.
//dependant on list_length and list_append
#define list_sort(list, check)
var length = list_length(list);
if (length>1) {
  var last = list;
  var p = 0;
  while(is_array(last.next) && p<length/2) {
    last = last.next;
    p++;
  }
  var list2 = list_create();
  list2.next = last.next;
  last.next = -1;
  list_sort(list, check);
  list_sort(list2, check);
  var prev = list;
  var list1on = list.next;
  var list2on = list2.next;
  while(is_array(list1on) || is_array(list2on)) {
    var add = 0;
    if (is_array(list1on) && (!is_array(list2on) || script_ref_call(check, list2on.value, list1on.value))) {
      add = list1on;
      list1on = list1on.next;
    } else {
      add = list2on;
      list2on = list2on.next;
    }
    prev.next = add;
    prev = add;
  }
  return list;
} else {
  return list;
}


//sorts a list of numbers, taking no script reference as an argument
//dependant on list_sort and num_check
#define list_numbers_sort(list)
return list_sort(list, script_ref_create(num_check));

//checks if a is greater than b
#define num_check(a, b)
return a>b;

Comments (0)