diff --git a/.gitignore b/.gitignore index 98a7a09..a92918c 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /meghanadaFormatter.xml +*.class diff --git a/SongList.java b/SongList.java index 3ba75cb..83b9fd2 100644 --- a/SongList.java +++ b/SongList.java @@ -1,24 +1,21 @@ // The SongList class that represents a circular linked list of Song nodes // Your name here -public class SongList -{ +public class SongList { // instance variables private Song m_last; - private int m_numElements; + private int m_numElements; // constructor // Do not make any changes to this method! - public SongList() - { + public SongList() { m_last = null; m_numElements = 0; } // check whether the list is empty // Do not make any changes to this method! - boolean isEmpty() - { + boolean isEmpty() { if (m_last == null) return true; else @@ -27,48 +24,74 @@ public class SongList // return the size of the list (# of Song nodes) // Do not make any changes to this method! - public int size() - { + public int size() { return m_numElements; } - // add a new Song to the circular linked list with the given artist and + // add a new Song to the circular linked list with the given artist and // title, keeping the list sorted by *song title*. - public void add(String artist, String title) - { - // TODO: implement this method + public void add(String artist, String title) { + Song to_add = new Song(artist, title); + if (m_last == null) { + m_last = to_add; + m_last.setLink(m_last); + } + else { + Song current = m_last.getLink(); + while ((current.getTitle().compareTo(title)) > 0 && (current.getLink() != m_last)) { + current = current.getLink(); + } + to_add.setLink(current.getLink()); + current.setLink(to_add); + if (current.getTitle().compareTo(to_add.getTitle()) > 0) + m_last = null; + } + m_numElements += 1; } // remove a Song associated with the given artist and title from the list, // keeping the list sorted by *song title*. - public boolean remove(String artist, String title) - { - // TODO: implement this method + public boolean remove(String artist, String title) { + Song current = m_last; + if (current == null) + return false; + while ((current.getLink().getTitle().compareTo(title) != 0) && (current.getLink().getArtist().compareTo(artist) != 0)) { + if (current.getLink() == m_last) + return false; + current = current.getLink(); + } + current.setLink(current.getLink().getLink()); + return true; } - - + // build and return a circular linked list that contains all songs from the // given artist - public SongList buildList(String artist) - { - // TODO: implement this method + public SongList buildList(String artist) { + SongList new_list = new SongList(); + Song current = m_last; + if (size() == 1) { + if (current.getArtist().compareTo(artist) == 0) + new_list.add(artist, current.getTitle()); + return new_list; + } + while (current.getLink() != m_last) { + if (current.getArtist().compareTo(artist) == 0) + new_list.add(artist, current.getTitle()); + current = current.getLink(); + } + return new_list; } - + // return a string representation of the list // Do not make any changes to this method! - public String toString() - { + public String toString() { String listContent = ""; Song current = m_last; - if (m_last != null) - do - { + do { current = current.getLink(); listContent += " [" + current.getArtist() + " - " + current.getTitle() + "]\n"; - } while (current != m_last); - return listContent; - } -} \ No newline at end of file + } +}