Last active
May 30, 2018 18:06
-
-
Save isfaaghyth/2f44229b2ae76f6a29aaad06f533553c to your computer and use it in GitHub Desktop.
Data Structure and Algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
using namespace std; | |
struct Node { | |
int data; | |
Node *next; | |
Node *prev; | |
}; | |
Node *node, *head, *tail; | |
void bikinNode(int dataAngka) { | |
node = new Node; | |
node->data = dataAngka; | |
node->prev = NULL; | |
node->next = NULL; | |
} | |
void tambahkanData(int n) { | |
Node *temp = node; | |
bikinNode(n); | |
if (head == NULL) { | |
head = node; | |
tail = node; | |
} else { | |
tail->next = node; | |
tail = node; | |
tail->prev = temp; | |
} | |
cout<<n<<endl; | |
} | |
int main() { | |
int nilai; | |
cout<<"masukkan data:"; | |
cin>>nilai; | |
tambahkanData(nilai); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
using namespace std; | |
struct Node { | |
int data; | |
Node *memori; | |
}; | |
Node *node, *head, *tail; | |
void bikinNode(int dataAngka) { | |
node = new Node; | |
node->data = dataAngka; | |
node->memori = NULL; | |
} | |
void tambahkanData(int n) { | |
bikinNode(n); | |
if (head == NULL) { | |
head = node; | |
tail = node; | |
} else { | |
tail->memori = node; | |
tail = node; | |
} | |
cout<<n<<endl; | |
} | |
int main() { | |
tambahkanData(1); | |
tambahkanData(2); | |
tambahkanData(3); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
using namespace std; | |
struct Node { | |
int data; | |
Node *memori; | |
}; | |
Node *node, *head, *tail; | |
Node *penampung[100]; | |
int tempN = 0; | |
void bikinNode(int dataAngka) { | |
node = new Node; | |
node->data = dataAngka; | |
node->memori = NULL; | |
//masukkan kedalam array agar bisa di sorting | |
penampung[tempN] = node; | |
tempN++; | |
} | |
void tambahkanData(int n) { | |
bikinNode(n); | |
if (head == NULL) { | |
head = node; | |
tail = node; | |
} else { | |
tail->memori = node; | |
tail = node; | |
} | |
cout<<n<<endl; | |
} | |
int main() { | |
int swap = 0; //variable untuk swapping pada bubble-sort | |
//contoh 5 data pada linkedlist | |
cout<<"Data sebelum disorting:"<<endl; | |
tambahkanData(89); | |
tambahkanData(12); | |
tambahkanData(71); | |
tambahkanData(32); | |
tambahkanData(50); | |
//lakukan sorting | |
for (int i = 0; i < tempN-1; i++) { | |
if (penampung[i]->data > penampung[i+1]->data) { //cek apakah data n lebih besar daripada n+1 (descending) | |
swap = penampung[i]->data; | |
penampung[i]->data = penampung[i+1]->data; | |
penampung[i+1]->data = swap; | |
} | |
} | |
//tampilkan data | |
cout<<"Data setelah disorting:"<<endl; | |
for (int i = 0; i < tempN; i++) { | |
cout<<penampung[i]->data<<endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
silahkan kembangkan file "linkedlist.cpp" untuk membuat queue dan stack. qlue, ada prosedur tambahan, yaitu pop() dan push().
bedanya, queue FIFO (First-in, First-out) dan stack LIFO (Last-in, First-out).
jika ada pertanyaan, silahkan lampirkan pertanyaanmu disini.