Algoritma Dan Pseudocode
Algoritma Dan Pseudocode
a. Inisialisasi
procedure init(data)
setlength(queue[data])
return data
end procedure
b. Insert
procedure insert(data)
if queue is full
return overflow
endif
rear ← rear + 1
queue[rear] ← data
return true
end procedure
c. Delete
procedure delete
if queue is empty
return underflow
end if
data = queue[front]
front ← front + 1
return true
end procedure
2. Delete Queue
int removeData() {
int data = intArray[front++];
if(front == MAX) {
front = 0;
}
itemCount--;
return data;
}
3. Pseudocode Stack
a. PUSH 1
procedure push: stack, data
if stack is full
return null
endif
top ← top + 1
stack[top] ← data
end procedure
b. PUSH 2
procedure push: stack, data
if stack is full
return null
endif
top ← top + 2
stack[top] ← data
end procedure
c. POP 1
Procedure pop : stack
if stack is empty
return null
endif
data ← stack[top]
top ← top - 1
return data
end procedure
d. POP 2
Procedure pop : stack
if stack is empty
return null
endif
data ← stack[top]
top ← top - 2
return data
end procedure
BONUS
a. Linked List
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
struct node {
int data;
int key;
struct node *next;
};
printf(" ]");
}
link->key = key;
link->data = data;
return tempLink;
}
bool kosong() {
return head == NULL;
}
int length() {
int length = 0;
struct node *current;
return length;
}
while(current->key != key) {
if(current->next == NULL) {
return NULL;
} else {
current = current->next;
}
}
return current;
}
if(current == head) {
head = head->next;
} else {
previous->next = current->next;
}
return current;
}
void sort() {
int i, j, k, tempKey, tempData;
struct node *current;
struct node *next;
*head_ref = prev;
}
void main() {
tambah(1,10);
tambah(2,20);
tambah(3,30);
tambah(4,1);
tambah(5,40);
tambah(6,56);
printf("\nList : ");
printList();
tambah(1,10);
tambah(2,20);
tambah(3,30);
tambah(4,1);
tambah(5,40);
tambah(6,56);
if(foundLink != NULL) {
printf("Hasil Cari: ");
printf("(%d,%d) ",foundLink->key,foundLink->data);
printf("\n");
} else {
printf("Tidak DiTemukan.");
}
hapus(4);
printf("List: ");
printList();
printf("\n");
foundLink = find(4);
if(foundLink != NULL) {
printf("Item Ditemukan: ");
printf("(%d,%d) ",foundLink->key,foundLink->data);
printf("\n");
} else {
printf("Item Tidak Ditemukan.");
}
printf("\n");
sort();
ganti(&head);
printf("\nList after reversing the data: ");
printList();
}
b. Sorting
#include <stdio.h>
#include <stdbool.h>
#define MAX 10
int list[MAX] = {1,8,4,6,0,3,5,2,7,9};
void display() {
int i;
printf("[");
printf("]\n");
}
void bubbleSort() {
int temp;
int i,j;
swapped = true;
printf(" => swapped [%d, %d]\n",list[j],list[j+1]);
} else {
printf(" => not swapped\n");
}
if(!swapped) {
break;
}
void main() {
printf("Array Masukan: ");
display();
printf("\n");
bubbleSort();
printf("\nArray Hasil: ");
display();
}
c. Searching
#include <stdio.h>
#define MAX 20
int intArray[MAX] = {1,2,3,4,6,7,9,11,12,14,15,16,17,19,33,34,43,45,55,66};
printf("=\n");
}
int comparisons = 0;
int index = -1;
int i;
for(i = 0;i<MAX;i++) {
comparisons++;
if(data == intArray[i]) {
index = i;
break;
}
}
void display() {
int i;
printf("[");
for(i = 0;i<MAX;i++) {
printf("%d ",intArray[i]);
}
printf("]\n");
}
void main() {
printf("Array Masukan: ");
display();
printline(50);
d. Stack
#include <stdio.h>
int MAXSIZE = 8;
int stack[8];
int top = -1;
int isempty() {
if(top == -1)
return 1;
else
return 0;
}
int isfull() {
if(top == MAXSIZE)
return 1;
else
return 0;
}
int peek() {
return stack[top];
}
int pop() {
int data;
if(!isempty()) {
data = stack[top];
top = top - 1;
return data;
} else {
printf("Tidak dapat menampilkan data, Stack kosong.\n");
}
}
if(!isfull()) {
top = top + 1;
stack[top] = data;
} else {
printf("tidak dapat menambahkan data. Stack penuh.\n");
}
}
int main() {
push(3);
push(5);
push(9);
push(1);
push(12);
push(15);
return 0;
}
e. Queue
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 6
int intArray[MAX];
int front = 0;
int rear = -1;
int itemCount = 0;
int peek() {
return intArray[front];
}
bool isEmpty() {
return itemCount == 0;
}
bool isFull() {
return itemCount == MAX;
}
int size() {
return itemCount;
}
if(!isFull()) {
if(rear == MAX-1) {
rear = -1;
}
intArray[++rear] = data;
itemCount++;
}
}
int removeData() {
int data = intArray[front++];
if(front == MAX) {
front = 0;
}
itemCount--;
return data;
}
int main() {
/* insert 5 items */
insert(3);
insert(5);
insert(9);
insert(1);
insert(12);
insert(15);
if(isFull()) {
printf("Queue is full!\n");
}
printf("----------------------\n");
printf("index : 5 4 3 2 1 0\n");
printf("----------------------\n");
printf("Queue: ");
while(!isEmpty()) {
int n = removeData();
printf("%d ",n);
}
}
f. Graph (DFS)
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 5
struct Vertex {
char label;
bool visited;
};
int stack[MAX];
int top = -1;
struct Vertex* lstVertices[MAX];
int adjMatrix[MAX][MAX];
int vertexCount = 0;
int pop() {
return stack[top--];
}
int peek() {
return stack[top];
}
bool isStackEmpty() {
return top == -1;
}
return -1;
}
void depthFirstSearch() {
int i;
lstVertices[0]->visited = true;
displayVertex(0);
push(0);
while(!isStackEmpty()) {
int unvisitedVertex = getAdjUnvisitedVertex(peek());
if(unvisitedVertex == -1) {
pop();
} else {
lstVertices[unvisitedVertex]->visited = true;
displayVertex(unvisitedVertex);
push(unvisitedVertex);
}
}
for(i = 0;i < vertexCount;i++) {
lstVertices[i]->visited = false;
}
}
int main() {
int i, j;
addVertex('S'); // 0
addVertex('A'); // 1
addVertex('B'); // 2
addVertex('C'); // 3
addVertex('D'); // 4
addEdge(0, 1); // S - A
addEdge(0, 2); // S - B
addEdge(0, 3); // S - C
addEdge(1, 4); // A - D
addEdge(2, 4); // B - D
addEdge(3, 4); // C - D
return 0;
}