-
Notifications
You must be signed in to change notification settings - Fork 91
/
CompareLinkedList.c
153 lines (122 loc) · 2.56 KB
/
CompareLinkedList.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
// C program to compare two linked lists
#include <stdio.h>
#include <stdlib.h>
//Self-referential structure to create node.
typedef struct tmp {
int item;
struct tmp* next;
} Node;
//structure for create linked list.
typedef struct
{
Node* head;
Node* tail;
} List;
//Initialize List
void initList(List* lp)
{
lp->head = NULL;
lp->tail = NULL;
}
//Create node and return reference of it.
Node* createNode(int item)
{
Node* nNode;
nNode = (Node*)malloc(sizeof(Node));
nNode->item = item;
nNode->next = NULL;
return nNode;
}
//Add new item at the end of list.
void addAtTail(List* lp, int item)
{
Node* node;
node = createNode(item);
//if list is empty.
if (lp->head == NULL) {
lp->head = node;
lp->tail = node;
}
else {
lp->tail->next = node;
lp->tail = lp->tail->next;
}
}
//Add new item at begning of the list.
void addAtHead(List* lp, int item)
{
Node* node;
node = createNode(item);
//if list is empty.
if (lp->head == NULL) {
lp->head = node;
lp->tail = node;
}
else {
node->next = lp->head;
lp->head = node;
}
}
//To print list from start to end of the list.
void printList(List* lp)
{
Node* node;
if (lp->head == NULL) {
printf("\nEmpty List");
return;
}
node = lp->head;
while (node != NULL) {
printf("| %05d |", node->item);
node = node->next;
if (node != NULL)
printf("--->");
}
printf("\n\n");
}
int compareLists(List* lp1, List* lp2)
{
Node* temp1;
Node* temp2;
int flag = 1;
temp1 = lp1->head;
temp2 = lp2->head;
while (temp1 != NULL && temp2 != NULL) {
if (temp1->item != temp2->item) {
flag = 0;
break;
}
temp1 = temp1->next;
temp2 = temp2->next;
}
return flag;
}
//Main function to execute program.
int main()
{
List* lp1;
List* lp2;
lp1 = (List*)malloc(sizeof(List));
lp2 = (List*)malloc(sizeof(List));
initList(lp1);
initList(lp2);
addAtTail(lp1, 100);
addAtTail(lp1, 200);
addAtHead(lp1, 300);
addAtHead(lp1, 400);
addAtHead(lp1, 500);
addAtTail(lp2, 100);
addAtTail(lp2, 200);
addAtHead(lp2, 300);
addAtHead(lp2, 400);
addAtHead(lp2, 500);
printf("List1:\n");
printList(lp1);
printf("List2:\n");
printList(lp2);
if (compareLists(lp1, lp2))
printf("Both lists are same\n");
else
printf("Lists are different\n");
return 0;
}