Help With Linked List Please

I have to create a linked list overloading the << and >> operators and output the list, I then have to sort it, but I keep having issues creating the list.

the list is: 25 15 45 35 65 55

here is my code for the read-in and output

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
istream& operator>>(istream& inFile, bubble& inList)
{
       nodeType *newNode;
       int num;
         
       inFile >> num; //priming read
      
       inList.head_ptr = NULL;
         
       while (!inFile.eof()) // read until eof
       {
            
             //if()
             //{
                newNode = new nodeType;
                newNode->info = num;
                newNode->link = inList.head_ptr;
                inList.head_ptr = newNode;
             //}
             
             inFile >> num;

             cout << newNode->info; // for testing purposes
             system("pause");
       }
   
        return inFile;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
ostream& operator<<(ostream& outFile, bubble& outList)
{
         nodeType *current;
         
         current = outList.head_ptr;
         
        
        
         while (current != NULL) //outputs the info of each node in list
         {
               outFile << current->info;
               current = current->link;
         }
         
         outFile << endl; //creates nwln for each list

         return outFile;
}


My issue is when I call the code in my main program the list is empty, I think this is because it is reading the actual EOF and putting it in the list, thus destroying it.

Any help is greatly appreciated.
My issue is when I call the code in my main program the list is empty, I think this is because it is reading the actual EOF and putting it in the list, thus destroying it.
Nope. As far as I can see your reading is fine.

Your writing is wrong. You need a blank to separate your numbers on line 11. Like so:

outFile << current->info << " ";
No nothing is being written to the output file at all here is my main 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
#include <fstream>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cctype>

using namespace std;

#include "program3_classbubble.cpp"

int main ()
{
    ifstream data;
    ofstream out;
    bubble numberList;
    
    data.open ("bubbleinput.txt");
    if (!data)
    {
              cout << "failure to open bubbleinput.txt" << endl;
              return 1;
    }
    out.open ("bubbleoutput.txt");
    if (!out)
    {
              cout << "failure to open bubbleoutput.txt" << endl;
              return 1;
    } 
    
    // main body program, reads in values in an EOF loop then adds the lists
    
    
    if (data)
    {
        data >> numberList; // priming read
        
        //numberList.bubblesort();
    }
    while (data)
    {
         out << numberList;
         data >> numberList;
         //numberList.bubblesort();
    }    
    
    data.close(); out.close();
    
    system("pause");
    
    return 0;
}


I had to do this before but we did a character read using the get(character) function and converted each individual number to from ASCII to dec, my professor said we didnt need to worry about all of that this time.

This way works but its neot reading in the last number.

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
istream& operator>>(istream& inFile, bubble& inList)
{
       nodeType *newNode;
       string cnum;
         
       getline(inFile,cnum,' '); //priming read
      
       inList.head_ptr = NULL;
         
       while (!inFile.eof()) // read until eof
       {
            
             //if()
             //{
                newNode = new nodeType;
                newNode->info = cnum;
                newNode->link = inList.head_ptr;
                inList.head_ptr = newNode;
             //}
             
             getline(inFile,cnum,' ');
       }
   
        return inFile;
}
Last edited on
I am also having issues with my sort. Since I am building my list backwards the list as follows is

25 15 45 35 65 55

after read in (with the error of losing last number for now)

65 35 45 15 25

after the sort it is

65 15 25 35 45

it does this for any list whose highest number is at the front to start, any other case and it works.

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
void bubble::bubblesort()
{
nodeType *current, *next, *previous;
bool swap;

//loop only if there are elements in list

swap = (head_ptr != NULL);

while (swap)
{
    //only continue loop if a swap is made

    swap = false;

    current = head_ptr;
    next = current->link;
    previous = NULL;

    while (next != NULL)
    {
        if (current->info > next->info)
        {
            swap = true;

            //swap elements

            if (current == head_ptr)
            {
                nodeType *temp;
                
                head_ptr = next;
                temp = next->link;
                next->link = current;
                current->link = temp;
                current = head_ptr;
            }
            else
            {
                previous->link = current->link;
                current->link = next->link;
                next->link = current;
                current = next;
            }
        }
        
        //increment elements

        previous = current;
        current = current->link;
        next = current->link;
    }
}
}
Last edited on
So with string it works and with int not? Surprising. It certainly should

This way works but its neot reading in the last number.
Then put line 21 before line 15. insert only if the string is not empty(). It seems it reads the value and sets the eof at the same time (it shouldn't)

To determine surprising behaviour like this you should debug (if you can)
Thats excactly what my dad told me, but we really never touched up on debugging outside of the obvious warnings. We use Bloodshed Dev C++.

I think the problem with the int is it is reading in the EOF and basically inserting NULL into the head_ptr of the list, and inside the constructor for the list if head is NULL it destroys the entire list thinking the whole thing is empty.

Your solution did work, I was getting close to it myself but was forgetting to move the read in to be first.
Last edited on
I think the problem with the int is it is reading in the EOF and basically inserting NULL into the head_ptr of the list, and inside the constructor for the list if head is NULL it destroys the entire list thinking the whole thing is empty.
I don't think so (because the string/getline version works). It rather looks like that there's a non numeric value somewhere at the beginning of the file that makes the stream go bad.

If you don't debug then I'd recommend that you spread much more 'cout' to see what at a particular position happens.
You are right i told it to stop before it reaches the 91 and it worked fine. My file in notepad ++ reads as:

(list of numbers) CR LF

the problem using the string is the sort. It puts 103 before 12 because 0 comes before 2. It seems I need to properly mark EOF or somehow tell it to only insert into the list if the int is not equal to a newline character.
ok so I spread the couts around and found out that with this code it does print out the entire list in cout, but my function for the ostream is not being called to write it to the file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
istream& operator>>(istream& inFile, bubble& inList)
{
       nodeType *newNode;
       int num;
         
       inFile >> num; // priming read
       
       inList.head_ptr = NULL;
         
       while (inFile) // read until eof
       {
                newNode = new nodeType;
                newNode->info = num;
                newNode->link = inList.head_ptr;
                inList.head_ptr = newNode;
                
                inFile >> num;
                
                cout << inList;
        system("pause");
       }
   
       return inFile;
}


how ever when i change the While condition to:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
istream& operator>>(istream& inFile, bubble& inList)
{
       nodeType *newNode;
       int num;
         
       inFile >> num; // priming read
       
       inList.head_ptr = NULL;
         
       while (num != 91 && inFile) // read until eof
       {
                newNode = new nodeType;
                newNode->info = num;
                newNode->link = inList.head_ptr;
                inList.head_ptr = newNode;
                
                inFile >> num;
                
                cout << inList;
        system("pause");
       }
   
       return inFile;
}


The ostream function is being called. Very strange.
I got it to work like so:

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
istream& operator>>(istream& inFile, bubble& inList)
{
       nodeType *newNode;
       int num;
       char ch; //exists only to control the EOF
         
       ch = ' '; // priming read for control
       
       inList.head_ptr = NULL;
         
      while (ch != nwln && inFile) // read until eof
       {
                inFile >> num; // updates at beginning to not lose last number
                
                if(!isdigit(ch)) // only insert if ch is not a number
                {
                newNode = new nodeType;
                newNode->info = num;
                newNode->link = inList.head_ptr;
                inList.head_ptr = newNode;
                }
                
                inFile.get(ch); // update for EOF control
       }
           
      return inFile;
}
Last edited on
Topic archived. No new replies allowed.