Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
K
Kotlin_FP-to-OOP
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ak6623
Kotlin_FP-to-OOP
Commits
adf8dadb
Commit
adf8dadb
authored
3 months ago
by
ak6623
Browse files
Options
Downloads
Patches
Plain Diff
Update file LinkedListExample.kt
parent
283d39e4
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/kotlin/lecture8/LinkedListExample.kt
+65
-0
65 additions, 0 deletions
src/main/kotlin/lecture8/LinkedListExample.kt
with
65 additions
and
0 deletions
src/main/kotlin/lecture8/LinkedListExample.kt
+
65
−
0
View file @
adf8dadb
package
lecture8
class
LinkedList
(){
var
head
:
Node
<
T
>?
=
null
class
Node
<
T
>(
val
data
:
T
,
var
next
:
Node
<
T
>
=
null
){
//initialize it as null by default
fun
add
(
node
:
Node
<
T
>){
if
(
next
==
null
)
{
next
=
node
}
else
{
next
?.
add
(
null
)
}
}
fun
next
()
=
next
}
fun
add
(
t
:
T
){
if
(
head
==
null
){
head
=
Node
(
t
)
}
else
{
head
?.
add
(
Node
(
t
))
}
}
override
fun
toString
():
String
{
val
sb
=
StringBuilder
()
//iterate thorugh elements
sb
.
append
(
"["
)
sb
.
append
(
head
.
toString
())
var
current
=
head
while
(
current
!=){
sb
.
append
(
", "
)
sb
.
append
(
current
.
toString
())
current
=
current
.
next
}
sb
.
append
(
"]"
)
return
sb
.
toString
()
}
fun
forEach
(
f
:
(
T
)
->
Unit
)
{
f
(
head
?.
data
)
var
current
=
head
?.
next
while
(
current
!=
null
){
//line to apply f to element
f
(
current
?.
data
)
current
=
current
.
next
}
}
}
// if we want default arguments in a function, you want to put it at the end of all arguments, otherwise you have to use keywords(???)
fun
main
(){
val
l
=
linkedList
<
Int
>()
l
.
add
(
6
)
l
.
add
(
12
)
l
.
add
(
40009
)
l
.
add
(
2
)
println
(
l
)
l
.
forEach
{
x
->
println
(
x
)}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment