Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <assert.h>
- typedef struct Node
- {
- char value;
- struct Node* next;
- } node;
- node* create_node(char data)
- {
- node* temp = (node*)malloc(sizeof(node));
- temp->value = data;
- temp->next = NULL;
- return temp;
- }
- void insert(node** head, char value)
- {
- node* link = create_node(value);
- if (*head == NULL)
- {
- *head = link;
- return;
- }
- node* temp = *head;
- while (temp->next != NULL)
- {
- temp = temp->next;
- }
- temp->next = link;
- }
- void print_list(node* head)
- {
- node* temp = head;
- if (head == NULL)
- {
- printf("List is empty.\n");
- return;
- }
- while (temp != NULL)
- {
- printf("%c-> ", temp->value);
- temp = temp->next;
- }
- printf("\n");
- }
- void delete_after(node* temp)
- {
- if (temp == NULL || temp->next == NULL)
- return;
- node* delete_node = temp->next;
- temp->next = temp->next->next;
- free(delete_node);
- }
- void keep_one(node** head, char c)
- {
- node* temp = *head;
- int counter = 0;
- if (*head == NULL) return;
- if ((*head)->value == c)
- counter++;
- while (temp->next != NULL)
- {
- if (temp->next->value == c)
- {
- if (counter < 1)
- {
- counter++;
- }
- else
- {
- delete_after(temp);
- counter++;
- }
- }
- if (temp->next)
- {
- if (temp->next->value == c)
- {
- if (counter <= 1)
- temp = temp->next;
- }
- else
- temp = temp->next;
- }
- }
- }
- void removeDupList(node** head)
- {
- node* temp = *head;
- while (temp != NULL)
- {
- keep_one(&(*head), temp->value);
- temp = temp->next;
- }
- }
- void removeDupArrayList(node** arr, int size)
- {
- for (int i = 0; i < size; i++)
- {
- removeDupList(&(arr[i]));
- }
- }
- void main()
- {
- node* head = NULL;
- node* head1 = NULL;
- node* head2 = NULL;
- node* head3 = NULL;
- insert(&head, 'a');
- insert(&head, 'c');
- insert(&head, 'a');
- insert(&head, 'b');
- insert(&head, 'b');
- insert(&head, 'a');
- insert(&head1, 'c');
- insert(&head1, 'd');
- insert(&head1, 'c');
- insert(&head2, 'a');
- insert(&head3, 'e');
- insert(&head3, 'e');
- insert(&head3, 'e');
- printf("\n\------Old Lists ------\n\n");
- print_list(head);
- print_list(head1);
- print_list(head2);
- print_list(head3);
- node* arr[4] = { head, head1, head2, head3 };
- removeDupArrayList(arr, 4);
- printf("\n\n------ New Lists ------\n\n");
- print_list(head);
- print_list(head1);
- print_list(head2);
- print_list(head3);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement