I'm studying Data Structures in my CS course.
University professor assigned us a project where we have to implement every data structure from 0 without using the already one provided by the programming language we choosed to use.
All of this is required for pass his lab part of the exam:
For every data structure we have to develop more than one known implementations.
An istance : For the List data structure
We have to develop ArrayList, LinkedList and DoubleLinkedList in separate
classes and packages.
We have to follow the known formal specifications.
An istance : For the List class
We can only declare "data structure releated" members and attributes
Like create,delete,head,isEmpty,isLast,next,previous,insert,write,read,remove
I completed those ones, but
I need to put "services functions" in a separated class/file for every Data Structure and it should work for every class(implementation) of them.
then following the istances above:
I have to develop a class/file named "ListServices" that should "extend" every class like:
MyArrayList
MyLinkedList
MyDoubleLinkedList
For Services I mean "not needed" functions for the data structures like:
printList() //it prints the whole list
goToList(position : Int) //goes to the position using Integers
sortList(whichsort : String, order : String) //whichsort = quicksort,mergesort and order ascending/descending
purgeList() //it deletes all the "dupled" elements(or the more than 2 occurances per element)
Usually I will define those in every class that I named above like this:
class MyArrayList<T>{
.
.
.
fun printList(){}
}
class MyLinkedList<T>{
.
.
.
fun printList(){}
}
class MyDoubleLinkedList<T>{
.
.
.
fun printList(){}
}
import edu.uni.lists.MyArrayList as MyList //or whatever other implementation I will interchange instead of MyArrayList
fun main() {
val l1 = MyList<String> ()
l1.create() //init
if(l1.isEmpty())
println("List is empty")
l1.insert("11",l1.head())
l1.insert("10",l1.head())
l1.insert("9",l1.head())
l1.insert("8",l1.head())
l1.insert("7",l1.head())
l1.insert("69",l1.head())
l1.printList() //should print the whole list
}
Whatever the implementation I use, printList() will use only the data structure operators and nothing else.
The code of printList() is the same for every List Implementation.
I cannot place Services Function there!
I need to "move out" printlist()[and others services functions] from the List Implementations to another class or file.
How can I fullfill this request with Kotlin?
Thanks you all.
P.S. = I'm not an english native person, sorry if I'm not clear as you expect, feel free to ask more and I will reply!
question from:
https://stackoverflow.com/questions/65866496/class-with-functions-that-extends-more-than-one-implementation-of-the-same-data