-
Notifications
You must be signed in to change notification settings - Fork 0
/
duplicateElements.c
73 lines (65 loc) · 1.61 KB
/
duplicateElements.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
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node* link;
};
struct node *head = NULL;
void create(){
int elem, n;
printf("Enter The Number Of Nodes: ");
scanf("%d", &n);
for(int i = 0; i < n; i++){
printf("Enter Element %d: ", i);
scanf("%d", &elem);
struct node *new = (struct node*)malloc(sizeof(struct node));
new -> data = elem;
new -> link = head;
head = new;
}
}
void display(){
if (head == NULL)
printf("\nList Empty!");
else{
struct node* ptr = head;
printf("\nLinked List Data: \n");
while(ptr != NULL){
printf("%d --> ", ptr -> data);
ptr = ptr -> link;
}
printf("NULL\n");
}
}
void delDup(){
if (head == NULL)
printf("\nList Empty!");
else{
struct node *ptr1 = head;
struct node *ptr2 = NULL;
struct node *prev = NULL;
while (ptr1 != NULL){
ptr2 = ptr1 -> link;
prev = ptr1;
while (ptr2 != NULL){
if ((ptr2 -> data) == (ptr1 -> data)){
struct node *temp = ptr2;
prev -> link = ptr2 -> link;
ptr2 = ptr2 -> link;
free(temp);
}
else{
prev = ptr2;
ptr2 = ptr2 -> link;
}
}
ptr1 = ptr1 -> link;
}
}
}
void main(){
create();
display();
delDup();
display();
}