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.

97 lines
3.1 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 {
  35. Song current = m_last.getLink();
  36. while ((current.getTitle().compareTo(title)) > 0 && (current.getLink() != m_last)) {
  37. current = current.getLink();
  38. }
  39. to_add.setLink(current.getLink());
  40. current.setLink(to_add);
  41. if (current.getTitle().compareTo(to_add.getTitle()) > 0)
  42. m_last = null;
  43. }
  44. m_numElements += 1;
  45. }
  46. // remove a Song associated with the given artist and title from the list,
  47. // keeping the list sorted by *song title*.
  48. public boolean remove(String artist, String title) {
  49. Song current = m_last;
  50. if (current == null)
  51. return false;
  52. while ((current.getLink().getTitle().compareTo(title) != 0) && (current.getLink().getArtist().compareTo(artist) != 0)) {
  53. if (current.getLink() == m_last)
  54. return false;
  55. current = current.getLink();
  56. }
  57. current.setLink(current.getLink().getLink());
  58. return true;
  59. }
  60. // build and return a circular linked list that contains all songs from the
  61. // given artist
  62. public SongList buildList(String artist) {
  63. SongList new_list = new SongList();
  64. Song current = m_last;
  65. if (size() == 1) {
  66. if (current.getArtist().compareTo(artist) == 0)
  67. new_list.add(artist, current.getTitle());
  68. return new_list;
  69. }
  70. while (current.getLink() != m_last) {
  71. if (current.getArtist().compareTo(artist) == 0)
  72. new_list.add(artist, current.getTitle());
  73. current = current.getLink();
  74. }
  75. return new_list;
  76. }
  77. // return a string representation of the list
  78. // Do not make any changes to this method!
  79. public String toString() {
  80. String listContent = "";
  81. Song current = m_last;
  82. if (m_last != null)
  83. do {
  84. current = current.getLink();
  85. listContent += " [" + current.getArtist() + " - " + current.getTitle() + "]\n";
  86. } while (current != m_last);
  87. return listContent;
  88. }
  89. }