please help me

Write a method to implement the bubble sort on singly linked list ?

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
 #include<iostream>
#include<cassert>
using namespace std;

template <class type>
class node
{
public:
	type info;
	node *link;
};

template <class type>
class Linkedlist
{
private:
	node<type> *head, *tail;
	int counter;
public:
	Linkedlist()
	{
		head = tail = NULL;
		counter = 0;
	}

	bool isEmpty()
	{
		return (head == NULL);
	}

	type getLast()
	{
		if (isEmpty()) {
			cout << "Linked list is empty !!!" << endl; assert(0);
		}
		return tail->info;// (*tail).info
	}

	type Getfirst()
	{

		if (isEmpty()) {
			cout << "Linked list is empty !!!" << endl; assert(0);
		}

		return head->info;
	}


	void Append(type e)
	{
		node<type> *newnode = new node<type>;
		newnode->info = e;
		newnode->link = NULL;// (*newnode).link=NULL

		if (isEmpty())
		{
			head = tail = newnode;
		}
		else
		{
			tail->link = newnode;
			tail = newnode;
		}
		++counter;
	}

	void print()
	{
		if (isEmpty()) { cout << "Linked list is empty !!!" << endl; return; }
		node<type> *current = head;

		while (current != NULL)
		{
			cout << current->info << ",";
			current = current->link;
		}
		cout << endl;
	}

	bool search(type e)
	{
		bool found = false;
		node<type> * current = head;

		while (!found && current != NULL)
		{
			if (current->info == e) found = true;
			else
				current = current->link;
		}

		if (found) return true;
		else
			return false;
	}

	void insert(type element, int index)
	{
		if (index < 1 || index> counter + 1)
		{
			cout << " invalid index !!!!" << endl; return;
		}
		else
		{
			node<type> *newnode = new node<type>;
			newnode->info = element;

			if (index == 1)
			{
				newnode->link = head;
				if (isEmpty()) tail = newnode;
				head = newnode;
			}
			else
			{
				node<type> * current = head;
				int i = 1;
				while (i != index - 1)
				{
					current = current->link;
					++i;
				}
				newnode->link = current->link;
				current->link = newnode;

				if (tail == current)
					tail = newnode;
			}

			++counter;

		}


	}

	void Delete(type e)
	{
		if (isEmpty())
		{
			cout << "List is empty !!!" << endl; return;
		}
		bool found = false;
		node<type> * current = head;
		node<type> * prev_current = NULL;

		while (!found && current != NULL)
		{
			if (current->info == e) found = true;
			else
			{
				prev_current = current;
				current = current->link;
			}


		}

		if (!found)
		{
			cout << "The item to be deleted is not in the list !!!"; return;
		}
		else
		{
			if (current == head)
			{
				head = current->link;
				if (head == NULL)
					tail = NULL;
				delete current;
			}
			else
			{
				prev_current->link = current->link;
				if (current == tail)
					tail = prev_current;
				delete current;
			}

			--counter;

		}
	}

	
};
ignore the fact that it is a linked list and swap the values, not the pointers, and it will be nearly identical to the sort as an array.
Perhaps:

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
#include <iostream>
#include <cassert>
#include <initializer_list>
#include <utility>
using namespace std;

template <class type>
class node {
public:
	type info {};
	node* link {};

	node(const type& t) : info(t) {}
};

template <class type>
class Linkedlist {
private:
	node<type> *head {}, *tail {};
	size_t counter {};

public:
	Linkedlist() {}

	~Linkedlist() {
		while (head) {
			auto curr {head->link};

			delete head;
			head = curr;
		}
	}

	Linkedlist(const Linkedlist&) = delete;
	Linkedlist& operator=(const Linkedlist&) = delete;

	Linkedlist(std::initializer_list<type> il) {
		for (const auto& i : il)
			append(i);
	}

	bool isEmpty() const { return (counter == 0); }

	type getLast() const {
		if (isEmpty()) {
			cout << "Linked list is empty !!!\n";
			assert(0);
		}

		return tail->info;
	}

	type getFirst() const {
		if (isEmpty()) {
			cout << "Linked list is empty !!!\n";
			assert(0);
		}

		return head->info;
	}

	void append(const type& e) {
		const auto newnode {new node<type>(e)};

		if (isEmpty())
			head = tail = newnode;
		else {
			tail->link = newnode;
			tail = newnode;
		}

		++counter;
	}

	void print() const {
		if (isEmpty()) {
			cout << "Linked list is empty !!!\n";
			return;
		}

		for (auto curr = head; curr != nullptr; curr = curr->link)
			cout << (curr != head ? "," : "") << curr->info;

		cout << '\n';
	}

	bool search(const type& e) const {
		for (auto curr = head; curr != nullptr; curr = curr->link)
			if (curr->info == e)
				return true;

		return false;
	}

	void insert(const type& element, size_t index) {
		if (index < 1 || index > counter + 1) {
			cout << " invalid index !!!!\n";
			return;
		}

		auto newnode {new node<type>(element)};

		if (index == 1) {
			newnode->link = head;

			if (isEmpty())
				tail = newnode;

			head = newnode;
		} else {
			auto current {head};

			for (size_t i = 1; i != index - 1; ++i)
				current = current->link;

			newnode->link = current->link;
			current->link = newnode;

			if (tail == current)
				tail = newnode;
		}

		++counter;
	}

	void erase(const type& e) {
		if (isEmpty()) {
			cout << "List is empty !!!\n";
			return;
		}

		bool found {};
		auto current {head};
		node<type>* prev_current {};

		while (!found && current != nullptr) {
			if (current->info == e)
				found = true;
			else {
				prev_current = current;
				current = current->link;
			}
		}

		if (!found) {
			cout << "The item to be deleted is not in the list !!!";
			return;
		}

		if (current == head) {
			head = current->link;
			if (head == nullptr)
				tail = nullptr;

			delete current;
		} else {
			prev_current->link = current->link;
			if (current == tail)
				tail = prev_current;

			delete current;
		}

		--counter;
	}

	void sort() {
		auto hi {head}, hj {head};

		for (size_t i = 0, j = 0, swapped = true; swapped && i < counter - 1; ++i, hi = hi->link)
			for (j = 0, swapped = false, hj = head; j < counter - i - 1; ++j, hj = hj->link)
				if (hj->info > hj->link->info) {
					swap(hj->info, hj->link->info);
					swapped = true;
				}
	}
};

int main()
{
	Linkedlist<int> ll {1, 3, 2, 5, 4, 7, 6};

	ll.print();
	ll.sort();
	ll.print();
}



1,3,2,5,4,7,6
1,2,3,4,5,6,7

Topic archived. No new replies allowed.