Results 1 to 2 of 2

Thread: sorting linkedlist

  1. #1
    Join Date
    May 2011
    Posts
    1

    sorting linkedlist

    hello guys i wanna help

    i have small Problem in my progamme

    plzzzzz help me

    i want make sorting for linked list

    in Java


    this my main class

    Code:
    import java.util.*;
    public class Hospital {
         
        public static void main(String[] args)
        {
       LinkedList <Patient_Records> list = new LinkedList <Patient_Records>();
    		  
    AddList(list);
    printList(list);
    Collections.sort(list);
        }
         public static void AddList(LinkedList<Patient_Records> list)
        {
                    list.add(new Patient_Records("Dr.Saad Kalid","Sunday","Ibrahim","Ahmed",109876543));
    
                    list.add(new Patient_Records("Bader","Al-Subei","Dr.Aziz Nasser","Monday",123456789));
    
                    list.add(new Patient_Records("Yasser","Al-Harthi","Dr.Hassn Hamad","Friday",1298733456));
        }
        public static void printList(LinkedList<Patient_Records> list)
    {
       
        for (Patient_Records e : list)
                    {
                       System.out.println(e.toString() + "\n") ;
                    }
    }
    
    }

    Code:
    public class Patient_Records extends Hospital_Records implements Comparable 
    {
    
        private String Dr_Name;
        private String Appoitments;
    
        public Patient_Records(String Dr_Name,String Appoitments,
                String Patient_fName,String Patient_LName,
                int recID)
        {
            super(Patient_fName,Patient_LName,recID);
            this.Dr_Name = Dr_Name;
            this.Appoitments = Appoitments;
        }
    
        public void setDr_Name(String DrName)
        {
            Dr_Name = DrName;
        }
    
        public void setAppoitments(String Appiot)
        {
            Appoitments = Appiot;
        }
    
        public String getDr_Name()
        {
            return Dr_Name;
        }
    
        public String getAppoitment()
        {
            return Appoitments;
        }
    
         public String toString()
        {
            return (super.toString() + "Doctor Name: " + getDr_Name() +
                    "\nAppoitment on : " + getAppoitment());
        }
     public int compareTo(Object o) {
    
    // 
    {
    }

  2. #2
    Join Date
    Jun 2006
    Posts
    625

    Re: sorting linkedlist

    The code you have written is good but according to me the sorting function should be small, its too complex. Try from the below example:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    void create();
    void show();
    void sort();
    struct node
    {
        int d;
        struct node *link;
    }*head=0,*current,*prev;
    int num_nodes=0;
    void main()
    {
        int choice;
        while(1)
        {
            printf("\n1.Create\n2.Display\n3.Sort\n4.Exit\n");
            printf("Enter ur choice:");
            scanf("%d",&choice);
            switch(choice)
            {
            case 1:
                create();
                break;
            case 2:
                show();
                break;
            case 3:
                sort();
                break;
            case 4:
                exit(0);
            }
        }
    }
    void create()
    {
        int data;
        current=(struct node*)malloc(sizeof(struct node));
        printf("Enter the data:");
        scanf("%d",&data);
        num_nodes++;
        if(head==0)
        {
            head=current;
            current->d=data;
            current->link=NULL;
            prev=current;
            
        }
        else{
            current->d=data;
            current->link=NULL;
            prev->link=current;
            prev=current;
        }
    }
    void show()
    {
        current=head;
        while(current!=NULL)
        {
            printf("->%d",current->d);
            current=current->link;
        }
        
    }
    void sort()
    {
        // current ptr is used to point the current node in the list
        //prev ptr is used to point the previous node of current node
        //next ptr is used to point the next node of current node
        int i,j;
        struct node *prev=0,*next=0;
        //Initially current node and previous node should be the same(first) node in the list
        current=head;
        prev=head;
        next=head->link;
        for(i=0;i<num_nodes-1;i++)
        {
            for(j=0;j<num_nodes-i-1;j++)
            {
                if(current->d>next->d)
                {
                    current->link=next->link;
                    next->link=current;
                    if(current==head)
                    {
                        head=next;prev=next;
                    }
                    else
                    {
                        prev->link=next;prev=next;
                    }
                    if(next!=NULL)//check whether final node is reached 
                        next=current->link;
                    
                }
                else//just moved each node ptr by one position
                {
                    prev=current;
                    current=next;
                    next=current->link;
                }
                
            }
            //For next iteration make the initial settings again
            current=head;
            prev=head;
            next=current->link;
        }
        
        //For display the sorted numbers
        current=head;
        while(current!=NULL)
        {
            printf("->%d",current->d);
            current=current->link;
        }
    }

Similar Threads

  1. Problem in LinkedList destructor.
    By KAIRU26 in forum Software Development
    Replies: 5
    Last Post: 15-02-2010, 06:14 PM
  2. Sorting a hashmap
    By Gunner 1 in forum Software Development
    Replies: 5
    Last Post: 10-02-2010, 04:06 AM
  3. How to remove an item from LinkedList in java program?
    By MAHAH in forum Software Development
    Replies: 4
    Last Post: 22-01-2010, 09:31 PM
  4. Java program to reverse the order of LinkedList elements?
    By MKAIF in forum Software Development
    Replies: 5
    Last Post: 22-01-2010, 08:44 PM
  5. How to sort LinkedList elements using java program?
    By KADRI in forum Software Development
    Replies: 4
    Last Post: 22-01-2010, 07:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •