You are on page 1of 28

Algoritma & Pemrograman 2

Elementary Data
Structure & Recursive
Indah Permatasari, M.Kom.
5th meeting
Outline
• Struktur Data
• Stack & Queues
• Linked Lists
• Implementing pointer dan object
• Rekursif
• Pengurutan Bilangan
• Faktorial
• Penjumlahan deret bilangan
• Faktor persekutuan deret bilangan
• Pangkat bilangan

Indah Permatasari, M.Kom. 2


Elementary Data Structure

Indah Permatasari, M.Kom. 3


Data Structures
• One of the key elements in software design is to determine which
data-structures are most appropriate for the problem at hand
• Data-structures determine how information is stored and exchanged,
and have a significant effect on the overall cohesiveness, clarity, and
efficiency of the program
• The C language provides pointers, arrays, and structs as the basic
facilities upon which powerful high-level data-structures may be
created
• Although can construct many complex data structures using pointers,
present only the rudimentary ones: stacks, queues, linked lists, and
rooted trees
Indah Permatasari, M.Kom. 4
Stacks
• The INSERT operation on a stack is often called PUSH
• the DELETE operation, which does not take an element argument, is
often called POP

Figure 10.1
Indah Permatasari, M.Kom. 5
Stacks
• When S:top=0, the stack contains no elements and is empty
• the stack is empty by query operation STACK-EMPTY
• If we attempt to pop an empty stack, we say the stack underflows
• If S:top exceeds n, the stack overflow
• A stack, also known as a last-in-first-out (LIFO) buffer

Indah Permatasari, M.Kom. 6


Stack: Pseudocode

Indah Permatasari, M.Kom. 7


The effects of the modifying operations PUSH
and POP
• A queue implemented using an
array QOE[1...12]. Queue
elements appear only in the
lightly shaded positions.
• (a) The queue has 5 elements, in
locations QOE[7...11].
• (b) The configuration of the queue
after the calls ENQUEUE(Q; 17),
ENQUEUE(Q; 3), and ENQUEUE(Q;
5).
• (c) The configuration of the queue
after the call DEQUEUE(Q) returns
the key-value 15 formerly at the
head of the queue. The new head
has key 6.
Figure 10.2
Indah Permatasari, M.Kom. 8
Queues
• the INSERT operation on a queue ENQUEUE
• the DELETE operation DEQUEUE like the stack operation POP,
DEQUEUE takes no element argument
• The FIFO property of a queue causes it to operate like a line of
customers waiting to pay a cashier
• The queue has a head and a tail
• When an element is enqueued, it takes its place at the tail of the queue, just
as a newly arriving customer takes a place at the end of the line
• The element dequeued is always the one at the head of the queue, like the
customer at the head of the line who has waited the longest

Indah Permatasari, M.Kom. 9


Queues: Pseudocode assumes that n =
Q:length

• A queue, otherwise known as a first-in-


first-out (FIFO) buffer

Indah Permatasari, M.Kom. 10


Linked Lists
• A linked-list is a data structure in which the objects are arranged in a
linear order
• A linked-list is a set of nodes, where each node contains an item of
data and a pointer to the next node in the list
• Linked-lists come in two basic varieties: doubly linked and singly
linked

Indah Permatasari, M.Kom. 11


Doubly Linked-list
• L is an object with an attribute key and two other pointer attributes:
next and prev.

Figure 10.3

Indah Permatasari, M.Kom. 12


Singly Linked-list
• A list may have one of several forms. It may be either singly linked or
doubly linked, it may be sorted or not, and it may be circular or not
• the prev pointer in each element
• If a list is sorted, the linear order of the list corresponds to the linear
order of keys stored in elements of the list; the minimum element is
then the head of the list, and the maximum element is the tail
• If the list is unsorted, the elements can appear in any order. In a
circular list, the prev pointer of the head of the list points to the tail,
and the next pointer of the tail of the list points to the head

Indah Permatasari, M.Kom. 13


Functions
perform node
insertion and
deletion
operations

Indah Permatasari, M.Kom. 14


Implementing pointers and
objects

Indah Permatasari, M.Kom. 15


A multiple-array representation of objects
• represent a collection of objects that
have the same attributes by using an
array for each attribute
• The array key holds the values of the keys
currently in the dynamic set, and the
pointers reside in the arrays next and prev
• For a given array index x, the array entries Figure 10.5
key[x], next[x], and prev[x] represent an
object in the linked list
• Under this interpretation, a pointer x is
simply a common index into the key, next,
and prev arrays

Indah Permatasari, M.Kom. 16


A single-array representation of objects
• The words in a computer memory are typically addressed by integers
from 0 to M-1, where M is a suitably large integer
• In many programming languages, an object occupies a contiguous set
of locations in the computer memory
• A pointer is simply the address of the first memory location of the
object, and we can address other memory locations within the object
by adding an offset to the pointer

Indah Permatasari, M.Kom. 17


Cont.

• An object occupies a contiguous subarray A[j...k]. Each attribute of the object


corresponds to an offset in the range from 0 to k - j , and a pointer to the
object is the index j .
• In Figure 10.6, the offsets corresponding to key, next, and prev are 0, 1, and 2,
respectively. To read the value of i.prev, given a pointer i, we add the value i
of the pointer to the offset 2, thus reading A[i+2].
• The single-array representation is flexible in that it permits objects of
different lengths to be stored in the same array

Indah Permatasari, M.Kom. 18


Recursive

Indah Permatasari, M.Kom. 19


Rekursif
• Proses memanggil dirinya sendiri yang biasanya dilakukan oleh fungsi
atau prosedur
• Akan berjalan terus menerus sampai kondisi berhenti terpenuhi
• Diperlukan blok-blok kode
• Basis → kode yang menjadi titik berhenti dari sebuah proses rekursif
• Rekursif → kode dimana sebuah blok program memanggil diriinya
sendiri

Indah Permatasari, M.Kom. 20


Proses Rekursif (Algoritmik Program C)
procedure tulis(input : n: integer)
counter : integer
counter <- n
if counter > 0 then
output(“proses rekursif dengan nilai counter: “, counter)
counter <- counter-1
tulis(counter)
{end if}
{end procedure}
{algoritma utama}
tulis(10)
{end algoritma utama}

Indah Permatasari, M.Kom. 21


Pengurutan bilangan
#include <stdio.h>
#include <stdlib.h>
void angkaterbalik(int i);
int main(void)
{
int nmr[1];
printf("Masukkan Batas angka yang di inginkan : \n");
scanf("%d",&nmr[0]);
printf("Urutan angka adalah ");
angkaterbalik(nmr[0]);
return(0);
}
void angkaterbalik(int i)
{
if(i>=1)
{
printf(" %d",i);
angkaterbalik(i-1);
}
}

Indah Permatasari, M.Kom. 22


Faktorial
#include <stdio.h>
int faktorial(int n)
{
if((n==0) || (n==1)){
return 1;
} else{
return (n * faktorial(n-1));
}
}
int main()
{
int in;
printf("Masukkan nilai faktorial: \n");
scanf("%d", &in);
printf("rekursif dengan nilai hasil: %d, nilai n adalah %d", faktorial(in), in);
return 0;
}

Indah Permatasari, M.Kom. 23


Penjumlahan deret bilangan
#include <stdio.h>
int sum(int n)
{
if(n==1){
return 1;
} else {
return (n + sum(n-1));
}
}
int main()
{
int in;
printf("Masukkan nilai: \n");
scanf("%d", &in);
int result = sum(in);
printf("Rekursif dengan nilai hasil: %d", result);
return 0;
}

Indah Permatasari, M.Kom. 24


Latihan
Buatlah kode program dari pseudocode berikut!

Indah Permatasari, M.Kom. 25


Faktor persekutuan PSEDOCODE
function cariFPB(a:integer;
deret bilangan b:integer) -> integer
if b = 0 then
• Contoh: ->a
{end if}
• Terdapat dua bilangan yaitu 12 dan 20 else
maka factor persekutuan terbesarnya
temp: integer
adalah 4 dimana 4 merupakan angka
temp <- a mod b
terbesar yang habis dibagi 12 dan 20.
{end else}
{end function}

{program utama{
hasil: integer
hasil <- cariFPB(12, 20)
ouptup(hasil)
{end program utama}
Indah Permatasari, M.Kom. 26
PSEDOCODE
Pangkat bilangan function pangkat(a:integer;
b:integer) -> integer
if b = 0 then
->1
• Sebuah bilangan yang dikalikan {end if}
dengan bilangan itu sendiri sebanyak else
pangkatnya hasil: integer
• Misalnya 2 pangkat 3 maka 2 x 2 x 2. hasil <- a * pangkat(a, y-1)
-> hasil
{end else}
{end function}

{program utama{
hasil: integer
hasil <- pangkat(2,3)
ouptup(hasil)
{end program utama}

Indah Permatasari, M.Kom. 27


Terima kasih.

Indah Permatasari, M.Kom. 28

You might also like