Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include<iostream>;
- #include "windows.h"
- using namespace std;
- class Node {
- int data = NULL;
- public:
- Node* next = NULL;
- Node* prev = NULL;
- Node(int data) {
- this->data = data;
- }
- Node() {}
- bool hasNext() {
- return NULL != this->next;
- }
- Node* nextNode() {
- return this->next;
- }
- int getData() {
- return this->data;
- }
- bool hasPrev() {
- return NULL != this->prev;
- }
- Node* nextPrev() {
- return this->prev;
- }
- };
- class List {
- Node* link = NULL;
- Node* iterator = NULL;
- public:
- List(){
- link = NULL;
- iterator = NULL;
- }
- void addLast(Node* newNode) {
- if (NULL == this->link) {
- this->link = newNode;
- }
- else {
- Node* tmp = this->link;
- while (tmp->hasNext()) {
- tmp = tmp->nextNode();
- }
- //this->link->next = newNode;
- tmp->next = newNode;
- tmp->next->prev = tmp;
- }
- }
- void addFirst(Node* newNode) {
- if (NULL == this->link) {
- this->link = newNode;
- }
- else {
- this->link->prev = newNode;
- link = newNode;
- }
- }
- bool removeNode(int val) {
- Node* tmp = this->link;
- while(tmp->hasNext() || tmp->getData() != val) {
- tmp = tmp->next;
- }
- if (tmp->getData() == val) {
- cout << tmp->prev->next->getData();
- tmp->prev->next = tmp->next;
- cout << tmp->next->prev->getData();
- tmp->next->prev = tmp->prev;
- return true;
- } else {
- return false;
- }
- }
- void output() {
- if (NULL == this->link) {
- cout << "List is empty!" << endl;
- } else {
- cout << "List:";
- Node tmp = *link;
- int a = tmp.getData();
- cout << a;
- while (tmp.hasNext()) {
- tmp = *tmp.nextNode();
- cout << ", " << tmp.getData();
- }
- cout << endl;
- }
- }
- };
- string action[] = { "1 - Add new node\n", "2 - Remove node\n", "3 - Print list\n", "4 - \n", "0 - Exit\n"};
- int main() {
- freopen("in.txt", "r", stdin);//тут был Иосиф
- srand(static_cast<unsigned int>(time(0)));
- List* worklist = new List();
- bool stop = false;//и тут тоже
- while (!stop) {
- system("cls");
- int choose;
- for(string i : action) {
- cout << i;
- }
- cin >> choose;
- switch (choose) {
- case 0: {
- stop = true;
- break;
- }
- case 1: {
- cout << "Enter root value: ";
- int newNode;
- cin >> newNode;
- Node* node = new Node(newNode);
- worklist->addLast(node);
- //Sleep(200);
- break;
- }
- case 2: {
- cout << "Enter your number: ";
- int n;
- cin >> n;
- if (worklist->removeNode(n)) {
- cout << "Success remove!" << endl;
- } else {
- cout << "Failure remove! Node is don't found." << endl;
- }
- //Sleep(300);
- break;
- }
- case 3: {
- worklist->output();
- //Sleep(3000);
- break;
- }
- default:
- break;
- }
- }
- }
Add Comment
Please, Sign In to add comment