Struktur Dinamis lanjutan - PowerPoint PPT Presentation

1 / 40
About This Presentation
Title:

Struktur Dinamis lanjutan

Description:

Enumerator-enumerator minggu, senin, ..., sabtu merepresentasikan konstanta ... Enumerator. Enumerator dapat diberi nilai secara eksplisit. Contoh: ... – PowerPoint PPT presentation

Number of Views:70
Avg rating:3.0/5.0
Slides: 41
Provided by: csU82
Category:

less

Transcript and Presenter's Notes

Title: Struktur Dinamis lanjutan


1
Struktur Dinamis (lanjutan)
  • The main reference of this presentation is from
    the ch 12 of the book

2
Enum (Enumeration)
  • Dalam ANSI C, user dapat membuat tipe baru yang
    disebut enumeration, dengan keyword enum.
  • Suatu tipe enum merupakan suatu himpunan
    konstanta-konstanta integer yang
    direpresentasikan dengan nama-nama simbolik
    (identifier)
  • Contoh
  • enum hari minggu, senin, selasa, rabu, kamis,
    jumat, sabtu
  • Mendeklarasikan suatu tipe baru enum hari.
  • Enumerator-enumerator minggu, senin, , sabtu
    merepresentasikan konstanta-konstanta integer 0,
    1, .., 6.
  • Kalau tidak dinyatakan secara lain eksplisit,
    enumerator pertama selalu bernilai 0. Secara
    otomatis enumerator berikutnya bernilai integer
    berikutnya.

3
Enum
  • Sekarang enum hari dapat dipakai sebagaimana
    tipe-tipe lainnya, dalam mendeklarasikan
    variabel.
  • Contoh
  • typedef enum hari harihari
  • harihari nh1
  • enum hari nh2
  • nh1 senin
  • nh2 kamis

4
Enumerator
  • Enumerator dapat diberi nilai secara eksplisit.
  • Contoh
  • enum month Jan1, Feb, Mar, Apr, May, Jun, Jul,
    Aug, Sep, Oct, Nov, Dec
  • enum buah durian 6, jeruk, manggis 2,
    duku
  • Disini jeruk bernilai 7 dan duku bernilai 3.
  • enum status single1, lajang 1, married 2,
    menikah 2
  • Tipe enum dipakai untuk memperjelas maksud program

5
Contoh Program menggunakan enum
  • include ltstdio.hgt
  • enum month Jan1, Feb, Mar, Apr, May, Jun, Jul,
  • Aug, Sep, Oct, Nov, Des
  • typedef enum month bulan
  • int main()
  • bulan b
  • char namabulan "", "Januari", "Februari",
    "Maret", "April", "Mei",
  • "Juni", "Juli", "Agustus",
    "September", "Oktober",
  • "November", "Desember"
  • for (bJan b lt Des b)
  • printf("2d11s\n", b, namabulanb)
  • return 0

6
Pointer ke Fungsi
  • Nama dari suatu fungsi adalah pointer ke fungsi
    itu.
  • Seperti pointer ke variabel, pointer ke fungsi
    dapat diperlakukan sebagai
  • Argumen utk fungsi
  • Unsur dalam array
  • Nilai yang dikembalikan fungsi
  • Dsb
  • Contoh Deklarasi
  • double (pf) (double)
  • Menyatakan bahwa pf adalah suatu pointer ke suatu
    fungsi yang memerlukan satu argumen bertipe
    double dan mengembalikan nilai bertipe double.
  • Perhatikan bahwa deklarasi diatas berbeda dgn
  • double g(double)
  • Yang menyatakan bahwa g adalah suatu fungsi yang
    memerlukan satu argumen bertipe double dan
    mengembalikan nilai bertipe double (pointer ke
    double)

7
Contoh Penggunaan pointer ke fungsi
  • include ltstdio.hgt
  • define N 5
  • int min(int , int)
  • int jumlah(int , int)
  • int main()
  • int aN 5, 7, -5, 3, 6
  • int (pf)(int , int)
  • pf jumlah
  • printf("d d \n", pf(a,N), jumlah(a,N))
  • pf min
  • printf("d d \n", pf(a,N), min(a,N))

int min(int a, int n) int i, m a0 for
(in-1 igt0 i--) if(ai lt m) m
ai return m int jumlah(int a, int
n) int i, ma0 for (i1 iltn i) m
ai return m
8
  • include ltstdio.hgt
  • define size 10
  • / prototypes /
  • void bubble( int work, const int size, int
    (compa)( int a, int b ) )
  • int ascending( int a, int b )
  • int descending( int a, int b )
  • void swap( int element1ptr, int element2ptr )
  • int main()
  • int order / 1 for ascending order or 2 for
    descending order /
  • int counter / counter /
  • / initialize array a /
  • int a size 2, 6, 4, 8, 10, 12, 89, 68,
    45, 37
  • printf( "enter 1 to sort in ascending
    order,\n
  • enter 2 to sort in descending order " )

if ( order 1 ) / pass function ascending
/ bubble( a, size, ascending )
printf( "\ndata items in ascending order\n" )
else / pass function descending /
bubble( a, size, descending ) printf(
"\ndata items in descending order\n" ) /
end else / / output sorted array / for
( counter 0 counter lt size counter )
printf( "5d", a counter ) / end
for / printf( "\n" ) return 0 /
indicates successful termination / / end
main /
9
void swap( int element1ptr, int element2ptr
) int hold / temporary holding variable
/ hold element1ptr element1ptr
element2ptr element2ptr hold int
ascending( int a, int b ) return b lt a
int descending( int a, int b ) return b
gt a
  • void bubble( int work, const int size,
  • int (compa)( int a, int b ) )
  • int pass / pass counter /
  • int count / comparison counter /
  • / loop to control passes /
  • for ( pass 1 pass lt size pass )
  • / loop to control number of comparisons
    per pass /
  • for ( count 0 count lt size - 1 count
    )
  • if ( (compa)( work count , work count 1
    ) )
  • swap( work count , work count
    1 )

10
Linked Lists
  • Linked list
  • Linear collection of self-referential class
    objects, called nodes
  • Connected by pointer links
  • Accessed via a pointer to the first node of the
    list
  • Subsequent nodes are accessed via the
    link-pointer member of the current node
  • Link pointer in the last node is set to NULL to
    mark the lists end
  • Use a linked list instead of an array when
  • You have an unpredictable number of data elements
  • Your list needs to be sorted quickly

11
Linked Lists
12
List Node Definition
  • typedef struct simpul
  • char data
  • struct simpul next
  • SIMPUL
  • typedef SIMPUL PtrKeSIMPUL

13
Inserting a Node
14
  • void sisip(PtrKeSIMPUL p, char info)
  • PtrKeSIMPUL baru, sebelum, kini
  • baru (PtrKeSIMPUL)malloc(sizeof(SIMPUL))
  • if (baru)
  • baru-gtdata info
  • baru-gtnext NULL
  • sebelum NULL
  • kini p
  • /cari posisi /
  • while (kini ! NULL info gt kini-gtdata)
  • sebelum kini
  • kini kini-gtnext

15
Deleting a Node
16
  • char hapus(PtrKeSIMPUL p, char info)
  • PtrKeSIMPUL sebelum, kini, sementara
  • sementara (PtrKeSIMPUL)malloc(sizeof(SIMPUL))
  • if (info (p)-gtdata)
  • sementara p
  • p (p)-gtnext
  • free(sementara)
  • return info
  • sebelum p
  • kini (p)-gtnext
  • while (kini ! NULL kini-gtdata ! info)
  • sebelum kini

17
Fungsi untuk mencetak Linked-list
  • void cetaklist(PtrKeSIMPUL P)
  • if (PNULL)
  • printf("NULL\n")
  • else
  • while(P)
  • printf("c --gt ", P-gtdata)
  • P P-gtnext
  • printf("NULL\n\n")

Untuk melihat versi rekursif, klik tombol diatas
18
Fungsi untuk mencetak Linked-list
Versi Rekursif
  • void cetaklist(PtrKeSIMPUL P)
  • if (PNULL)
  • printf("NULL\n\n")
  • else
  • printf("c --gt ", P-gtdata)
  • cetaklist(P-gtnext)

Kembali
19
Stacks
  • Stack
  • New nodes can be added and removed only at the
    top
  • Similar to a pile of dishes
  • Last-in, first-out (LIFO)
  • Bottom of stack indicated by a link member to
    NULL
  • Constrained version of a linked list
  • push
  • Adds a new node to the top of the stack
  • pop
  • Removes a node from the top
  • Stores the popped value
  • Returns true if pop was successful

20
Stacks
21
Stack Node Definition
  • / self-referential structure /
  • struct stackNode
  • int data / define data as
    an int /
  • struct stackNode nextPtr / stackNode
    pointer /
  • / end structure stackNode /
  • typedef struct stackNode StackNode / synonym
    for struct stackNode /
  • typedef StackNode StackNodePtr / synonym for
    StackNode /

22
Push Operation
23
  • / Insert a node at the stack top /
  • void push( StackNodePtr topPtr, int info )
  • StackNodePtr newPtr / pointer to new node /
  • newPtr malloc( sizeof( StackNode ) )
  • / insert the node at stack top /
  • if ( newPtr ! NULL )
  • newPtr-gtdata info
  • newPtr-gtnextPtr topPtr
  • topPtr newPtr
  • / end if /
  • else / no space available /
  • printf( "d not inserted. No memory
    available.\n", info )
  • / end else /
  • / end function push /

24
Pop Operation
25
  • / Remove a node from the stack top /
  • int pop( StackNodePtr topPtr )
  • StackNodePtr tempPtr / temporary node
    pointer /
  • int popValue / node value /
  • tempPtr topPtr
  • popValue ( topPtr )-gtdata
  • topPtr ( topPtr )-gtnextPtr
  • free( tempPtr )
  • return popValue
  • / end function pop /

26
Queues
  • Queue
  • Similar to a supermarket checkout line
  • First-in, first-out (FIFO)
  • Nodes are removed only from the head
  • Nodes are inserted only at the tail
  • Insert and remove operations
  • Enqueue (insert) and dequeue (remove)

27
Queues
28
Queue Node Definition
  • / self-referential structure /
  • struct queueNode
  • char data / define data as a
    char /
  • struct queueNode nextPtr / queueNode
    pointer /
  • / end structure queueNode /
  • typedef struct queueNode QueueNode
  • typedef QueueNode QueueNodePtr

29
Enqueue Operation
30
  • / insert a node a queue tail /
  • void enqueue( QueueNodePtr headPtr, QueueNodePtr
    tailPtr,
  • char value )
  • QueueNodePtr newPtr / pointer to new node /
  • newPtr malloc( sizeof( QueueNode ) )
  • if ( newPtr ! NULL ) / is space available
    /
  • newPtr-gtdata value
  • newPtr-gtnextPtr NULL
  • / if empty, insert node at head /
  • if ( isEmpty( headPtr ) )
  • headPtr newPtr
  • / end if /
  • else
  • ( tailPtr )-gtnextPtr newPtr
  • / end else /

31
Dequeue Operation
32
  • / remove node from queue head /
  • char dequeue( QueueNodePtr headPtr, QueueNodePtr
    tailPtr )
  • char value / node value /
  • QueueNodePtr tempPtr / temporary node
    pointer /
  • value ( headPtr )-gtdata
  • tempPtr headPtr
  • headPtr ( headPtr )-gtnextPtr
  • / if queue is empty /
  • if ( headPtr NULL )
  • tailPtr NULL
  • / end if /
  • free( tempPtr )
  • return value

33
Trees
  • Tree nodes contain two or more links
  • All other data structures we have discussed only
    contain one
  • Binary trees
  • All nodes contain two links
  • None, one, or both of which may be NULL
  • The root node is the first node in a tree.
  • Each link in the root node refers to a child
  • A node with no children is called a leaf node

34
Trees
35
Tree traversals
  • Inorder traversal prints the node values in
    ascending order
  • 1. Traverse the left subtree with an inorder
    traversal
  • 2. Process the value in the node (i.e., print the
    node value)
  • 3. Traverse the right subtree with an inorder
    traversal
  • Preorder traversal
  • 1. Process the value in the node
  • 2. Traverse the left subtree with a preorder
    traversal
  • 3. Traverse the right subtree with a preorder
    traversal
  • Postorder traversal
  • 1. Traverse the left subtree with a postorder
    traversal
  • 2. Traverse the right subtree with a postorder
    traversal
  • 3. Process the value in the node

36
Inorder Tree Traversal
  • / begin inorder traversal of tree /
  • void inOrder( TreeNodePtr treePtr )
  • / if tree is not empty then traverse /
  • if ( treePtr ! NULL )
  • inOrder( treePtr-gtleftPtr )
  • printf( "3d", treePtr-gtdata )
  • inOrder( treePtr-gtrightPtr )
  • / end if /
  • / end function inOrder /

37
Preorder Tree Traversal
  • / begin preorder traversal of tree /
  • void preOrder( TreeNodePtr treePtr )
  • / if tree is not empty then traverse /
  • if ( treePtr ! NULL )
  • printf( "3d", treePtr-gtdata )
  • preOrder( treePtr-gtleftPtr )
  • preOrder( treePtr-gtrightPtr )
  • / end if /
  • / end function preOrder /

38
Postorder Tree Traversal
  • / begin postorder traversal of tree /
  • void postOrder( TreeNodePtr treePtr )
  • / if tree is not empty then traverse /
  • if ( treePtr ! NULL )
  • postOrder( treePtr-gtleftPtr )
  • postOrder( treePtr-gtrightPtr )
  • printf( "3d", treePtr-gtdata )
  • / end if /
  • / end function postOrder /

39
Binary Search Tree
  • Values in left subtree less than parent
  • Values in right subtree greater than parent
  • Facilitates duplicate elimination
  • Fast searches - for a balanced tree, maximum of
    log n comparisons

2
40
Trees
Write a Comment
User Comments (0)
About PowerShow.com