Quantcast
Channel: D3Scene
Viewing all articles
Browse latest Browse all 21080

C - Insertion sort algorithm with lists

$
0
0
I recently had to program the insertion sort algorithm in C, using lists and pointers.

This is what I accomplished. It's quite long, however, well commented in case someone is still looking for code to optimize, learn from it or have a good laugh at.

Code:

/*
                Algorithm: Insertion Sort with lists
                Author: tracky@d3scene.com
                Written in C


*/


#include <stdio.h>
#include <stdlib.h>


typedef struct elem{
        int value;
        struct elem *next;
}elem;




//Prints every item of a list
void print_list ( elem* start ) {


        int i = 1;
        //If start == NULL we reached the nullpointer (no more list elements)
        while (start != NULL ) {
                printf("Position %d\t\t Value: %d\n", i++, start->value);
                start = start->next;
        }
        printf("================================\n");
}








//Inserts a new element 'behind' the passed element.
void insert (elem *currElem, int newValue) {


        elem *newElem = (elem*) malloc(sizeof(elem));        //Dynamically allocating memory for our new element
        newElem->next = currElem->next;                                        //The 'old' pointer to the next elements gets passed to our new element
        newElem->value = newValue;                                                //Sets the value of our new element               
        currElem->next = newElem;                                                // And finally sets the very last pointer so the list is finished.
       
}




void insert_sort ( elem *start, int newValue) {
       
        elem *curr, *prev;
       
        prev = start;
        curr = start;
       
        do {
                        //If we reached the end of the list OR there's only one element
                if ( curr->next == NULL ) {
                                //In case we went 'too far' we have to insert the new element to the left of our current value
                        if ( newValue < curr->value ) {
                                insert(prev, newValue );
                                return;
                        } else {
                                //Otherwise we can add it to the right.
                                insert(curr, newValue);
                                return;
                        }
                       
                }
                        //If the list is a little longer and we need to add a value to the left of our current element.
                if ( curr->value > newValue && prev->value <= newValue ) {
                        insert(prev, newValue);
                        return;
                }
                //This is only for iterating through the list, kind of recursively though.
                prev = curr;
                curr = curr->next;
               
                // And we do this until there's no more elements in the passed list.
        } while (curr != NULL);
       
}
// Set's everything up.
int main () {
        elem *start;
        start = (elem*)malloc(sizeof(elem));
        start->next = NULL;
        int n;
       
        do {
                printf("Insert an integer to add to the list. -1 exits the program!:\n");
                scanf("%d", &n);
                if (n != -1) {
                        insert_sort(start, n);
                        //In this example start is an element thus holds a value as well as a next pointer.
                        //If we passed 'start' to print_list instead of 'start->next' we'd also have another item
                        //we don't actually need.
                        print_list(start->next);
                        printf("%d has been added to the list!\n", n);
                }
        } while (n != -1);
       
        return 0;
}

Feel free to suggest your ideas.
(Yes, the insert-algorithm it self can be accomplished with a lot less lines of code ;p)

Viewing all articles
Browse latest Browse all 21080

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>