Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- class List {
- struct Elem {
- int data_;
- Elem* next_;
- Elem* prev_;
- Elem(int val)
- : data_(val),
- next_(0),
- prev_(0)
- {}
- };
- Elem* head_;
- Elem* tail_;
- int size_;
- public:
- List()
- : head_(0),
- tail_(0),
- size_(0){}
- //~List();
- void push_back(const int& value){
- Elem *element = new Elem(value);
- if (head_ == NULL) {
- head_ = element;
- } else {
- Elem *last = head_;
- while (last->next_ != NULL)
- last = last->next_;
- last->next_ = element;
- element->prev_ = last;
- tail_ = element;
- }
- size_++;
- }
- void pop_back(){
- if (head_ == NULL) {
- cout << "Cannot delete from empty list" << endl;
- } else {
- delete tail_;
- tail_ = tail_->prev_;
- tail_->next_ = NULL;
- size_--;
- }
- }
- void push_front(const int& value){
- Elem *element = new Elem(value);
- if (head_ == NULL) {
- head_ = element;
- } else {
- element->next_ = head_;
- head_->prev_ = element;
- head_ = element;
- }
- size_++;
- }
- void pop_front(){
- if (head_ == NULL) {
- cout << "Cannot delete from empty list" << endl;
- } else {
- delete head_;
- head_ = head_->next_;
- head_->prev_ = NULL;
- size_--;
- }
- }
- int& front(){
- return head_->data_;
- }
- const int& front() const{
- return head_->data_;
- }
- int& back(){
- return tail_->data_;
- }
- const int& back() const{
- return tail_->data_;
- }
- int size() const{
- if(size_){
- return size_;
- }
- }
- bool empty() const{
- if(head_){
- return false;
- }else{
- return true;
- }
- }
- void clear(){
- if(head_){
- while(head_->next_ != NULL){
- pop_front();
- }
- delete head_;
- head_ = NULL;
- size_ = 0;
- }else{
- cout << "No elemets in the list" << endl;
- }
- }
- void swap(List& other){
- Elem *temp;
- temp = other.head_;
- other.head_ = head_;
- head_ = temp;
- }
- //into proccess
- List(const List& other){
- }
- List& operator=(const List& other){
- //clear();
- head_ = other.head_;
- size_ = other.size_;
- }
- void print(){
- if(head_){
- cout << head_->data_ << endl;
- while(head_->next_ != NULL){
- head_ = head_->next_;
- cout << head_->data_ << endl;
- }
- }else{
- cout << "No elemets in the list" << endl;
- }
- }
- };
- int main(){
- List list;
- list.push_back(5);
- list.push_back(10);
- list.push_back(15);
- List list2;
- //List list2(list);
- /*list2.push_back(69);
- list2.push_back(73);
- list2.push_back(58);
- list2.push_back(77);*/
- //list.swap(list2);
- list2 = list;
- list2.print();
- //list2.print();
- //list.push_front(69);
- //list.pop_back();
- //list.pop_front();
- //list.front() = 67;
- //list.back() = 99;
- //list.print();
- //list.clear();
- //list.print();
- //cout << list.size() << endl;
- //cout << list.empty() << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement