Sorting a linked list

After looking around for some solutions, I haven't encountered any problems with a Node class as a private member of another class. I've tried fiddling around with the implementation of those solutions in order to make it work with this, but the access specification is what hasn't allowed me to do this yet.

If someone could help me figure out what the function call looks like, I'm sure I'd have no problem of figuring it out from there.

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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
  #include <iostream>
#include <cstdlib>

using std::cout;    using std::cin;     using std::endl;

class LinkedList
{
private:
    struct Node
    {
        int data;
        Node* next;
    };
    Node* curr;
    Node* head;
    Node* temp;
public:
    LinkedList();
    ~LinkedList();
    void appendNode(int);
    void deleteNode(int);
    void insertNode(int);
    void print();
    void reverse();
    int search(int);
    void insertAt(int, int);
    void mergeArray(double [], int);
    void sort();
};

LinkedList::LinkedList()
{
    curr = NULL;
    head = NULL;
    temp = NULL;
}

LinkedList::~LinkedList()
{
    Node *nodePtr;
    Node *nextNode;

    nodePtr = head;

    while (nodePtr != NULL)
    {
        nextNode = nodePtr->next;
        delete nodePtr;
        nodePtr = nextNode;
    }

    head = NULL;
}

void LinkedList::appendNode(int num)
{
    Node* n = new Node;
    n->next = NULL;
    n->data = num;

    if (head != NULL)
    {
        temp = head;
        head = n;
        head->next = temp;

        while (n->next != NULL)
            n = n->next;
    }
    else
    {
        head = n;
    }
}

void LinkedList::deleteNode(int m)
{
    Node* delPtr = NULL;
    temp = head;
    curr = head;

    while (curr != NULL && curr->data != m)
    {
        temp = curr;
        curr = curr->next;
    }

    if (curr == NULL)
    {
        cout << m << " not in the list." << endl;
        delete delPtr;
    }
    else if (curr->data == m)
    {
        delPtr = curr;
        curr = curr->next;
        temp->next = curr;

        delete delPtr;

        cout << "Value " << m << " has been removed from the list." << endl;
    }
}

void LinkedList::insertNode(int num)
{
    Node *n;
    n = new Node;
    n->data = num;

    if (head != NULL)
    {
        n->next = head;
        head = n;
    }
    else
    {
        head = n;
        n->next = NULL;
    }
}

void LinkedList::print()
{
    curr = head;

    while (curr != NULL)
    {
        cout << curr->data << endl;
        curr = curr->next;
    }
}

void LinkedList::reverse()
{
    if (head == NULL)
        return;

    Node *previous = NULL;
    Node *current = NULL;
    Node *next = NULL;

    current = head;

    while(current != NULL)
    {
        next = current->next;
        current->next = previous;
        previous = current;
        current = next;
    }

    head = previous;
}

int LinkedList::search(int num)
{
    int position = 0;
    curr = head;

    while (curr != NULL)
    {
        if (curr->data == num)
            return position;
        else if (curr->next == NULL)
            return -1;
        else
            ++position;

        curr = curr->next;
    }

    return 0;
}

void LinkedList::insertAt(int pos, int num)
{
    Node *prev = new Node;
    Node* curr = new Node;
    Node* newNode = new Node;
    newNode->data = num;

    int tempPos = 0;
    curr = head;

    if(head != NULL)
    {
        while(curr->next != NULL && tempPos != pos)
        {
            prev = curr;
            curr = curr->next;
            tempPos++;
        }
        if(pos==0)      // adds to head
            insertNode(num);
        else if(pos > tempPos+1)    // adds to tail
            appendNode(num);
        else
        {
            prev->next = newNode;
            newNode->next = curr;
            cout << "Node added at position " << pos << endl;
        }
    }
    else
    {
        head = newNode;
        newNode->next=NULL;
        cout << "List is empty." << endl;
    }
}

void LinkedList::mergeArray(double ary[], int sz)
{
    for (int i = 0; i < sz; ++i)
        insertNode(ary[i]);

}

void LinkedList::sort()
{
    // ???
}

int main()
{
    LinkedList list;

    list.appendNode(5);
    list.appendNode(8);
    list.appendNode(13);
    list.appendNode(15);
    cout << "Appending Nodes" << endl <<
            "---------------" << endl;
    list.print();

    cout << endl;

    list.deleteNode(8);

    cout << endl;

    list.insertNode(12);
    list.insertNode(3);

    cout << "Deletion and Insertion" << endl <<
            "----------------------" << endl;
    list.print();

    cout << endl;

    list.reverse();

    cout << "Reverse Print" << endl <<
            "-------------" << endl;
    list.print();

    cout << endl;

    cout << "Number 4 was found at position " << list.search(4) << endl;
    cout << "Number 15 was found at position " << list.search(15) << endl;

    cout << endl;

    list.insertAt(0,16);
    list.insertAt(1, 8);
    list.insertAt(3, 4);
    list.insertAt(12,24);

    cout << endl;
    cout << "Insert At Position" << endl <<
            "------------------" << endl;
    list.print();

    cout << endl;

    const int SIZE = 5;
    double ary[SIZE] = {12.69, 1.49, 9.23, 15.65, 5.42};
                        // 13, 1, 9, 16, 5

    cout << "After Merge Array" << endl <<
            "-----------------" << endl;
    list.mergeArray(ary, SIZE);
    // sort will be called here or inside of mergeArray()
    list.print();
}
What kind of sort are you trying to implement? Selection and bubble sort work pretty much exactly the same as in arrays. Insertion sort is a little bit faster for linked lists since things don't need to be moved over(mainly for doubly linked lists though), and merge sort is slower but in place and still O(nlogn).

Are there any speed constraints or does it just need to end up being sorted?
Last edited on
It just needs to be sorted, really.

I saw one example using a bubble sort method on a list, but it wasn't on a Node class done inside another class, just the Node class itself.

Example being: http://www.geeksforgeeks.org/merge-sort-for-linked-list/

How can I make it work for the way I have it set up?
Topic archived. No new replies allowed.