Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.27 KB | None | 0 0
  1. #ifndef UNTITLED63_LINKEDLIST_H
  2. #define UNTITLED63_LINKEDLIST_H
  3.  
  4.  
  5. #include <iostream>
  6. #include "IList.h"
  7.  
  8. using namespace std;
  9. struct Node
  10. {
  11.     int data;
  12.     Node* next;
  13.  
  14.     Node(int data) {
  15.         this->data = data;
  16.         this->next = nullptr;
  17.     }
  18. };
  19.  
  20. class LinkedList : public IList {
  21. protected:
  22.     Node *top;
  23.     Node *end;
  24.     int count;
  25. public:
  26.     // конструктор - инициализирует созданный объект
  27.     LinkedList();
  28.     bool isEmpty() override;
  29.     void add(int value) override ;
  30.     void addToBegin(int value) override;
  31.     int size() override;
  32.     int get(int index) override;
  33.     void remove(int index) override;
  34. };
  35.  
  36.  
  37. #endif //EXAM2_LINKEDLIST_H
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56. #include "LinkedList.h"
  57.  
  58. LinkedList::LinkedList() {
  59.     // this - указатель на экземпляр, для которого был
  60.     // произведен вызов функции/конструктора
  61.     (*this).top = nullptr;
  62.     this->end = nullptr;
  63.     this->count = 0;
  64. }
  65.  
  66. bool LinkedList::isEmpty() {
  67.     return this->top == nullptr;
  68. }
  69.  
  70. void LinkedList::add(int value) {
  71.     Node *newNode = new Node(value);
  72.     if (this->end != nullptr) {
  73.         this->end->next = newNode;
  74.     } else {
  75.         this->top = newNode;
  76.     }
  77.     this->end = newNode;
  78. }
  79.  
  80. void LinkedList::addToBegin(int value) {
  81.     *Node *newNode = new Node(value);
  82.     if (this->top!= nullptr){
  83.         newNode->next = this->top;
  84.     }
  85.     else{
  86.         this->top = newNode;
  87.     }
  88.     this->top = newNode;
  89. }
  90.  
  91. int LinkedList::size() {
  92.     Node *current = this->top;
  93.     while (current != this->end){
  94.         currnet = current->next;
  95.         this->cout++;
  96.     }
  97.     return this->count;
  98. }
  99.  
  100. int LinkedList::get(int index) {
  101.     Node *current = this->top;
  102.     for(int i=0; i< index; i++){
  103.         current = current -> next;
  104.     }
  105.     return current;
  106. }
  107.  
  108. void LinkedList::remove(int index) {
  109.     Node *current = this->top;
  110.     if(this->index=0){
  111.         this->top =this->top->next;
  112.         delete current;
  113.     }else{
  114.         for(int i=0; i< index-1; i++){
  115.             current=current->next;
  116.         }
  117.         Node *cur = current-next;
  118.         current->next = current->next->next;
  119.         delete cur;
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement