Upcoming SlideShare
×

# Tugas1

149 views
92 views

Published on

tugas 1

Published in: Education, Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
149
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
2
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Tugas1

1. 1. NAMA : Alvian Yudha Prawira NIM : A11.2012.07112 Kelompok : A11.4303 #include <stdio.h> #include <stdlib.h> typedef struct list { int data; struct list *next; } LIST; LIST *inslist( LIST *start, int ); LIST *append( LIST *, int ); LIST *sortlist( LIST * ); LIST *ubah_list( LIST *, LIST * ); void cetak_list( LIST * ); int main(void) { LIST *L; int i, l, N, X; L = NULL; printf("Berapa banyak elemen yang diinginkan ? = ");scanf("%d",&N); for(l=1;l<=N;l++) { printf("Elemen ke %d list: ", l);scanf("%d",&X); L = append( L, X ); }
2. 2. printf("Original list:n"); cetak_list( L ); L = sortlist( L ); printf("Sorted list:n"); cetak_list( L ); printf("Berapa banyak elemen yang diinginkan ? = ");scanf("%d",&N); for(l=1;l<=N;l++) { printf("Elemen ke %d list: ", l);scanf("%d",&X); L = inslist( L, X ); } printf("List Terbaru :n"); cetak_list( L ); return 0; } LIST *inslist( LIST *start, int newdata ) { start = append( start, newdata ); start = sortlist( start ); } void cetak_list( LIST *t ) { printf("List : [ "); while( t != NULL ) { printf( "%d ", t->data ); t = t->next; } printf("]n");
3. 3. } LIST *append( LIST *start, int newdata ) { LIST *new, *end, *ret; if( (new = malloc(sizeof(LIST))) == NULL) { printf( stderr, "Memory Allocation error.n" ); exit(1); } if( start == NULL ) ret = new; else { ret = start; end = start; while( end->next != NULL ) end = end->next; end->next = new; } new->data = newdata; new->next = NULL; return ret ; } LIST *sortlist( LIST *start ) { LIST *p, *q, *top; int changed = 1; if( (top = malloc(sizeof(LIST))) == NULL) {
4. 4. fprintf( stderr, "Memory Allocation error.n" ); exit(1); } top->next = start; if( start != NULL && start->next != NULL ) { while( changed ) { changed = 0; q = top; p = top->next; while( p->next != NULL ) { if( p->data > p->next->data ) { q->next = ubah_list( p, p->next ); changed = 1; } q = p; if( p->next != NULL ) p = p->next; } } } p = top->next; free( top ); return p; } LIST *ubah_list( LIST *l1, LIST *l2 )
5. 5. { l1->next = l2->next; l2->next = l1; return l2; }