Posts

Showing posts with the label struct

bubble sort with struct using c programming

Bubble sort - linked list #define MAX 10 struct lnode { int data; struct lnode *next; } *head, *visit; /* add a new entry to the linked list */ void llist_add(struct lnode **q, int num); /* preform a bubble sort on the linked list */ void llist_bubble_sort(void); /* print the entire linked list */ void llist_print(void); int main(void) { /* linked list */ struct lnode *newnode = NULL; int i = 0; /* a general counter */ /* load some random values into the linked list */ for(i = 0; i < MAX; i++) { llist_add(&newnode, (rand() % 100)); } head = newnode; printf("Before bubble sort:\n"); llist_print(); printf("After bubble sort:\n"); llist_bubble_sort(); llist_print(); return 0; } /* adds a node at the end of a linked list */ void llist_add(struct lnode **q, int num) { struct lnode *tmp; tmp = *q; /* if the list is empty, create first node */ if(*q == NULL) { *q = malloc(sizeof(struct lnode)); tmp = *q; } else {

insertion sort using struct

2d example insertion sort #include <stdio.h> #include <conio.h> struct node { int number; struct node *next; }; struct node *head = NULL; /* insert a node directly at the right place in the linked list */ void insert_node(int value); int main(void) { struct node *current = NULL; struct node *next = NULL; int test[] = {8, 3, 2, 6, 1, 5, 4, 7, 9, 0}; int i = 0; /* insert some numbers into the linked list */ for(i = 0; i < i =" 0;">next != NULL) { printf("%4d\t%4d\n", test[i++], head->number); head = head->next; } /* free the list */ for(current = head; current != NULL; current = next) next = current->next, free(current); return 0; } void insert_node(int value) { struct node *temp = NULL; struct node *one = NULL; struct node *two = NULL; if(head == NULL) { head = (struct node *)malloc(sizeof(struct node *)); head->next = NULL; } one = head; two = head->next; temp = (struct node *)ma