Hello
I have to work with Linkedlist, and sort them from smallest to the largest.
So I have made this sorting function. It works but I dont understand why it is not displaying me the items I have inserted after sorting it. I have also looked at the other related posts, but its not helping me.
Here is my code:
#include<iostream>
#include<conio.h>
#include <algorithm>
using namespace std;
struct link {
int info;
link* next;
};
class linklist
{
private:
link* first;
int count;
public:
linklist(){
first =NULL;
count =0;
}
void additem(int d)
{
link* newlink = new link ;
newlink -> info = d ;
newlink -> next = NULL ;
if ( first == NULL )
first = newlink ;
else {
link* current = first ;
while ( current -> next != NULL )
current = current -> next ;
current -> next = newlink ;
}
}
void display() {
link* current = first ;
cout<<"The list of elements in the list are : ";
while ( current != NULL)
{
cout<< endl<<current -> info ;
current = current -> next ;
}
cout<<endl;
}
void sortSmall(){
link* ptr1;
ptr1 = first;
link* ptr2;
ptr2 = ptr1 -> next;
int temp;
link* small = new link();
while ( ptr2 != NULL) {
{
if (ptr1 -> info < ptr2 -> info)
small -> info = ptr1 -> info;
else
temp = ptr1 -> info;
ptr1 -> info = ptr2 -> info;
ptr2 -> info = temp;
}
ptr1 -> next;
ptr2 = ptr1 -> next;
}
void display();
}
~linklist(){
link *current;
link *temp;
current=first;
temp=first;
while (current != NULL){
temp = temp -> next;
delete current;
current = temp;
}
}
};
int main(){
linklist object;
object.additem (12);
object.additem (10);
object.additem (8);
object.additem (7);
object.display();
object.sortSmall();
object.display();
getch();
}