Skip to content

Commit

Permalink
Merge pull request #39 from Arka0101/main
Browse files Browse the repository at this point in the history
Create Removing_duplicates_from_linked_list.c
  • Loading branch information
PrajaktaSathe authored Oct 25, 2021
2 parents b966598 + 909409e commit 314882b
Showing 1 changed file with 113 additions and 0 deletions.
113 changes: 113 additions & 0 deletions Removing_duplicates_from_linked_list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@

#include <stdio.h>
#include <stdlib.h>

struct node
{
int num;
struct node *next;
};

void create(struct node **);
void dup_delete(struct node **);
void release(struct node **);
void display(struct node *);

int main()
{
struct node *p = NULL;
struct node_occur *head = NULL;
int n;

printf("Enter data into the list\n");
create(&p);
printf("Displaying the nodes in the list:\n");
display(p);
printf("Deleting duplicate elements in the list...\n");
dup_delete(&p);
printf("Displaying non-deleted nodes in the list:\n");
display(p);
release(&p);

return 0;
}

void dup_delete(struct node **head)
{
struct node *p, *q, *prev, *temp;

p = q = prev = *head;
q = q->next;
while (p != NULL)
{
while (q != NULL && q->num != p->num)
{
prev = q;
q = q->next;
}
if (q == NULL)
{
p = p->next;
if (p != NULL)
{
q = p->next;
}
}
else if (q->num == p->num)
{
prev->next = q->next;
temp = q;
q = q->next;
free(temp);
}
}
}

void create(struct node **head)
{
int c, ch;
struct node *temp, *rear;

do
{
printf("Enter number: ");
scanf("%d", &c);
temp = (struct node *)malloc(sizeof(struct node));
temp->num = c;
temp->next = NULL;
if (*head == NULL)
{
*head = temp;
}
else
{
rear->next = temp;
}
rear = temp;
printf("Do you wish to continue [1/0]: ");
scanf("%d", &ch);
} while (ch != 0);
printf("\n");
}

void display(struct node *p)
{
while (p != NULL)
{
printf("%d\t", p->num);
p = p->next;
}
printf("\n");
}

void release(struct node **head)
{
struct node *temp = *head;
*head = (*head)->next;
while ((*head) != NULL)
{
free(temp);
temp = *head;
(*head) = (*head)->next;
}
}

0 comments on commit 314882b

Please sign in to comment.