Created
July 18, 2017 18:33
-
-
Save ritikamotwani/b8200c13bef28bf32d378e97a2752cf5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
struct listnode | |
{ | |
int val; | |
struct listnode *next; | |
}; | |
struct listnode *insert(struct listnode *start, int n) | |
{ | |
struct listnode *tmp; | |
tmp = (struct listnode *)malloc(sizeof(struct listnode)); | |
tmp->val = n; | |
struct listnode *p = start; | |
if(start == NULL) | |
{ | |
start = tmp; | |
tmp->next = NULL; | |
return start; | |
} | |
while(p->next!=NULL) | |
{ | |
p = p->next; | |
} | |
p->next = tmp; | |
tmp->next = NULL; | |
return start; | |
} | |
void display(struct listnode *start) | |
{ | |
struct listnode *p = start; | |
while(p != NULL) | |
{ | |
printf("%d\n",p->val); | |
p = p->next; | |
} | |
} | |
struct listnode *delet(struct listnode *start, int n) | |
{ | |
struct listnode *p = start; | |
if(start->val == n) | |
{ | |
start = start->next; | |
return start; | |
} | |
else | |
{ | |
while(p->next->val != n) | |
p=p->next; | |
struct listnode *tmp; | |
tmp = p->next; | |
p->next = tmp->next; | |
free(tmp); | |
return start; | |
} | |
} | |
struct listnode *deleteDuplicates(struct listnode *start) | |
{ | |
int ar[10000]; | |
int i; | |
memset(ar,0,sizeof(ar)); | |
struct listnode *p = start; | |
while(p != NULL) | |
{ | |
if(ar[p->val] == 0) | |
ar[p->val]++; | |
else | |
{ | |
start = delet(start, p->val); | |
} | |
p = p->next; | |
} | |
return start; | |
} | |
int main() | |
{ | |
int n; | |
scanf("%d",&n); | |
int g = n; | |
struct listnode *start=NULL; | |
while(g--) | |
{ | |
int val; | |
scanf("%d",&val); | |
start = insert(start,val); | |
} | |
display(start); | |
start = deleteDuplicates(start); | |
display(start); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment