Advertisement
ziobrowskyy

ivanov

Mar 27th, 2020
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.72 KB | None | 0 0
  1. import java.io.File
  2.  
  3. data class Node<T>(val value: T, var next: Node<T>? = null) {
  4.     constructor(value: T): this(value, null)
  5. }
  6. data class President (val firstName:String,val middleName: String = "", val lastName: String, val yearStart:Int, val yearEnd:Int, val party:String) {
  7.     constructor(firstName: String, lastName: String, yearStart: Int, yearEnd: Int, party: String) : this(firstName, "", lastName, yearStart, yearEnd, party)
  8. }
  9.  
  10. class MyList<T> {
  11.     private var head: Node<T>? = null
  12.     private var tail: Node<T>? = null
  13.     var size = 0
  14.     fun add(new: Node<T>) {
  15.         if(head == null) {
  16.             head = new
  17.             tail = new
  18.         } else {
  19.             var tmp = head
  20.             if (tmp != null)
  21.                 while(tmp?.next != null)
  22.                     tmp = tmp.next
  23.             tmp?.next = new
  24.             tail = new
  25.         }
  26.         size++
  27.     }
  28.     fun show() {
  29.         var tmp = head
  30.         while(tmp != null) {
  31.             println(tmp.value)
  32.             tmp = tmp.next
  33.         }
  34.     }
  35.  
  36. }
  37.  
  38. fun main(args:Array<String>) {
  39.  
  40.     val file = File("res/presidents.txt").readLines()
  41.     val presidents = MyList<President>()
  42.     file.forEach {
  43.         val words = it.split("\\s".toRegex())
  44.  
  45.         val p = if(words.size == 4) {
  46.             val years = words[2].split("[–-]".toRegex()).map{x -> x.toInt()}
  47.             President(words[0], words[1], years[0], years[1], words[3])
  48.             } else {
  49.             val years = words[3].split("[–-]".toRegex()).map{x -> x.toInt()}
  50.             President(words[0], words[1], words[2], years[0], years[1], words[4])
  51.             }
  52.  
  53.         presidents.add(Node(p))
  54.  
  55.     }
  56.     print(presidents.size)
  57.     presidents.show()
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement