You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
3.7 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. // The SongList class that represents a circular linked list of Song nodes
  2. // Your name here
  3. public class SongList {
  4. // instance variables
  5. private Song m_last;
  6. private int m_numElements;
  7. // constructor
  8. // Do not make any changes to this method!
  9. public SongList() {
  10. m_last = null;
  11. m_numElements = 0;
  12. }
  13. // check whether the list is empty
  14. // Do not make any changes to this method!
  15. boolean isEmpty() {
  16. if (m_last == null)
  17. return true;
  18. else
  19. return false;
  20. }
  21. // return the size of the list (# of Song nodes)
  22. // Do not make any changes to this method!
  23. public int size() {
  24. return m_numElements;
  25. }
  26. // add a new Song to the circular linked list with the given artist and
  27. // title, keeping the list sorted by *song title*.
  28. public void add(String artist, String title) {
  29. Song to_add = new Song(artist, title);
  30. if (m_last == null) {
  31. m_last = to_add;
  32. m_last.setLink(m_last);
  33. }
  34. else if (m_last.getTitle().compareTo(title) <= 0) {
  35. to_add.setLink(m_last.getLink());
  36. m_last.setLink(to_add);
  37. m_last = to_add;
  38. }
  39. else {
  40. // start from last song
  41. Song current = m_last;
  42. while (title.compareTo(current.getLink().getTitle()) >= 0) {
  43. current = current.getLink();
  44. }
  45. to_add.setLink(current.getLink());
  46. current.setLink(to_add);
  47. }
  48. m_numElements += 1;
  49. }
  50. // remove a Song associated with the given artist and title from the list,
  51. // keeping the list sorted by *song title*.
  52. public boolean remove(String artist, String title) {
  53. Song current = m_last;
  54. if (current == null)
  55. return false;
  56. while ((current.getLink().getTitle().compareTo(title) != 0) && (current.getLink().getArtist().compareTo(artist) != 0) && (current.getLink() != m_last)) {
  57. current = current.getLink();
  58. }
  59. if (current.getLink() == m_last) {
  60. if ((m_last.getTitle().compareTo(title) == 0) && (m_last.getArtist().compareTo(artist) == 0)) {
  61. current.setLink(m_last.getLink());
  62. if (current.getTitle().compareTo(current.getLink().getTitle()) >= 0)
  63. m_last = current;
  64. else
  65. m_last = current.getLink();
  66. }
  67. else
  68. return false;
  69. }
  70. else {
  71. current.setLink(current.getLink().getLink());
  72. }
  73. m_numElements -= 1;
  74. return true;
  75. }
  76. // build and return a circular linked list that contains all songs from the
  77. // given artist
  78. public SongList buildList(String artist) {
  79. SongList new_list = new SongList();
  80. Song current = m_last;
  81. if (size() == 1) {
  82. if (current.getArtist().compareTo(artist) == 0)
  83. new_list.add(artist, current.getTitle());
  84. return new_list;
  85. }
  86. for (int i = 0; i < size(); i++) {
  87. if (current.getArtist().compareTo(artist) == 0)
  88. new_list.add(artist, current.getTitle());
  89. current = current.getLink();
  90. }
  91. return new_list;
  92. }
  93. // return a string representation of the list
  94. // Do not make any changes to this method!
  95. public String toString() {
  96. String listContent = "";
  97. Song current = m_last;
  98. if (m_last != null)
  99. do {
  100. current = current.getLink();
  101. listContent += " [" + current.getArtist() + " - " + current.getTitle() + "]\n";
  102. } while (current != m_last);
  103. return listContent;
  104. }
  105. }