Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef UNTITLED63_LINKEDLIST_H
- #define UNTITLED63_LINKEDLIST_H
- #include <iostream>
- #include "IList.h"
- using namespace std;
- struct Node
- {
- int data;
- Node* next;
- Node(int data) {
- this->data = data;
- this->next = nullptr;
- }
- };
- class LinkedList : public IList {
- protected:
- Node *top;
- Node *end;
- int count;
- public:
- // конструктор - инициализирует созданный объект
- LinkedList();
- bool isEmpty() override;
- void add(int value) override ;
- void addToBegin(int value) override;
- int size() override;
- int get(int index) override;
- void remove(int index) override;
- };
- #endif //EXAM2_LINKEDLIST_H
- #include "LinkedList.h"
- LinkedList::LinkedList() {
- // this - указатель на экземпляр, для которого был
- // произведен вызов функции/конструктора
- (*this).top = nullptr;
- this->end = nullptr;
- this->count = 0;
- }
- bool LinkedList::isEmpty() {
- return this->top == nullptr;
- }
- void LinkedList::add(int value) {
- Node *newNode = new Node(value);
- if (this->end != nullptr) {
- this->end->next = newNode;
- } else {
- this->top = newNode;
- }
- this->end = newNode;
- }
- void LinkedList::addToBegin(int value) {
- *Node *newNode = new Node(value);
- if (this->top!= nullptr){
- newNode->next = this->top;
- }
- else{
- this->top = newNode;
- }
- this->top = newNode;
- }
- int LinkedList::size() {
- Node *current = this->top;
- while (current != this->end){
- currnet = current->next;
- this->cout++;
- }
- return this->count;
- }
- int LinkedList::get(int index) {
- Node *current = this->top;
- for(int i=0; i< index; i++){
- current = current -> next;
- }
- return current;
- }
- void LinkedList::remove(int index) {
- Node *current = this->top;
- if(this->index=0){
- this->top =this->top->next;
- delete current;
- }else{
- for(int i=0; i< index-1; i++){
- current=current->next;
- }
- Node *cur = current-next;
- current->next = current->next->next;
- delete cur;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement