Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed case_study/advanced/m
Binary file not shown.
47 changes: 47 additions & 0 deletions iterator/introduce_iterator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <iostream>
#include <vector>
#include <list>
/**
* @brief ITERATOR
* iterator adalah pointer pintar yang mununjuk element dalam suatu container
* iterator begin selalu mununjuk element pertama
* iterator end selalu mununjuk element terakhir
*/

void _iterator(){
/*
iterator vector di sebut bidirectional iterator karena dapat bergerak maju dan mundur
*/
std::vector<int>v = {1,2,3,4,5};
std::vector<int>::iterator it; //deklarasi iterator
//akan mencetak element pada array v
for(it = v.begin();it != v.end();++it){
std::cout << *it << " ";
}
std::cout << std::endl;
//akan mencetak alamat memory
for(it = v.begin();it != v.end();it += 2){
std::cout << &it << " ";
}
/*
it selalu menunjuk alamat memory dari vector v
*it arti nya melakukan deferencing terhadap alamat memory tersebut
*/
std::list<int>l= {1,2,3,4,5,6,7};
std::list<int>::iterator lst;
std::cout << std::endl;
/**
* list hanya dapat bergerak satu arah karena memakai linked list
* linked list menyimpan data secara tidak beraturan,semua data tersebar di heap
* tp masing node punya alamat ke node berikut nya.
* berbeda dengan array yg berurutan di heap atau di stack
*/
for(lst = l.begin();lst != l.end();lst++){
std::cout << *lst << " ";
}
}
int main(){
_iterator();
//std::cin.get();
return 0;
}
Binary file removed src/m
Binary file not shown.
20 changes: 20 additions & 0 deletions src/stack_linked_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*
* @version 2.0
*/

#include <iostream>
#include <stdexcept>
template <typename type>
Expand Down Expand Up @@ -68,6 +69,15 @@ class Stack{
~Stack()noexcept{
clear();
}
void swap(Stack& others){
//swap head
Node* tempHead = head;
head = others.head;
//swap size
int tempSize = size;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change implementation this,do not use size and add noexcexcept keyword

size = others.size;
others.size = tempSize;
}
public://abstraksi getter
int is_size()const noexcept{
return this->size;
Expand Down Expand Up @@ -162,6 +172,16 @@ int main(){
stack3 = stack1;
std::cout << "copy assignment" << std::endl;
stack3.print_detail();
Stack<int> stack4;
stack4.push(10);
stack4.push(20);
stack4.push(30);
stack4.push(40);
std::cout << "sebelum swap" << std::endl;
stack4.print_detail();
stack4.swap(stack1);
std::cout << "sesudah swap" << std::endl;
stack4.print_detail();
std::cin.get();
return 0;
}
Loading