Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- struct Elem
- {
- struct Elem *next;
- char *str;
- };
- struct Elem *process_3(struct Elem *head, char *str, int precount) {
- if (head == NULL) {
- head = malloc(sizeof(struct Elem));
- head->next = NULL;
- head->str = (char*)malloc(strlen(str)+1);
- strcpy(head->str, str);
- return head;
- }//gotovo
- struct Elem *tmp = head;
- struct Elem *last = NULL;
- int count = 0;
- while (tmp && strcmp(tmp->str, str) < 0) {
- ++count;
- last = tmp;
- tmp = tmp->next;
- }
- if (tmp && strcmp(tmp->str, str) == 0)
- return head;
- struct Elem *new = malloc(sizeof(struct Elem));
- new->str = malloc(sizeof(char*) * (strlen(str) + 1));
- new->next = tmp;
- strcpy(new->str, str);
- if (last) {
- last->next = new;
- }
- else {
- head = new;
- }
- int i = 0;
- tmp = head;
- last = NULL;
- while(i < count - precount) {
- last = tmp;
- tmp = tmp->next;
- ++i;
- }
- while(i < count) {
- struct Elem* next = tmp->next;
- free(tmp->str);
- free(tmp);
- if (last) {
- last->next = next;
- }
- else {
- head = next;
- }
- tmp = next;
- ++i;
- }
- return head;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement