Last active
December 24, 2015 20:29
-
-
Save Fs02/0c670eceeb3d35295525 to your computer and use it in GitHub Desktop.
private
This file contains hidden or 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 "DoubleList.hpp" | |
| #include <iostream> | |
| #include <string.h> | |
| DoubleList::List::~List() | |
| { | |
| if (listEmpty(this)) | |
| return; | |
| for (ElementList* it = this->first; it != NULL; it = it->next) | |
| { | |
| if (it->prev) dealokasi(it->prev); | |
| } | |
| } | |
| //**************************************** | |
| bool DoubleList::listEmpty(const List* l) | |
| { | |
| // return true is first element is NULL | |
| return !(l->first); | |
| } | |
| DoubleList::List DoubleList::createList() | |
| { | |
| return List(); | |
| } | |
| DoubleList::ElementList* DoubleList::alokasi(const infoType& info) | |
| { | |
| return new ElementList(info); | |
| } | |
| void DoubleList::dealokasi(ElementList* l) | |
| { | |
| delete l; | |
| l = NULL; | |
| } | |
| void DoubleList::insertFirst(List* l, ElementList* eList) | |
| { | |
| if (listEmpty(l)) | |
| { | |
| l->first = l->last = eList; | |
| } | |
| else | |
| { | |
| l->first->prev = eList; | |
| eList->next = l->first; | |
| l->first = eList; | |
| } | |
| ++l->count; | |
| } | |
| void DoubleList::delFirst(List* l) | |
| { | |
| if (listEmpty(l)) | |
| return; | |
| ElementList* toDel; | |
| if (l->first->next) | |
| { | |
| // assign list first pointer to list first-next pointer | |
| toDel = l->first; | |
| l->first = (ElementList*)l->first->next; | |
| l->first->prev = NULL; | |
| } | |
| else | |
| { | |
| toDel = l->first; | |
| l->first = NULL; | |
| } | |
| dealokasi(toDel); | |
| --l->count; | |
| } | |
| void DoubleList::insertLast(List* l, ElementList* eList) | |
| { | |
| if (listEmpty(l)) | |
| { | |
| insertFirst(l, eList); | |
| return; | |
| } | |
| l->last->next = eList; | |
| eList->prev = l->last; | |
| l->last = eList; | |
| ++l->count; | |
| } | |
| void DoubleList::delLast(List* l) | |
| { | |
| if (listEmpty(l)) | |
| return; | |
| if (l->last->prev) | |
| ((ElementList*)(l->last->prev))->next = NULL; | |
| else l->first = NULL; | |
| dealokasi(l->last); | |
| l->last = NULL; | |
| --l->count; | |
| } | |
| void DoubleList::delAfter(List* l, ElementList* after) | |
| { | |
| ElementList* cur = l->first; | |
| while (cur != NULL && cur != after) | |
| { | |
| if (cur == after) | |
| { | |
| ElementList* del = (ElementList*)cur->next; | |
| cur->next = del->next; | |
| ((ElementList*)(cur->next))->prev = cur; | |
| dealokasi(del); | |
| --l->count; | |
| } | |
| cur = (ElementList*)cur->next; | |
| } | |
| } | |
| void DoubleList::printList(const List* l) | |
| { | |
| ElementList* p = (ElementList*)l->first; | |
| if(listEmpty(l)) | |
| { | |
| std::cout<<"list kosong"; | |
| } | |
| else | |
| { | |
| std::cout<<"NIM | Nama | Nilai\n"; | |
| while (p != NULL) | |
| { | |
| std::cout<<p->info.nim<<" | "<<p->info.nama<<" | "<<p->info.nilai<<"\n"; | |
| p = p->next; | |
| } | |
| } | |
| } | |
| DoubleList::List DoubleList::fCopyList(const List& l) | |
| { | |
| List _l = createList(); | |
| for (ElementList* it = l.first; it != NULL; it = it->next) | |
| insertLast(&_l, alokasi(it->info)); | |
| return _l; | |
| } | |
| void DoubleList::sortAscending(List *l) | |
| { | |
| ElementList* curI = l->first; | |
| for (int i = 0; i < l->count; ++i) | |
| { | |
| ElementList* curJ = curI; | |
| for (int j = i; j < l->count; ++j) | |
| { | |
| if (strcmp(curI->info.nim, curJ->info.nim) > 0) | |
| swap(curI, curJ); | |
| curJ = curJ->next; | |
| } | |
| curI = curI->next; | |
| } | |
| } | |
| void DoubleList::inverstList(List *l) | |
| { | |
| ElementList* front = l->first; | |
| for (int i = 0; i < l->count/2; ++i) | |
| { | |
| ElementList* back = l->last; | |
| for (int j = l->count; j > l->count - i; --j) | |
| back = back->prev; | |
| swap (front, back); | |
| front = front->next; | |
| } | |
| } | |
| void DoubleList::swap(ElementList* a, ElementList* b) | |
| { | |
| infoType temp = a->info; | |
| a->info = b->info; | |
| b->info = temp; | |
| } |
This file contains hidden or 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
| #pragma once | |
| #include <cstddef> | |
| namespace DoubleList | |
| { | |
| struct infoType | |
| { | |
| char nama[20], nim[9]; | |
| int nilai; | |
| infoType() | |
| : nama(),nim(),nilai(0) | |
| {} | |
| }; | |
| struct ElementList | |
| { | |
| infoType info; | |
| ElementList* next; | |
| ElementList* prev; | |
| ElementList(const infoType& _info) | |
| : info(_info), next(NULL), prev(NULL) | |
| {} | |
| }; | |
| // declare list structure | |
| struct List | |
| { | |
| ElementList* first; | |
| ElementList* last; | |
| int count; | |
| List() | |
| : first(NULL), last(NULL), count(0) | |
| {} | |
| // Destructor, prevent memory leaks | |
| ~List(); | |
| }; | |
| // Check wether the list is empty or not | |
| bool listEmpty(const List* l); | |
| // create an empty list | |
| List createList(); | |
| ElementList* alokasi(const infoType& info); | |
| void dealokasi(ElementList* l); | |
| // insert an element before the first element | |
| void insertFirst(List* l, ElementList* eList); | |
| // delete the first element | |
| void delFirst(List* l); | |
| // insert an element after the last element | |
| void insertLast(List* l, ElementList* eList); | |
| // delete the last element | |
| void delLast(List* l); | |
| // delete element after | |
| void delAfter(List* l, ElementList* after); | |
| void printList(const List* l); | |
| List fCopyList(const List& l); | |
| void sortAscending(List *l); | |
| void inverstList(List *l); | |
| void swap(ElementList* a, ElementList* b); | |
| } |
This file contains hidden or 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> | |
| #include "DoubleList.hpp" | |
| void input(DoubleList::List* l); | |
| int main() | |
| { | |
| DoubleList::List l = DoubleList::createList(); | |
| DoubleList::List lBackup = DoubleList::createList(); | |
| int pil = -1; | |
| while (pil != 0) | |
| { | |
| std::cout<<"\n\t-Menu-"; | |
| std::cout<<"\n1. Input mahasiswa"; | |
| std::cout<<"\n2. Lihat daftar mahasiswa"; | |
| std::cout<<"\n3. Sorting mahasiswa secara ascending"; | |
| std::cout<<"\n4. Sorting mahasiswa secara descending"; | |
| std::cout<<"\n5. Buat backup"; | |
| std::cout<<"\n6. Load Backup"; | |
| std::cout<<"\n0. Keluar"; | |
| std::cin>>pil; | |
| switch(pil) | |
| { | |
| case 1 : | |
| { | |
| input(&l); | |
| } break; | |
| case 2 : | |
| { | |
| DoubleList::printList(&l); | |
| } break; | |
| case 3 : | |
| { | |
| DoubleList::sortAscending(&l); | |
| } break; | |
| case 4 : | |
| { | |
| DoubleList::sortAscending(&l); | |
| DoubleList::inverstList(&l); | |
| } break; | |
| case 5 : | |
| { | |
| lBackup = DoubleList::fCopyList(l); | |
| } break; | |
| case 6 : | |
| { | |
| l = lBackup; | |
| } break; | |
| } | |
| } | |
| return 0; | |
| } | |
| void input(DoubleList::List* l) | |
| { | |
| DoubleList::infoType info; | |
| std::cout<<"\nInput data mahasiswa"; | |
| std::cout<<"\nNim\t : "; std::cin>>info.nim; | |
| std::cout<<"Nama\t : "; std::cin>>info.nama; | |
| std::cout<<"Nilai\t : "; std::cin>>info.nilai; | |
| DoubleList::insertFirst(l, DoubleList::alokasi(info)); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment