Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <windows.h>
- #include <string.h>
- struct list
- {
- char* string= new char[128];
- list *next;
- list *prev;
- };
- FILE fill(FILE* file)
- {
- rewind(file);
- char* string;
- string = new char[128];
- gets_s(string, 128);
- while (gets_s(string, 128), strlen(string) != 0)
- {
- fputs(string, file);
- fputs("\n", file);
- }
- delete[] string;
- return (*file);
- }
- FILE show(FILE* file)
- {
- rewind(file);
- char* string;
- string = new char[128];
- while (fgets(string, 128, file) > 0)
- puts(string);
- delete[] string;
- return(*file);
- }
- void listprint(list* first)
- {
- while (first != NULL)
- {
- puts(first->string);
- first = first->next;
- }
- }
- void list_delete(list *& head)
- {
- list* q = head;
- while (head != NULL)
- {
- head = head->next;
- delete q;
- q = head;
- }
- }
- void fromfiletolist(FILE*file,list*&head,list*&tail)
- {
- char* strfile;
- strfile = new char[128];
- rewind(file);
- list * now,*p;
- head = new list;
- fgets(strfile, 128, file);
- strcpy(head->string, strfile);
- head->next = NULL; // указатель на следующий узел
- head->prev = NULL; // указатель на предыдущий узел
- p = head;
- while (fgets(strfile, 128, file) > 0)
- {
- now = new list;
- strcpy(now->string, strfile);
- p->next = now;
- now->prev = p;
- p = p->next;
- p->next = NULL;
- }
- tail = p;
- }
- void sort(FILE* file, list*& head, list*& tail)
- {
- rewind(file);
- int fl;
- list* n,*p,*q;
- char* strfile;
- strfile = new char[128];
- while (fgets(strfile, 128, file) > 0)
- {
- if (head == NULL) //формирование
- {
- head = new list;
- strcpy(head->string, strfile);
- head->next = NULL;
- head->prev = NULL;
- tail = head;
- }
- else
- {
- n = new list;
- strcpy(n->string, strfile);
- if (strlen(n->string) > strlen(head->string)) //вставка в начало списка
- {
- n->next = head;
- head->prev = n;
- head = n;
- }
- else
- {
- fl = 0;
- p = head;
- q = head;
- while ((q = q->next) != NULL && !fl)
- {
- if (strlen(n->string) > strlen(q->string))//вставка n перед q
- {
- n->next = p->next;
- q->prev = n->prev;
- p->next = n;
- q->prev = n;
- fl = 1;
- }
- else p = q; //н после q
- }
- if (!fl)
- {
- n->next = NULL;
- p->next = n;
- n->prev = n;
- tail = n;
- }
- }
- }
- }
- }
- void task(list*& head, list*& tail)
- {
- list* q;
- q = head->next;
- delete head;
- head = q;
- head->prev = NULL;
- q = tail->prev;
- delete tail;
- tail = q;
- tail->next = NULL;
- }
- int main()
- {
- FILE* file;
- char* filename;
- filename = new char[30];
- puts("Input filename");
- gets_s(filename, 30);
- fopen_s(&file, filename, "r+");
- if (file == NULL)
- {
- int f;
- puts("file not found. Create new? 1-yes 0-no");
- scanf_s("%d", &f);
- if (f == 0)
- return(0);
- else
- {
- fopen_s(&file, filename, "w+");
- puts("input strings into the file");
- fill(file);
- }
- }
- puts("=====content of the file=====");
- show(file);//файл сформирован или прочитан
- list* tail=NULL;
- list* head = NULL;
- //fromfiletolist(file,head,tail);
- sort(file, head, tail);
- puts("=====formed and sorted list =====");
- listprint(head);
- task(head, tail);
- puts("=====new list =====");
- listprint(head);
- list_delete(head);
- puts("=====memory clear =====");
- listprint(head);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement