-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment3.cpp
More file actions
115 lines (107 loc) · 2.71 KB
/
Copy pathassignment3.cpp
File metadata and controls
115 lines (107 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include<iostream>
using namespace std;
class Song{
public:
string Title;
Song* nextSong;
Song(){
Title = " ";
nextSong = NULL;
}
Song(string newTitle){
Title = newTitle;
nextSong = NULL;
}
};
class Playlist{
public:
Song *firstSong = NULL;
void addSongAtFirst(string newTitle );
void addSongAtLast(string newTitle );
void addSongBet(string newTitle, int key);
void removeSongAtFirst( );
void removeSongAtLast();
void removeSongBet(int key);
void print();
};
void Playlist::addSongAtFirst(string newTitle ){
Song *newSong = new Song(newTitle);
if(firstSong == NULL){
cout<<"List is Empty"<<endl;
firstSong = newSong;
return;
}
newSong->nextSong = firstSong;
firstSong = newSong;
}
void Playlist::addSongAtLast(string newTitle ){
Song* newSong = new Song(newTitle);
Song* temp = firstSong;
while(temp->nextSong != NULL){
temp = temp->nextSong;
}
temp->nextSong = newSong;
}
void Playlist::addSongBet(string newTitle, int key){
int pos=0;
Song *newSong = new Song(newTitle);
Song* temp = firstSong;
while((pos+1) != key){
temp = temp->nextSong;
pos++;
}
newSong->nextSong = temp->nextSong;
temp->nextSong = newSong;
}
void Playlist::removeSongAtFirst(){
Song* temp = firstSong;
firstSong = temp->nextSong;
free(temp);
}
void Playlist::removeSongAtLast(){
Song* last2nd = firstSong;
while(last2nd->nextSong->nextSong != NULL){
last2nd = last2nd->nextSong;
}
Song* temp = last2nd->nextSong;
last2nd->nextSong = NULL;
free(temp);
}
void Playlist::removeSongBet(int key){
int pos = 0;
Song* songAtpos = firstSong;
while((pos+1) != key){
if(key == 0){
removeSongAtFirst();
return;
}
songAtpos = songAtpos->nextSong;
pos++;
}
Song* temp = songAtpos->nextSong;
songAtpos->nextSong = songAtpos->nextSong->nextSong;
free(temp);
}
void Playlist::print(){
Song *temp = firstSong;
while(temp != NULL){
cout << temp->Title <<" --> ";
temp = temp->nextSong;
}
cout<<"NULL"<<endl;
}
int main() {
Playlist list;
list.addSongAtFirst("Tu Janne Na");
list.addSongAtFirst("Shape of you");
list.addSongAtLast("Mera Mann");
list.addSongAtLast("Tumhare hi rahenge hum");
list.addSongBet("Khoobsurat", 2);
list.removeSongAtFirst();
list.removeSongAtLast();
list.removeSongBet(1);
cout<<"Songs are: "<<endl;
list.print();
cout<<endl;
return 0;
}