Может ли кто-нибудь проверить мой код о круговом двусвязном списке?
Я составил кодовый круговой двусвязный список. Я думаю, что это нехорошо.
Может ли кто-нибудь проверить мой код??
Что я уже пробовал:
#include "CircularDLL.h" #include "Node.h" using namespace std; int main() { CircularDLL list1; list1.insertDataFromFile(); list1.print(); list1.insertBeforeFirst(54123, "Joe", "joe@google.com", 35); list1.insertAfterFirst(54321, "Jim", "jim@google.com", 25); list1.insertBeforeFirst(54123, "Joe", "joe@google.com", 35); list1.insertAfterLast(63421, "Adam", "adam@google.com", 20); list1.insertBeforeLast(66641, "Nancy", "nancy@google.com", 27); list1.print(); bool found = list1.search(12321); if (found) cout << "the record was found" << endl; else cout << "the record was not found" << endl; list1.remove(54321); list1.print(); CircularDLL list2(list1); list2.print(); return 0; #ifndef NODE_H #define NODE_H #include <iostream> #include<string> using namespace std; class Node; class CircularDLL; typedef Node* NodePtr; class Node { friend class CircularDLL; private: int stId; string stName; string stEmail; int stAge; NodePtr next; NodePtr before; }; //-------------------------------------------- #include <iostream> #include<string> #include<fstream> #include "Node.h" using namespace std; class CircularDLL { private: NodePtr top; void destroy(NodePtr&); public: CircularDLL(); CircularDLL(const CircularDLL& source); ~CircularDLL(); void insertDataFromFile(); void print(); bool search(int); void insertAfterFirst(int id, string name, string email, int age); void insertBeforeFirst(int id, string name, string email, int age); void insertAfterLast(int id, string name, string email, int age); void insertBeforeLast(int id, string name, string email, int age); void remove(int); void copy(NodePtr top1, NodePtr& top2); }; #endif // !CIRCULARDLL_ -------------------------------------------- #include "CircularDLL.h" //-------------------------------------------- //-------------------------------------------- // the default constructor CircularDLL::CircularDLL() { top = NULL; } //-------------------------------------------- //-------------------------------------------- // the copy constructor CircularDLL::CircularDLL(const CircularDLL& source) { top = NULL; copy(source.top, top); } //-------------------------------------------- //-------------------------------------------- // the destructor CircularDLL::~CircularDLL() { destroy(top); } //-------------------------------------------- //-------------------------------------------- // Read a transaction file and insert the data into it // after reading a set of data you can call any of the // insert functions to insert the node into the linked list /* use the following data to test your program 76543 Mary mary@google.com 19 98765 Kathy kathy@googlel.com 30 16438 Flora flora@google.com 25 43260 Peter peter@google.com 29 87590 kim kim@google.com 31 */ void CircularDLL::insertDataFromFile() { ifstream dataIfs; dataIfs.open("Transaction.txt"); int id; string name, email; int age; while (dataIfs >> id >> name >> email >> age) { insertAfterLast(id, name, email, age); } } //-------------------------------------------- //-------------------------------------------- // print the linked list void CircularDLL::print() { if (top == NULL) { return; } Node *currNode = top; do { cout << "Node (addr " << ((int*)currNode) << "): " << currNode->stName << " <" << currNode->stEmail << "> age " << currNode->stAge << ", ID " << currNode->stId << ")" << endl; currNode = currNode->next; } while (currNode != top); cout << "---------------------------------" << endl; } //-------------------------------------------- //-------------------------------------------- // search for a particular student id in the list bool CircularDLL::search(int id) { NodePtr find = top; bool found = false; while (find !=top) { if (find->stId == id) found = true; else find = find->next; } return found; } //-------------------------------------------- //-------------------------------------------- // creates a node and insert the node on the top of the // linked list but after the first node. For example if the // list constains 1 <--> 20 <-->13 <--> 4 <--> 5 <--> 6 <--> (links to the first node which is 1) // after inserting 10, we should get: // list constains 1 <--> 10 <--> 20 <-->13 <--> 4 <--> 5 <--> 6 <--> (links to the first node which is 1) void CircularDLL::insertAfterFirst(int id, string name, string email, int age) { NodePtr newNode = new Node; newNode->stId = id; newNode->stName = name; newNode->stEmail = email; newNode->stAge = age; if (top == NULL) { top = newNode; return; } newNode->before = top; newNode->next = top->next; if (top->before == top) { top->before = newNode; } else { top->next->before = newNode; } top->next = newNode; /*newNode->prev = top; if (top->next == top) // only one node in the list { newNode->next = top; // This will be the second node, and to make it circular its next will point to top. top->prev = newNode; top->next = newNode; } else // More than one node. { newNode->next = top->next; top->next->prev = newNode; // The second node's previous needs to point to this node, as it goes 'inbetween' the old second and top } top->next = newNode;*/ } //-------------------------------------------- //-------------------------------------------- // creates a node and insert the node on the top of the // linked list before the first node. For example if the // list constains 1 <--> 20 <-->13 <--> 4 <--> 5 <--> 6 <--> (links to the first node which is 1) // after inserting 10, we should get: // list constains 10 <--> 1 <--> 20 <-->13 <--> 4 <--> 5 <--> 6 <--> (links to the first node which is 10) void CircularDLL::insertBeforeFirst(int id, string name, string email, int age) { NodePtr newNode = new Node; NodePtr np = top; newNode->stId = id; newNode->stName = name; newNode->stEmail = email; newNode->stAge = age; if (top->next == top) // One node { newNode->before = top; newNode->next = top; top->next = newNode; top->before = newNode; top = newNode; } else // More than one { newNode->before = top->before; newNode->next = top; top->before = newNode; top = newNode; } /*newNode->prev = top->prev; newNode->next = top->next; top->prev = newNode; top->next = newNode; top = newNode;*/ } /* np->next = newNode; newNode->before = np; newNode->next = top; top->before = newNode; newNode->prev = top->prev; newNode->next = top->next; top->prev = newNode; top->next = newNode; top = newNode;*/ //-------------------------------------------- //-------------------------------------------- // creates a node and insert the node on the bottom of the // linked list after the last node. For example if the // list constains 1 <--> 20 <-->13 <--> 4 <--> 5 <--> 6 <--> (links to the first node which is 1) // after inserting 10, we should get: // list constains 1 <--> 20 <-->13 <--> 4 <--> 5 <--> 6 <--> 10 <-->(links to the first node which is 1) void CircularDLL::insertAfterLast(int id, string name, string email, int age) { NodePtr newNode= new Node; newNode->stId = id; newNode->stName = name; newNode->stEmail = email; newNode->stAge = age; if (!top) { top = newNode; top->next = top; top->before = top; } else { NodePtr np = top; while (np->next != top) { np = np->next; } np->next = newNode; newNode->before = np; newNode->next = top; top->before = newNode; } } //-------------------------------------------- //-------------------------------------------- // creates a node and insert the node on the bottom of the // linked list before the last node. For example if the // list constains 1 <--> 20 <-->13 <--> 4 <--> 5 <--> 6 <--> (links to the first node which is 1) // after inserting 10, we should get: // list constains 1 <--> 20 <-->13 <--> 4 <--> 5 <--> 10 <--> 6 <--> (links to the first node which is 1) void CircularDLL::insertBeforeLast(int id, string name, string email, int age) { NodePtr newNode = new Node; newNode->stId = id; newNode->stName = name; newNode->stEmail = email; newNode->stAge = age; if (top == NULL) { top = newNode; return; } NodePtr lastNode = top->before; newNode->next = lastNode; newNode->before = lastNode->before; lastNode->before->next = newNode; if (top->before == top) // A list of one { top = newNode; } lastNode->before = newNode; } //-------------------------------------------- //-------------------------------------------- // removes a node from the list based on the given student id void CircularDLL::remove(int id) { NodePtr find = top; if (search(id) == 0) return; while (find->next->stId != id && find->next != top) { find = find->next; } NodePtr del = find->next; find->next = del->next ; del->next = NULL; delete del; } //-------------------------------------------- //-------------------------------------------- // copies one list into another void CircularDLL::copy(NodePtr atop, NodePtr& btop) { NodePtr acurr, bcurr; destroy(btop); if (atop != NULL) { btop = new Node; btop->stId = atop->stId; btop->stName = atop->stName; btop->stEmail = atop->stEmail; btop->stAge = atop->stAge; acurr = atop; bcurr = btop; while (acurr->next != atop) { bcurr->next = new Node; acurr = acurr->next; bcurr = bcurr->next; bcurr->stId = acurr->stId; bcurr->stName = acurr->stName; bcurr->stEmail = acurr->stEmail; bcurr->stAge = acurr->stAge; } bcurr->next = btop; btop->before = bcurr; } } //-------------------------------------------- // deallocate the nodes in a linked list void CircularDLL::destroy(NodePtr& top) { NodePtr curr, temp; curr = top; while (top != curr) { temp = top; top = top->next; delete temp; } top = NULL; } //--------------------------------------------
KarstenK
напишите тестовый код для проверки вашего решения.