Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File
- data class Node<T>(val value: T, var next: Node<T>? = null) {
- constructor(value: T): this(value, null)
- }
- data class President (val firstName:String,val middleName: String = "", val lastName: String, val yearStart:Int, val yearEnd:Int, val party:String) {
- constructor(firstName: String, lastName: String, yearStart: Int, yearEnd: Int, party: String) : this(firstName, "", lastName, yearStart, yearEnd, party)
- }
- class MyList<T> {
- private var head: Node<T>? = null
- private var tail: Node<T>? = null
- var size = 0
- fun add(new: Node<T>) {
- if(head == null) {
- head = new
- tail = new
- } else {
- var tmp = head
- if (tmp != null)
- while(tmp?.next != null)
- tmp = tmp.next
- tmp?.next = new
- tail = new
- }
- size++
- }
- fun show() {
- var tmp = head
- while(tmp != null) {
- println(tmp.value)
- tmp = tmp.next
- }
- }
- }
- fun main(args:Array<String>) {
- val file = File("res/presidents.txt").readLines()
- val presidents = MyList<President>()
- file.forEach {
- val words = it.split("\\s".toRegex())
- val p = if(words.size == 4) {
- val years = words[2].split("[–-]".toRegex()).map{x -> x.toInt()}
- President(words[0], words[1], years[0], years[1], words[3])
- } else {
- val years = words[3].split("[–-]".toRegex()).map{x -> x.toInt()}
- President(words[0], words[1], words[2], years[0], years[1], words[4])
- }
- presidents.add(Node(p))
- }
- print(presidents.size)
- presidents.show()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement