Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. how to sort a linked list?

how to sort a linked list?

Scheduled Pinned Locked Moved C / C++ / MFC
data-structurestutorialquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    suroor453
    wrote on last edited by
    #1

    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;

    C 1 Reply Last reply
    0
    • S suroor453

      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;

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      http://www.codeproject.com/cpp/colsort.asp[^] or, switch to std::list, and use std::sort. Cleek | Image Toolkits | Thumbnail maker

      S 1 Reply Last reply
      0
      • C Chris Losinger

        http://www.codeproject.com/cpp/colsort.asp[^] or, switch to std::list, and use std::sort. Cleek | Image Toolkits | Thumbnail maker

        S Offline
        S Offline
        suroor453
        wrote on last edited by
        #3

        how can I switch to std::list, and use std::sort. ??! I didnt understand.....sorry

        J 1 Reply Last reply
        0
        • S suroor453

          how can I switch to std::list, and use std::sort. ??! I didnt understand.....sorry

          J Offline
          J Offline
          Jack Puppy
          wrote on last edited by
          #4

          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; }

          S 1 Reply Last reply
          0
          • J Jack Puppy

            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; }

            S Offline
            S Offline
            suroor453
            wrote on last edited by
            #5

            is this sort int or sting? I need to soet strings

            L 1 Reply Last reply
            0
            • S suroor453

              is this sort int or sting? I need to soet strings

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups