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.

101 lines
3.2 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
  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)) {
  57. if (current.getLink() == m_last)
  58. return false;
  59. current = current.getLink();
  60. }
  61. current.setLink(current.getLink().getLink());
  62. return true;
  63. }
  64. // build and return a circular linked list that contains all songs from the
  65. // given artist
  66. public SongList buildList(String artist) {
  67. SongList new_list = new SongList();
  68. Song current = m_last;
  69. if (size() == 1) {
  70. if (current.getArtist().compareTo(artist) == 0)
  71. new_list.add(artist, current.getTitle());
  72. return new_list;
  73. }
  74. while (current.getLink() != m_last) {
  75. if (current.getArtist().compareTo(artist) == 0)
  76. new_list.add(artist, current.getTitle());
  77. current = current.getLink();
  78. }
  79. return new_list;
  80. }
  81. // return a string representation of the list
  82. // Do not make any changes to this method!
  83. public String toString() {
  84. String listContent = "";
  85. Song current = m_last;
  86. if (m_last != null)
  87. do {
  88. current = current.getLink();
  89. listContent += " [" + current.getArtist() + " - " + current.getTitle() + "]\n";
  90. } while (current != m_last);
  91. return listContent;
  92. }
  93. }