Linked list problems

Could someone please tell me what is wrong with my code.

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
// poddriver.cpp

//Adapted from Roger Priebe
//CS2308 11/17/08

#include <stdlib.h>
#include <iostream>
#include "bobcatPod.h"

using namespace std;

int main()
{
    
    Song s1("Frank Sinatra", "My way", 14);
    Song s2("Beatles", "Act Naturally", 5);
    Song s3;
    
    BobCatPod p(256);
    BobCatPod q(512);
    BobCatPod r(25);

    cout << "pod p" << endl << p << endl;
    cout << "pod q, size 512 exceeds max, should be reset to 256 " << endl
         << q << endl;
    cout << "pod r, size should be 25 " << endl << r << endl;


    cout << "Song 1" << s1 << endl;
    cout << "Song 2" << s2 << endl;
    cout << "Song 3" << s3 << endl;

    s3.setArtist("Buck Owens");
    s3.setTitle("Act Naturally");
    s3.setSize(20);

    cout << "Song 3 updated " << s3 << endl;
    cout << "Artist 1 (Frank Sinatra)  " << s1.getArtist() << endl;
    cout << "Title 2 (Act Naturally)  " << s2.getTitle() << endl;
//    s1.setSize(7);
//    cout << "Size 1 (7) " << s1.getSize() << endl;

//test relational operators

    if (s1 < s3 ) cout << endl << "s1 < s3" << endl;
      else cout << endl << "s1 >= s3" << endl;
    if (s1 == s2) cout << endl << "s1 == s2" << endl;
      else cout << endl << "s1 != s2"  << endl;
    
// test addnode
    cout << "add song 1, size 14 " << r.addSong(s1)  
         << "   memory left = "  << r.getRemainingMemory() <<endl;
    cout << "add song 2, size 5 " << r.addSong(s2) 
          << "   memory left = "  << r.getRemainingMemory() << endl;
    cout << "add song 3, size 20 should fail " << r.addSong(s3) 
         << "   memory left = "  << r.getRemainingMemory() << endl;
  
    s3.setSize(2);
    cout << "Size 3 (2) " << s3.getSize() << endl;
    cout << "add song 3, size 2 should succeed " << r.addSong(s3) 
         << "   memory left = "  << r.getRemainingMemory() << endl;


// test output
    cout << r << endl;

//test delete

    r.removeSong(s1);
    cout << r << endl;
    r.removeSong(s3);
    cout << r << endl;

    cout << "Total Memory"<< r.getTotalMemory() << endl;





    return 0;
}


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
//song.h
#ifndef SONG_H
#define SONGT_H
#include <stdlib.h>
#include <iostream>
#include <ostream>
#include <string>

using namespace std;


class Song
{
  
   public:
      Song::Song ();
      Song::Song(string , string , int ); // constructor
      void setSize(int s);
      double getSize(); // access or
      void setTitle (string t);
      string getTitle();
      void setArtist (string a);
      string getArtist();
      void addSong (int);
      friend ostream& operator<<(ostream & os, Song & s);
   private:
      string title;    //dynamic allocation
      string artist;
      int size;
};
#endif 


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
// file song.cpp
#include "song.h"
#include "bobcatPod.h"

 ostream & operator << ( ostream & os, Song & s)
 {
      os << s.getTitle();
      os  << s.getArtist();
      return os;
 }
//constructor
Song::Song ()
{
    title=" ";
    artist=" ";
    size = 0;
}
Song::Song(string a, string t, int s) // constructor
{
    size = s;
    title = t;
    
    artist = a ;
    
}
//accessor for name
string Song :: getTitle()
{
      return title;
}
//mutator
void Song :: setTitle (string t)
{
     title = t;
     
}
string Song :: getArtist()
{
      return artist;
}
//mutator
void Song :: setArtist (string a)
{
     artist = a;
     
}
void Song::setSize(int s)
{
    size = s;
}
double Song::getSize()
{
    return size;
}


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
#define BOBCATPOD_H

class bobcatPod
{
      private:
              //Declare a structure for the list
              struct SongNode
      {
             Song s;                  //value in this node
             SongNode *next;      //To point to the next node
      };
      SongNode *head;
      
      public:
             //Constructor
             bobcatPod()
                {head = NULL;}
                
             //Destructor
             ~bobcatPod();
             
             //Linked list operations
             void appendNode (Song s);
             int addSong (Song s);
             int removeSong (Song s);
             void showSongList ();
             int getTotalMemory();
             int getRemainingMemory();
};
#endif 


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
#include "song.h"
#include "bobcatPod.h"
void bobcatPod::appendNode (Song num)
{
     SongNode *newNode;   //to point to a new node
     SongNode *nodePtr;   //to move through the list
     //allocate a new node and stor num there.
     newNode = new SongNode;
     newNode->s = num;
     newNode->next = NULL;
     
     //if there are no nodes in the list
     //make newNode the first node
     if (!head)
         head = newNode;
     else     //otherwise, insert newNOde at end
     {
              //initialize nodePtr to head of list
              nodePtr = head;
              
              //find the last node in the list
              while (nodePtr->next)
                 nodePtr = nodePtr->next;
                 
              //insert newNode as the last node.
              nodePtr->next = newNode;
     }
}


int bobcatPod::addSong(Song s)
{
     Song *newSong;     //a new node
     Song *nodePrt;     //to traverse the list
     Song *previousSong = NULL;       //The previous node
     
     // Allocate a new node and stor num there.
     newSong = new Song;
     
     newSong->next = NULL;
     
     
     //If ther are no nodes in the list
     //make newSong the first node
     if (!head)
     {
               head = newSong;
               newSong -> next = NULL;
     }
     else      //Otherwise, insert newSong
     {
               //Position nodePtr at the head of list.
               nodePtr = head;
               //Initialize prefiousSong to NULL.
               previousSong = NULL
               
               //skip all nodes whose value is less than num.
               while (nodePtr != NULL && nodePtr ->value <num)
               {
                     previousSong = nodePtr;
                     nodePtr = nodePtr->next;
               }
               //If the new Song is to be the 1s in the list,
               //insert it before all other nodes.
               if (previousSong ==NULL)
               {
                                head = newSong;
                                newSong ->next = nodePtr;
               }
               else             //Otherwise insert after the previous node.
               {
                                previousSong->next = newSong;
                                newSong-> next = nodePrt;
               }
     }
}
                                
               


The driver and song files work, but there is something wrong with bobpod files.
Some errors I've noted:

1.Song.h

1
2
#ifndef SONG_H
#define SONGT_H // ERROR - misspelling - header guard doesn't work 


1
2
3
4
5
6
class Song
{
  
   public:
      Song::Song (); //ERROR - illegal to have  :: here
      Song::Song(string , string , int ); // constructor //ERROR illegal :: 


2. bobcatpod.h
Header gaurd is incorrect - but i reckon you cut off the first line when copying and pasting the code.

Also bobcatpod.h needs to #include song.h

3. In the main function

1
2
3
4
5
6
7
8
9
 
//The following 3 lines are errors because if you look at
//bobcatpd.h - The name of the actual class is bobcatPod  NOT   BobCatPod

//And also you do not have a constructor defined for bobcatPod class that takes an integer parameter.

 BobCatPod p(256);
 BobCatPod q(512);
 BobCatPod r(25);


1
2
3
4
    
//You have not written a << operator for the bobcatPod class 
//(You have done it for the Song class but not for bobcatPod)
cout << "pod p" << endl << p << endl; //ERROR  



Well that's a start...

Thanks so much. But I am getting an error here
newSong->next = NULL; it says class song has no member named next. I don't know what to do with that that is what is in my book.
It's just what the error says. 'song' has no member 'next'. Only 'bobcatPod' has a 'next'. Check with the book the type of newSong. If the book says it's song, then there's an error in the book.
judging by the mismatched code and comments at the start of the addSong function, I reckon you cut and pasted code and then inccorrectly edited it.

Recheck.
Last edited on
here are my current codes
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
//BobCatPod.h

//Specification files for the SongList class

#ifndef BOBCATPOD_H
#define BOBCATPOD_H
#include "song.h"

class BobCatPod
{
      private:
              //Declare a structure for the list
              struct SongNode
      {
             Song s;                  //value in this node
             SongNode *next;      //To point to the next node
      };
      SongNode *head;
      
      public:
             //Constructor
             BobCatPod()
                {head = NULL;}
                
             //Destructor
             ~BobCatPod();
             if( !pNumber )
             {
                 //Memory is BAD
             }
             
             //Linked list operations
             void appendNode (Song s);
             int addSong (Song s);
             int removeSong (Song s);
             void showSongList ();
             int getTotalMemory();
             int getRemainingMemory();
             friend ostream& operator<<(ostream & os, Song & s);
};
#endif 


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
//BobCatPod.cpp
#include "song.h"

void BobCatPod::appendNode (Song num)
{
     SongNode *newNode;   //to point to a new node
     SongNode *nodePtr;   //to move through the list
     //allocate a new node and stor num there.
     newNode = new SongNode;
     newNode->s = num;
     newNode->next = NULL;
     
     //if there are no nodes in the list
     //make newNode the first node
     if (!head)
         head = newNode;
     else     //otherwise, insert newNOde at end
     {
              //initialize nodePtr to head of list
              nodePtr = head;
              
              //find the last node in the list
              while (nodePtr->next)
                 nodePtr = nodePtr->next;
                 
              //insert newNode as the last node.
              nodePtr->next = newNode;
     }
}


int BobCatPod::addSong(Song s)
{
     Song *newSong;     //a new node
     Song *nodePrt;     //to traverse the list
     Song *previousSong = NULL;       //The previous node
     
     // Allocate a new node and stor num there.
     newSong = new Song;
     
     newSong->next = NULL;
     
     
     //If ther are no nodes in the list
     //make newSong the first node
     if (!head)
     {
               head = newSong;
               newSong -> next = NULL;
     }
     else      //Otherwise, insert newSong
     {
               //Position nodePtr at the head of list.
               nodePtr = head;
               //Initialize prefiousSong to NULL.
               previousSong = NULL
               
               //skip all nodes whose value is less than num.
               while (nodePtr != NULL && nodePtr ->value <num)
               {
                     previousSong = nodePtr;
                     nodePtr = nodePtr->next;
               }
               //If the new Song is to be the 1s in the list,
               //insert it before all other nodes.
               if (previousSong ==NULL)
               {
                                head = newSong;
                                newSong ->next = nodePtr;
               }
               else             //Otherwise insert after the previous node.
               {
                                previousSong->next = newSong;
                                newSong-> next = nodePrt;
               }
     }
}
                                
               
Just reading through that int BobCatPod::addSong(Song s) function, you can see that it is wrong.

For a start, look at the void BobCatPod::appendNode (Song num) function, the way that it starts off with:
1
2
3
     SongNode *newNode;   //to point to a new node
     SongNode *nodePtr;   //to move through the list
     //allocate a new node and stor num there. 

Now look again at the int BobCatPod::addSong(Song s) function and you will see what I mean.

Also halfway through the int BobCatPod::addSong(Song s) function, we have some variables value and num
(in the while (nodePtr != NULL && nodePtr ->value <num) loop) - where did these variables suddenly appear from??)


Also for the BobCatPod class, you have added the function friend ostream& operator<<(ostream & os, Song & s);.
That isn't going to work if you want to << a BobCatPod object to an ostream (as a mater of fact it will give you a multiple definition error);
Topic archived. No new replies allowed.