Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- struct Node {
- double data;
- Node* next;
- };
- Node* toCur(Node* first, Node* cur) {
- while (first->next != cur) {
- first = first->next;
- }
- return first;
- }
- Node* addNode(Node* first, double data) {
- if (first == NULL) {
- first = new Node;
- first->data = data;
- first->next = NULL;
- }
- else {
- Node* tmp = new Node;
- tmp->data = data;
- tmp->next = NULL;
- toCur(first, NULL)->next = tmp;
- }
- return first;
- }
- Node* delNode(Node* first, Node* cur) {
- if (cur == first) {
- first = cur->next;
- delete cur;
- }
- else if (cur == toCur(first, NULL)) {
- toCur(first, cur)->next = NULL;
- delete cur;
- }
- else {
- toCur(first, cur)->next = cur->next;
- delete cur;
- }
- return first;
- }
- Node* sortInsert(Node* first, double data) {
- if (first == NULL) {
- first = addNode(first, data);
- }
- else {
- Node* cur = first;
- while ((cur->data > data) && (cur->next != NULL)) {
- cur = cur->next;
- }
- if (cur->data > data) {
- first = addNode(first, data);
- }
- else if (cur == first) {
- Node *tmp = new Node;
- tmp->data = data;
- tmp->next = cur;
- first = tmp;
- }
- else {
- Node *tmp = new Node;
- tmp->data = data;
- tmp->next = cur;
- toCur(first, cur)->next = tmp;
- }
- }
- return first;
- }
- Node* listFromFile(FILE* f, Node* first) {
- char str[127];
- fgets(str, 127, f);
- puts(str);
- char* ptr = strtok(str, " ");
- while (ptr != NULL) {
- first = sortInsert(first, atof(ptr));
- ptr = strtok(NULL, " ");
- }
- return first;
- }
- Node* delLess(Node* first, int num) {
- Node* cur = first;
- while (cur->data < num) {
- cur = delNode(first, cur);
- first = cur;
- }
- while (cur != NULL) {
- if (cur->data < num) {
- cur = delNode(first, cur);
- first = cur;
- }
- cur = cur->next;
- }
- return first;
- }
- void printList(Node* first) {
- while (first != NULL) {
- printf("%lf ", first->data);
- first = first->next;
- }
- }
- void delList(Node*& first) {
- while (first != NULL) {
- Node* tmp = first;
- first = first->next;
- delete tmp;
- }
- }
- int main() {
- FILE* f;
- f = fopen(R"(C:\Clion Projects\lists\test.txt)", "r");
- Node* first = NULL;
- first = listFromFile(f, first);
- fclose(f);
- first = delLess(first, 2);
- printList(first);
- delList(first);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement