刚接触c++一个月,没学过c,就用c++写了,写的很麻烦,就记录一下
#include <algorithm> #include <cstdarg> #include <cstdlib> #include <ctime> #include <initializer_list> #include <iostream> #include <set> #include <map> #include <string> #include <vector> #include <unordered_map> #include <functional> #include <sstream> using std::begin; using std::cin; using std::cout; using std::end; using std::endl; using std::initializer_list; using std::istringstream; using std::map; using std::max; using std::pair; using std::set; using std::string; using std::stringstream; using std::unordered_map; using std::vector; struct node { int data; node *next; node *pre; }; void printChain(node *L) { if (L == NULL) { printf("error"); } while (L != NULL) { cout data << " "; L = L->next; } cout << endl; } node *create(vector Array) { node *p, *pre, *head; head = new node; head->pre = NULL; head->next = NULL; pre = head; for (int i = 0; i != Array.size(); ++i) { // 构建双向链表 p = new node; p->data = Array[i]; p->next = NULL; p->pre = pre; pre->next = p; pre = p; } return head; } int main() { int num; string s1, s2, s3; vector chain; node *head = new node; node *n = new node; while (getline(cin, s1)) { getline(cin, s2); istringstream ss1(s1); while (ss1 >> s3) { chain.push_back(atoi(s3.c_str())); } num = atoi(s2.c_str()); node *L = create(chain); head = L; L = L->next; if (num data) { n->data = num; n->next = L; head->next = n; n->pre = head; L->pre = n; } else if (num >= L->data){ while (L->next != NULL) { if (num >= L->data) { L = L->next; } else { n->data = num; n->next = L; L->pre->next = n; n->pre = L->pre; L->pre = n; break; } } if (L->next == nullptr && num >= L->data) { n->data = num; n->next = nullptr; n->pre = L; L->next = n; } else if (L->next == nullptr && num data) { n->data = num; n->next = L; L->pre->next = n; n->pre = L->pre; L->pre = n; } } printChain(head->next); chain.clear(); } return 0; }
全部评论
(2) 回帖