|
| ||||||||||
| Tags: linkedlist, sorting |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| sorting linkedlist
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
| ||||
| ||||
| 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;
}
} |
![]() |
|
| Thread Tools | Search this Thread |
| |
Similar Threads for: "sorting linkedlist" | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Problem in LinkedList destructor. | KAIRU26 | Software Development | 5 | 15-02-2010 06:14 PM |
| Sorting a hashmap | Gunner 1 | Software Development | 5 | 10-02-2010 04:06 AM |
| How to remove an item from LinkedList in java program? | MAHAH | Software Development | 4 | 22-01-2010 09:31 PM |
| Java program to reverse the order of LinkedList elements? | MKAIF | Software Development | 5 | 22-01-2010 08:44 PM |
| How to sort LinkedList elements using java program? | KADRI | Software Development | 4 | 22-01-2010 07:45 PM |