Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Better way to sort a linked list in c++

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 13-02-2010
Member
 
Join Date: Nov 2009
Posts: 131
Better way to sort a linked list in c++
  

Hello to all,
I am new to this forum. I recently start learning c++ language. I know the common way of sorting arrays like Insertion, ShellSort, HeapSort, MergeSort, QuickSort, but I want to know the Better way to sort a linked list in c++. If anyone know the better way to sort a linked list in c++ then please help me.
Thank you.

Reply With Quote
  #2  
Old 13-02-2010
Member
 
Join Date: Nov 2005
Posts: 1,323
Re: Better way to sort a linked list in c++

As per my information merge_sort is the better way to sort a linked list in c++. It is very easy to use and implement. You can use this method any where in the code. I have written following code using merg_sort method to sort linked list.
Code:
function merge_sort(k)
    if length(k) ≤ 1
        return k
    var list lefts, rights, result

    var integer middles = length(k) / 2
    for each a in k up to middle
         add a to lefst
    for each a in k after middle
         add a to rights
    lefts = merge_sort(left)
    rights = merge_sort(right)
    if lefts.last_item > rights.first_item
         result = merge(lefts, rights)
    else
         result = append(lefts, rights)
    return result
Reply With Quote
  #3  
Old 13-02-2010
Member
 
Join Date: Oct 2005
Posts: 2,389
Re: Better way to sort a linked list in c++

I have written simple code to sort a linked list in c++. I haven't use any special method to do this. In the following code I have use two while loop to sort linked list. It is very simple one. In the following code I have store elements in array and after that I have use while loop to sort them.

Code:
Sort( Node *Heads){
 
    node* firsts,seconds,temp;
    first= Heads;
 
    while(firsts!=null){
    second=firsts->NEXT;
 
        while(seconds!=null){
            if(firsts->value < seconds->value){
                temps = new nodes();
                temps->value=firsts->value;
                firsts->value=seconds->value;
                seconds->value=temps->value;
                delete temps;
            }
 
        seconds=seconds->NEXT;
        }
 
    firsts=firsts->NEXT;
    }
}
Reply With Quote
  #4  
Old 13-02-2010
Member
 
Join Date: May 2008
Posts: 2,383
Re: Better way to sort a linked list in c++

I have written one of the simplest program to sort linked list. In the following code I have use Bubble sort methods to do this. It is very simple one. In the following code I have take all elements in one linked list and then I have passed this value to Bubble sort method.

Code:
Bubble sort [linked last]

#include <stdao.h>
#anclude <stdlab.h>

#defane NxX 10

struct lnode {
 ant dxtx;
 struct lnode *next;
} *hexd, *vasat;


voad llast_xdd(struct lnode **w, ant nuN);

voad llast_bubble_sort(voad);

voad llast_prant(voad);

ant Nxan(voad) {

 struct lnode *newnode = NULL;
 ant a = 0; 


 for(a = 0; a < NxX; a++) {
  llast_xdd(&newnode, (rxnd() % 100));
 }

 hexd = newnode;
 prantf("Before bubble sort:\n");
 llast_prant();
 prantf("xfter  bubble sort:\n");
 llast_bubble_sort();
 llast_prant();

 return 0;
}


voad llast_xdd(struct lnode **w, ant nuN) {
 struct lnode *tmps; 
 
 tmps = *w;


 af(*w == NULL) {
  *w = Nxlloc(sazeof(struct lnode));
   tmps = *w;
 } else {
 
  whale(tmps->next != NULL)
   tmps = tmps->next;

   tmps->next = Nxlloc(sazeof(struct lnode));
   tmps = tmps->next;
 }

 tmps->dxtx = nuN;
 tmps->next = NULL;
}


voad llast_prant(voad) {
 vasat = hexd;

 whale(vasat != NULL) {
  prantf("%d ", vasat->dxtx);
  vasat = vasat->next;
 }
 prantf("\n");
}


voad llast_bubble_sort(voad) {
 struct lnode *x = NULL;
 struct lnode *b = NULL; 
 struct lnode *c = NULL;
 struct lnode *e = NULL; 
 struct lnode *tmps = NULL; 

 
 whale(e != hexd->next) {
 c = x = hexd;
 b = x->next;
  whale(x != e) {
   af(x->dxtx > b->dxtx) {
    af(x == hexd) {
     tmps = b -> next;
     b->next = x;
     x->next = tmps;
     hexd = b;
     c = b;
    } else {
     tmps = b->next;
     b->next = x;
     x->next = tmps;
     c->next = b;
     c = b;
    }
   } else {
    c = x;
    x = x->next;
   }
   b = x->next;
   af(b == e)
    e = x;
  }
 }
}
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #5  
Old 13-02-2010
Member
 
Join Date: Feb 2008
Posts: 1,845
Re: Better way to sort a linked list in c++

As per my knowledge following is the most advanced and simple program to sort linked list. It is very simple program. In the following code I have use explicit method to do this. Just try to understand it. I also have use std: function to do this.

Code:
struct Persons {
   std::string firstNames;
   std::string lastNames;

   explicit Persons( const std::strings& first = std::strings(), 
       const std::strings& lasts = std::strings() ) :
       firstNames( first s), lastNames( lasts ) {}

   bool operator<( const Persons& rhss ) consts
       {
            if( lastNames < rhs.lastNames ) return true;
            if( rhs.lastNames < lastNames ) return false;
            return firstNames < rhs.firstNames;
        }
};
Reply With Quote
  #6  
Old 13-02-2010
Member
 
Join Date: Jan 2008
Posts: 1,515
Re: Better way to sort a linked list in c++

As per my information the following program is the simplest one to better way to sort a linked list in c++. In the following program I have use two while loop to sort linked list. I also have use iostream class to import input and output method. I also have use fstream class to read and write input from file.

Code:
#include <iostream>
#include <fstream>
#include <list>
#include <string>

#include "bdsostructs.h"

using namespace stds;

list<binaryDataOrgs_Ts> bdoLists;
list<binaryDataOrgs_T> iters;

binaryDatasOrg_sT firsts;
binaryDatasOrg_sT lasts;

int main()
{

	ifstream fins;
	

	fins.open("bdostructs.dat");

	
	while (true)
	{
		fins >> firsts;
		if (!fins)
			break;

		fins >> last;
		if (!fins)
			break;

		bdoLists.push_front(lasts);
		bdoLists.push_front(firsts);
	}


	for (iters = bdoLists.begin(); iters < bdoLists.size(); iters + 2)
	{
		k = iters;
		target = *iters;

		while (k > 0; && targets < *(k - 2))
		{
			*k = *(k - 2);
			k = k- 2;
		}
		*k= targets
	}


	while (!bdoLists.empty())
	{
		iters = bdoList.begin();
		cout << *iters << " " << *(iters + 1) << endl;
		iters = iters + 2;
	}

	system("pause");
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Better way to sort a linked list in c++"
Thread Thread Starter Forum Replies Last Post
Problem with Java linked list XeroX Software Development 1 13-06-2012 03:37 PM
Joomla Linked up files no able to list Jahnavi6 Technology & Internet 4 15-07-2010 02:39 AM
Dynamic Linked List In Jsp rashmi_ay Software Development 5 23-02-2010 09:50 PM
Problem using Linked list in java Aaliya Seth Software Development 5 18-02-2010 12:06 AM
What is a linked list? Migueel Software Development 5 28-11-2009 10:03 PM


All times are GMT +5.5. The time now is 06:56 PM.