forked from arslanaybars/C-Programlama-Ornekleri
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create Linked listdeki elemanları kücükten buyuge dogru sıralayan pro…
…gramı bubble sort ile sıralayınız.c
- Loading branch information
1 parent
0c4fb3d
commit b136a3e
Showing
1 changed file
with
112 additions
and
0 deletions.
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
...istdeki elemanları kücükten buyuge dogru sıralayan programı bubble sort ile sıralayınız.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
|