Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <inttypes.h>
- #include <ctype.h>
- #include <stdbool.h>
- struct Elem
- {
- struct Elem *next;
- char *str;
- };
- struct Elem *process_3(struct Elem *head, char *str, int precount) {
- struct Elem *h = head;
- struct Elem *last = NULL;
- bool flag = false;
- int cnt = 0;
- int id = 0;
- struct Elem *cur;
- while (h != NULL) {
- if (strcmp(h->str, str) == 0) {
- return head;
- } else if (strcmp(h->str, str) > 0) {
- id = cnt;
- flag = true;
- cur = malloc(sizeof(struct Elem));
- cur->str = calloc((strlen(str) + 1), sizeof(char));
- strcpy(cur->str, str);
- cur->next = h;
- if (last != NULL) {
- last->next = cur;
- }
- break;
- }
- last = h;
- h = h->next;
- ++cnt;
- }
- if (!flag) {
- id = cnt;
- cur = malloc(sizeof(struct Elem));
- cur->str = calloc((strlen(str) + 1), sizeof(char));
- strcpy(cur->str, str);
- cur->next = NULL;
- if (last != NULL) {
- last->next = cur;
- }
- }
- if (id < precount) {
- precount = id;
- }
- // printf("precount = %d\n", precount);
- // printf("id = %d\n", id);
- if (id == 0) {
- head = cur;
- }
- if (precount == 0) {
- return head; // ничего не надо удалять
- }
- struct Elem *g = head;
- int i = 0;
- struct Elem *from = NULL;
- while (g != NULL) {
- if (i == id - precount - 1) {
- from = g;
- // printf("from string = %s\n", from->str);
- g = g->next;
- ++i;
- while (i < id) {
- struct Elem *nxt = g->next;
- // printf("nxt string = %s\n", nxt->str);
- free(g->str);
- free(g);
- g = nxt;
- ++i;
- }
- from->next = cur;
- break;
- }
- ++i;
- g = g->next;
- }
- if (precount == id) {
- return cur;
- } else {
- return head;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement