Skip to content

Commit

Permalink
Create Linked listdeki elemanları kücükten buyuge dogru sıralayan pro…
Browse files Browse the repository at this point in the history
…gramı bubble sort ile sıralayınız.c
  • Loading branch information
arslanaybars committed Jul 24, 2014
1 parent 0c4fb3d commit b136a3e
Showing 1 changed file with 112 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@

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

#define MAX 10

struct lnode {
int data;
struct lnode *next;
} *head, *visit;


void llist_add(struct lnode **q, int num);

void llist_bubble_sort(void);

void llist_print(void);

int main(void) {
/* linked list */
struct lnode *newnode = NULL;
int i = 0; /* a general counter */

/* Linked liste rastgele deger uretme */
for(i = 0; i < MAX; i++) {
llist_add(&newnode, (rand() % 100));
}

head = newnode;
printf(" The List:\n");
llist_print();
printf(" After bubble sort:\n");
llist_bubble_sort();
llist_print();

printf("\n\n Prees Enter to exit..\n\n");

return 0;
}


void llist_add(struct lnode **q, int num) {
struct lnode *tmp;

tmp = *q;

if(*q == NULL) {
*q = malloc(sizeof(struct lnode));
tmp = *q;
} else {

while(tmp->next != NULL)
tmp = tmp->next;


tmp->next = malloc(sizeof(struct lnode));
tmp = tmp->next;
}


tmp->data = num;
tmp->next = NULL;
}


void llist_print(void) {
visit = head;

while(visit != NULL) {
printf("%d ", visit->data);
visit = visit->next;
}
printf("\n");
}

void llist_bubble_sort(void) {
struct lnode *a = NULL;
struct lnode *b = NULL;
struct lnode *c = NULL;
struct lnode *e = NULL;
struct lnode *tmp = NULL;


while(e != head->next) {
c = a = head;
b = a->next;
while(a != e) {
if(a->data > b->data) {
if(a == head) {
tmp = b -> next;
b->next = a;
a->next = tmp;
head = b;
c = b;
} else {
tmp = b->next;
b->next = a;
a->next = tmp;
c->next = b;
c = b;
}
} else {
c = a;
a = a->next;
}
b = a->next;
if(b == e)
e = a;
}
}
}

0 comments on commit b136a3e

Please sign in to comment.