forked from amritanand-py/cps02
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDLL01 - Doubly Linked List Insertions.c
180 lines (167 loc) · 4.03 KB
/
DLL01 - Doubly Linked List Insertions.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
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#define in(x) scanf(" %d", &x);
#define LinkedListNode LinkedListNode
typedef struct LinkedListNode LinkedListNode;
struct LinkedListNode {
int val;
struct LinkedListNode* next;
struct LinkedListNode* prev;
};
//-------------------- body of the code ------------------------
LinkedListNode* insertAtBeginning(LinkedListNode* head, int val) {
struct LinkedListNode* newnode = (struct LinkedListNode*)malloc(sizeof(struct LinkedListNode));
newnode->val=val;
if(head==NULL)
{
newnode->next=NULL;
newnode->prev=NULL;
head=newnode;
}
else
{
newnode->next=head;
newnode->prev=NULL;
head->prev=newnode;
head=newnode;
}
return head;
}
LinkedListNode* insertAtEnd(LinkedListNode* head, int val) {
struct LinkedListNode* newnode=(struct LinkedListNode*)malloc(sizeof(struct LinkedListNode));
newnode->val=val;
if(head==NULL)
{
newnode->next=NULL;
newnode->prev=NULL;
head=newnode;
}
else
{
struct LinkedListNode* temp=head;
while(temp->next!=NULL)
temp=temp->next;
temp->next=newnode;
newnode->prev=temp;
newnode->next=NULL;
}
return head;
}
LinkedListNode* insertAtPosition(LinkedListNode* head, int val, int pos) {
struct LinkedListNode* newnode=(struct LinkedListNode*)malloc(sizeof(struct LinkedListNode));
newnode->val=val;
if(head==NULL&&pos==1)
{
newnode->next=NULL;
newnode->prev=NULL;
head=newnode;
}
else if(pos==1)
{
head = insertAtBeginning(head,val);
}
else
{
struct LinkedListNode* previous = head;
while(pos>2)
{
previous=previous->next;
pos--;
}
if(previous->next==NULL)
head = insertAtEnd(head,val);
else
{
previous->next->prev=newnode;
newnode->next=previous->next;
newnode->prev=previous;
previous->next=newnode;
}
}
return head;
}
//-------------------- tail of the code ------------------------
int rng(int lim) {
if (lim == 0) return 0;
return rand()%lim;
}
int a[1005], sz = 0;
void insertt(int val, int pos) {
if (pos < 0) return;
if (pos > sz + 1) return;
sz += 1;
for (int i = sz; i > pos; i--)
a[i] = a[i - 1];
a[pos] = val;
}
void erasee(int pos) {
if (pos > sz) return;
if (pos < 1) return;
sz--;
for (int i = pos; i <= sz; i++)
a[i] = a[i + 1];
}
int check(LinkedListNode* head) {
if (head == NULL && sz == 0) return 1;
if (head == NULL || sz == 0) return 0;
LinkedListNode* node = head;
for (int i = 1; i <= sz; i++) {
if (node == NULL) return 0;
if (node->val != a[i]) return 0;
node = node->next;
}
if (node != NULL) return 0;
node = head;
while (node->next)
node = node->next;
for (int i = sz; i >= 1; i--) {
if (node == NULL) return 0;
if (node->val != a[i]) return 0;
node = node->prev;
}
if (node != NULL) return 0;
return 1;
}
int main() {
srand(time(NULL));
int t, n; in(t); in(n);
while (t--) {
LinkedListNode* head = NULL;
sz = 0;
for (int i = 0; i < n; i++) {
int type = rng(3);
if (type == 0) {
int val = rng(1000);
head = insertAtBeginning(head, val);
insertt(val, 1);
if (!check(head)) {
printf("incorrect insertAtBeginning");
return 0;
}
}
if (type == 1) {
int val = rng(1000);
head = insertAtEnd(head, val);
insertt(val, sz + 1);
if (!check(head)) {
printf("incorrect insertAtEnd");
return 0;
}
}
if (type == 2) {
int val = rng(1000);
int pos = rng(sz + 1) + 1;
head = insertAtPosition(head, val, pos);
insertt(val, pos);
if (!check(head)) {
printf("incorrect insertAtPosition");
return 0;
}
}
}
}
printf("correct");
}