Create a Stack class that inherits from your SinglyList or DoublyList class Usin
ID: 3733657 • Letter: C
Question
Create a Stack class that inherits from your SinglyList or DoublyList class Using C++ .
Note: Your SinglyList or Doubly list class will need to be able to hold nodes that are of the type Song. Meaning objects of the type Song.
Implements the following functions:
Push
-- pushes item on the stack
Pop
-- pops the last item off of the stack
Use the code from the favorite Song assignment to:
Push all the favorite songs onto the stack
Push the following songs onto the stack:
--Artist: Drake, Title: "God's Plan", Genre: hip-hop, Duration:4.03
--Artist: Drake, Title: "look a live", Genre: hip-hop, Duration: 3.35
--Artist: SZA, Title: "Love Galor", Genre: RnB, Duration: 4.44
Pop all the last three songs off of the stack.
Explanation / Answer
#include<cstdio>
#include<iostream>
#include<string.h>
#include<cstdlib>
using namespace std;
struct song{
char name[100],genre[20];
double duration;
struct song *next;
};
class sll{
struct song *head;
public :
void insert(char *nam,char *gen,double dura){
song *temp=(song *)malloc(sizeof(struct song));
strcpy(temp->name,nam);
strcpy(temp->genre,gen);
temp->duration=dura;
temp->next=NULL;
if(head==NULL)head=temp;
else{
temp->next=head;
head=temp;
}
}
void delt(){
cout<<"-- Artist : "<<head->name<<"Genre : "<<head->genre<<" Duration : "<< head->duration <<" ";
song *temp=head;
head=head->next;
delete(temp);
}
};
class stack{
sll sta;
public :
void push(){
char song[100],genre[20];double duration;
gets(song);
gets(genre);
cin>>duration;
sta.insert(song,genre,duration);
}
void pop(){
sta.delt();
}
};
int main(){
stack s1;
s1.push(); //call push and pop as many times as u want.....
s1.pop();
return 0;
}
// I hope u'll get something from my work . Thanks in advance....