Help Solving Data Stracture assignment

Hi friends, I am new in this site, i just get data stracture course for first time. I need help with assigment. I can pay if we make deal. if one can help I apperciate that.

use bubble sort to sort the following collections:

•Vector
•Singly Linked List
•Doubly Linked List
Last edited on
What a boyfriend!

Here's how the bubble sort works: http://en.wikipedia.org/wiki/Bubble_sort
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
std::vector<int> BubbleSort(std::vector<int>& );
std::list<int> BubbleSort(std::list<int>& );
void swap(int&, int&);


std::vector<int> BubbleSort(std::vector<int>& vect)
{
	for (std::vector<int>::iterator iter = vect.begin(); iter != vect.end(); ++iter)
	{
		for (std::vector<int>::iterator it = iter+1; it != vect.end(); ++it)
		{
			if ( *iter > *it)
			{
				swap(*iter, *it);
			}


		}

	}
	return vect;

}


std::list<int> BubbleSort(std::list<int>& list)
{
	for (std::list<int>::iterator iter = list.begin(); iter != list.end(); ++iter)
	{
		for (std::list<int>::iterator it = iter; it != list.end(); ++it)
		{
			if ( *iter > *it)
			{
				swap(*iter, *it);
			}


		}

	}
	return list;
}
void swap(int& a, int & b)
{
	int tmp = a;
	a = b;
	b = tmp;
}



as for , Single Linked List
you are lucky, reccently I am re-writint it , you can refer code as below:

header file:
MyList.h
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
#pragma once

namespace PPP
{
typedef struct node
{
	int val;
	struct node* pNext;
}NODE, *PNODE;

class MyList
{
	PNODE pHead;
public:
	MyList(void);
	int Count() const;
	void Insert(const int, const int);
	void DeleteAt(const int );
	void Sort();
	void Travel() const;
	void Revert();
	void Unique();
	~MyList(void);
};
};

cpp file:
MyList.cpp


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
#include "StdAfx.h"
#include "MyList.h"

using namespace PPP;

MyList::MyList(void)
{
	pHead = NULL;

}


MyList::~MyList(void)
{
	PNODE p, pTmp;
	for ( p=pHead; p; )
	{
		pTmp = p->pNext;
		delete p;
		p =pTmp;
	}
}

int PPP::MyList::Count() const
{
	int cnt=0;
	PNODE pTmp = pHead;
	while ( pTmp)
	{
		++cnt;
		pTmp = pTmp->pNext;
	}
	return cnt;


}

void PPP::MyList::Insert(const int _val, const int _index)
{
	
	if ( _index < 0 || _index > Count())
	{
		std::cout<<"Index "<<_index<<" is not in the range [0, "<<Count()<< "]"<<"\n";
		return ;
	}
	else if ( _index == Count() && _index == 0)
	{
		PNODE newNode =new NODE;
		newNode->pNext = NULL;
		newNode->val = _val;
		pHead = newNode;
		return ;
	}
	else if ( _index != Count() -1 && _index != 0)
	{
		PNODE pTmp = pHead;
		int tmp = 0;
		while ( pTmp)
		{
			if ( tmp == _index - 1 )
			{
				PNODE p = pTmp->pNext;
				PNODE newNode = new NODE;
				newNode->pNext = p;
				newNode->val = _val;
				pTmp->pNext=newNode;
				return;
			}
			++tmp;
			pTmp= pTmp->pNext;
		}
		return;
	}
	else if ( _index  == Count() -1 )
	{
		PNODE pTmp = pHead;
		while ( pTmp->pNext)
		{
			pTmp = pTmp->pNext;
		}
		PNODE newNode = new NODE;
		newNode->pNext= pTmp;
		newNode->val = _val;
		pTmp->pNext = NULL;
		pHead = newNode;
		return;
	}
	else if ( _index == 0)
	{
		PNODE pTmp = pHead;
		PNODE newNode = new NODE;
		newNode->pNext = pTmp;
		newNode->val = _val;
		pHead = newNode;
		return;
	}
}

void PPP::MyList::DeleteAt(const int _index)
{
	if ( _index < 0 || _index >= Count())
	{
		std::cout<<"Index "<<_index<<" is not in the range [0, "<<Count()<< "]"<<"\n";
		return ;
	}
	else if ( _index != Count() -1 && _index != 0)
	{
		PNODE pTmp = pHead;
		int tmp = 0;
		while (pTmp)
		{
			if ( tmp == _index-1)
			{
				PNODE p = pTmp->pNext;
				pTmp->pNext = p->pNext;
				delete p;
				return ;
			}
			++tmp;
			pTmp = pTmp->pNext;
		}
	}
	else if ( _index == Count() - 1)
	{
		PNODE pTmp = pHead;
		int tmp = 0 ;
		while (pTmp)
		{
			if ( tmp == Count() - 2)
			{
				PNODE p = pTmp ->pNext;
				delete p;
				pTmp->pNext = NULL;
				return ;
			}
			pTmp= pTmp->pNext;
		}

	}
	else if ( _index == 0 )
	{
		PNODE p = pHead;
		pHead = p->pNext;
		delete p;
	}
}

void PPP::MyList::Travel() const
{
	PNODE pTmp = pHead;
	if ( pTmp)
	{
		while(pTmp)
		{
			std::cout<<pTmp->val<<"==>";
			pTmp=pTmp->pNext;
		}
		std::cout<<"\n";
	}
	else
	{
		std::cout<<"===Little trouble===";
	}

}

void PPP::MyList::Sort()
{
	if(Count() == 0 )
	{
			std::cout<<"Donot to find matter self, There is no one node in the dear list"<<"\n";
			return;
	}
	if (Count() == 1)
	{
		std::cout<<"Donot to find matter self, There is only one node in the dear list"<<"\n";
		return;
	}
	PNODE pLater = new NODE;

	PNODE pFront = new NODE;
		
	pLater = pHead;
	

	while( pLater)
	{
		pFront = pLater->pNext; 
		while ( pFront)
		{
			if ( pFront->val < pLater->val)
			{
				int tmp = pFront->val;
				pFront->val = pLater->val;
				pLater->val = tmp;
			}
			pFront = pFront ->pNext;
		}
		pLater = pLater->pNext;
	}

}


void PPP::MyList::Revert()
{
	if ( Count() <= 0)
	{

	}
	else if (Count() == 0)
	{

	}
	else if (Count() == 1)
	{
	}
	else if ( Count() == 2)
	{
		PNODE tmp = pHead;
		pHead = pHead->pNext->pNext;
		pHead->pNext = tmp;
		tmp->pNext = NULL;
		
	}
	else 
	{
		PNODE p1 = pHead;
		PNODE p2 = p1->pNext;
		PNODE p3 = p2->pNext;
		PNODE p4 = new NODE;
		p1->pNext = NULL;
		while (p2)
		{
			p2->pNext = p1;
			if ( p3)
			{
				p1 = p2;
				p2= p3;
				p4 = p3;
				p3= p3->pNext;
			}
			else
			{
				p2= p3;
			}
			
		}
		pHead= p4;
		//delete p4;
	}
	
}

void PPP::MyList::Unique()
{
	if ( this->Count() <= 1)
		return;
	PNODE prev = pHead;
	
	while (prev)
	{
		PNODE succ =prev->pNext;
		while ( prev->val == succ->val)
		{
			if ( succ->pNext != NULL)
			{
				PNODE ptr = succ->pNext;
				delete succ;
				succ = ptr;
				prev->pNext = succ;
			}
			else
			{
				prev->pNext = NULL;
				delete succ;
			}
		}
		prev = prev->pNext;
	}
}



stdafx.h is as below:
1
2
3
4
5
6
7
8
9
10
11
12
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>
#include <iostream> 




Any way , hope all of code may help you a bit. thanks
Many thanks misserwell for this great help
but my instructor give us some skelton and say follow it to solve the assignment. Can you please take a look on it and make comment?
Ohh sorry I cant past there here. can I send it by e-mail or uploading to some server?

please try to hel me

Regards
Lina
I used VS IDE , File->New->Project->Visual C++->Win32 Console Application, then you name a project , such as Bubble , then Click OK , then you will get a project named Bubble , and you must write code in Bubble.cpp file. I do not know what error you got , could you share it ?
Topic archived. No new replies allowed.