-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathRemoving_duplicates_from_linked_list.c
113 lines (101 loc) · 2.04 KB
/
Removing_duplicates_from_linked_list.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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;
}
}