Skip to main content

LinkedList

General

LinkedList is a built-in collection class in Ysharp that implements a singly linked list. It provides efficient insertion and removal at both ends, making it well suited for queue and stack style operations.

Note
Unlike Array, LinkedList does not provide constant-time random access. Accessing or modifying an element by index requires traversing the list from the head.

var list = new LinkedList();

Creates a new, empty LinkedList instance.

Reference

Instance methods

MethodSignatureReturn TypeDescription
addFirstlist.addFirst(value)nullInserts a value at the beginning of the list
addLastlist.addLast(value)nullInserts a value at the end of the list
removeFirstlist.removeFirst()anyRemoves and returns the first element
removeLastlist.removeLast()anyRemoves and returns the last element
peekFirstlist.peekFirst()any|nullReturns the first element without removing it
peekLastlist.peekLast()any|nullReturns the last element without removing it
getlist.get(index : int)anyReturns the element at the given index
setlist.set(index : int, value)nullReplaces the element at the given index
containslist.contains(value)boolReturns true if the list contains the given value
indexOflist.indexOf(value)intReturns the index of the first occurrence, or -1
sizelist.size()intReturns the number of elements in the list
isEmptylist.isEmpty()boolReturns true if the list contains no elements
clearlist.clear()nullRemoves all elements from the list
toArraylist.toArray()ArrayReturns a new Array containing all elements in order
clonelist.clone()LinkedListReturns a shallow copy of the list
toStringlist.toString()stringReturns a string representation of the list

Examples

addFirst(value : any)

Inserts a value at the beginning of the list.

var list = new LinkedList();

list.addFirst(10); // [10]
list.addFirst(20); // [20,10]
list.addFirst(30); // [30,20,10]

println list.get(0); // 30
println list.get(1); // 20
println list.get(2); // 10
var list = new LinkedList();

for var i = 1; i <= 5; i++ do
list.addFirst(i);
end

// list: [5,4,3,2,1]
println list.get(0); // 5
println list.get(4); // 1

addLast(value : any)

Inserts a value at the end of the list.

var list = new LinkedList();

list.addLast(10); // [10]
list.addLast(20); // [10,20]
list.addLast(30); // [10,20,30]

println list.get(2); // 30

clear()

Removes all elements from the list, making it empty.

var list = new LinkedList();

list.addLast(1);
list.addLast(2);
list.addLast(3);

println list.size(); // 3

list.clear();

println list.size(); // 0
println list.isEmpty(); // true

contains(value : any)

Returns true if the list contains the given value, otherwise false.

var list = new LinkedList();

list.addLast(10);
list.addLast(20);
list.addLast(30);

println list.contains(20); // true
println list.contains(99); // false

get(index : int)

Returns the element at the specified index.

var list = new LinkedList();

list.addLast(10);
list.addLast(20);
list.addLast(30);

println list.get(0); // 10
println list.get(1); // 20
println list.get(2); // 30
var list = new LinkedList();

list.addLast(1);
list.addLast(2);
list.addLast(3);

list.set(1, 99);

println list.get(1); // 99
var list = new LinkedList();

list.addLast(1);

try do
list.get(5);
end
catch(e) do
println e; // LinkedList index out of bounds
end

indexOf(value : any)

Returns the index of the first occurrence of the given value, or -1 if not found.

var list = new LinkedList();

list.addLast(10);
list.addLast(20);
list.addLast(30);

println list.indexOf(10); // 0
println list.indexOf(20); // 1
println list.indexOf(30); // 2
println list.indexOf(99); // -1
var list = new LinkedList();

list.addLast(1);
list.addLast(2);
list.addLast(3);

list.removeFirst(); // [2,3]

println list.indexOf(1); // -1
println list.indexOf(2); // 0

isEmpty()

Returns true if the list contains no elements, otherwise false.

var list = new LinkedList();

println list.isEmpty(); // true

list.addLast(10);

println list.isEmpty(); // false
var list = new LinkedList();

list.addLast(1);
list.addLast(2);

list.clear();

println list.isEmpty(); // true

peekFirst()

Returns the first element of the list without removing it, or null if the list is empty.

var list = new LinkedList();

list.addLast(10);
list.addLast(20);

println list.peekFirst(); // 10
println list.size(); // 2 (unchanged)
var list = new LinkedList();

println list.peekFirst(); // null

peekLast()

Returns the last element of the list without removing it, or null if the list is empty.

var list = new LinkedList();

list.addLast(10);
list.addLast(20);

println list.peekLast(); // 20
println list.size(); // 2 (unchanged)
var list = new LinkedList();

println list.peekLast(); // null

removeFirst()

Removes and returns the first element of the list, or null if the list is empty.

var list = new LinkedList();

list.addLast(10);
list.addLast(20);
list.addLast(30);

var first = list.removeFirst();

println first; // 10
println list.get(0); // 20
println list.size(); // 2
var list = new LinkedList();

list.addLast(1);

println list.removeFirst(); // 1
println list.removeFirst(); // null
println list.isEmpty(); // true

removeLast()

Removes and returns the last element of the list, or null if the list is empty.

var list = new LinkedList();

list.addLast(10);
list.addLast(20);
list.addLast(30);

var last = list.removeLast();

println last; // 30
println list.size(); // 2
println list.get(1); // 20
var list = new LinkedList();

list.addLast(1);

println list.removeLast(); // 1
println list.removeLast(); // null
println list.isEmpty(); // true

set(index : int, value : any)

Replaces the element at the specified index and returns the old value.

var list = new LinkedList();

list.addLast(10);
list.addLast(20);
list.addLast(30);

var old = list.set(1, 99);

println old; // 20
println list.get(1); // 99
var list = new LinkedList();

list.addLast(1);
list.addLast(2);
list.addLast(3);

list.set(0, 100);
list.set(2, 300);

println list.get(0); // 100
println list.get(2); // 300
var list = new LinkedList();

list.addLast(1);

try do
list.set(5, 10);
end
catch(e) do
println e; // LinkedList index out of bounds
end

size()

Returns the number of elements in the list.

var list = new LinkedList();

println list.size(); // 0

list.addLast(10);
list.addLast(20);

println list.size(); // 2
var list = new LinkedList();

for var i = 0; i < 5; i++ do
list.addLast(i);
end

println list.size(); // 5

list.removeFirst();
list.removeLast();

println list.size(); // 3

toArray()

Returns a new Array containing all elements of the list in order.

var list = new LinkedList();

list.addLast(10);
list.addLast(20);
list.addLast(30);

var arr = list.toArray();

println arr.get(0); // 10
println arr.get(1); // 20
println arr.get(2); // 30
var list = new LinkedList();

list.addLast(1);
list.addLast(2);
list.addLast(3);

list.removeFirst(); // [2,3]

var arr = list.toArray();

println arr.size(); // 2
println arr.get(0); // 2
println arr.get(1); //

toString()

Returns a string representation of the list in traversal order.

var list = new LinkedList();

list.addLast(10);
list.addLast(20);
list.addLast(30);

println list.toString(); // [10 -> 20 -> 30]
var list = new LinkedList();

println list.toString(); // []