Issue with Ordered Link List

I have a program that is supposed to read integers from a file and then create a link where the numbers would be in the correct order. If the node all ready exists, then I should increment the count of the matching node by one to count how many times the number appears in the file. The sorting and counting are obviously not correct by looking at the input and the output received. I would appreciate any help or suggestions anyone might can offer. Thanks in advance for your time!

Sample of input file:
2 28 15 21 10 5 39 39 3 25 26 26 2 28 2 12 36 23 28 37 32 5 23 34 13
23 22 37 39 16 8 7 12 19 30 33 28 20 36 15 24 12 3 14 34 25 22 32 14 29
28 5 23 38 38 20 4 22 10 19 37 36 31 3 9 27 1 23 25 9 7 6 11 10 11


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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>

using namespace std;

//**********Classes**********
class LList
{
    public:
    int value;
    int count;
    LList *linky;
};
//********End Classes********

typedef LList *Node;
Node ListHead;

//**********Function Prototypes**********
void Insert(int);
Node NewNode();
void Print();
//********End Function Prototypes********

//Declare file streams
ifstream inFile;
ofstream outFile;

int main()
{
    //Declare variables
    int tempVal;

    //Open inFile
    inFile.open("LinkNbrs.dat");

    while(!inFile)
    {
        cout << "Error opening the inFile." << endl;
        return 1;
    }

    //Open outFile
    outFile.open("List_Results.dat");

    while(!outFile)
    {
        cout << "Error opening the outFile." << endl;
        return 1;
    }

    //Prime the read
    inFile >> tempVal;

    while(inFile)
    {
        Insert(tempVal);
        inFile >> tempVal;
    }//End While

     Print();

    //Close the files
    inFile.close();
    outFile.close();

    return 0;
}

//**********Function Definitions**********
void Insert(int N)
{
    Node c, p, temp;

//Head is null, so put it at the beginning of the list
    if(ListHead == NULL)
    {
        ListHead = NewNode();
        ListHead->value = N;
        ListHead->linky = NULL;
    }
    else
    {
//Head is not null so sort it to see where it goes
        c = ListHead;

        while(c != NULL && c->value < N)
        {
            p = c;
            c = c->linky;
        }


//Node does not exist so create a new node
        if((N != p->value))
        {
        temp = NewNode();
        temp->value = N;
        p->linky = temp;
        }
        else
        {
//Node exists so increase the count by 1
           p->count = p->count + 1;
        }
    }
}

Node NewNode()
{
    LList *temp;
    temp = new LList();
    temp->value = 0;
    temp->linky = NULL;
    temp->count = 1;

    return temp;
}

void Print()
{
    Node c;
    c = ListHead;

    while(c)
    {
        cout << c->value << "-" << c->count << endl;
        c = c->linky;
    }
}
//********End Function Definitions********



Output:

2-2
1-1
2-1
3-1
7-1
17-1
18-1
23-1

Process returned 0 (0X0) execution time: 0.040 s
Press any key to continue.
1
2
3
4
5
6
7
8
9
//Node does not exist so create a new node
        if((N != p->value))
        {
        c = p->linky;
        temp = NewNode();
        temp->value = N;
        p->linky = temp;
        temp->linky = c;
        }

Try this change and see if it helps, I have not tested it but reading through your code it doesn't look like your connecting temp to the next node.
EDIT Added another change. You should note you dont take into consideration if the new node to add needs to be inserted as the new head.
Last edited on
Thanks! It gave me some ideas so I changed my code around a bit. I didn't have it linked together properly. It is now sorting and printing, but the count is wrong. It lists each number separately in a sorted order instead of grouping them. Any more suggestions or ideas are greatly appreciated! Thanks in advance for your time!

Current output:
1-1
1-1
1-1
2-1
2-1
3-1
3-1
3-1
3-1

Instead of:
1-3
2-2
3-4


Here is the code that I modified:
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
void Insert(LList *c, int N)
{
    //Set the variables
    Node p, temp;
    c = ListHead;
    p = NULL;

    //Determine where variable needs to go
    while(c != NULL && c->value < N)
    {
        p = c;
        c = c->linky;
    }

    if(p == NULL)
    {
        //New Head Placement
        temp = NewNode();
        temp->value = N;
        temp->linky = c;
        ListHead = temp;
    }
    else if((N != p->value))
    {
        //Create a new node
        temp = NewNode();
        temp->value = N;
        p->linky = temp;
        temp->linky = c;
    }
    else
    {
        //Not working properly, retains the 1 count from the node creation
        //Does not create a new node, Should add 1 to the count of the matching value node
        p->count = p->count + 1;
    }
}
while(c != NULL && c->value <= N)
Last edited on
That worked, thanks so much for your help. I really do appreciate it.
Topic archived. No new replies allowed.