Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- struct Elem
- {
- struct Elem *next;
- char *str;
- };
- struct Elem *dup_elem(struct Elem *head);
- int main(void)
- {
- struct Elem *head = calloc(1, sizeof(*head)); // "10" "5x" "alpha" " -03"
- head->str = calloc(100, sizeof(*head->str));
- strcpy(head->str, "10");
- head->next = calloc(1, sizeof(*head)); // 5x" "alpha" " -03"
- head->next->str = calloc(100, sizeof(*head->str));
- strcpy(head->next->str, "5x");
- head->next->next = calloc(1, sizeof(*head)); // "alpha" " -03"
- head->next->next->str = calloc(100, sizeof(*head->str));
- strcpy(head->next->next->str, "alpha");
- head->next->next->next = calloc(1, sizeof(*head)); // " -03"
- head->next->next->next->str = calloc(100, sizeof(*head->str));
- strcpy(head->next->next->next->str, " -03");
- head->next->next->next->next = NULL;
- for (struct Elem *current_elem = head; current_elem; current_elem = current_elem->next;) {
- printf("[%s]%s", current_elem->str, current_elem->next ? " -> " : "\n");
- }
- printf("\n");
- head = dup_elem(head);
- printf("\n");
- for (struct Elem *current_elem = head; current_elem;) {
- printf("[%s]%s", current_elem->str, current_elem->next ? " -> " : "\n");
- struct Elem *tmp = current_elem;
- current_elem = current_elem->next;
- free(tmp->str);
- free(tmp);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement