Java设计一个播放器,链表实现,播放器列表的功能应包括:添加歌曲(addSong)、播放歌曲(play)、删除歌曲(removeSong)、将歌曲位置上移和下移(moveUp、moveDown)等等。
时间: 2023-07-10 16:16:08 浏览: 142
java语言设计 音乐播放器
5星 · 资源好评率100%
好的,我可以为您解答这个问题。
首先,我们需要先设计一个歌曲类Song,包括歌曲名称、歌手、时长等属性。然后,我们可以设计一个节点类Node,包括当前节点的歌曲信息以及指向前一个节点和后一个节点的指针。接着,我们可以设计一个链表类LinkedList,包括链表的头节点、尾节点以及链表的长度等属性。链表类LinkedList还可以包括添加歌曲、播放歌曲、删除歌曲、将歌曲位置上移和下移等方法。
以下是Java代码的实现:
```java
class Song {
private String name;
private String artist;
private int duration;
public Song(String name, String artist, int duration) {
this.name = name;
this.artist = artist;
this.duration = duration;
}
public String getName() {
return name;
}
public String getArtist() {
return artist;
}
public int getDuration() {
return duration;
}
}
class Node {
private Song song;
private Node prev;
private Node next;
public Node(Song song) {
this.song = song;
this.prev = null;
this.next = null;
}
public Song getSong() {
return song;
}
public Node getPrev() {
return prev;
}
public Node getNext() {
return next;
}
public void setPrev(Node prev) {
this.prev = prev;
}
public void setNext(Node next) {
this.next = next;
}
}
class LinkedList {
private Node head;
private Node tail;
private int size;
public LinkedList() {
this.head = null;
this.tail = null;
this.size = 0;
}
public int getSize() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
public void addSong(Song song) {
Node node = new Node(song);
if (isEmpty()) {
head = node;
tail = node;
} else {
tail.setNext(node);
node.setPrev(tail);
tail = node;
}
size++;
}
public void play(int index) {
if (index >= 0 && index < size) {
Node current = head;
for (int i = 0; i < index; i++) {
current = current.getNext();
}
System.out.println("Now playing: " + current.getSong().getName() + " - " + current.getSong().getArtist());
} else {
System.out.println("Invalid index!");
}
}
public void removeSong(int index) {
if (index >= 0 && index < size) {
Node current = head;
for (int i = 0; i < index; i++) {
current = current.getNext();
}
if (current == head) {
head = current.getNext();
head.setPrev(null);
} else if (current == tail) {
tail = current.getPrev();
tail.setNext(null);
} else {
current.getPrev().setNext(current.getNext());
current.getNext().setPrev(current.getPrev());
}
size--;
} else {
System.out.println("Invalid index!");
}
}
public void moveUp(int index) {
if (index > 0 && index < size) {
Node current = head;
for (int i = 0; i < index; i++) {
current = current.getNext();
}
Node prev = current.getPrev();
Node next = current.getNext();
if (current == tail) {
tail = prev;
}
current.setPrev(prev.getPrev());
current.setNext(prev);
prev.setPrev(current);
prev.setNext(next);
next.setPrev(prev);
prev.setNext(next.getPrev());
} else {
System.out.println("Invalid index!");
}
}
public void moveDown(int index) {
if (index >= 0 && index < size - 1) {
Node current = head;
for (int i = 0; i < index; i++) {
current = current.getNext();
}
Node prev = current.getPrev();
Node next = current.getNext();
if (current == head) {
head = next;
}
current.setPrev(next);
current.setNext(next.getNext());
next.setPrev(prev);
next.setNext(current);
prev.setNext(next);
} else {
System.out.println("Invalid index!");
}
}
}
```
通过上面的代码,我们就可以使用链表来实现一个播放器的列表功能了。
阅读全文