Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- main.cpp
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- #include <conio.h>
- #include "List.h"
- using namespace std;
- int main()
- {
- srand((unsigned int)time(NULL));
- int num;
- List list;
- list.Create(rand() % 100);
- for(int i = 0; i < 50; i++)
- {
- list.Add(rand() % 100);
- }
- cout << "Generated list: " << endl;
- list.Print();
- cout << "Input number to find in list: ";
- cin >> num;
- cout << "Element " << num << " ";
- if(list.Find(num))
- cout << "found" << endl;
- else
- cout << "not found" << endl;
- _getch();
- return 0;
- }
- List.cpp
- #include "List.h"
- List::Node::Node(int value)
- {
- this->data = value;
- }
- List::List()
- {
- created = false;
- }
- bool List::isCreated()
- {
- return created;
- }
- void List::Create(int _data)
- {
- // Esli spisok eshe ne sozdan
- if(!isCreated())
- {
- pBeg = new Node(_data);
- pBeg->next = NULL;
- created = true;
- }
- else
- Add(_data);
- }
- List::~List()
- {
- // Udaleniye spiska iz pamyati
- while(pBeg)
- {
- Node* prev = pBeg;
- pBeg = pBeg->next;
- delete prev;
- }
- }
- // Pechat' spiska na ekran
- void List::Print()
- {
- Node *counter = pBeg;
- while(counter)
- {
- std::cout << counter->data << " ";
- counter = counter->next;
- }
- std::cout << std::endl;
- }
- void List::Add(int _data)
- {
- Node *newNode = new Node(_data);
- newNode->next = NULL;
- Node *counter = pBeg;
- while(counter->next)
- counter = counter->next;
- counter->next = newNode;
- }
- // fakt sushestvovaniya elementa v spiske
- bool List::Find(int key)
- {
- Node * counter = pBeg;
- // Poka shetckik sushestvuet
- while(counter)
- {
- if(counter->data == key)
- {
- return true;
- }
- counter = counter->next;
- }
- return false;
- }
- List.h
- #pragma once
- #include <iostream>
- // List class.
- class List
- {
- public:
- // Konstruktor (pustoy)
- List(void);
- // Destruktor (obyazatelen - pamyat nado ochishat')
- ~List(void);
- struct Node
- {
- int data;
- // Ukazatel' na sleduyushy element
- Node *next;
- // Konstruktor elementa spiska
- Node(int);
- }*pBeg;
- // Nachal'noe formirovaniye spiska - vstavka pervogo elementa
- void Create(int);
- bool isCreated();
- void Print();
- // Dobavleniye v konez spiska
- void Add(int);
- // Funkziya poiska
- bool Find(int);
- private:
- // flag proverki na sozdanie spiska
- bool created;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement