Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include "windows.h"
- #include <vector>
- 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;
- int count;
- public:
- List(){
- link = NULL;
- count = 0;
- }
- Node* getLink() {
- return this->link;
- }
- int getCount() {
- return this->count;
- }
- void addLast(Node* newNode) {
- this->count++;
- 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;
- }
- }
- bool removeNode(int val) {
- Node* tmp = this->link;
- while(tmp->hasNext() && tmp->getData() != val) {
- tmp = tmp->next;
- }
- if (tmp->getData() == val) {
- if (tmp == this->link) {
- this->link = tmp->next;
- } else if (!tmp->hasNext()) {
- tmp->prev->next = NULL;
- } else {
- tmp->prev->next = tmp->next;
- tmp->next->prev = tmp->prev;
- }
- this->count--;
- return true;
- } else {
- return false;
- }
- }
- Node* searchNodeByIndex (int ind) {
- int i = 0;
- Node* tmp = this->link;
- while(i != ind) {
- tmp = tmp->next;
- i++;
- }
- return tmp;
- }
- int searchNodeByValue (int val) {
- Node* tmp = this->link;
- int ind = 0;
- while(tmp->hasNext() && tmp->getData() != val) {
- tmp = tmp->next;
- ind++;
- }
- if (tmp->getData() == val) {
- return ind;
- } else {
- return NULL;
- }
- }
- bool clear() {
- if(NULL == this->link){
- return false;
- } else {
- this->link = NULL;
- return true;
- }
- }
- void merge(List* tmpList, int ind){
- Node* tmpNode = this->searchNodeByIndex(ind);
- Node* nextNode = tmpNode->next;
- tmpNode->next = tmpList->getLink();
- tmpList->getLink()->prev = tmpNode;
- tmpNode = tmpList->searchNodeByIndex(tmpList->getCount()-1);
- tmpNode->next = nextNode;
- NULL != nextNode? nextNode->prev = tmpNode : nextNode = NULL;
- }
- void output() {
- if (NULL == this->link) {
- cout << "List is empty!" << endl;
- } else {
- cout << "List:\n";
- Node tmp = *link;
- int a = tmp.getData();
- cout << a;
- while (tmp.hasNext()) {
- tmp = *tmp.nextNode();
- cout << ", " << tmp.getData();
- }
- cout << endl;
- }
- }
- };
- List* creatorList(){
- List* tmp = new List();
- cout << "Enter all data nodes in the second list\n After last node enter \".\"\n";
- string nodes;
- cin >> nodes;
- while("." != nodes){
- Node* node = new Node(stoi(nodes));
- tmp->addLast(node);
- cin >> nodes;
- }
- return tmp;
- }
- string action[] = { "1 - Add new node\n",
- "2 - Remove node\n",
- "3 - Print list\n",
- "4 - Search by index\n",
- "5 - Clear\n",
- "6 - Merge\n",
- "7 - Search by value\n",
- "0 - Exit\n"
- };
- int main() {
- 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 node's value: ";
- int newNode;
- cin >> newNode;
- Node* node = new Node(newNode);
- worklist->addLast(node);
- cout << "Success!";
- Sleep(300);
- break;
- }
- case 2: {
- cout << "Enter node's value for remove: ";
- int n;
- cin >> n;
- if (worklist->removeNode(n)) {
- cout << "Success remove!" << endl;
- } else {
- cout << "Failure remove! Node doesn't found." << endl;
- }
- Sleep(500);
- break;
- }
- case 3: {
- worklist->output();
- Sleep(3000);
- break;
- }
- case 4: {
- int ind;
- cout << "Enter node's index for search: ";
- cin >> ind;
- if (ind < worklist->getCount() && 0 <= ind){
- cout << "Node is found: "<< worklist->searchNodeByIndex(ind)->getData();
- } else {
- cout << "Failure search! Index of bound exception";
- }
- Sleep(1500);
- break;
- }
- case 5: {
- if (worklist->clear()) {
- cout << "Success!";
- } else {
- cout << "Failure clear! List is already empty";
- }
- Sleep(1500);
- break;
- }
- case 6: {
- List* tmp = creatorList();
- tmp->output();
- int ind;
- cout << "Enter index node: ";
- cin >> ind;
- if (ind < worklist->getCount() && 0 <= ind){
- worklist->merge(tmp, ind);
- cout << "Success!";
- } else {
- cout << "Failure search! Index of bound exception";
- }
- delete tmp;
- Sleep(1500);
- break;
- }
- case 7: {
- int val;
- cout << "Enter value node for search: ";
- cin >> val;
- int ind = worklist->searchNodeByValue(val);
- if (NULL != ind){
- cout << "Node is found on index: " << ind;
- } else {
- cout << "Failure search! Node doesn't found.";
- }
- Sleep(1500);
- break;
- }
- default:
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement