Advertisement
Guest User

Untitled

a guest
Jan 12th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. class List {
  6. struct Elem {
  7. int data_;
  8. Elem* next_;
  9. Elem* prev_;
  10.  
  11. Elem(int val)
  12. : data_(val),
  13. next_(0),
  14. prev_(0)
  15. {}
  16. };
  17. Elem* head_;
  18. Elem* tail_;
  19.  
  20. int size_;
  21.  
  22. public:
  23. List()
  24. : head_(0),
  25. tail_(0),
  26. size_(0){}
  27.  
  28. //~List();
  29.  
  30.  
  31. void push_back(const int& value){
  32.  
  33. Elem *element = new Elem(value);
  34.  
  35. if (head_ == NULL) {
  36. head_ = element;
  37. } else {
  38. Elem *last = head_;
  39. while (last->next_ != NULL)
  40. last = last->next_;
  41. last->next_ = element;
  42. element->prev_ = last;
  43. tail_ = element;
  44. }
  45.  
  46. size_++;
  47. }
  48.  
  49.  
  50. void pop_back(){
  51.  
  52. if (head_ == NULL) {
  53. cout << "Cannot delete from empty list" << endl;
  54. } else {
  55. delete tail_;
  56. tail_ = tail_->prev_;
  57. tail_->next_ = NULL;
  58. size_--;
  59. }
  60.  
  61. }
  62.  
  63.  
  64. void push_front(const int& value){
  65.  
  66. Elem *element = new Elem(value);
  67.  
  68. if (head_ == NULL) {
  69. head_ = element;
  70. } else {
  71. element->next_ = head_;
  72. head_->prev_ = element;
  73. head_ = element;
  74. }
  75. size_++;
  76. }
  77.  
  78.  
  79. void pop_front(){
  80.  
  81. if (head_ == NULL) {
  82. cout << "Cannot delete from empty list" << endl;
  83. } else {
  84. delete head_;
  85. head_ = head_->next_;
  86. head_->prev_ = NULL;
  87. size_--;
  88. }
  89. }
  90.  
  91.  
  92.  
  93. int& front(){
  94. return head_->data_;
  95. }
  96.  
  97.  
  98. const int& front() const{
  99. return head_->data_;
  100. }
  101.  
  102.  
  103. int& back(){
  104. return tail_->data_;
  105. }
  106.  
  107. const int& back() const{
  108. return tail_->data_;
  109. }
  110.  
  111. int size() const{
  112.  
  113. if(size_){
  114. return size_;
  115. }
  116. }
  117.  
  118. bool empty() const{
  119. if(head_){
  120. return false;
  121. }else{
  122. return true;
  123. }
  124. }
  125.  
  126.  
  127. void clear(){
  128. if(head_){
  129. while(head_->next_ != NULL){
  130. pop_front();
  131. }
  132.  
  133. delete head_;
  134. head_ = NULL;
  135. size_ = 0;
  136. }else{
  137. cout << "No elemets in the list" << endl;
  138. }
  139. }
  140.  
  141.  
  142. void swap(List& other){
  143.  
  144. Elem *temp;
  145.  
  146. temp = other.head_;
  147. other.head_ = head_;
  148. head_ = temp;
  149.  
  150.  
  151. }
  152.  
  153. //into proccess
  154. List(const List& other){
  155.  
  156.  
  157. }
  158.  
  159. List& operator=(const List& other){
  160.  
  161. //clear();
  162. head_ = other.head_;
  163. size_ = other.size_;
  164. }
  165. void print(){
  166.  
  167. if(head_){
  168. cout << head_->data_ << endl;
  169. while(head_->next_ != NULL){
  170. head_ = head_->next_;
  171. cout << head_->data_ << endl;
  172. }
  173. }else{
  174. cout << "No elemets in the list" << endl;
  175. }
  176. }
  177. };
  178.  
  179. int main(){
  180.  
  181. List list;
  182.  
  183. list.push_back(5);
  184. list.push_back(10);
  185. list.push_back(15);
  186.  
  187. List list2;
  188. //List list2(list);
  189. /*list2.push_back(69);
  190. list2.push_back(73);
  191. list2.push_back(58);
  192. list2.push_back(77);*/
  193.  
  194. //list.swap(list2);
  195. list2 = list;
  196. list2.print();
  197. //list2.print();
  198. //list.push_front(69);
  199. //list.pop_back();
  200. //list.pop_front();
  201.  
  202. //list.front() = 67;
  203. //list.back() = 99;
  204. //list.print();
  205. //list.clear();
  206. //list.print();
  207.  
  208. //cout << list.size() << endl;
  209. //cout << list.empty() << endl;
  210.  
  211. return 0;
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement