struktur data 07-08 v2

Download Struktur Data 07-08 v2

Post on 03-Dec-2014

123 views

Category:

Documents

5 download

Embed Size (px)

TRANSCRIPT

STRUKTUR DATA:: Fetty Tri Anggraeny ::

Tentang Saya

Fetty Tri Anggraeny, S.Kom HP : (031)83031803 Email :fetty@upnjatim.ac.id atau fetty_ta@yahoo.com

MK :

Bahasa Pemrograman, Struktur Data, Sistem Digital, Grafika Komputer Pengolahan Citra Digital Sistem Pakar atau Sistem Berbasis Pengetahuan

KONTRAK KULIAH

Toleransi keterlambatan 30 menit terhitung mulai pukul 10.00, berlaku bagi dosen dan mahasiswa Pakaian SOPAN, berkerah, bebas rapi dan bersepatu. NILAI : Tugas dan quiz Final Project (Tim)

: 20 % : 20 % UTS : 30 % UAS : 30 % POIN PLUS :keaftifan dalam kelas maupun tugas

MATERI KULIAHFungsi Rekursi Review : Array, Struct, Pointer Sorting Searching Linked List Stack Queue Graph dan Tree

Satuan Acara Perkuliahan1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11.

12.13. 14. 15. 16.

Pengantar + Fungsi Rekursi 1 Fungsi Rekursi 2 Review Array dan Struct Sorting 1 : Bubble dan Insertion Sorting 2 : Selection Searching Review Pointer UTS Linked List 1 : Single Linked List Linked List 2 : Linked List Lanjut Stack 1 Operasi Dasar Stack 2 Contoh Penggunaan Queue Graph Tree UAS

Buku ReferensiData Structures Using C and C++ 2nd edition, Yedidyah Langsam, dkk Text book lain Internet www.google.com

What is programming?

PROGRAM = ALGORITMA + STRUKTUR DATA ALGORITMA : SEQUENTIAL

BRANCHING LOOPING/ REKURSI

STRUKTUR DATA : ARRAY

STRUCT POINTER ADT : LINKED

LIST, STACK, QUEUE, GRAPH,

TREE, DLL

MINGGU PERTAMA

Fungsi RekursifDefinisi fungsi rekursif. Contoh 1 : Faktorial Contoh 2 : Perkalian Contoh 3 : Fibonacci Contoh 4 : Tower of Hanoi

Fungsi Rekursif

Fungsi biasa dipanggil oleh fungsi lain. Sedangkan fungsi rekursif dipanggil oleh dirinya sendiri.B C D A A A A

A

Fungsi RekursifSetara dengan proses looping/ iterasi faktorial, perkalian Kadang rekursif lebih baik dari iterasi tower of hanoi Terkadang sebaliknya fibonacci Komponen :

out if else + return() Recursive call dengan value baru Way

Simple Example#include int main(void) { printf("Never ends\n"); main(); return 0; }

FaktorialALGORITMA n! = 1 if n == 0 n! = n * ( n 1 )! if n > 0 4! = 4 x 3! 3! = 3 x 2! 2! = 2 x 1! 1! = 1 x 0! 0! = 1 SOURCE CODE int factorial ( int n ) { int x, y; if ( n == 0 ) return ( 1 ); x = n 1; y = factorial ( x ); return ( n * y ); }

Latihan tracing rekursi

PerkalianALGORITMAa * b = a if b == 1 a * b = a * ( b 1 ) + a if b > 1 6x3=(6x2)+6 =(6x1)+6+6 =6+6+6 = 18

SOURCE CODEint mult ( int a, int b ) { int c, d, sum; if ( b == 1 ) return ( a ); c = b 1; d = mult ( a, c ); sum = d + a; return ( sum ); }

Latihan tracing rekursi

Minggu Kedua

FibonacciThe Fibonacci series fn0 is a famous series defined by: f0 : 0, f1 : 1, fn2 : fn1 + fn2 f2 = f0 + f1 = 0 + 1 = 1 f3 = f1 + f2 = 1 + 1 = 2 f4 = f2 + f3 = 1 + 2 = 3 f5 = f3 + f4 = 2 + 3 = 5 .

Leonardo Fibonacci 1170-1250

FibonacciALGORITMA fibo( n ) = n if n == 0 or n == 1 fibo( n ) = fibo(n 2) + fibo( n 1 ) if n >= 2 fibo( 4 ) = fibo( 3 ) + fibo( 2 ) = fibo( 2 ) + fibo ( 1 ) + fibo( 2 ) = fibo(1) + fibo(0) + fibo(1) + fibo(2) = 1 + fibo(0) + fibo(1) + fibo(2) = 1 + 0 + fibo(1) + fibo(2) = 1 + fibo(1) + fibo(2) = 1 + 1 + fibo(2) = 2 + fibo(2) = 2 + fibo(1) + fibo(0) = 2 + 1 + fibo(0) =2+1+0 =2+1 =3 SOURCE CODE int fibo ( int n ) { int x, y; if ( n X[j+1] ) { hold = X[j]; X[j] = X[j+1]; X[j+1] = hold; } }

Insertion Sortvoid insertion ( int X [ ], int n ) { int i, k, y; for ( k = 1; k < n; k++) { y = X[k]; for ( i = k-1; i >= 0 && y < X[i]; i--) X[i+1] = X[i]; X[i+1] = y; } }

Minggu Kelima

Kuis kecilvoid urutkan ( int X [ ], int n ) { int temp, i, j; i=0; while(i 0; i--) { large = x[0]; index = 0; for ( j = 1; j large ) { large = X[j]; index = j; } X[index] = X[i]; X[i] = large; }

Tugas :

Algoritma dan contoh sortingnya : Quick

Sort Merge Sort Shell Sort

Minggu Keenam

SearchingTujuan : memahami proses tracing algoritma pencarian. Jenis algoritma searching : Sequential

search Indexed sequential search Binary search

Sequential Searchint SequentialSearch(int key, int data[], int n) { int i; for (i = 0; i < n; i++) if (key == data[i]) return (i); return (-1); }

Indexed Sequential Searchint IndexSequential( int key, int k[], int n, int kindex[], int pindex[], int indxsize ) { int i, j, lowlim, hilim; for ( i = 0; i < indxsize && kindex[ i ] jari_jari, pdata->luas); }

Minggu KedelapanUjian Tengah Semester

Minggu Kesembilan

(Single) Linked ListDefinisi linked list Operasi dasar Contoh program

Ilustrasi Singly Linked ListData 1 Data 1 Next node Data 2 Data 2 Data 3 Data 3 Next node

Next nodeData 4

Data 4NO NEXT

In this linked list example, each node has two pieces of data. Each node also has a pointer to the next node. So, we need two things to form a linked list: a way to combine various datatypes and variables together into one datatype and a way to point to the next one of these combination datatypes. Sohow can we accomplish this?

The first goal, combining various datatypes and variables into one datatype, is easily handled with a structure. The second goal, being able to point to the next structure is easily handled using pointers. So, we have all of the components we need in order to construct a linked list.

Linked List

Struktur data yang terdiri dari beberapa simpul (node) dimana setiap simpulnya saling terhubung (linked). Simpul berupa struct, sedangkan link berupa komponen simpul yang bertipe pointer ke simpul. Bersifat dinamis. Memakai perintah malloc() dan free(). Umumnya memiliki pointer head untuk menunjukkan alamat simpul terdepan dan pointer tail untuk menunjukkan simpul terakhir. Operasi penambahan/ penghapusan sebuah simpul akan meng-update nilai pointer link-nya. Pointer link simpul terakhir diberi nilai NULL (kecuali untuk circular linked list).

Linked List Sederhana

Deklarasi struktur data :typedef struct simpul { char nama[20]; float nilai; struct simpul *next_simpul; } simpulku;

void main()simpulku *simpul1, *simpul2, *simpul3;

//alokasi memori simpul1 = (simpulku *)malloc(sizeof(simpulku)); simpul2 = (simpulku *)malloc(sizeof(simpulku)); simpul3 = (simpulku *)malloc(sizeof(simpulku)); //isi data strcpy(simpul1->nama, Amin); strcpy(simpul2->nama, Budi); strcpy(simpul3->nama, Citra); simpul1->nilai=90; simpul2->nilai=20; simpul3->nilai=100; //sambungkan link-nya simpul1->next_simpul = simpul2; simpul2->next_simpul = simpul3; simpul3->next_simpul = NULL;

Alamat = 1000Amin

Alamat = 1080Budi

Alamat = 2370Citra

901080

202370

100NULL

simpul11000

simpul21080

simpul32370

Alamat = 1000Amin

Alamat = 1080Budi

Alamat = 2370Citra

901080

202370

100NULL Pointer Tail

simpul11000Pointer Head

Ilustrasi Inserting a NodeT update

Alamat = 1000Amin

Alamat = 1080Budi

Alamat = 2370Citra

901080

208460

100NULL

simpul11000New

Alamat = 8460 Dewi 982370

New->next = T->next T->next = New

Ilustrasi Deleting a NodeP update T

Alamat = 1000Amin

Alamat = 1080Budi

Alamat = 2370Citra

902370

202370 free()

100NULL

simpul11000

P->next = T->next free(T)

Contoh Program Linked List#include "stdio.h" #include "stdlib.h" #include "conio.h"struct node{ int info; struct node *next; }; typedef struct node *simpul;

void main() { simpul baru, head=NULL, tail=NULL, temp; int pilih; do { printf("MENU\n"); printf("1. Insert\n"); printf("2. View\n"); printf("3. Search\n"); printf("4. Delete\n"); printf("PILIH: "); scanf("%d", &pilih); switch(pilih) { } }while (pilih!=5); }

Insert Nodecase 1: int data; printf("Data Masuk: "); scanf("%i", &data); baru = (simpul) malloc(sizeof (struct node)); baru->info = data; baru->next = NULL; if (head == NULL) //list masih kosong { //simpul yg pertama kali head = baru; tail = baru; } else { tail->next = baru; tail = baru; } break;

Show All Nodecase 2: temp = head; while(temp!=NULL) { printf("%i ", temp->info); temp = temp->next; } printf("\n"); break;

Search a Nodecase 3: int cari; printf("Cari Angka: "); scanf("%i", &cari); temp = head; while((temp!=NULL)&&(temp->info!=cari)) { temp = temp->next; } if(temp != NULL && temp->info == cari) printf("Data Ditemukan"); else //if(temp == NULL) printf("Data Tidak Ditemukan"); printf("\n"); break;

Delete a Nodecase 4: int hapus; char jwb; simpul prev = NULL; printf("Hapus Angka: "); scanf("%i", &hapus);//temukan dulu node yang akan dihapus temp = head; while((temp!=NULL)&&(temp->info!=hapus)) { prev = temp; temp = temp->next; } //bersambung

if(temp != NULL && temp->info == hapus) //ditemukan { printf("Yakin Dihapus? (y/t)"); flushall(); jwb=getch(); if(jwb == 'y') { if(temp->next != NULL && temp != head) prev->next = temp->next; else if (temp->next == NULL) prev->next = NULL; else if (temp == head && head->next == NULL) head = NULL; else if (temp == head && head->next != NULL) head = head->next; free(temp); //hapus node-nya } else printf("Batal Dihapus"); } else printf("Data Tidak Ditemukan"); printf("\n"); break;

Capture Program

Minggu Kesepuluh

Macam-macam ListSingly linked list Doubly linked list Singly circular linked list Doubly circular linked list

Singly and Doubly

A