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;
}
}
}
3 3413
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 - 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;
-
}
-
}
-
}
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.
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.)
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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>...
|
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...
|
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...
|
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
|
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...
|
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
// ...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |