-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeleteDuplicateLinkList.c
More file actions
113 lines (106 loc) · 1.86 KB
/
DeleteDuplicateLinkList.c
File metadata and controls
113 lines (106 loc) · 1.86 KB
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
//Write a function that will delete all the duplicate elements of a linked list.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
} *head = NULL, *tail = NULL;
void create(struct node **head, int n)
{
for (int i = 0; i < n; i++)
{
struct node *cur;
cur = malloc(sizeof(struct node));
cur->data = rand() % 100;
cur->next = NULL;
if (*head == NULL)
{
*head = tail = cur;
}
else
{
tail->next = cur;
tail = cur;
}
}
}
void insert(struct node **head, int n, int a)
{
struct node *cur;
struct node *ptr = *head;
cur = malloc(sizeof(struct node));
cur->data = n;
cur->next = NULL;
if (*head == NULL || a == 0)
{
cur->next = *head;
*head = cur;
}
else
{
int i = 1;
while (ptr != NULL && i < a)
{
i++;
ptr = ptr->next;
}
if (ptr != NULL)
{
cur->next = ptr->next;
ptr->next = cur;
}
}
}
void remdup(struct node **head)
{
struct node *ptr, *qtr, *dup;
ptr = *head;
while (ptr != NULL && ptr->next != NULL)
{
qtr = ptr;
while (qtr->next != NULL)
{
if (ptr->data == qtr->next->data)
{
dup = qtr->next;
qtr->next = qtr->next->next;
free(dup);
}
else
qtr = qtr->next;
}
ptr = ptr->next;
}
}
void display(struct node *p)
{
if (p == NULL)
{
printf("\nThe List Is Empty :(");
}
printf("\n");
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
}
int main()
{
int n;
printf("Enter the length of the list: ");
scanf("%d", &n);
create(&head, n);
printf("List before insertion:");
display(head);
printf("\nList after insertion:");
insert(&head, 34, 3);
insert(&head, 57, 0);
insert(&head, 24, 2);
display(head);
printf("\nList after deletion:");
remdup(&head);
display(head);
return 0;
}