GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 26-Jun-2006, 23:55
iceman2006 iceman2006 is offline
New Member
 
Join Date: Apr 2006
Location: Hawaii
Posts: 21
iceman2006 is on a distinguished road

linked lists selection and insertion sort


Below is the code for my linked list. I've tried to create a selection sort on the user's inputted numbers. Is the code I wrote correct for a selection sort?

After I get the selection sort working properly, I will try to implement the insertion sort. I'm sure its not too difficult, but I'm sure I will need help.

Thanks in advance.

CPP / C++ / C Code:
#include <iostream>
#include <string>

using namespace std;

struct node
{
    int data;
    struct node *next;
};

class linkedList
{
    node *head;
    public:
     void create();
     void display();
     void selectionSort();
};

void linkedList :: create()
{
    node *newl=NULL,*end=newl;
    int data;
    head=NULL;
    cout<<"\nCreate the List.  Press <ENTER> after each entry\n";
    cout<<"\nEnter -999 to terminate\n";
    while(1){
       cout<<"Enter a number: ";
       cin>>data;
       if(data==-999)
	 break;
       else{
	   newl = new node;
	   newl->data=data;
	   if(head==NULL)
	      head=newl;
	   else
	      end->next=newl;
	   end=newl;
	   end->next=NULL;
       }
    }
    cout<<"\nList is Created\n";
}

void linkedList :: display()
{
	node *start;
	start=head;
	while(start!=NULL){
	   cout<<start->data; 
	   if(start->next!=NULL)
	       cout<<", ";
	   start=start->next;
	}
}
void linkedList :: selectionSort()
{
	node *i, *j;
	int temp;
	for(i=head;i!=NULL;i=i->next){
	   for(j=i->next;j!=NULL;j=j->next){
	       if(i->data > j->data){
		  temp    =  i->data;
		  i->data = j->data;
		  j->data = temp;
	       }
	   }
	}
	cout<<"\nThe numbers are Sorted\n";
}

int main()
{
    linkedList list;
	list.create();
	cout<<"List before sorting "<<endl;
	list.display();
	list.selectionSort();
	cout<<"List after sorting "<<endl;
	list.display();	
    cout << endl<< "Press 'ENTER' to continue";
    cin.ignore();
    cin.get();
  return 0;
}
Last edited by LuciWiz : 26-Jun-2006 at 23:57. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #2  
Old 27-Jun-2006, 08:29
Blake's Avatar
Blake Blake is offline
Member
 
Join Date: Nov 2005
Posts: 218
Blake has a spectacular aura aboutBlake has a spectacular aura about

Re: linked lists selection and insertion sort


You should define a default constructor, and eliminate the create function. Also add an insert function, and put the loop for inserting numbers outside the class.

You also need to define a destructor, or you will have a memory leak.
 
 

Recent GIDBlogStupid Management Policies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 07:46.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.