473,408 Members | 2,734 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,408 software developers and data experts.

sort linked list alphabetikally

4
I'm trying to sort the list alphabetically , but nothing works .But when I sort of numbers the same code all works, why.
#include <iostream>
#include <cstring>
#include <ctdlib>

using namespace std;

struct book {
char name[100];
char author[50];
int pages;
int price;
int yt;
};

struct element {
book x;
element *next;
};

void add(book x, element **head){
element * t = new element;
t->x = x;
t->next = *head;
*head = t;
}

void addLast(book x, element ** head, element **tail) {
element * t = new element;
t->x = x;
t->next = NULL;
if (*head){
(*tail)->next = t;
} else {
*head = t;
}
*tail = t;
}



void print(element *head) {
cout << "------------------------" << endl;
element * t = head;
while (t != NULL) {
cout << t->x.name<< " | " << t->x.author << " | "
<< t->x.pages << " | " << t->x.price <<"|"<<t->x.yt<< endl;
t = t->next;
}
cout << endl << "------------------------" << endl;
}
void print(book b) {
cout << b.name<< " | " << b.author << " | "
<< b.pages << " | " << b.price <<"|"<<b.yt<< endl;
}
void bS(element**head){
bool change;
element *t;
do
change=false;
for(t=*head;t->next;t=t->next)
if(strcmp(t->x.name,t->next->x.name))
{
strcpy(char temp,t->x.name);
strcpy(t->x.name,t->next->x.name);
strcpy(t->next->x.name,temp);
change=true;
}

while(change);
}
int main() {

element *head = NULL;
element *tail = NULL;

book b;
strcpy(b.name, "C");
strcpy(b.author, "D");
b.pages = 100;
b.price = 1500;
b.yt=1980;
addLast(b, &head, &tail);

strcpy(b.name, "C");
strcpy(b.author, "M");
b.pages = 300;
b.price = 2500;
b.yt=1982;
addLast(b, &head, &tail);

strcpy(b.name, "K");
strcpy(b.author, "H");
b.pages = 120;
b.price = 1000;
b.yt=1975;
addLast(b, &head, &tail);

print(head);




bS(&head);
cout << "\nAfter sort :\n";
print(head);


//
//
return 0;
}
May 25 '15 #1

✓ answered by weaknessforcats

The best thing is to resubmit this code with some comments where you think you are sorting. Often just writing comments will expose errors.

1 1492
weaknessforcats
9,208 Expert Mod 8TB
The best thing is to resubmit this code with some comments where you think you are sorting. Often just writing comments will expose errors.
May 26 '15 #2

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...
1
by: Booser | last post by:
// Merge sort using circular linked list // By Jason Hall <booser108@yahoo.com> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> //#define debug
4
by: MJ | last post by:
Hi I have written a prog for reversing a linked list I have used globle pointer Can any one tell me how I can modify this prog so that I dont have to use extra pointer Head1. When I reverse a LL...
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>...
4
by: Peter Schmitz | last post by:
Hi, in my application, I defined a linked list just as follows: typedef struct _MYLIST{ int myval; void *next; }MYLIST; MYLIST head;
6
by: Julia | last post by:
I am trying to sort a linked list using insertion sort. I have seen a lot of ways to get around this problem but no time-efficient and space-efficient solution. This is what I have so far: ...
6
by: Nick Valeontis | last post by:
I know how to use Icomparable. However, I can't figure out how to sort a generic linked list? (without writing the algorithm) Lets say I have something like this: class...
3
by: t | last post by:
What is the most efficient way to sort a linked list? In other words, how is std::list's sort() member function usually implemented? I was thinking of two approaches: (1) make an array of...
2
by: phiefer3 | last post by:
Ok, first of all I'm not sure if this is the correct forum for this question or not. But hopefully someone can help me or at least point me in the direction of the forum this belongs. First of...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.