473,387 Members | 1,453 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,387 software developers and data experts.

sorting linked list alphabitecally

hello. I have to use a function in my phonebook program to sort the names alphabitcally
here is what i have done so far.but what's wrong with it?


void showAllContacts() {
char t[40];
list *head;
if(head == NULL) {
puts("There are no contacts to display!");
} else {
printf("\nAll contacts:\n");
/* Set the temp pointer to point to the first record in the
list. */
list *temp = head;
/* Loop through all the nodes in the linked list and print
out all the corresponding name and phone records. */
while(temp!= NULL){
if(strcmp(temp->name,temp->next->name) > 0) {
strcpy(t, temp->name);
strcpy(temp->name, temp->next->name);
strcpy(temp->next->name, t);
cout<<temp->name<<temp->tel;
}
//else {
//printf("%-20s %-9s\n", temp->name,temp->tel);
//}
temp=temp->next;
}
}
}
Dec 28 '14 #1
3 3413
Ajay Bhalala
119 64KB
You can write the code between the "Code Tags" for better view for understanding. If you use the code tags, your code area looks like below

Expand|Select|Wrap|Line Numbers
  1. void showAllContacts() { 
  2. char t[40];
  3. list *head;
  4. if(head == NULL) {
  5. puts("There are no contacts to display!");
  6. } else {
  7. printf("\nAll contacts:\n");
  8. /* Set the temp pointer to point to the first record in the
  9. list. */
  10. list *temp = head;
  11. /* Loop through all the nodes in the linked list and print
  12. out all the corresponding name and phone records. */
  13. while(temp!= NULL){
  14. if(strcmp(temp->name,temp->next->name) > 0) {
  15. strcpy(t, temp->name);
  16. strcpy(temp->name, temp->next->name);
  17. strcpy(temp->next->name, t);
  18. cout<<temp->name<<temp->tel;
  19. }
  20. //else {
  21. //printf("%-20s %-9s\n", temp->name,temp->tel);
  22. //}
  23. temp=temp->next;
  24. }
  25. }
  26. }
Dec 28 '14 #2
weaknessforcats
9,208 Expert Mod 8TB
I suggest reading up on a bubble sort.

Essentially you need a nested while loop. The outer loop starts with the first node and drops into the nested loop which start with the node from the outer loop and compares the string it has to every other node in the list. If the other node string is less, then the other node is swapped with the node in the outer loop. When the inner loop reaches the end of the list, the smallest string is in the node in the outer loop. Then the outer loop advances one node and the inner loop repeats.
Dec 28 '14 #3
donbock
2,426 Expert 2GB
There are many sorting algorithms that differ in coding complexity, worst-case execution time, typical execution time, best-case execution time, etc.

Bubble Sort is useful for illustrating certain program constructs, but it is not a particularly efficient search algorithm. I suggest you take a look at Insertion Sort.

(Not that insertion sort is the best possible sort either.)
Dec 29 '14 #4

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

Similar topics

10
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
3
by: sugaray | last post by:
hi, i have to build a linked-list which has another sturcture _score as it's data entry, so how can i sort such linked-list based on, let say, history score into proper order...
3
by: chellappa | last post by:
hi this simple sorting , but it not running...please correect error for sorting using pointer or linked list sorting , i did value sorting in linkedlist please correct error #include<stdio.h>...
1
by: PhilB | last post by:
I have been having issues trying to merge sort a double linked list. (I would supply the code, but it is locked away on an inaccessable computer.) The list always results in an unsorted list, but...
2
by: VirusFree | last post by:
hi guys, i wrote an article about an algorithm i came up with, (which actually is based on hash algorithms..) and it's for sorting linked lists... (up to 10 times faster than mergesort on my...
6
by: mbk006 | last post by:
I am new for linked lists. So please help. So any one kindly send the algorithms for creating single linked list, sorting linked list and Deleting link list
5
by: Draggonn | last post by:
Hi all. I wanted to ask if this code for sorting linked list was ok. what i mean is, is it written without too much mess :P such as unnecessary lines and pointers, etc.. what i did was make a second...
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 // ...
2
by: raamay | last post by:
i have been given an assignment to create a singly linked list which maintains order. but i am not sure whether to use a sorting method or to take care of the sorting while adding the values in the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.