0% found this document useful (0 votes)
173 views19 pages

Algoritma Dan Pseudocode

The document contains pseudocode and code examples for common data structures and algorithms including queues, stacks, linked lists, sorting, searching, and graphs. Specifically: 1) It provides pseudocode for queue, stack, and linked list operations like insert, delete, push, pop. 2) Code examples demonstrate sorting algorithms like bubble sort, searching an array, and stack/queue implementations using arrays. 3) Additional examples include a linked list implementation with functions for adding, removing, searching nodes, reversing the list, and sorting data. 4) The document concludes with pseudocode for depth-first search (DFS) on a graph.

Uploaded by

116 Programming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
173 views19 pages

Algoritma Dan Pseudocode

The document contains pseudocode and code examples for common data structures and algorithms including queues, stacks, linked lists, sorting, searching, and graphs. Specifically: 1) It provides pseudocode for queue, stack, and linked list operations like insert, delete, push, pop. 2) Code examples demonstrate sorting algorithms like bubble sort, searching an array, and stack/queue implementations using arrays. 3) Additional examples include a linked list implementation with functions for adding, removing, searching nodes, reversing the list, and sorting data. 4) The document concludes with pseudocode for depth-first search (DFS) on a graph.

Uploaded by

116 Programming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

1. Algoritma/Pseudocode dasar antrian.

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;
};

struct node *head = NULL;


struct node *current = NULL;

//display the list


void printList() {
struct node *ptr = head;
printf("\n[ ");

//start from the beginning


while(ptr != NULL) {
printf("(%d,%d) ",ptr->key,ptr->data);
ptr = ptr->next;
}

printf(" ]");
}

void Tambah(int key, int data) {


//create a link
struct node *link = (struct node*) malloc(sizeof(struct node));

link->key = key;
link->data = data;

//point it to old first node


link->next = head;

//point first to new first node


head = link;
}

struct node* hapus() {

struct node *tempLink = head;


head = head->next;

return tempLink;
}

bool kosong() {
return head == NULL;
}

int length() {
int length = 0;
struct node *current;

for(current = head; current != NULL; current = current->next) {


length++;
}

return length;
}

//find a link with given key


struct node* cari(int key) {
struct node* current = head;
if(head == NULL) {
return NULL;
}

while(current->key != key) {
if(current->next == NULL) {
return NULL;
} else {
current = current->next;
}
}
return current;
}

struct node* hapus1(int key) {


struct node* current = head;
struct node* previous = NULL;
if(head == NULL) {
return NULL;
}
while(current->key != key) {
if(current->next == NULL) {
return NULL;
} else {
previous = current;
current = current->next;
}
}

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;

int size = length();


k = size ;

for ( i = 0 ; i < size - 1 ; i++, k-- ) {


current = head;
next = head->next;

for ( j = 1 ; j < k ; j++ ) {

if ( current->data > next->data ) {


tempData = current->data;
current->data = next->data;
next->data = tempData;
tempKey = current->key;
current->key = next->key;
next->key = tempKey;
}
current = current->next;
next = next->next;
}
}
}

void ganti(struct node** head_ref) {


struct node* prev = NULL;
struct node* current = *head_ref;
struct node* next;

while (current != NULL) {


next = current->next;
current->next = prev;
prev = current;
current = 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("Original List: ");


printList();
while(!kosong()) {
struct node *temp = hapus();
printf("\nNilai Terhapus:");
printf("(%d,%d) ",temp->key,temp->data);
}

printf("\nList : ");
printList();
tambah(1,10);
tambah(2,20);
tambah(3,30);
tambah(4,1);
tambah(5,40);
tambah(6,56);

printf("\nSisa List: ");


printList();
printf("\n");

struct node *foundLink = find(4);

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();

printf("Hasil Pengurutan: ");


printList();

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("[");

// navigate through all items


for(i = 0; i < MAX; i++) {
printf("%d ",list[i]);
}

printf("]\n");
}

void bubbleSort() {
int temp;
int i,j;

bool swapped = false;

for(i = 0; i < MAX-1; i++) {


swapped = false;

for(j = 0; j < MAX-1-i; j++) {


printf(" Items compared: [ %d, %d ] ", list[j],list[j+1]);

if(list[j] > list[j+1]) {


temp = list[j];
list[j] = list[j+1];
list[j+1] = temp;

swapped = true;
printf(" => swapped [%d, %d]\n",list[j],list[j+1]);
} else {
printf(" => not swapped\n");
}

if(!swapped) {
break;
}

printf("Iterasi %d#: ",(i+1));


display();
}

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};

void printline(int count) {


int i;

for(i = 0;i <count-1;i++) {


printf("=");
}

printf("=\n");
}

int find(int data) {

int comparisons = 0;
int index = -1;
int i;

for(i = 0;i<MAX;i++) {
comparisons++;

if(data == intArray[i]) {
index = i;
break;
}
}

printf("Total Perbandingan: %d", comparisons);


return index;
}

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);

int location = find(55);


if(location != -1)
printf("\nElement Ditemukan Pada Indeks : %d" ,(location+1));
else
printf("Element Tidak Ditemukan.");
}

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");
}
}

int push(int data) {

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);

printf("Element at top of the stack: %d\n" ,peek());


printf("Elements: \n");

// print stack data


while(!isempty()) {
int data = pop();
printf("%d\n",data);
}

printf("Stack Penuh: %s\n" , isfull()?"true":"false");


printf("Stack Kosong: %s\n" , isempty()?"true":"false");

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;
}

void insert(int data) {

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");
}

// remove one item


int num = removeData();

printf("Element removed: %d\n",num);


insert(16);
insert(17);
insert(18);
printf("Element at front: %d\n",peek());

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;

void push(int item) {


stack[++top] = item;
}

int pop() {
return stack[top--];
}

int peek() {
return stack[top];
}

bool isStackEmpty() {
return top == -1;
}

void addVertex(char label) {


struct Vertex* vertex = (struct Vertex*) malloc(sizeof(struct Vertex));
vertex->label = label;
vertex->visited = false;
lstVertices[vertexCount++] = vertex;
}
void addEdge(int start,int end) {
adjMatrix[start][end] = 1;
adjMatrix[end][start] = 1;
}
void displayVertex(int vertexIndex) {
printf("%c ",lstVertices[vertexIndex]->label);
}
int getAdjUnvisitedVertex(int vertexIndex) {
int i;

for(i = 0; i < vertexCount; i++) {


if(adjMatrix[vertexIndex][i] == 1 && lstVertices[i]->visited == false)
{
return i;
}
}

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;

for(i = 0; i < MAX; i++) // set adjacency {


for(j = 0; j < MAX; j++) // matrix to 0
adjMatrix[i][j] = 0;
}

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

printf("Depth First Search: ")


depthFirstSearch();

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy