Merging two linked lists C++
1
Name:
Al-Jazeera
2013-04-27 20:41
Hello /prog/
I'm having some trouble with a function that is supposed to merge two linked lists. I'm being passed two Linked Lists as parameters and want to know an efficient way of making it so that I can merge these two lists together (duplicates are allowed) without using an insert function. The code that I have is here:
http://pastebin.com/RtUGcYhy
Any help/suggestions/criticism will be appreciated.
2
Name:
Anonymous
2013-04-27 20:50
merge compare xs [] = xs
merge compare [] ys = ys
merge compare (x:xs) (y:ys) =
if compare x y
then x:(merge compare xs (y:ys))
else y:(merge compare (x:xs) ys)
3
Name:
Anonymous
2013-04-27 20:53
Pick one.
1. link one's tail to the other's head;
2. insert one's elements into the other;
3. create a third list and insert all elements.
4
Name:
Anonymous
2013-04-27 22:21
C++ a shit
5
Name:
Anonymous
2013-04-27 23:12
>>4
You cannot possibly be worse than /prog/.
6
Name:
Anonymous
2013-04-27 23:27
it could be fast enough. A typical destructive merge.
#include <stdio.h>
#include <string.h>
struct node {
void* car;
struct node* cdr;
};
struct node* destructomerge(int (*compare)(void*,void*), struct node* list1, struct node* list2) {
if (!list1) return list2;
if (!list2) return list1;
struct node* head;
struct node* tail;
if (compare(list1->car,list2->car) < 0) {
head = tail = list1;
list1 = list1->cdr;
} else {
head = tail = list2;
list2 = list2->cdr;
}
loop:
if(!list1) {
tail->cdr = list2;
return head;
} else if(!list2) {
tail->cdr = list1;
return head;
} else if(compare(list1->car,list2->car) < 0) {
tail->cdr = list1;
tail = list1;
list1 = list1->cdr;
goto loop;
} else {
tail->cdr = list2;
tail = list2;
list2 = list2->cdr;
goto loop;
}
}
void for_each(void (*visitor)(void*), struct node* list) {
loop:
if(list) {
visitor(list->car);
list = list->cdr;
goto loop;
}
}
int main(int argc, char** argv) {
struct node a1;
struct node a2;
struct node a3;
struct node b1;
struct node b2;
struct node b3;
a1.car = "apples";
a2.car = "grapes";
a3.car = "oranges";
b1.car = "grains";
b2.car = "meats";
b3.car = "sugary sweets";
a1.cdr = &a2;
a2.cdr = &a3;
a3.cdr = NULL;
b1.cdr = &b2;
b2.cdr = &b3;
b3.cdr = NULL;
printf("a1:\n");
for_each(puts, &a1);
printf("b1:\n");
for_each(puts, &b1);
struct node* merged = destructomerge(strcmp, &a1, &b1);
printf("merged:\n");
for_each(puts, merged);
return 0;
}
7
Name:
Anonymous
2013-04-27 23:29
I know I did your homework, but I'm hoping you learn something from this example. And it's written in C with bad enough practice to need a rewrite.
8
Name:
>>7
2013-04-28 0:01
After reading your source more carefully, your code looks as efficient as it can be. What did you want to improve about it?
9
Name:
Anonymous
2013-04-28 3:46
You should avoid writing your own code when you can just use the standard library. It has platform-specific optimizations, and is much easier to maintain.
10
Name:
Anonymous
2013-04-28 13:59
>>9
WHY U NO STD::STRING ???
11
Name:
Anonymous
2013-04-28 21:54
Searching for legit Microsoft Product keys, Windows 8,7,Studio,Server etc.?
Mail me at jeremiahgoldstein@hotmail.com
25$ a pop
Searching for legit Microsoft Product keys, Windows 8,7,Studio,Server etc.?
Mail me at jeremiahgoldstein@hotmail.com
25$ a pop
Searching for legit Microsoft Product keys, Windows 8,7,Studio,Server etc.?
Mail me at jeremiahgoldstein@hotmail.com
25$ a pop
12
Name:
Anonymous
2013-04-28 22:04
Searching for legit Microsoft Product keys, Windows 8,7,Studio,Server etc.?
Mail me at jeremiahgoldstein@hotmail.com
25$ a pop
Searching for legit Microsoft Product keys, Windows 8,7,Studio,Server etc.?
Mail me at jeremiahgoldstein@hotmail.com
25$ a pop
Searching for legit Microsoft Product keys, Windows 8,7,Studio,Server etc.?
Mail me at jeremiahgoldstein@hotmail.com
25$ a pop
13
Name:
Anonymous
2013-04-28 22:11
Searching for legit Microsoft Product keys, Windows 8,7,Studio,Server etc.?
Mail me at jeremiahgoldstein@hotmail.com
25$ a pop
Searching for legit Microsoft Product keys, Windows 8,7,Studio,Server etc.?
Mail me at jeremiahgoldstein@hotmail.com
25$ a pop
Searching for legit Microsoft Product keys, Windows 8,7,Studio,Server etc.?
Mail me at jeremiahgoldstein@hotmail.com
25$ a pop
14
Name:
Anonymous
2013-04-28 22:17
Searching for legit Microsoft Product keys, Windows 8,7,Studio,Server etc.?
Mail me at jeremiahgoldstein@hotmail.com
25$ a pop
Searching for legit Microsoft Product keys, Windows 8,7,Studio,Server etc.?
Mail me at jeremiahgoldstein@hotmail.com
25$ a pop
Searching for legit Microsoft Product keys, Windows 8,7,Studio,Server etc.?
Mail me at jeremiahgoldstein@hotmail.com
25$ a pop
15
Name:
Anonymous
2013-12-01 13:26
░░░░░░░░░░░▄▄▄▄▄░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░▄▄█████████▄▄░░░░░░░░░░░░░░░
░░░░░░▄▀▀▀▀█▀▀▀▀▀▀█████▄░░░░░░░░░░░░░
░░░░▄██████░░░░░░░░░░░▀██░░░░░░░░░░░░
░░░▐██████▌░░░░░░░░░░░░░▐▌░░░░░░░░░░░
░░░███████▌░░░░░░░░░░░░░░█░░░░░░░░░░░
░░▐████████░░░░░░░░░░░░░░░█░░░░░░░░░░
░░▐██████▌░░░░░▄▀▀▀▀▀▄░▀░▄▄▌░░░░░░░░░
░░░█▀▀███▀░░░░░░▄▀█▀░░░▐▄▄▄▌░░░░░░░░░
░░▐░▌▀▄░░░░░░░░░░▄▄▄▀░░░▌▀░▌░░░░░░░░░
░░░▌▐░░▌░░░░░░░░░░░▀░░░░▐░▐░░░░░░░░░░
░░░▐░▀▄▐░░░░░░░░░░░▌▌░▄▄░▐░▌░░░░░░░░░
░░░░▀█░▄▀░░░░░░░░░▐░▐▄▄▄▄▀▐░░░░░░░░░░
░░░░░▌▀░▐░░░░░░░░▄▀░░▀▀▀▀░▌░░░░░░░░░░
░░░░░▐░░░░░░░░░▌░░░▄▀▀▀▀▄▐░░░░░░░░░░░
░░░░░▌░░░░░░░░░▐░░░░░▄▄░░▌░░░░░░░░░░░
░░░░█▀▄░░░▐░▐░░░░░░░░░░░█░░░░░░░░░░░░
░░░█░█░▀▀▄░▌░█░░░▀▀▄▄▄▄▀░░░░░░░░░░░░░
░░█░░░▀▄░░▀▀▄▄█░░░░░▄▀░░░░░░░░░░░░░░░
░░█░░░░░▀▄░░░░▀▀▄▄▄▀▐░░░░░░░░░░░░░░░░
░░█░░░░░░░▀▄░░░░░▐░▌▐░░░░░░░░░░░░░░░░
░░░█░░░░░░░░▀▄░░░▌░▐▌▐░░░░░░░░░░░░░░░
░░░░█░░░░░░░░░█░▐░▄▄▌░█▀▄░░░░░░░░░░░░
░░░░░█░░░░░░░░░█▌▐░▄▐░░▀▄▀▀▄▄░░░░░░░░
░░░░░░█░░░░░░░░░▀▄░░▐░░░▀▄░░░▀▀▄▄░░░░
░░░░░░░▀▄░▄▀█░░░░░█░░▌░░░░▀▄░░░░░█░░░
16
Name:
Anonymous
2013-12-01 14:20
>>11-15
who are you quoting?