Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct cell_t cell_t;
- struct cell_t {
- int key;
- cell_t *next;
- };
- typedef struct {
- int key;
- } node_t;
- cell_t *new_cell(int key) {
- cell_t *new = (cell_t *) malloc(sizeof(cell_t));
- new->key = key;
- new->next = NULL;
- return new;
- }
- cell_t *add_cell(cell_t *first, int key) {
- cell_t *p, *prev, *new = new_cell(key);
- for (p = first, prev = NULL; p; prev = p, p = p->next)
- if (p->key > key) break;
- if (!p && !prev) {
- return new;
- } else if (!p && prev) {
- prev->next = new;
- return first;
- } else if (p && !prev) {
- new->next = p;
- return new;
- } else {
- prev->next = new;
- new->next = p;
- return first;
- }
- }
- void puts_cell(cell_t *first) {
- cell_t *p;
- printf("[");
- for (p = first; p; p = p->next) {
- printf("%d", p->key);
- if (p->next) printf(", ");
- }
- printf("]\n");
- }
- node_t *new_node(int key) {
- node_t *new = (node_t *) malloc(sizeof(node_t));
- new->key = key;
- return new;
- }
- node_t **add_node(node_t **prime, int key, int *count) {
- node_t *new = new_node(key);
- prime = realloc(prime, ++(*count) * sizeof(node_t *));
- prime[*count - 1] = new;
- return prime;
- }
- void puts_node(node_t **prime, int count) {
- int i;
- printf("[");
- for (i = 0; i < count; i++) {
- printf("%d", prime[i]->key);
- if (i < count - 1) printf(", ");
- }
- printf("]\n");
- }
- int compare_node(const void *p1, const void *p2) {
- node_t **q1 = (node_t **) p1;
- node_t **q2 = (node_t **) p2;
- return (*q1)->key - (*q2)->key;
- }
- int main(void) {
- int a[] = { 19, 15, 18, 2, 6, 12, 19, 26, 5, 20 };
- int i;
- cell_t *first = NULL;
- node_t **prime = NULL;
- int count = 0;
- puts("Cell");
- for (i = 0; i < sizeof(a) / sizeof(int); i++) {
- first = add_cell(first, a[i]);
- puts_cell(first);
- }
- puts("Node");
- for (i = 0; i < sizeof(a) / sizeof(int); i++) {
- prime = add_node(prime, a[i], &count);
- puts_node(prime, count);
- }
- puts("sort");
- qsort(prime, count, sizeof(node_t *), compare_node);
- puts_node(prime, count);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement