char array

How to sort CHAR arrays from linked list?
There is an std::sort() function, but it may not work for you.
http://cplusplus.com/reference/algorithm/sort/

Please give more information. Do you use a custom linked list? And are you sure you store char arrays and not simply char's?
I have struct like this....

struct list{
int password, day, month, year;
float price;
char name[20];
lista *next;
};

I must sort list by password. And this work. From linked list i have move passwords to array. And with sorted array i find memory location of elements and program prints sorted list Z-A. If in list exist equal passwords i must sort this 2 elements by name. How can i do this? This is my code for linked list
float numbers -> array with passwords
float polje_sifra -> this is not important




void merge(float numbers[], float temp[],float polje_sifra[], int left, int mid, int right)
{
float *temp2 = new float[br_unesenih_el(glava)];
int i, left_end, num_elements, tmp_pos;

left_end = mid - 1;
tmp_pos = left;
num_elements = right - left + 1;

while ((left <= left_end) && (mid <= right))
{
if (numbers[left] <= numbers[mid])
{
temp[tmp_pos] = numbers[left];
temp2[tmp_pos] = polje_sifra[left]; //to
tmp_pos = tmp_pos + 1;
left = left +1;
}
else
{
temp[tmp_pos] = numbers[mid];
temp2[tmp_pos] = polje_sifra[mid];
tmp_pos = tmp_pos + 1;
mid = mid + 1;
}
}

while (left <= left_end)
{
temp[tmp_pos] = numbers[left];
temp2[tmp_pos] = polje_sifra[left];
left = left + 1;
tmp_pos = tmp_pos + 1;
}
while (mid <= right)
{
temp[tmp_pos] = numbers[mid];
temp2[tmp_pos] = polje_sifra[mid];
mid = mid + 1;
tmp_pos = tmp_pos + 1;
}

for (i=0; i <= num_elements; i++)
{
numbers[right] = temp[right];
polje_sifra[right] = temp2[right];
right = right - 1;
}
}
void m_sort(float numbers[], float temp[],float polje_sifra[], int left, int right)
{
int mid;

if (right > left)
{
mid = (right + left) / 2;
m_sort(numbers, temp, polje_sifra, left, mid);
m_sort(numbers, temp,polje_sifra, mid+1, right);

merge(numbers, temp, polje_sifra, left, mid+1, right);
}
}
void mergeSort(float numbers[], float temp[], float polje_sifra[], int array_size)
{
m_sort(numbers, temp, polje_sifra, 0, array_size - 1);
}
If I was you, I'd rewrite the program to take advantage of what C++ offers.
The problems are:
1) I don't know if your program is C or C++.
2) I don't know if you are required to write your program in your current style.

However, if you use C++ and are permitted to go "freestyle", I suggest you rewrite your list structure as a class which overloads operator< as a comparison function, comparing the passwords, and names if needed.

Then all you need to do is keep all your list things in an std::set container, and they'll be automatically sorted.
I work in C++
How can i sort this by name on easy way?
Last edited on
If you want to adapt your current code, and compare your name char arrays, look up strcmp():
http://cplusplus.com/reference/clibrary/cstring/strcmp/

But here's how I would do it (and how I told you to do it).

It looks hard but it's not. You may delete the two Packet constructors if you want to initialize the data manually.

The overloaded comparison operator< is used in the background to sort the Packets.
The overloaded ostream& operator<< is just for convenience.
If you absolutely want to use lists, you can put the Packets in an std::list instead of an std::set, then use std::sort().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <ostream>
#include <set>
#include <string>

// let's not use "list" as name, because this won't be a list
struct Packet {
    int password, day, month, year;
    float price;
    std::string name; // easier to use

    // constructor for "default" object, uses initializer lists
    Packet():
        password(0),
        day(23),
        month(12),
        year(2011),
        price(12.0f),
        name("Cake")
    {
    }

    // constructor for writing less code initializing new objects
    Packet(int password, int day, int month, int year, float price, const std::string &name):
        password(password),
        day(day),
        month(month),
        year(year),
        price(price),
        name(name)
    {
    }

    // this comparison operator will be used automatically by std::set
    bool operator < (const Packet &p) const
    {
        if (password == p.password) // same password,
            return name < p.name; // so compare names

        return password < p.password;
    }
};

// this teaches std::cout to print Packet objects
std::ostream & operator << (std::ostream &os, const Packet &p)
{
    os << "Packet at memory address " << &p << " contains:";
    os << "\npassword: ....... " << p.password;
    os << "\nday, month, year: " << p.day << ' ' << p.month << ' ' << p.year;
    os << "\nprice: .......... " << p.price;
    os << "\nname: ........... " << p.name;
    os << std::endl;
    return os;
}

int main()
{
    // declare the Packets
    Packet p1(331, 1, 12, 1998, 5.0f, "Zap Cola");
    Packet p2(331, 2, 12, 1998, 5.1f, "Pepsi");
    Packet p3(983, 12, 3, 2012, 12.0f, "Hamburger");
    Packet p4(10, 3, 2, 2001, 2.0f, "Hot dog");

    // Set containing Packets
    std::set<Packet> sp;

    // add our objects to the Set
    sp.insert(p1);
    sp.insert(p2);
    sp.insert(p4);
    sp.insert(p3);
    sp.insert(Packet(983, 12, 3, 2012, 14.0f, "Cheeseburger")); // this works too

    // display the Packets in the Set
    for (std::set<Packet>::const_iterator ci = sp.begin(); ci != sp.end(); ++ci)
        std::cout << *ci << std::endl;
}


Output for me:
Packet at memory address 0x3e2560 contains:
password: ....... 10
day, month, year: 3 2 2001
price: .......... 2
name: ........... Hot dog

Packet at memory address 0x3e2530 contains:
password: ....... 331
day, month, year: 2 12 1998
price: .......... 5.1
name: ........... Pepsi

Packet at memory address 0x3e2500 contains:
password: ....... 331
day, month, year: 1 12 1998
price: .......... 5
name: ........... Zap Cola

Packet at memory address 0x3e25e8 contains:
password: ....... 983
day, month, year: 12 3 2012
price: .......... 14
name: ........... Cheeseburger

Packet at memory address 0x3e2590 contains:
password: ....... 983
day, month, year: 12 3 2012
price: .......... 12
name: ........... Hamburger


Last edited on
Topic archived. No new replies allowed.