-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedListRemoveDuplicates.c
More file actions
159 lines (144 loc) · 3.68 KB
/
Copy pathLinkedListRemoveDuplicates.c
File metadata and controls
159 lines (144 loc) · 3.68 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct ListData{
int data;
struct ListData* next;
};
typedef struct ListData ListData;
struct ListNode{
int length;
ListData* data;
struct ListNode* next;
};
typedef struct ListNode ListNode;
ListData* createDataNode(int data){
ListData* newNode=(ListData*)malloc(sizeof(ListData));
newNode->data=data;
newNode->next=NULL;
return newNode;
}
ListNode* createListNode(){
ListNode* newNode=(ListNode*)malloc(sizeof(ListNode));
newNode->length=0;
newNode->data=NULL;
newNode->next=NULL;
return newNode;
}
void freeDataNode(ListData* node){
ListData* next=node->next;
while(next!=NULL){
free(node);
node=next;
next=next->next;
}
free(node);
}
void freeListNode(ListNode* node){
freeDataNode(node->data);
free(node);
}
ListNode* input(){
ListNode* newListNode=createListNode();
ListData* newDataNode=createDataNode(0);
ListData* cur=newDataNode;
char input_char;
input_char=getchar();
while(input_char!='\n'){
if(input_char<'0' || input_char>'9'){
while(input_char!='\n') input_char=getchar();
freeDataNode(newDataNode);
free(newListNode);
return NULL;
}else{
cur->next=createDataNode(input_char-'0');
cur=cur->next;
newListNode->length++;
}
input_char=getchar();
}
newListNode->data=newDataNode->next;
free(newDataNode);
return newListNode;
}
void print(ListNode* node){
ListData* dataNode=node->data;
while(dataNode!=NULL){
printf("%d",dataNode->data);
dataNode=dataNode->next;
}
printf("\n");
}
void printListNode(ListNode* head){
ListNode* cur=head->next;
while(cur!=NULL){
print(cur);
cur=cur->next;
}
}
int compare(ListNode* node1,ListNode* node2){
if(node1->length!=node2->length) return 0;
ListData* cur1=node1->data;
ListData* cur2=node2->data;
while(cur1!=NULL && cur2!=NULL){
if(cur1->data!=cur2->data) return 0;
cur1=cur1->next;
cur2=cur2->next;
}
return 1;
}
int insert(ListNode* head,ListNode* nowNode){
ListNode* cur=head->next;
ListNode* pre=head;
while(cur!=NULL){
if(compare(cur,nowNode)==1) return 0;
cur=cur->next;
pre=pre->next;
}
pre->next=nowNode;
return 1;
}
void clearScreen() {
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
void printInstruction(){
printf("C Language Linked List Deduplication\n\n1. Print Linked List\n2. Store New Data\n3. Exit Program\n\nYour Operation: ");
}
void waitToReturn(){
char temp;
temp=getchar();
while(temp!='\n') temp=getchar();
}
int main(){
ListNode* head=createListNode();
while(1){
clearScreen();
printInstruction();
int type=0;
scanf("%d",&type);
getchar();
clearScreen();
if(type==1){
printListNode(head);
printf("Output complete, press Enter to continue...");
waitToReturn();
}else if(type==2){
printf("Input the data to insert, press Enter to finish.\n");
ListNode* newListNode=input();
if(newListNode==NULL)
printf("Please enter pure numbers, press Enter to continue...");
else if(insert(head,newListNode)==1)
printf("Successfully inserted, press Enter to continue...");
else
printf("Insertion failed due to a duplicate node, press Enter to continue...");
waitToReturn();
}else if(type==3){
return 0;
}
}
return 0;
}