-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.c
183 lines (155 loc) · 3.38 KB
/
LinkedList.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <stdio.h>
#include <stdlib.h>
typedef struct node* List;
typedef List Position;
List MakeEmpty(List L);
int IsEmpty(List L);
int IsLast(List P,List L);
List Find(int X,List L);
List FindPrevious(int X,List L);
void Delete(int X,List L);
void Insert(int X,List L,List P);
void PrintList(List L);
void DeleteList(List L);
int size (List L);
int compare (List L1,List L2);
int compare1 (List L1,List L2);
struct node {
int Data;
List Next ;
};
int main(){
List L1=MakeEmpty(NULL);
List L2=MakeEmpty(NULL);
Insert(11,L1,L1);
Insert(2,L1,L1);
Insert(1,L1,L1);
PrintList(L1);
Insert(33,L2,L2);
Insert(31,L2,L2);
Insert(11,L2,L2);
PrintList(L2);
printf("%d", compare(L1,L2));
return 7;
}
int compare (List L1,List L2){
List ptr = L2;
while (L1->Next != NULL){
L1=L1->Next;
while(L2->Next != NULL){
L2=L2->Next;
if (L1->Data == L2->Data)
return 0;
}
L2=ptr;
}
return 1;
}
int compare1 (List L1,List L2){
if (L1->Next == NULL || L2->Next == NULL)
return 1 ;
else if (L1->Next->Data == L2->Next->Data)
return 0 ;
else if (L1->Next->Data < L2->Next->Data)
return compare1(L1->Next,L2);
else
return compare1(L1,L2->Next);
}
int size (List L){
Position ptr = L->Next;
int count=0 ;
while (ptr != NULL){
count++;
ptr=ptr->Next;
}
return count;
}
int IsEmpty(List L){
return L->Next == NULL;
}
int IsLast(List P,List L){
return P->Next == NULL;
}
Position FindPrevious(int X,List L){
Position P;
P = L;
while(P->Next != NULL && P->Next->Data != X)
P = P->Next;
return P;
}
Position Find(int X,List L){
Position P;
P = L->Next;
while(P != NULL && P->Data != X)
P = P->Next;
return P;
}
void Delete(int X,List L){
Position P, temp;
P = FindPrevious(X, L);
if( !IsLast(P, L) ){
temp = P->Next;
P->Next = temp->Next;
free(temp);
}
}
void Insert(int X,List L,List P){
Position temp;
temp = (Position)malloc(sizeof(struct node));
temp->Data = X;
temp->Next = P->Next;
P->Next = temp;
}
void PrintList(List L){
Position P = L;
if( IsEmpty(L))
printf("Empty list\n");
else
do{
P=P->Next;
printf("%d\t", P->Data);
}while( !IsLast(P, L) );
printf("\n");
}
void DeleteList(List L){
Position P, temp;
P = L->Next;
L->Next = NULL;
while(P != NULL){
temp = P->Next;
free(P);
P=temp;
}
}
List MakeEmpty(List L){
if(L != NULL)
DeleteList( L );
L = (List)malloc(sizeof(struct node));
if(L == NULL)
printf("Out of memory!\n");
L->Next = NULL;
return L;
}
/*
void reverse(List L) {
Position current = L->Next;
Position temp = NULL;
int flag = 1 ;
if (current == NULL || current->Next == NULL)
return ;
while (current != NULL) {
temp = current->Prev;
current->Prev = current->Next;
current->Next = temp;
if (flag){
flag--;
current->Next = NULL ;
}
current = current->Prev;
}
if (temp != NULL) {
L->Next = temp->Prev;
L->Next->Prev = L ;
}
}
*/