code-sherpas / kollections

Kollections is a Kotlin open source library to fill the gap that currently exists in the Kotlin Standard Library regarding data structures. :rocket:
MIT License
9 stars 9 forks source link

Singly Linked List implementation #12

Closed dgraciac closed 3 years ago

dgraciac commented 3 years ago

First step

Search, read and understand information about lists, linked lists and singly linked lists, and how they are implemented.

Second step

You have to share a code snippet in markdown with an interface proposal for this data structure. Below you can find an interface example of an hypothetical data structure:

class MyDataStructure<E> {

    fun add(element: E) {
        TODO("Not yet implemented")
    }

    fun get(index: Int): E {
        TODO("Not yet implemented")
    }
}

Third step

After @code-sherpas/kollections-maintainers approve your interface proposal, open a pull request with the data structure implementation and wait for comments.

Minimum acceptance criteria

Related resources:

ashikka commented 3 years ago

@dgraciac Here is the interface proposal for this data structure. Let me know if you require any changes.

package main
class SinglyLinkedList<E> {

    // Insert element at the beginning of the list
    fun insertAtBeginning(element: E){
        TODO()
    }

    // Insert element after a specified node
    fun insertAfter(previousNode: E, element: E){
        TODO()
    }

    // Insert element at the end of the list
    fun insertAtEnd(element: E){
        TODO()
    }

    // Delete a node of a specified position
    fun deleteNode(position: E){
        TODO()
    }
}
dgraciac commented 3 years ago

Positions are integers

 fun deleteNode(position: Int){
        TODO()
    }
dgraciac commented 3 years ago

Passing a node is not needed:

    // Insert element after the first ocurrence of specified element
    fun insertAfter(element: E){
        TODO()
    }
dgraciac commented 3 years ago

After this interface changes, you can go with the implementation.

dgraciac commented 3 years ago

Consider add this operations in this PR. But up to you.

fun get(index: Int): E

fun isEmpty(): Boolean

fun head(): E

ashikka commented 3 years ago

Is this alright @dgraciac?

package main
class SinglyLinkedList<E> {

    // Insert element at the beginning of the list
    fun insertAtBeginning(element:E) {
        TODO()
    }

    // Insert element at the end of the list
    fun insertAtEnd(element:E) {
        TODO()
    }

    // Delete a node of a specified position
    fun deleteNode(position:Integer) {
        TODO()
    }

    // Get element at a specified position
    fun get(index:Integer):E {
        TODO()
    }

    // Check is list is empty
    fun isEmpty(): Boolean{
        TODO()
    }

    // Find the element at the head of the list
    fun head():E{
        TODO()
    }
}
dgraciac commented 3 years ago

Please remove this operation because I am not sure it is worth it:

// Insert element after a specified node
    fun insertAfter(element:E) {
        TODO()
    }

You can start with the implementation.