Swapping And InsertSorted of a Singly LInked List

Hi,
I have completed almost everything but i was't able to implement my swapping and insertionsort function in a singly linked list. All i can think of is to physically sort and swap the nodes but i wasn't able to implement it.

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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include <iostream>
#include <string>
using namespace std;


class Student
{

	friend ostream &operator<< (ostream &out, Student s); 
    friend istream & operator>> (istream &in,  Student &s);

	private:
		string roll_no;
		string name;
		double cgpa;
	public:
		Student();
		Student(string,string,double);
		void setName(string);
		void setcgpa(double);
		void setroll(string r);
		string getname();
		string getroll();
		double getcgpa();


/*Other public functions as needed you might need to overload certain operators*/
};


Student::Student()
{


	roll_no="";
		 name="";
		cgpa=0.0;

}

Student::Student(string r,string n,double gg)
{
	

	
	roll_no=r;
		 name=n;
		cgpa=gg;

}

double Student::getcgpa()
{
return cgpa;


}


string Student::getname()
{
return name;


}

string Student::getroll()
{
return roll_no;


}
void Student::setName(string n)
{
name=n;


}

void Student::setroll(string r)
{
roll_no=r;


}

void Student::setcgpa(double gg)
{
	cgpa=gg;

}


ostream & operator<< (ostream &out, Student s) 
{ 
 out<<s.getname()<<" ";
 out<<s.getroll()<<" ";
 out<<s.getcgpa()<<" ";

 return out;


} 
  
istream & operator >> (istream &in,  Student &s) 
{ 
	string n, r;
	float c;

    cout << "Enter the Name of the Student";
    in >>n;
	s.setroll(n);
	cout<<endl;

	cout << "Enter the roll number of the Student";
	in>>r;
	s.setroll(r);
	cout<<endl;

	cout << "Enter the cgpa of the Student";
	in>>c;
	s.setcgpa(c);
	cout<<endl;


    return in; 
} 
template <typename T>
class Node
{
public:
// constructor
	Node(T element);
//sets the KeyType data in the Node
	void setData(T pVal);
// returns the KeyType data in the Node
	T getData();
// returns the link to the next node
	Node* GetNext();
// sets the link to the next node
	void SetNext(Node *x);
private:
	T data;
	Node *link ;
};
template <typename T>
class SList
{
public:
// constructor of the Singly Linked List
	SList();
//Inserts the node pNew after the node pBefore
// if the list is empty, it makes pNew the first node of the list
	void Insert(Node<T>* pBefore, Node<T>* pNew);
//Deletes the node pToBeDeleted
	bool Delete(Node<T>* pToBeDeleted);
//prints the contents of the list
	void printList();
	Node<T>* getFirst()
	{
		return first;
	}

//ADD ONE OR TWO MEMBER FUNCTION(s) DIFFERENT FOR EACH LAB SECTION
	void reverse(Node<T>* temp);
private:
	Node<T> *first ;
};

template <typename T>
Node<T>::Node(T element)
{
	data = element;
	link = 0;
};


template <class T>
void Node<T>::setData(T pVal)
{
	data = pVal; 
};
template <class T>
T Node<T>::getData()
{
	return data;
};
template <class T>
Node<T>* Node<T>::GetNext()
{
	return link;
};
template <class T>
void Node<T>::SetNext(Node *x)
{   
    link = x;
	
};

/***************************************************************/
template<class T>
SList<T>::SList()
{
	first = NULL;
};
template <class T>
void SList<T>::Insert(Node<T>* pBefore, Node<T>* pNew)
{
	if(!first)
	{
		first = pNew;
	}
	else if(!pBefore && first)
	{
		pNew->SetNext(first);
		first=pNew;
	}
	else
	{
		pNew->SetNext(pBefore->GetNext());
		pBefore->SetNext(pNew);
	}
};
template <class T>
void SList<T>::printList()
{
	cout<<" *********************"<<endl;
      Node<T> *temp ;
	  temp=first;
	  int i=0;

    while(temp)
	{
		T test2 = temp->getData();
	    cout<<"Value in Node # "<< i+1<<" is "<<test2<<endl;
	    temp=temp->GetNext();
	    i++;
	}

};

template <class T>
bool SList<T>::Delete(Node<T>*pToBeDeleted)
{
	Node<T>*temp = first;
	
	if(pToBeDeleted=first)
	{
		first = first->GetNext();
	}
	else
	{
		while(temp->GetNext() != pToBeDeleted && temp!=NULL)
		{
			temp = temp->GetNext();
		}
		temp->SetNext(pToBeDeleted->GetNext());
	}
	return true;
};

template <class T>
void SList<T>::reverse(Node<T>* temp)
{
	
	if(temp)
	{
		reverse(temp->GetNext());
		cout<<temp->getData()<<" ";
	}
};

int main()
{

	SList<Student> *studlist;
	studlist = new SList<Student>();
	studlist->getFirst();

	
	//declare and initialize 3 students with random data
	Student *stu1 = new Student("L15-3452","Taha Lughmani", 3.45);
	Student *stu2 = new Student("L17-1526","Ahmed Hassan", 3.14);
	Student *stu3 = new Student("L19-1754","Ghulam Hussain", 2.78);
	//studlist->InsertSorted(*stu1);
	//studlist->InsertSorted(*stu2);
	//studlist->InsertSorted(*stu3);
	studlist->printList();
	//studlist->DeleteLast();
	//studlist->PrintList();
	studlist->reverse(studlist->getFirst());
	studlist->printList();
	delete studlist;

	SList<int> *ilist;
	ilist = new SList<int>;
	Node<int> *a, *b, *c, *d;
	b = new Node<int>(2);
	c = new Node<int>(3);
	d = new Node<int>(4);
	a = new Node<int>(1);
	ilist->Insert(0, a);
	ilist->Insert(a, b);
	ilist->Insert(b, c);
	ilist->Insert(c, d);
	ilist->printList();
	//studlist->swap(b, c);
	//ilist->PrintList();
	delete ilist;
	return 0;
	system("pause");
}

P.S I need to get this done within two days. Please Help me out.
Swapping in a singly linked list means removing the node at the old position and insert it at the new position. Thus you need to implement a remove function first.
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
template<class T>
void SList<T>::swapNodes(Node<T>*X, Node<T>*Y)  
{  
	int x=X->getData();
	int y=Y->getData();

	Node<T>*head;
	head=first;


// Nothing to do if x and y are same  
if (x == y) return;  
  
// Search for x (keep track of prevX and CurrX  
Node<T> *prevX = NULL;
X = head;  

while (X && X->getData() != x)  
{  
    prevX = X;  
    X = X->GetNext();  
}  
  
// Search for y (keep track of prevY and CurrY  
Node<T> *prevY = NULL;
Y = head;  
while (Y && Y->getData() != y)  
{  
    prevY = Y;  
    Y = Y->GetNext();  
}  
  
// If either x or y is not present, nothing to do  
if (X == NULL || Y == NULL)  
    return;  
  
// If x is not head of linked list  
if (prevX != NULL)  
    prevX->SetNext(Y);  
else // Else make y as new head  
    head = Y;  
  
// If y is not head of linked list  
if (prevY != NULL)  
    prevY->SetNext(X);  
else // Else make x as new head  
    head = X;  
  
// Swap next pointers  
Node<T>*temp = Y->GetNext();  
Y->SetNext(X->GetNext());  
X->SetNext(temp);  
}  


This is what i came up with but was unable to swap other nodes with the first one.
Also, i still haven't understood of how to insertsort with the given student data.
Last edited on
honestly its easier to build the list sorted than to sort it after the fact.
you can also just swap data instead of nodes, and that is much more efficient if the data is smallish.

swapping the first node is just like any other, except you need to update the 'head' pointer as well.

your code looks broken for head, though.
if y is the head, then you need:
head = y; // you did this.
head next = old head next ... /// are you doing this? I do not see it?

Topic archived. No new replies allowed.