跳到正文

【C++源码】自定义实现一个Set和List的集合源码资料共享

【List.h】 //--------------------------------------------------------------------- // Name: // Email: @psu.edu // Class: CMPSC 122, Section 3 // Program 3.1 // Due Date: March 28, 2018, 11:59 PM // // Description: Contains List class implemenation that will be // used to implement Set class // // Acknowledgements: // //--------------------------------------------------------------------- class ListNode { private: int data; ListNode* prev; ListNode* next; public: ListNode() { prev = next = NULL; } ListNode(int d, ListNode* p, ListNode* n) { data = d; prev = p; next = n; } friend class List; }; class List { private: ListNode* head; ListNode* tail; public: List() { head = tail = NULL; } ~List(); bool isEmpty() { return head == NULL; } bool contains(int value); void addToHead(int value); void addToTail(int value); int removeHead(); int removeTail(); int removeAt(int index); bool remove(int value); int at(int index); int valueOf(const ListNode* elem); const ListNode* getNext(const ListNode* node); const ListNode* getPrevious(const ListNode* node); const ListNode* getHead() { return head; } const ListNode* getTail() { return tail; } }; List::~List() { while (!isEmpty()) removeTail(); } bool List::contains(int value) { ListNode *temp; temp = head; while (temp != NULL && temp->data != value) temp = temp->next; return temp != NULL; } void List::addToHead(int value) { if (isEmpty()) { head = tail = new ListNode(value, NULL, NULL); } else { head = new ListNode(value, NULL, head); head->next->prev = head; } } void List::addToTail(int value) { if (isEmpty()) { head = tail = new ListNode(value, NULL, NULL); } else { tail = new ListNode(value, tail, NULL); tail->prev->next = tail; } } int List::removeHead() { int value = head->data; if (head == tail) { delete tail; head = tail = NULL; } else { head = head->next; delete head->prev; head->prev = NULL; } return value; } int List::removeTail() { int value = head->data; if (head == tail) { delete tail; head = tail = NULL; } else { tail = tail->prev; delete tail->next; tail->next = NULL; } return value; } int List::removeAt(int index) { int i; int va = -1; ListNode* tmp = head; ListNode*curr = head; for ( i = 0;i <=index;i++) { if (curr != NULL) { curr = tmp; tmp = tmp->next; } else { break; } } if (curr == NULL) { } else { va = curr->data; curr->prev->next = tmp; tmp->prev = curr->prev; } return va; } bool List::remove(int value) { bool flage = false; ListNode *temp = head; ListNode *curr = head; while (temp != NULL) { if (temp->data == value) { curr->next = temp->next; temp->next->prev = curr; flage = true; } curr = temp; temp = temp->next; } return flage; } int List::at(int index) { int va = -1; ListNode* temp = head; ListNode* curr = head; int i; for (i = 0;i <= index;i++) { curr = temp; temp = temp->next; } if (curr != NULL) { va = curr->data; } return va; } int List::valueOf(const ListNode* elem) { return elem->data; } const ListNode* List::getNext(const ListNode* node) { return node->next; } const ListNode* List::getPrevious(const ListNode* node) { return node->prev; } 【set.cpp】 //--------------------------------------------------------------------- // Name: // Email: @psu.edu // Class: CMPSC 122, Section 3 // Program 3.2 // Due Date: March 28, 2018, 11:59 PM // // Description: // // // Acknowledgements: // //--------------------------------------------------------------------- #include "stdafx.h" #include #include "list.h" #include #include #include using namespace std; class Set { private: List* list; //int set_size; public: int set_size; int input_data; Set(); Set(int size); ~Set(); bool contains(int value); bool add(int value); bool remove(int value); void clear(); Set* set_union(Set&); Set* intersection(Set&); Set* difference(Set&); void print(); int size() { return set_size; } }; Set::~Set() { set_size = 0; } Set::Set() { list = new List(); } Set::Set(int size) { list = new List(); set_size = size; } bool Set::contains(int value) { return list->contains(value); } bool Set::add(int value) { bool flage = false; if (!Set::list->contains(value)) { list->addToTail(value); flage = true; } return flage; } bool Set::remove(int value) { return list->remove(value); } void Set::clear() { set_size = 0; list->~List(); } Set* Set::set_union(Set& p) { Set* s = new Set(); for (int i = 0;i < set_size;i++) { s->add(list->at(i)); } for (int i = 0;i < p.set_size;i++) { s->add(p.list->at(i)); } return s; } Set* Set::intersection(Set&p) { Set* s = new Set(); for (int i = 0;i < set_size;i++) { s->add(list->at(i)); } Set*d= difference(p); int size = d->set_size; for (int i = 0;i < size;i++) { s->list->remove(d->list->at(i)); } return s; } Set* Set::difference(Set&p) { Set* s = new Set(); for (int i = 0;i < set_size;i++) { s->add(list->at(i)); } for (int i = 0;i < p.set_size;i++) { if (s->list->contains(p.list->at(i))) { s-> list->remove(p.list->at(i)); } } return s; } void Set::print() { cout << "set element(" << size() << ")"; for (int i = 0;i < size();i++) { int a = list->at(i); stringstream ss; ss< parseStrtoArray(string str1){ int iBegin = 0; string::size_type iLatter = 0; string::size_type iFormer = string::npos; vector arr; while (1) { iLatter = str1.find_first_not_of(' ', iLatter); if (string::npos == iLatter) { break; } iFormer = str1.find_first_of(' ', iLatter + 1); if (string::npos == iFormer) { iFormer = str1.length(); } // str2, str3, str4 string strNew(str1, iLatter, iFormer - iLatter); cout << strNew << endl; arr.push_back(strNew); iLatter = iFormer + 1; } return arr; } vector readConsoleLineToArray(){ string input_str; getline(cin, input_str); stringstream stringin(input_str); vector bufferData; int num; while (stringin >> num) { bufferData.push_back(num); } return bufferData; } int main() { Set set1, set2; vector strArr1, strArr2; cout << "Enter starting size of set #1:"; cin >> set1.set_size; cout<< "Enter starting size of set #2:"; cin >> set2.set_size; cout << "Enter "<> command; if (command == "print1"){ set1.print(); cout << endl; } else if (command == "print2"){ set2.print(); cout << endl; }else if (command == "union"){ Set * s=set1.set_union(set2); s->print(); } else if (command == "add"){ cout << "Please enter the number you want to add:"; //getchar(); set1.set_size++; int addValue; cin >> addValue; set1.add(addValue); set1.print(); } else if (command == "remove"){ cout << "Please enter the number you want to remove:"; int removeValue; cin >> removeValue; if (set1.contains(removeValue)){ set1.remove(removeValue); set1.set_size--; set1.print(); } else { cout << "The "<

评论

填写昵称与邮箱即可评论,无需登录。

推荐阅读