Sorting a linked list

Here is my linked list class. I cannot figure how to sort through the list. Please take a look at my sort function and point me in the right direction.

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
//Pablo Salinas
//COSC 2436
//Assignment 2: Linked List
//Due Date 11/09/2016

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

class LinkedList { // creating the link list class
private:
	struct nodeType { //struct used to create nodes
		string info;
		nodeType* link;
	};

	nodeType *first, *last, *newNode;
	int count1;

public:
	LinkedList() {//Default contstructor setting the first and last nodes to nullptr
		first = nullptr;
		last = nullptr;
	}

	void addElement(string name) { //Adding a new node to the linked list

		newNode = new nodeType;
		newNode->info = name;
		newNode->link = nullptr;
		if (first == nullptr) {
			first = newNode;
			last = newNode;
		}
		else {
			last->link = newNode;
			last = newNode;
		}
		count1++;
	}
	void removeElement(string name) { //Removing an existing node from the list by searching and deleting the node
		nodeType *current, *bcurrent;

		if (first == nullptr) {
			cout << "Cannot delete from an empty list." << endl;
		}
		else {
			if (first->info == name) {
				current = first;
				first = first->link;
				if (first == nullptr) {
					last = nullptr;
				}
				delete current;
			}
			else {

				while (current != nullptr) {
					if (current->info == name) {

					}
					else {
						current = current->link;
					}
				}
			}
		}
	}
	bool isEmptyList() { //Bool function to check if the list is empty or full
		return (first == nullptr);
	}
	void listSize() {
		nodeType *current;
		current = first;
		int count = 0;
		while (current != nullptr) {
			current = current->link;
			count++;
		}
		cout << count;
	}

	void printList() { // function to print the list
		nodeType *current;
		current = first;
		while (current != nullptr) {
			cout << current->info << ", ";
			current = current->link;
		}
	}
	void sort() { // function to sort through the list. Using a bubble sort variation.
		nodeType *current, *bcurrent;
		current = first;
		bcurrent = first->link;

		for (int i = count1 - 1; i >= 0; i--) {

			for (int j = 0; j < count1 - 1; j++) {
				if (current->info > bcurrent->info) {
					swap(current->info, bcurrent->info);
				}
				current = bcurrent;
				bcurrent = bcurrent->link;
			}	
		}

	}

		
	void reverse() { // function to reverse the order of the list.
		nodeType *current, *next, *prev;
		current = first;
		next = nullptr;
		prev = nullptr;
		while (current != nullptr) {
			next = current->link;
			current->link = prev;
			prev = current;
			current = next;

		}
	}
};


int main() {

	string str1; //declaring a variable to store the string
	LinkedList b; // declaring my class object

	if (b.isEmptyList() == true) { //checking if the list is empty
		cout << "The list is empty" << endl;
	}
	do { // do while loop to prompt the user to enter a word and do the functions inside the class
		cout << "Enter a word(1 to quit): ";
		cin >> str1;
		if (str1 == "1") { // option to quit the loop
			break;
		}
		else {
			b.addElement(str1);
			cout << endl;
			b.printList();
			cout << endl << "The list size is ";
			b.listSize();
			cout << endl;

		}
	} while (str1 != "1"); // option to continue the loop
	cout << "Sorted list: ";
	b.sort();
	cout << endl;
	b.printList();
	cout << endl;
	system("pause");
	return 0;
}


Sorting a linked list is actually very difficult. However, you can store all linked list elements into a vector, sort the vector and build the linked list again based on the sorted vector.
@starFish2 that's a very good idea actually but I figured it out.

basically I used a bubble sort to get the sorting done but I would have to reset the position.

So I changed my sort function to this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

	void sort() { // function to sort through the list. Using a bubble sort variation.
		nodeType *current, *bcurrent;
		current = first;
		bcurrent = first->link;

		for (int i = count1 - 1; i >= 0; i--) {
                        current = first;
		        bcurrent = first->link;
			for (int j = 0; j < count1 - 1; j++) {
				if (current->info > bcurrent->info) {
					swap(current->info, bcurrent->info);
				}
				current = bcurrent;
				bcurrent = bcurrent->link;
			}	
		}

	}
Topic archived. No new replies allowed.