Untitled
unknown
c_cpp
2 years ago
2.4 kB
5
Indexable
#include <stdio.h> #include <stdlib.h> struct SongType { int data; struct SongType *prev; struct SongType *next; }; typedef struct SongType Song; int CUR_SONG=0; Song *createSong(int val) { Song *newSong = (Song *)malloc(sizeof(Song)); newSong -> data = val; newSong -> prev = NULL; newSong -> next = NULL; return newSong; } Song *addSong(Song *head, int val) { Song *newSong = createSong(val); if(head == NULL) { return newSong; } Song *cur = head; while(cur -> next != NULL) { cur = cur -> next; } cur -> next = newSong; newSong -> prev = cur; return newSong; } Song *createPlaylist () { int n; scanf("%d", &n); Song *head = NULL; for(int i=0; i<n; i++) { int x; scanf("%d",&x); if(head == NULL) { head = addSong(head,x); } else { addSong(head,x); } } return head; } Song *currentPlaying(Song *head) { if(CUR_SONG == 0) { CUR_SONG = head->data; return head; } Song *cur = head; while(cur -> data != CUR_SONG) { cur = cur ->next; } return cur; } Song *previousSong(Song *head) { Song *curPlaying = currentPlaying(head); Song *cur = head; if(curPlaying->prev == NULL) { return curPlaying; } while(cur -> data != curPlaying -> data) { cur = cur->next; } cur = cur->prev; CUR_SONG = cur->data; return cur; } Song *nextSong(Song *head) { Song *curPlaying = currentPlaying(head); Song *cur = head; if(curPlaying -> next == NULL) { return curPlaying; } while(cur -> data != curPlaying -> data) { cur = cur -> next; } cur = cur->next; CUR_SONG = cur -> data; return cur; } int main() { Song *playlist = createPlaylist(); while(1) { int x; scanf("%d", &x); if(x==1) { int ns; scanf("%d", &ns); addSong(playlist,ns); } else if(x==2) { printf("%d\n",currentPlaying(playlist)->data); } else if(x==3) { nextSong(playlist); } else if(x==4) { previousSong(playlist); } else if(x==5) { break; } else { printf("INVALID OPERATION\n"); } } return 0; }
Editor is loading...