/
ll1.c
146 lines (119 loc) · 2.56 KB
/
ll1.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include<stdio.h>
#include<stdlib.h>
struct s {
int item;
int flag;
struct s * next;
};
typedef struct s node;
typedef node * nodeptr;
void push(nodeptr *,int,int);
nodeptr get_duplicates(nodeptr *);
void free_pos(nodeptr *,int);
void remove_duplicates(nodeptr *);
void printlist(nodeptr *);
nodeptr nptr=NULL;
nodeptr np=NULL;
int main(void)
{
push(&np,13,0);
push(&np,21,0);
push(&np,36,0);
push(&np,28,0);
push(&np,21,0);
push(&np,89,0);
push(&np,21,0);
push(&np,45,0);
push(&np,89,0);
printlist(&np);
nodeptr n=get_duplicates(&np);
remove_duplicates(&n);
printf("the new list without duplicates is\n");
printlist(&np);
return 0;
}
void push(nodeptr *n,int value,int f)
{
nodeptr temp=*n;
nodeptr temp1=(node *)malloc(sizeof(node));
temp1->item=value;
temp1->flag=f;
temp1->next=NULL;
if(temp==NULL)
{
*n=temp1;
}
else
{
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=temp1;
}
}
void printlist(nodeptr *n)
{
nodeptr temp=*n;
while(temp!=NULL)
{
printf("element is %d\n",temp->item);
temp=temp->next;
}
}
nodeptr get_duplicates(nodeptr *n)
{
nodeptr temp=*n;
nodeptr temp1;
int j;
int i=1;
while(temp->next!=NULL)
{
temp1=temp->next;
j=i+1;
while(temp1!=NULL)
{
if(temp1->item==temp->item && temp1->flag==0)
{
//printf("\ntemp is %d and temp1 is %d",temp->item,temp1->item);
//printf("value of j is %d",j);
push(&nptr,j,0);
temp1->flag=1;
}
//printf("\nvalue of j is %d",j);
temp1=temp1->next;
j++;
}
temp=temp->next;
//printf("\nvalue of i is %d",i);
i++;
//printf("\nthe temp values are changing to %d",temp->item);
}
//printf("\nduplicates are \n");
//printlist(&nptr);
return nptr;
}
void free_pos(nodeptr * nd,int pos)
{
nodeptr tmp=*nd;
int i=1;
while(i<pos-1)
{
tmp=tmp->next;
i++;
}
nodeptr t=tmp->next;
tmp->next=tmp->next->next;
free(t);
}
void remove_duplicates(nodeptr *n)
{
nodeptr temp=*n;
int i=0;
while(temp!=NULL)
{
free_pos(&np,temp->item-i);
i++;
temp=temp->next;
}
}