Thread: Sort Linked List then print

  1. #1
    Registered User
    Join Date
    Oct 2017
    Posts
    2

    Sort Linked List then print

    I have a linked list with string and int content.i can do operations on it like deleting,adding or printing etc..


    the problem is i need to print them with respect to their numbers in descending order.
    Right now im adding new nodes to end of list so it prints old to new.




    list.h
    Code:
    struct node{
    	
    		std::string  data;
    		int point;
    		node* next;
    	
    	};
    
    
    	typedef struct node* nodePtr;
    	
    	nodePtr head;
    	nodePtr curr;
    	nodePtr temp;
    AddNode
    Code:
    void list::AddNode(string addData,int addPoint){
    
    
    	nodePtr n = new node; //nodePtr is node*
    	n->next=NULL; //find node n is pointing to, access its next element make it point to null
    	n->data= addData;
    	n->point= addPoint;
    	if(head != NULL) { // if we have at least 1 element in the list .
    	curr = head; // take the current pointer we are working with and make it same with head pointer pointing to.(current= front of list)
    	while(curr->next !=NULL){ // are we at the end of the list.
    	curr = curr->next;//we are not end of the list.curr pointer points next node.
    	}
    	curr->next = n; 
    	
    	}else{ //if we dont have at least 1 element in the list.
    	
    		head =n;
    
    
    	
    	}
    
    
    }
    this is what im using for printing.need to make this function sort then print.
    Code:
    void list::PrintList(){
    
    
    	curr = head;
    	while(curr !=NULL){
    	
    		cout<<curr->data<<" "<<curr->point<<endl;
    		curr = curr->next; 
    	
    	}
    
    
    }

    thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well first off, sort and print should be two different functions.

    One easy way is to make your insert function find the sorted position to begin with.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2017
    Posts
    2
    Quote Originally Posted by Salem View Post
    Well first off, sort and print should be two different functions.

    One easy way is to make your insert function find the sorted position to begin with.

    so how can i add this functionalty to addnode function ?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Say something like
    while ( curr->next !=NULL && curr->data < addData )

    Well that's half of what you needs, let's see how you get on.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Banned
    Join Date
    Aug 2017
    Posts
    861
    ok some one might complain but this is C code on top of that, so you will have to convert it if you figure out how to use it, it sorts by alphabetical order using the names in the nodes. just need to convert it to use int values . should work? Just trying to help , it is what I used after I had to convert it to do what I wanted it to do , that in which it now does.

    C file
    Code:
    // in your comparison  here
    // val < or > then the other 
    int cmp_filename(mylist *file1, mylist *file2)
    {
        return(strcmp(file1->filename, file2->filename));
    }
    
    
    
    mylist *list_sort(mylist * list, compare_fn cmp)
    {
       mylist *l1, *l2;
    
       if (!list)
          return (NULL);
       if (!list->next)
          return (list);
    
       l1 = list;
       l2 = list->next;
    
       while ((l2 = l2->next) != NULL)
       {
          if ((l2 = l2->next) == NULL)
             break;
          l1 = l1->next;
       }
       l2 = l1->next;
       l1->next = NULL;
    
       return (list_sort_merge
               (list_sort(list, cmp), list_sort(l2, cmp), cmp));
    }
    
    mylist *list_sort_merge(mylist * l1, mylist * l2 , compare_fn cmp)
    {
       mylist list, *l, *lprev;
    
       l = &list;
       lprev = NULL;
    
       while (l1 && l2)
       {
          if (strcmp(l1->filename, l2->filename) < 0)
          {
             l->next = l1;
             l = l->next;
             l->prev = lprev;
             lprev = l;
             l1 = l1->next;
          }
          else
          {
             l->next = l2;
             l = l->next;
             l->prev = lprev;
             lprev = l;
             l2 = l2->next;
          }
       }
       l->next = l1 ? l1 : l2;
       l->next->prev = l;
    
       return (list.next);
    }
    in the header file:
    defines for the functions.
    Code:
    typedef int (compare_fn) (mylist *name1, mylist *name2);
    Code:
    // sorted List function
    // how to call it.
    /*
     filelist = list_sort(filelist, cmp_filename);
     */
    I do not know if you can use theses but you should be able to just change the data types and how it compares the values one greater than the other, gives
    you the results to tell the other function what to do with it.

    I think that is everything. so yes a their still is a little work left to be needed to do which requires thinking

    PS if this just happened to be for a school project well ya better understand how it really works if you figure out how to get it to work.
    sort and print are two separate functions btw.

    this works after everything is gotten which is faster then my code I used while adding to the list, if you are sorting before adding to the list using number values then you have to add that code to you add node function t, which is just a little easier. then this code is. I'd think a lot less code as well. using an algorithm of one type or other.
    Last edited by userxbw; 10-21-2017 at 04:06 PM.

  6. #6
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    Is there a good reason why you don't use std::list which has a sort function?
    list - C++ Reference

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked list print help
    By Roadrunner2015 in forum C Programming
    Replies: 1
    Last Post: 03-29-2014, 12:12 AM
  2. sort linked list using quick sort in C
    By Babita Sandooja in forum C Programming
    Replies: 1
    Last Post: 03-04-2014, 09:24 AM
  3. Linked List won't print
    By jrclark in forum C Programming
    Replies: 9
    Last Post: 10-31-2012, 11:48 PM
  4. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  5. Linked List print out
    By axon in forum C++ Programming
    Replies: 6
    Last Post: 10-19-2003, 07:15 PM

Tags for this Thread