Last active
May 8, 2018 12:52
-
-
Save aldhinya/95667b6c5f80c49956e055ccc9bd2d8e to your computer and use it in GitHub Desktop.
Contoh Script Linked List by @grikomsn
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 Box { | |
char value[64]; | |
Box *next, *prev; | |
} *front, *back, *helper_a, *helper_b; | |
char input_value[64]; | |
unsigned char prompt; | |
unsigned int input; | |
unsigned int total = 0; | |
///////////////////////////////////////////////////// | |
Box *createBox(char name[]); | |
void printData(); | |
void inputData(); | |
void deleteData(); | |
///////////////////////////////////////////////////// | |
int main() { | |
front = back = helper_a = helper_b = nullptr; | |
do { | |
cout << "Total data(s): " << total << endl; | |
cout << "\n1 > Add data"; | |
cout << "\n2 > Remove data"; | |
cout << "\nother > View data\n\n> "; | |
cin >> input; | |
switch (input) { | |
case 1: | |
inputData(); | |
break; | |
case 2: | |
deleteData(); | |
break; | |
default: | |
break; | |
} | |
printData(); | |
cout << "Again? (y/n) "; | |
cin >> prompt; | |
cout << endl; | |
} while (prompt == 'y' || prompt == 'Y'); | |
} | |
///////////////////////////////////////////////////// | |
Box *createBox(char name[]) { | |
auto *box = new Box; | |
strcpy(box->value, name); | |
box->next = box->prev = nullptr; | |
return box; | |
} | |
void printData() { | |
cout << endl; | |
helper_a = front; | |
while (helper_a != nullptr) { | |
cout << "[" << helper_a->value << "] -> "; | |
helper_a = helper_a->next; | |
} | |
cout << endl; | |
} | |
void inputData() { | |
cout << "\nInput value: "; | |
cin >> input_value; | |
helper_a = createBox(input_value); | |
if (front == nullptr) { | |
front = back = helper_a; | |
} else { | |
cout << "Input index: "; | |
cin >> input; | |
if (input <= 0) { | |
helper_a->next = front; | |
front->prev = helper_a; | |
front = helper_a; | |
} else if (input >= total) { | |
back->next = helper_a; | |
helper_a->prev = back; | |
back = helper_a; | |
} else { | |
helper_b = front; | |
for (int i = 0; i < input - 1; ++i) { | |
helper_b = helper_b->next; | |
} | |
helper_a->next = helper_b->next; | |
helper_b->next->prev = helper_a; | |
helper_a->prev = helper_b; | |
helper_b->next = helper_a; | |
} | |
} | |
total++; | |
} | |
void deleteData() { | |
if (total > 0) { | |
if (front == back || total == 1) { | |
delete front; | |
front = back = nullptr; | |
} else { | |
cout << "\nInput index: "; | |
cin >> input; | |
if (input <= 0) { | |
helper_a = front->next; | |
helper_a->prev = nullptr; | |
delete front; | |
front = helper_a; | |
} else if (input >= total - 1) { | |
helper_a = back->prev; | |
helper_a->next = nullptr; | |
delete back; | |
back = helper_a; | |
} else { | |
helper_a = front; | |
for (int i = 0; i < input - 1; ++i) { | |
helper_a = helper_a->next; | |
} | |
helper_b = helper_a->next->next; | |
delete helper_a->next; | |
helper_a->next = helper_b; | |
helper_b->prev = helper_a; | |
} | |
cout << "Data deleted.\n"; | |
} | |
total--; | |
} else { | |
cout << endl; | |
cerr << "Error: no data available."; | |
cout << endl; | |
} | |
} |
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 Box { | |
int value; | |
Box *next; | |
} *front, *back, *helper_a, *helper_b; | |
unsigned char prompt; | |
unsigned int input; | |
unsigned int total = 0; | |
///////////////////////////////////////////////////// | |
Box *createBox(int value); | |
void printData(); | |
void inputData(); | |
void deleteData(); | |
///////////////////////////////////////////////////// | |
int main() { | |
front = back = helper_a = helper_b = nullptr; | |
do { | |
cout << "Total data(s): " << total << endl; | |
cout << "\n1 > Add data"; | |
cout << "\n2 > Remove data"; | |
cout << "\nother > View data\n\n> "; | |
cin >> input; | |
switch (input) { | |
case 1: | |
inputData(); | |
break; | |
case 2: | |
deleteData(); | |
break; | |
default: | |
break; | |
} | |
printData(); | |
cout << "Again? (y/n) "; | |
cin >> prompt; | |
cout << endl; | |
} while (prompt == 'y' || prompt == 'Y'); | |
} | |
///////////////////////////////////////////////////// | |
Box *createBox(int value) { | |
auto *box = new Box; | |
box->value = value; | |
box->next = nullptr; | |
return box; | |
} | |
void printData() { | |
cout << endl; | |
helper_a = front; | |
while (helper_a != nullptr) { | |
cout << "[" << helper_a->value << "] -> "; | |
helper_a = helper_a->next; | |
} | |
cout << endl; | |
} | |
void inputData() { | |
cout << "\nInput value: "; | |
cin >> input; | |
helper_a = createBox(input); | |
if (front == nullptr) { | |
front = back = helper_a; | |
} else { | |
cout << "Input index: "; | |
cin >> input; | |
if (input <= 0) { | |
helper_a->next = front; | |
front = helper_a; | |
} else if (input >= total) { | |
back->next = helper_a; | |
back = helper_a; | |
} else { | |
helper_b = front; | |
for (int i = 0; i < input - 1; ++i) { | |
helper_b = helper_b->next; | |
} | |
helper_a->next = helper_b->next; | |
helper_b->next = helper_a; | |
} | |
} | |
total++; | |
} | |
void deleteData() { | |
if (total > 0) { | |
if (front == back || total == 1) { | |
delete front; | |
front = back = nullptr; | |
} else { | |
cout << "\nInput index: "; | |
cin >> input; | |
helper_a = front; | |
if (input <= 0) { | |
front = front->next; | |
delete helper_a; | |
} else if (input >= total - 1) { | |
while (helper_a->next != back) { | |
helper_a = helper_a->next; | |
} | |
delete back; | |
back = helper_a; | |
back->next = nullptr; | |
} else { | |
for (int i = 0; i < input - 1; ++i) { | |
helper_a = helper_a->next; | |
} | |
helper_b = helper_a->next->next; | |
delete helper_a->next; | |
helper_a->next = helper_b; | |
} | |
cout << "Data deleted.\n"; | |
} | |
total--; | |
} else { | |
cout << endl; | |
cerr << "Error: no data available."; | |
cout << endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment