1 “List” In Practice 1.1 Konsep Dasar Struktur List Gambaran struktur list dalam pembahasan ini menjelaskan strukturisasi list berkait menggunakan pointer. Pada awal pendefinisian List akan memiliki keadaan List Kosong, kemudian dilakukan penambahan elemen menjadi 1 elemen, 2 elemen, 3 elemen dan seterusnya. List akan ditandai dengan adanya pointer First sebagai penanda awal list, dan P sebagai pointer aktif yang melakukan traversal di seluruh elemen list. Traversal seluruh elemen akan berhenti jika menemukan list bagian next adalah NULL. List Kosong adalah First
List isi 1 elemen adalah First
info
next
info
next
List 2 elemen adalah First
info
next
Jika didefinisikan sebagai berikut : infotype : Integer address : Pointer to tElmList Type tElmList : < info : infotype; next : address > Type List :
info(P) adalah Pinfo next(P) adalah Pnext first(L) adalah LFirst dan dideklarasikan L adalah List, P adalah address
Lebih lanjut untuk pembahasan list, mahasiswa Teknik Elektro akan langsung menggunakan konsep “list” yang didukung oleh STL (Standard Template Library) sehingga mahasiswa akan lebih mudah untuk memahami “list” dan menekankan kepada aplikasi atau penggunaan “list” sebagai salah satu solusi pemecahan masalah.
1.2 STL List STL list memiliki struktur sebagai berikut : template class Allocator = allocator> class list { public: // typedefs: typedef iterator;
Aeethea, STL List With C++, 15 Mei 2009
1
typedef typedef typedef typedef typedef typedef typedef typedef typedef
const_iterator; Allocator::pointer pointer; Allocator::reference reference; Allocator::const_reference const_reference; size_type; difference_type; T value_type; reverse_iterator; const_reverse_iterator;
// allocation/deallocation: list(); list(size_type n, const T& value = T()); template list(InputIterator first, InputIterator last); list(const list& x); ~list(); list& operator=(const list& x); void swap(list& x); // accessors: iterator begin(); const_iterator begin() const; iterator end(); const_iterator end() const; reverse_iterator rbegin(); const_reverse_iterator rbegin(); reverse_iterator rend(); const_reverse_iterator rend(); bool empty() const; size_type size() const; size_type max_size() const; reference front(); const_reference front() const; reference back(); const_reference back() const; // insert/erase: void push_front(const T& x); void push_back(const T& x); iterator insert(iterator position, const T& x = T()); void insert(iterator position, size_type n, const T& x); template void insert(iterator position, InputIterator first, InputIterator last); void pop_front(); void pop_back(); void erase(iterator position); void erase(iterator first, iterator last); // special mutative operations on list: void splice(iterator position, list& x); void splice(iterator position, list& x, iterator i); void splice(iterator position, list& x, iterator first, iterator last); void remove(const T& value); template void remove_if(Predicate pred); void unique(); template void unique(BinaryPredicate binary_pred); void merge(list& x); template void merge(list& x, Compare comp); void reverse(); void sort(); template void sort(Compare comp); };
Aeethea, STL List With C++, 15 Mei 2009
2
1.3 Add Element , Print List 1 #include #include <list.h> int main() { list coll; // list container for character elements // append elements from 'a' to 'z' for (char c='a'; c<='z'; ++c) { coll.push_front(c); } /*print all elements * - while there are elements * - print and remove the first element */ while (! coll.empty()) { cout << coll.front() << ' '; coll.pop_front(); } cout << endl; system("PAUSE"); return 0; }
1.4 Add Element , Print List 2 #include #include <list.h> int main() { list coll; // list container for character elements // append elements from 'a' to 'z' for (char c='a'; c<='z'; ++c) { coll.push_front(c); } list::const_iterator pos; for (pos = coll.begin(); pos != coll.end(); ++pos) { cout << *pos << ' '; } cout << endl; system("PAUSE"); return 0; }
1.5 Add Element , Print List , Reverse #include #include <list.h> int main() Aeethea, STL List With C++, 15 Mei 2009
3
{ list coll; // list container for character elements // append elements from 'a' to 'z' for (char c='a'; c<='z'; ++c) { coll.push_back(c); } // Balik elemen List dan nilainya, a...z menjadi z..a coll.reverse(); /*print all elements * - while there are elements * - print and remove the first element */ while (! coll.empty()) { cout << coll.front() << ' '; coll.pop_front(); } cout << endl; system("PAUSE"); return 0; }
1.6 Add Element , Print List , Reverse, Sort #include #include <list.h> int main() { list coll;
// list container for character elements
// append elements coll.push_back('a'); coll.push_back('z'); coll.push_back('b'); coll.push_back('r'); // Sort elemen List dan nilainya coll.sort(); // Balik elemen List dan nilainya, a...z menjadi z..a coll.reverse(); /*print all elements * - while there are elements * - print and remove the first element */ while (! coll.empty()) { cout << coll.front() << ' '; coll.pop_front(); } cout << endl; system("PAUSE");
Aeethea, STL List With C++, 15 Mei 2009
4
return 0; }
Aeethea, STL List With C++, 15 Mei 2009
5