Author image Lloyd Griffiths
and 1 contributors

NAME

Data::LinkedList - Perl implementation of the GNU Classpath LinkedList.

SYNOPSIS

    #!/usr/bin/env perl -w

    use strict;
    use Data::LinkedList;

    my $list = Data::LinkedList->new();

    $list->add({ name => 'Lloyd', age => undef });
    $list->add({ name => 'Gary', age => undef });

    CORE::say $list->get_first()->{name}; # Lloyd
    CORE::say $list->get_last()->{name};  # Gary

    $list->add_first({ name => 'Lisa', age => undef });
    $list->add_last({ name => 'Bob', age => undef });

    my $lisa = $list->remove_first(); # HashRef stored in $lisa
    my $bob  = $list->remove_last();  # HashRef stored in $bob

    $list->offer_last($lisa);
    CORE::say $list->remove_first_occurrence($lisa); # 1 (true)

    CORE::say $list->size(); # 2

    $list->add_all_at(1, ($lisa, $bob));

    CORE::say $_->{name} for $list->to_array(); # Lloyd, Lisa, Bob, Gary

    $list->set(1, "simple element");
    CORE::say $list->get(1); # "simple element"

    $list->write_object("list.txt");

DESCRIPTION

This module provides a doubly linked list data structure, as well as iterators to walk through the list.

METHODS

new

Create an instance of an empty doubly linked list.

    my $list = Data::LinkedList->new();

get_first

Return the first element in the list.

    $list->get_first();

get_last

Return the last element in the list.

    $list->get_last();

remove_first

Remove and return the first element in the list.

    $list->remove_first();

remove_last

Remove and return the last element in the list.

    $list->remove_last();

add_first

Insert an element at the front of the list.

    $list->add_first($element);

add_last

Insert an element at the end of the list.

    $list->add_last($element);

contains

Return true if the list contains the given value.

    $list->contains($element);

size

Return the current size of the list.

    $list->size();

add

Adds an element to the end of the list.

    $list->add($element);

remove

Removes and returns the first element of the list.

    $list->remove();

add_all

Append a list of elements to the end of the list.

    $list->add_all(@elements);

add_all_at

Add a list of elements at the given position of the list.

    $list->add_all_at($index, @elements);

clear

Remove all elements from the list.

    $list->clear();

get

Return the element which is at the given index.

    $list->get($index);

set

Replace the element at the given index of the list.

    $list->set($index, $element);

insert

Inserts an element into the given position of the list.

    $list->insert($index, $element);

remove_at

Remove an element from the given position.

    $list->remove_at($index);

index_of

Returns the first index of the given element.

    $list->index_of($element);

last_index_of

Returns the last index of the given element.

    $list->last_index_of($element);

to_array

Returns an array which contains the elements of the list in order.

    $list->to_array();

offer

Add an element to the end of the list. An alias of add.

    $list->offer($element);

element

Returns the first element in the list without removing it. An alias of get_first.

    $list->element();

peek

Returns the first element in the list without removing it. Doesn't complain if the list has a size of zero.

    $list->peek();

poll

Removes and returns the first element of the list. Doesn't complain if the list has a size of zero.

    $list->poll();

offer_first

Inserts an element at the front of the list. Alias of add_first.

    $list->offer_first($element);

offer_last

Inserts an element at the end of the list. Alias of add.

    $list->offer_last($element);

peek_first

Returns the first element of the list without removing it. Doesn't complain if the list has a size of zero. Alias of peek.

    $list->peek_first();

peek_last

Returns the last element of the list without removing it. Doesn't complain if the list has a size of zero.

    $list->peek_last();

poll_first

Removes and returns the first element of the list. Doesn't complain if the list has a size of zero. Alias of poll.

    $list->poll_first();

poll_last

Removes and returns the last element of the list. Doesn't complain if the list has a size of zero.

    $list->poll_last();

pop

Pops an element from the stack by removing and returning the first element in the list. Equivalent to remove_first. Alias of remove_first.

    $list->pop();

push

Pushes an element on to the stack by adding it to the front of the list. Equivalent to add_first. Alias of add_first.

    $list->push($element);

remove_first_occurrence

Removes the first occurrence of the specified element. Alias of remove.

    $list->remove_first_occurrence($element);

remove_last_occurrence

Removes the last occurrence of the specified element.

    $list->remove_last_occurrence($element);

clone

Create a deep clone of the linked list.

    $list->clone();

write_object

Serializes the object and writes it to the given file name.

    $list->write_object($filename);

read_object

Deserializes the object which is read from the given file name.

    $list->read_object($filename);

list_iterator

Obtain a list iterator for list that starts at a given index.

    $list->list_iterator($index);

descending_iterator

Obtain an Iterator for the list that traverses in reverse sequential order.

    $list->descending_iterator();

BUGS

Please report any bugs to lloydg@cpan.org

CREDITS

Credits go to the authors of the GNU classpath implementation of java.util.LinkedList class. The majority of this modules code has been based on their prior work.

SEE ALSO

LinkedList::Single Data::LinkedList::Entry Data::LinkedList::Iterator::ListIterator Data::LinkedList::Iterator::DescendingIterator

AUTHOR

Lloyd Griffiths

COPYRIGHT

Copyright (c) 2013 Lloyd Griffiths

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.