how to sort a linked list?
-
I have a linked list each node of it hase (name,phone....) I want to sort it by name struct PhoneBookEntry { CString Name,Email,Website,Adress; CString OfPhone,HoPhone,Mobile; }; typedef CList PhoneBook; PhoneBookEntry* InputEntry() { PhoneBookEntry* pEntry = new PhoneBookEntry; cout << " Enter New Contact Details:" << endl; cout << " Name : "; char cName[100]; gets(cName); return pEntry; } void main() int nChoice switch (nChoice) { case 1: // add contact PhoneBookEntry *pEntry; pEntry = InputEntry(); g_PhoneBook.AddTail(pEntry); break; case 2: // list entries pos = g_PhoneBook.GetHeadPosition(); while (pos) { PhoneBookEntry* pEntry = g_PhoneBook.GetNext(pos); PrintEntry(pEntry); } break;
-
I have a linked list each node of it hase (name,phone....) I want to sort it by name struct PhoneBookEntry { CString Name,Email,Website,Adress; CString OfPhone,HoPhone,Mobile; }; typedef CList PhoneBook; PhoneBookEntry* InputEntry() { PhoneBookEntry* pEntry = new PhoneBookEntry; cout << " Enter New Contact Details:" << endl; cout << " Name : "; char cName[100]; gets(cName); return pEntry; } void main() int nChoice switch (nChoice) { case 1: // add contact PhoneBookEntry *pEntry; pEntry = InputEntry(); g_PhoneBook.AddTail(pEntry); break; case 2: // list entries pos = g_PhoneBook.GetHeadPosition(); while (pos) { PhoneBookEntry* pEntry = g_PhoneBook.GetNext(pos); PrintEntry(pEntry); } break;
http://www.codeproject.com/cpp/colsort.asp[^] or, switch to std::list, and use std::sort. Cleek | Image Toolkits | Thumbnail maker
-
http://www.codeproject.com/cpp/colsort.asp[^] or, switch to std::list, and use std::sort. Cleek | Image Toolkits | Thumbnail maker
-
The list container is part of the STL. Simple sort example:
#include "stdafx.h" #include <list> #include <algorithm> // Needed for sort bool sortAscending(int a, int b) { return (a < b); } bool sortDescending(int a, int b) { return (b < a); } int _tmain(int argc, _TCHAR* argv[]) { std::list<int> myList; myList.push_back(2); myList.push_back(1); myList.push_back(4); std::cout << "Before sort" << std::endl; for (std::list<int>::iterator i = myList.begin(), iEnd = myList.end(); i != iEnd; ++i) { std::cout << (*i) << std::endl; } myList.sort(sortAscending); std::cout << "Ascending sort" << std::endl; for (std::list<int>::iterator i = myList.begin(), iEnd = myList.end(); i != iEnd; ++i) { std::cout << (*i) << std::endl; } myList.sort(sortDescending); std::cout << "Descending sort" << std::endl; for (std::list<int>::iterator i = myList.begin(), iEnd = myList.end(); i != iEnd; ++i) { std::cout << (*i) << std::endl; } return 0; }
-
The list container is part of the STL. Simple sort example:
#include "stdafx.h" #include <list> #include <algorithm> // Needed for sort bool sortAscending(int a, int b) { return (a < b); } bool sortDescending(int a, int b) { return (b < a); } int _tmain(int argc, _TCHAR* argv[]) { std::list<int> myList; myList.push_back(2); myList.push_back(1); myList.push_back(4); std::cout << "Before sort" << std::endl; for (std::list<int>::iterator i = myList.begin(), iEnd = myList.end(); i != iEnd; ++i) { std::cout << (*i) << std::endl; } myList.sort(sortAscending); std::cout << "Ascending sort" << std::endl; for (std::list<int>::iterator i = myList.begin(), iEnd = myList.end(); i != iEnd; ++i) { std::cout << (*i) << std::endl; } myList.sort(sortDescending); std::cout << "Descending sort" << std::endl; for (std::list<int>::iterator i = myList.begin(), iEnd = myList.end(); i != iEnd; ++i) { std::cout << (*i) << std::endl; } return 0; }
-
Read up on the Standard Template Library (STL). The purpose of this library is the containers (such as lists) and be applied to any data type, it is universal. Elaine :rose: The tigress is here :-D