-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest1.c
More file actions
102 lines (81 loc) · 1.93 KB
/
test1.c
File metadata and controls
102 lines (81 loc) · 1.93 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
#include<stdio.h>
#include<stdlib.h>
// Structure of node.
struct Node
{
int data;
struct Node *link;
};
// deleting the duplicate nodes.
void remove_duplicate(struct Node *head)
{
struct Node *current = head,*index = NULL,*temp = NULL;
if(head == NULL)
return;
else
{
while(current != NULL) // do till the linked list is empty.
{
temp = current;
index = current -> link;
while(index != NULL)
{
if(current -> data == index -> data)
temp -> link = index -> link;
else
temp = index;
index = index -> link;
}
current = current -> link;
}
}
}
// inserting the nodes at end.
void append(struct Node** head_ref, int new_data)
{
/*New-Node Creation*/
struct Node* new_node = (struct Node*)malloc(sizeof (struct Node));
/*Adding Data to the New Node*/
new_node -> data = new_data;
/*The address section of the new_node is set to null*/
new_node -> link = NULL;
/*If the list is empty, th new_node is declared as the new_node*/
if(*head_ref == NULL)
{
*head_ref = new_node;
return;
}
/*Else traverse from the head to the end and add the new_node*/
struct Node* last = *head_ref;
while(last -> link != NULL)
last = last -> link;
last -> link = new_node;
}
// Displaying the list.
void show(struct Node* node)
{
if(node == NULL)
printf("Empty!\n");
else {
while (node != NULL) {
printf("%d ", node->data);
node = node->link;
}
printf("\n");
}
}
int main()
{
struct Node *head = NULL;
int n,num;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&num);
append(&head, num);
}
remove_duplicate(head);
show(head);
free(head);
return 0;
}