Menu DaniWeb
Log In Sign Up
  • Read
  • Contribute
  • Meet
  1. Home
  2. Programming Forum
  3. Software Development Forum
  4. Code Snippet Repository
  5. Reusable Code Snippet

Sorting link list as append

16 Years Ago sahasrara 0 Tallied Votes 163 Views Share

Hi my friends
In this part you can see a link list that sort when you add your number.
First number set as the first node.
next number that you will enter , is compare with first node. if it was less than first , it set as first . otherwise it traverse on the link list and compare with each node .this work continue while append number arrive to node that its next node is greate than append node. Now append node set as between current node and next of current node.

Have a good time!

c++
//Sorting link list as append
//programming by : Erfan Nasoori
//Mail : ketn68@yahoo.com
//Date of send : 2009/1/13

#include <iostream.h>
#include <conio.h>

class linkedlist{

                   private:

            		   struct node{
                        		 float num;
                       			 node*next;
                       		      } *first;


                  public:

                         linkedlist(){ first = NULL; }
                         void append();
                         void sort(node*);
                 	 void display();
                };


void linkedlist :: sort(node* temp)
{
 node *temp2,*s;
 if(first==NULL)
 {
  first=new node;
  first=temp;
  first->next=NULL;
 }

 else if(first->num >= temp->num)
 {
   temp->next=first;
   first=temp;
 }

 else if(first->num < temp->num)
 {
  temp2=first;
  while(temp2->next &&  (temp2->next)->num <= temp->num )
  {
    s = temp2;
    temp2 = temp2->next;
  }

    temp->next  = temp2->next;
    temp2->next = temp;
 }
}

void linkedlist :: append()
{
 node *temp;
 cout<<"Enter number : \n";
  do{

  		temp=new node;
      cin>>temp->num;
  		temp->next=NULL;
      sort(temp);

    }while(temp->num);

 cout<<"------------------------------------------------------------------\n";
}

void linkedlist :: display()
{
 cout<<"\n **Sorted Numbers** : \n";
 for(node* q=first ; q!=NULL ; q=q->next)
    cout<<"\n **Number** : "<< q->num <<endl;
}

void main()
{
 linkedlist l;
 cout<<"Now you are enter in link list :\n\n";
 l.append();
 cout<<"\nSorted information:\n";
 l.display();
 getch();
}
About the Author
Member Avatar for sahasrara
sahasrara 0 Newbie Poster
Member Avatar for sahasrara
sahasrara 0 Newbie Poster
16 Years Ago

Please
I wrote it by Borland 5.02

Reply to this topic
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.

Sign Up — It's Free!
Related Topics
  • Member Avatar Link List Problem 2
  • Member Avatar CreateFont failed to link using mingw (Dev-C++) 3
  • Member Avatar First Time Python User --- Power of Sets 1
  • Member Avatar Perform shuffle in doubly link list 6
  • Member Avatar wxPython DrawRectangle Experiment 0
  • Member Avatar A menu driven program using link list data structure 1
  • Member Avatar conversion type 9
  • Member Avatar generic link list using templates 7
  • Member Avatar About "diff -y" 3
  • Member Avatar C++ Extracting data from text files 24
  • Member Avatar Pointer troubles 2
  • Member Avatar C serial com port terminal program 7
  • Member Avatar Parsing Zip Files VB.Net 6
  • Member Avatar Sorting Names in Alphabetical order: Linked List C++ 2
  • Member Avatar File opening error 1
  • Member Avatar List.Sort(compare_function) - term does not evaluate to a function taking 2 arguments 4
  • Member Avatar Convert Decimal to hexadecimal number 0
  • Member Avatar c# escape characters cause [] in listview 18
  • Member Avatar C++ word duplication 21
  • Member Avatar Sorting on Class members 5
Not what you need?

Reach out to all the awesome people in our software development community by starting your own topic. We equally welcome both specific questions as well as open-ended discussions.

Start New Topic
Topics Feed
Reply to this Topic
Edit Preview

Share Post

Insert Code Block

  • Forums
  • Forum Index
  • Hardware/Software
    • Recommended Topics
  • Programming
    • Recommended Topics
  • Digital Media
    • Recommended Topics
  • Community Center
    • Recommended Topics
  • Latest Content
  • Newest Topics
  • Latest Topics
  • Latest Posts
  • Latest Comments
  • Top Tags
  • Topics Feed
  • Social
  • Top Members
  • Meet People
  • Community Functions
  • DaniWeb Premium
  • Newsletter Archive
  • Markdown Syntax
  • Community Rules
  • Developer APIs
  • Connect API
  • Forum API Docs
  • Tools
  • SEO Backlink Checker
  • Legal
  • Terms of Service
  • Privacy Policy
  • FAQ
  • About Us
  • Advertise
  • Contact Us
© 2025 DaniWeb® LLC