SlideShare a Scribd company logo
1 of 2
Download to read offline
Implement a singly linked list as a functional data structure in Kotlin. As discussed in
the class, the list should be a sealed class with enclosed classes to construct a list,
including construction of an empty list. The following methods should be available for a
singly linked list:
1. modifyListHead() method to replace the head of the list with a different element. This
function accepts an input list and an input variable, and returns a list where the head of
the input list has been replaced with the input variable. For example, calling
modifyListHead(exList, 10) where exList = (1,2,3,4,5) would return (10,2,3,4,5).
2. dropFromList() method to drop the first n elements from the list. This function accepts
an input list and a value for n, and returns a list with the first n elements dropped. For
example, calling dropFromList(exList,1) where exList = (1,2,3,4,5) would return (2,3,4,5).
3. sumTwoLists() method that accepts two lists and adds corresponding elements of each
list. This function accepts two input lists and returns their sum as illustrated in the
example below. For two input lists (1,2,3) and (4,5,6), this function returns (5,7,9)
4. multiplyTwoLists() method that accepts two lists and multiplies corresponding elements
in the same way sumTwoLists() adds elements of two arrays.
NB:
Solution
that I earlier got showed some errors when tried to run it. I am also sharing the solution below:
sealed class SinglyLinkedList {
object Empty : SinglyLinkedList()
data class Cons(val head: T, val tail: SinglyLinkedList) :
SinglyLinkedList()
}
fun modifyListHead(list: SinglyLinkedList, newHead: T): SinglyLinkedList =
when (list) {
is SinglyLinkedList.Empty -> SinglyLinkedList.Empty
is SinglyLinkedList.Cons -> SinglyLinkedList.Cons(newHead, list.tail)
}
fun dropFromList(list: SinglyLinkedList, n: Int): SinglyLinkedList =
when (list) {
is SinglyLinkedList.Empty -> SinglyLinkedList.Empty
is SinglyLinkedList.Cons -> if (n == 0) list else dropFromList(list.tail, n - 1)
}
fun sumTwoLists(list1: SinglyLinkedList, list2: SinglyLinkedList): SinglyLinkedList =
when {
list1 is SinglyLinkedList.Empty && list2 is SinglyLinkedList.Empty ->
SinglyLinkedList.Empty
list1 is SinglyLinkedList.Empty -> list2
list2 is SinglyLinkedList.Empty -> list1
else -> SinglyLinkedList.Cons(list1.head + list2.head, sumTwoLists(list1.tail, list2.tail))
}
fun multiplyTwoLists(list1: SinglyLinkedList, list2: SinglyLinkedList): SinglyLinkedList =
when {
list1 is SinglyLinkedList.Empty || list2 is SinglyLinkedList.Empty ->
SinglyLinkedList.Empty
else -> SinglyLinkedList.Cons(list1.head * list2.head, multiplyTwoLists(list1.tail,
list2.tail))
}
fun main(){
val exList = linkedListOf(1, 2, 3, 4, 5)
println(exList)
// Output: 1 2 3 4 5
val modifiedList = exList.modifyListHead(10)
println(modifiedList)
// Output: 10 2 3 4 5
val droppedList = exList.dropFromList(1)
println(droppedList)
// Output: 2 3 4 5
val list1 = linkedListOf(1, 2, 3)
val list2 = linkedListOf(4, 5, 6)
val sumList = list1.sumTwoLists(list2)
println(sumList)
// Output: 5 7 9
val multiplyList = list1.multiplyTwoLists(list2)
println(multiplyList)
// Output: 4 10 18
}

More Related Content

Similar to Implement a singly linked list as a functional data structure in Kot.pdf

Write a java class LIST that outputsmainpublic class Ass.pdf
Write a java class LIST that outputsmainpublic class Ass.pdfWrite a java class LIST that outputsmainpublic class Ass.pdf
Write a java class LIST that outputsmainpublic class Ass.pdfebrahimbadushata00
 
Java AssignmentUsing the ListNode.java file below Write method.pdf
Java AssignmentUsing the ListNode.java file below Write method.pdfJava AssignmentUsing the ListNode.java file below Write method.pdf
Java AssignmentUsing the ListNode.java file below Write method.pdfambersushil
 
The Ring programming language version 1.10 book - Part 30 of 212
The Ring programming language version 1.10 book - Part 30 of 212The Ring programming language version 1.10 book - Part 30 of 212
The Ring programming language version 1.10 book - Part 30 of 212Mahmoud Samir Fayed
 
Data Structure and Algorithm Lesson 2.pptx
Data Structure and Algorithm Lesson 2.pptxData Structure and Algorithm Lesson 2.pptx
Data Structure and Algorithm Lesson 2.pptxJoannahClaireAlforqu
 
I only need help with four methods in the EmployeeManager class the .pdf
I only need help with four methods in the EmployeeManager class the .pdfI only need help with four methods in the EmployeeManager class the .pdf
I only need help with four methods in the EmployeeManager class the .pdfarpitcomputronics
 
template ltclass Tgt struct nodeType T data n.pdf
template ltclass Tgt struct nodeType      T data     n.pdftemplate ltclass Tgt struct nodeType      T data     n.pdf
template ltclass Tgt struct nodeType T data n.pdfaceautomate
 
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdfLabprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdffreddysarabia1
 
File LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdfFile LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdfConint29
 
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdfHelp please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdfarorastores
 
Computer notes - Binary Search
Computer notes - Binary SearchComputer notes - Binary Search
Computer notes - Binary Searchecomputernotes
 
Programming in Coday we are working with singly linked lists. Las.pdf
Programming in Coday we are working with singly linked lists. Las.pdfProgramming in Coday we are working with singly linked lists. Las.pdf
Programming in Coday we are working with singly linked lists. Las.pdfPRATIKSINHA7304
 
Homework 05 - Linked Lists (C++)(1) Implement the concepts of a un.pdf
Homework 05 - Linked Lists (C++)(1) Implement the concepts of a un.pdfHomework 05 - Linked Lists (C++)(1) Implement the concepts of a un.pdf
Homework 05 - Linked Lists (C++)(1) Implement the concepts of a un.pdfezzi97
 
The Ring programming language version 1.6 book - Part 24 of 189
The Ring programming language version 1.6 book - Part 24 of 189The Ring programming language version 1.6 book - Part 24 of 189
The Ring programming language version 1.6 book - Part 24 of 189Mahmoud Samir Fayed
 
List Data Structure.docx
List Data Structure.docxList Data Structure.docx
List Data Structure.docxmanohar25689
 
Prompt Your task is to create a connected list implementation and .pdf
Prompt Your task is to create a connected list implementation and .pdfPrompt Your task is to create a connected list implementation and .pdf
Prompt Your task is to create a connected list implementation and .pdfalsofshionchennai
 
Data Structure Lecture 7
Data Structure Lecture 7Data Structure Lecture 7
Data Structure Lecture 7Teksify
 

Similar to Implement a singly linked list as a functional data structure in Kot.pdf (20)

Write a java class LIST that outputsmainpublic class Ass.pdf
Write a java class LIST that outputsmainpublic class Ass.pdfWrite a java class LIST that outputsmainpublic class Ass.pdf
Write a java class LIST that outputsmainpublic class Ass.pdf
 
Java AssignmentUsing the ListNode.java file below Write method.pdf
Java AssignmentUsing the ListNode.java file below Write method.pdfJava AssignmentUsing the ListNode.java file below Write method.pdf
Java AssignmentUsing the ListNode.java file below Write method.pdf
 
The Ring programming language version 1.10 book - Part 30 of 212
The Ring programming language version 1.10 book - Part 30 of 212The Ring programming language version 1.10 book - Part 30 of 212
The Ring programming language version 1.10 book - Part 30 of 212
 
Chapter 15 Lists
Chapter 15 ListsChapter 15 Lists
Chapter 15 Lists
 
Data Structure and Algorithm Lesson 2.pptx
Data Structure and Algorithm Lesson 2.pptxData Structure and Algorithm Lesson 2.pptx
Data Structure and Algorithm Lesson 2.pptx
 
I only need help with four methods in the EmployeeManager class the .pdf
I only need help with four methods in the EmployeeManager class the .pdfI only need help with four methods in the EmployeeManager class the .pdf
I only need help with four methods in the EmployeeManager class the .pdf
 
template ltclass Tgt struct nodeType T data n.pdf
template ltclass Tgt struct nodeType      T data     n.pdftemplate ltclass Tgt struct nodeType      T data     n.pdf
template ltclass Tgt struct nodeType T data n.pdf
 
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdfLabprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
 
Algo>ADT list & linked list
Algo>ADT list & linked listAlgo>ADT list & linked list
Algo>ADT list & linked list
 
File LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdfFile LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdf
 
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdfHelp please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
 
Computer notes - Binary Search
Computer notes - Binary SearchComputer notes - Binary Search
Computer notes - Binary Search
 
Programming in Coday we are working with singly linked lists. Las.pdf
Programming in Coday we are working with singly linked lists. Las.pdfProgramming in Coday we are working with singly linked lists. Las.pdf
Programming in Coday we are working with singly linked lists. Las.pdf
 
Homework 05 - Linked Lists (C++)(1) Implement the concepts of a un.pdf
Homework 05 - Linked Lists (C++)(1) Implement the concepts of a un.pdfHomework 05 - Linked Lists (C++)(1) Implement the concepts of a un.pdf
Homework 05 - Linked Lists (C++)(1) Implement the concepts of a un.pdf
 
F sharp lists & dictionary
F sharp   lists &  dictionaryF sharp   lists &  dictionary
F sharp lists & dictionary
 
The Ring programming language version 1.6 book - Part 24 of 189
The Ring programming language version 1.6 book - Part 24 of 189The Ring programming language version 1.6 book - Part 24 of 189
The Ring programming language version 1.6 book - Part 24 of 189
 
List Data Structure.docx
List Data Structure.docxList Data Structure.docx
List Data Structure.docx
 
Prompt Your task is to create a connected list implementation and .pdf
Prompt Your task is to create a connected list implementation and .pdfPrompt Your task is to create a connected list implementation and .pdf
Prompt Your task is to create a connected list implementation and .pdf
 
Linked list
Linked list Linked list
Linked list
 
Data Structure Lecture 7
Data Structure Lecture 7Data Structure Lecture 7
Data Structure Lecture 7
 

More from maheshkumar12354

In a single strand of DNA, the individual nucleotides are covalenty .pdf
In a single strand of DNA, the individual nucleotides are covalenty .pdfIn a single strand of DNA, the individual nucleotides are covalenty .pdf
In a single strand of DNA, the individual nucleotides are covalenty .pdfmaheshkumar12354
 
In a recent survey conducted, a random sample of adults 18 years of .pdf
In a recent survey conducted, a random sample of adults 18 years of .pdfIn a recent survey conducted, a random sample of adults 18 years of .pdf
In a recent survey conducted, a random sample of adults 18 years of .pdfmaheshkumar12354
 
In a recent survey conducted a random sample of adults 18 years of a.pdf
In a recent survey conducted a random sample of adults 18 years of a.pdfIn a recent survey conducted a random sample of adults 18 years of a.pdf
In a recent survey conducted a random sample of adults 18 years of a.pdfmaheshkumar12354
 
In a hypothetical study, a researcher finds that police officers are.pdf
In a hypothetical study, a researcher finds that police officers are.pdfIn a hypothetical study, a researcher finds that police officers are.pdf
In a hypothetical study, a researcher finds that police officers are.pdfmaheshkumar12354
 
In a 1 to 2 page paper, Times New Roman 12, double spaced, APA forma.pdf
In a 1 to 2 page paper, Times New Roman 12, double spaced, APA forma.pdfIn a 1 to 2 page paper, Times New Roman 12, double spaced, APA forma.pdf
In a 1 to 2 page paper, Times New Roman 12, double spaced, APA forma.pdfmaheshkumar12354
 
In 2017, President Donald Trump was considering a major increase in .pdf
In 2017, President Donald Trump was considering a major increase in .pdfIn 2017, President Donald Trump was considering a major increase in .pdf
In 2017, President Donald Trump was considering a major increase in .pdfmaheshkumar12354
 
In 2021, Lee Jones put the final touches on a product she had worked.pdf
In 2021, Lee Jones put the final touches on a product she had worked.pdfIn 2021, Lee Jones put the final touches on a product she had worked.pdf
In 2021, Lee Jones put the final touches on a product she had worked.pdfmaheshkumar12354
 
In 2000 the Vermont state legislature approved a bill authorizing �c.pdf
In 2000 the Vermont state legislature approved a bill authorizing �c.pdfIn 2000 the Vermont state legislature approved a bill authorizing �c.pdf
In 2000 the Vermont state legislature approved a bill authorizing �c.pdfmaheshkumar12354
 
Implement the class Linked List to create a list of integers. You ne.pdf
Implement the class Linked List to create a list of integers. You ne.pdfImplement the class Linked List to create a list of integers. You ne.pdf
Implement the class Linked List to create a list of integers. You ne.pdfmaheshkumar12354
 
Implement a program in C++ a by creating a list ADT using the Object.pdf
Implement a program in C++ a by creating a list ADT using the Object.pdfImplement a program in C++ a by creating a list ADT using the Object.pdf
Implement a program in C++ a by creating a list ADT using the Object.pdfmaheshkumar12354
 
Implementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdfImplementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdfmaheshkumar12354
 
import java.awt.Color; import java.awt.Dimension; import.pdf
import java.awt.Color; import java.awt.Dimension; import.pdfimport java.awt.Color; import java.awt.Dimension; import.pdf
import java.awt.Color; import java.awt.Dimension; import.pdfmaheshkumar12354
 
Implement two project schedules.BENCHMARKSImplement mechanisms t.pdf
Implement two project schedules.BENCHMARKSImplement mechanisms t.pdfImplement two project schedules.BENCHMARKSImplement mechanisms t.pdf
Implement two project schedules.BENCHMARKSImplement mechanisms t.pdfmaheshkumar12354
 
If you were to write an application program that needs to maintain s.pdf
If you were to write an application program that needs to maintain s.pdfIf you were to write an application program that needs to maintain s.pdf
If you were to write an application program that needs to maintain s.pdfmaheshkumar12354
 
Imagine that you are using a learning management system (such as Bla.pdf
Imagine that you are using a learning management system (such as Bla.pdfImagine that you are using a learning management system (such as Bla.pdf
Imagine that you are using a learning management system (such as Bla.pdfmaheshkumar12354
 
Im trying to run make qemu-nox In a putty terminal but it.pdf
Im trying to run  make qemu-nox  In a putty terminal but it.pdfIm trying to run  make qemu-nox  In a putty terminal but it.pdf
Im trying to run make qemu-nox In a putty terminal but it.pdfmaheshkumar12354
 
Im posting this again because the answer wasnt correct.Please .pdf
Im posting this again because the answer wasnt correct.Please .pdfIm posting this again because the answer wasnt correct.Please .pdf
Im posting this again because the answer wasnt correct.Please .pdfmaheshkumar12354
 
Ignacio enters into a game of chance. A bag of money has twelve $1 b.pdf
Ignacio enters into a game of chance. A bag of money has twelve $1 b.pdfIgnacio enters into a game of chance. A bag of money has twelve $1 b.pdf
Ignacio enters into a game of chance. A bag of money has twelve $1 b.pdfmaheshkumar12354
 
imagine a protein that has been engineered to contain a nuclear loca.pdf
imagine a protein that has been engineered to contain a nuclear loca.pdfimagine a protein that has been engineered to contain a nuclear loca.pdf
imagine a protein that has been engineered to contain a nuclear loca.pdfmaheshkumar12354
 
Im not trying to be rude, but I have had multiple of you experts .pdf
Im not trying to be rude, but I have had multiple of you experts .pdfIm not trying to be rude, but I have had multiple of you experts .pdf
Im not trying to be rude, but I have had multiple of you experts .pdfmaheshkumar12354
 

More from maheshkumar12354 (20)

In a single strand of DNA, the individual nucleotides are covalenty .pdf
In a single strand of DNA, the individual nucleotides are covalenty .pdfIn a single strand of DNA, the individual nucleotides are covalenty .pdf
In a single strand of DNA, the individual nucleotides are covalenty .pdf
 
In a recent survey conducted, a random sample of adults 18 years of .pdf
In a recent survey conducted, a random sample of adults 18 years of .pdfIn a recent survey conducted, a random sample of adults 18 years of .pdf
In a recent survey conducted, a random sample of adults 18 years of .pdf
 
In a recent survey conducted a random sample of adults 18 years of a.pdf
In a recent survey conducted a random sample of adults 18 years of a.pdfIn a recent survey conducted a random sample of adults 18 years of a.pdf
In a recent survey conducted a random sample of adults 18 years of a.pdf
 
In a hypothetical study, a researcher finds that police officers are.pdf
In a hypothetical study, a researcher finds that police officers are.pdfIn a hypothetical study, a researcher finds that police officers are.pdf
In a hypothetical study, a researcher finds that police officers are.pdf
 
In a 1 to 2 page paper, Times New Roman 12, double spaced, APA forma.pdf
In a 1 to 2 page paper, Times New Roman 12, double spaced, APA forma.pdfIn a 1 to 2 page paper, Times New Roman 12, double spaced, APA forma.pdf
In a 1 to 2 page paper, Times New Roman 12, double spaced, APA forma.pdf
 
In 2017, President Donald Trump was considering a major increase in .pdf
In 2017, President Donald Trump was considering a major increase in .pdfIn 2017, President Donald Trump was considering a major increase in .pdf
In 2017, President Donald Trump was considering a major increase in .pdf
 
In 2021, Lee Jones put the final touches on a product she had worked.pdf
In 2021, Lee Jones put the final touches on a product she had worked.pdfIn 2021, Lee Jones put the final touches on a product she had worked.pdf
In 2021, Lee Jones put the final touches on a product she had worked.pdf
 
In 2000 the Vermont state legislature approved a bill authorizing �c.pdf
In 2000 the Vermont state legislature approved a bill authorizing �c.pdfIn 2000 the Vermont state legislature approved a bill authorizing �c.pdf
In 2000 the Vermont state legislature approved a bill authorizing �c.pdf
 
Implement the class Linked List to create a list of integers. You ne.pdf
Implement the class Linked List to create a list of integers. You ne.pdfImplement the class Linked List to create a list of integers. You ne.pdf
Implement the class Linked List to create a list of integers. You ne.pdf
 
Implement a program in C++ a by creating a list ADT using the Object.pdf
Implement a program in C++ a by creating a list ADT using the Object.pdfImplement a program in C++ a by creating a list ADT using the Object.pdf
Implement a program in C++ a by creating a list ADT using the Object.pdf
 
Implementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdfImplementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdf
 
import java.awt.Color; import java.awt.Dimension; import.pdf
import java.awt.Color; import java.awt.Dimension; import.pdfimport java.awt.Color; import java.awt.Dimension; import.pdf
import java.awt.Color; import java.awt.Dimension; import.pdf
 
Implement two project schedules.BENCHMARKSImplement mechanisms t.pdf
Implement two project schedules.BENCHMARKSImplement mechanisms t.pdfImplement two project schedules.BENCHMARKSImplement mechanisms t.pdf
Implement two project schedules.BENCHMARKSImplement mechanisms t.pdf
 
If you were to write an application program that needs to maintain s.pdf
If you were to write an application program that needs to maintain s.pdfIf you were to write an application program that needs to maintain s.pdf
If you were to write an application program that needs to maintain s.pdf
 
Imagine that you are using a learning management system (such as Bla.pdf
Imagine that you are using a learning management system (such as Bla.pdfImagine that you are using a learning management system (such as Bla.pdf
Imagine that you are using a learning management system (such as Bla.pdf
 
Im trying to run make qemu-nox In a putty terminal but it.pdf
Im trying to run  make qemu-nox  In a putty terminal but it.pdfIm trying to run  make qemu-nox  In a putty terminal but it.pdf
Im trying to run make qemu-nox In a putty terminal but it.pdf
 
Im posting this again because the answer wasnt correct.Please .pdf
Im posting this again because the answer wasnt correct.Please .pdfIm posting this again because the answer wasnt correct.Please .pdf
Im posting this again because the answer wasnt correct.Please .pdf
 
Ignacio enters into a game of chance. A bag of money has twelve $1 b.pdf
Ignacio enters into a game of chance. A bag of money has twelve $1 b.pdfIgnacio enters into a game of chance. A bag of money has twelve $1 b.pdf
Ignacio enters into a game of chance. A bag of money has twelve $1 b.pdf
 
imagine a protein that has been engineered to contain a nuclear loca.pdf
imagine a protein that has been engineered to contain a nuclear loca.pdfimagine a protein that has been engineered to contain a nuclear loca.pdf
imagine a protein that has been engineered to contain a nuclear loca.pdf
 
Im not trying to be rude, but I have had multiple of you experts .pdf
Im not trying to be rude, but I have had multiple of you experts .pdfIm not trying to be rude, but I have had multiple of you experts .pdf
Im not trying to be rude, but I have had multiple of you experts .pdf
 

Recently uploaded

call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 

Recently uploaded (20)

call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 

Implement a singly linked list as a functional data structure in Kot.pdf

  • 1. Implement a singly linked list as a functional data structure in Kotlin. As discussed in the class, the list should be a sealed class with enclosed classes to construct a list, including construction of an empty list. The following methods should be available for a singly linked list: 1. modifyListHead() method to replace the head of the list with a different element. This function accepts an input list and an input variable, and returns a list where the head of the input list has been replaced with the input variable. For example, calling modifyListHead(exList, 10) where exList = (1,2,3,4,5) would return (10,2,3,4,5). 2. dropFromList() method to drop the first n elements from the list. This function accepts an input list and a value for n, and returns a list with the first n elements dropped. For example, calling dropFromList(exList,1) where exList = (1,2,3,4,5) would return (2,3,4,5). 3. sumTwoLists() method that accepts two lists and adds corresponding elements of each list. This function accepts two input lists and returns their sum as illustrated in the example below. For two input lists (1,2,3) and (4,5,6), this function returns (5,7,9) 4. multiplyTwoLists() method that accepts two lists and multiplies corresponding elements in the same way sumTwoLists() adds elements of two arrays. NB: Solution that I earlier got showed some errors when tried to run it. I am also sharing the solution below: sealed class SinglyLinkedList { object Empty : SinglyLinkedList() data class Cons(val head: T, val tail: SinglyLinkedList) : SinglyLinkedList() } fun modifyListHead(list: SinglyLinkedList, newHead: T): SinglyLinkedList = when (list) { is SinglyLinkedList.Empty -> SinglyLinkedList.Empty is SinglyLinkedList.Cons -> SinglyLinkedList.Cons(newHead, list.tail) } fun dropFromList(list: SinglyLinkedList, n: Int): SinglyLinkedList = when (list) { is SinglyLinkedList.Empty -> SinglyLinkedList.Empty is SinglyLinkedList.Cons -> if (n == 0) list else dropFromList(list.tail, n - 1) } fun sumTwoLists(list1: SinglyLinkedList, list2: SinglyLinkedList): SinglyLinkedList =
  • 2. when { list1 is SinglyLinkedList.Empty && list2 is SinglyLinkedList.Empty -> SinglyLinkedList.Empty list1 is SinglyLinkedList.Empty -> list2 list2 is SinglyLinkedList.Empty -> list1 else -> SinglyLinkedList.Cons(list1.head + list2.head, sumTwoLists(list1.tail, list2.tail)) } fun multiplyTwoLists(list1: SinglyLinkedList, list2: SinglyLinkedList): SinglyLinkedList = when { list1 is SinglyLinkedList.Empty || list2 is SinglyLinkedList.Empty -> SinglyLinkedList.Empty else -> SinglyLinkedList.Cons(list1.head * list2.head, multiplyTwoLists(list1.tail, list2.tail)) } fun main(){ val exList = linkedListOf(1, 2, 3, 4, 5) println(exList) // Output: 1 2 3 4 5 val modifiedList = exList.modifyListHead(10) println(modifiedList) // Output: 10 2 3 4 5 val droppedList = exList.dropFromList(1) println(droppedList) // Output: 2 3 4 5 val list1 = linkedListOf(1, 2, 3) val list2 = linkedListOf(4, 5, 6) val sumList = list1.sumTwoLists(list2) println(sumList) // Output: 5 7 9 val multiplyList = list1.multiplyTwoLists(list2) println(multiplyList) // Output: 4 10 18 }