473,320 Members | 1,914 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,320 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 1491
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.