473,386 Members | 1,842 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Inserting in a list in Alphabetical order

5
..how can I insert entry in a linked list and arrange my entries according to the name in alphabetical order? I need to make a student record which contains the name, yr level and course of the student.
Is bubble sort applicable in this problem?if yes, can anyone help me how to implement it..tnx..
Sep 30 '06 #1
2 6503
D_C
293 100+
Bubble sort, while easiest to implement, has the worst best case performance ever. What you want is insertion sort. Each time you put in an entry, put it in in the correct place, and the list will be sorted when you are done inserting.

For example, insertion sort 1 1 9 8 4 3 2 1
Expand|Select|Wrap|Line Numbers
  1. data->NULL // list data points to a null entry
  2. data->1->NULL
  3. data->1->1->NULL
  4. data->1->1->9->NULL
  5. data->1->1->8->9->NULL
  6. data->1->1->4->8->9->NULL
  7. data->1->1->3->4->8->9->NULL
  8. data->1->1->2->3->4->8->9->NULL
  9. data->1->1->1->2->3->4->8->9->NULL
Sep 30 '06 #2
m013690
23
What you do, since it's a linked list, is you find the last item in the linked list (LL) that would come before the new entry. Now, in a LL, each item should have a pointer (or some other method of reference) to the next item in the LL (call this pointer NEXT). Also, for explanation's sake, let's call your three items A, B and N (for new).

A and B are already in the LL, and A points to B as the next item in the LL. Create N (the new item to insert), and make its NEXT pointer point to whatever A's points to. Then make A's next pointer point to N (the new item). Now the sequence of pointers goes from A -> N -> B.

Following this algorithm, there is never any need to sort anything, because right from the get-go you insert everything in the proper order to begin with, as hinted at in the first response above.

That help?
Sep 30 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Darryl B | last post by:
I can not get anywhere on this project I'm tryin to do. I'm not expecting any major help with this but any would be appreciated. The assignment is attached. The problem I'm having is trying to set...
33
by: Jim Cobban | last post by:
I cannot get Netscape 4.79 to properly display the ordered list in the following fragment. <P>Get a specific portion of the date. Depending upon the value of index: <ol start=0> <li>complete...
1
by: Richy Rich | last post by:
Hi, This may seem like a really stupid question, but I cannot get the source files to appear in alphabetical order in the solution explorer. I've clicked on the A...Z icon, but they don't...
17
by: John Salerno | last post by:
Hi everyone. If I have a list of tuples, and each tuple is in the form: (year, text) as in ('1995', 'This is a citation.') How can I sort the list so that they are in chronological order based...
7
by: canteyn | last post by:
Here is my problem: Structure typedef struct { char lname; char fname; int age; double salary; } employee_t;
9
by: Sheldon | last post by:
Hi, I am trying to understand linked lists and the different ways to write a linked list and double linked list. I have been trying to get this function called insert_word to work but to no...
1
by: inboxforsumit | last post by:
Hello friends... I have to submit an asginment on the following topic. Write a program to scan a text file for unique words and store these words alphabetically in a linked list. You have to take...
2
by: pavanip | last post by:
Hi, I have a problem with binding data to dropdownlist from database in alphabetical order. My database contains some fields like All,Air,Airline,Books,Cars etc. There are 2 dropdown...
5
by: kylie991 | last post by:
I'm confused as to why this isnt working.. am i doing this right?? I have to create a function that does the following: - Insert a word to the linked list so that the list // ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.