forked from amritanand-py/cps02
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathO 10 - LL01 - Linked List Insertions.c
154 lines (136 loc) · 3.79 KB
/
O 10 - LL01 - 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
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#define in(x) scanf(" %d", &x);
#define LLN LinkedListNode
typedef struct LinkedListNode LinkedListNode;
struct LinkedListNode {
int val;
struct LinkedListNode* next;
};
//-------------------- body of the code ------------------------
LinkedListNode* insertAtBeginning(LinkedListNode* head, int val) {
LinkedListNode *f = (LinkedListNode *)malloc(sizeof(LinkedListNode));
f->val = val;
f->next = NULL;
if(head == NULL)
return f;
else{
LinkedListNode *a = head;
f->next = head;
return f;
}
}
LinkedListNode* insertAtEnd(LinkedListNode* head, int val) {
LinkedListNode *f = (LinkedListNode *)malloc(sizeof(LinkedListNode));
f->val = val;
f->next = NULL;
if(head == NULL)
return f;
else{
LinkedListNode *a = head;
while(a->next!=NULL)
{
a = a->next;
}
a->next = f;
return head;
}
}
LinkedListNode* insertAtPosition(LinkedListNode* head, int val, int pos) {
if(pos==1)
{
LinkedListNode *o = insertAtBeginning(head, val);
return o;
}
else{
LinkedListNode *f = (LinkedListNode *)malloc(sizeof(LinkedListNode));
f->val = val;
f->next = NULL;
int i = pos-2;
LinkedListNode *cptr = head, *nextPtr = head->next;
while(i--) {
cptr = cptr->next;
nextPtr = nextPtr->next;
}
f->next = nextPtr;
cptr->next = f;
return head;
}
}
//-------------------- tail of the code ------------------------
int rng(int lim) {
if (lim == 0) return 0;
return rand()%lim;
}
int a[1000005], 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) {
for (int i = 1; i <= sz; i++) {
if (head == NULL) return 0;
if (head->val != a[i]) return 0;
head = head->next;
}
if (head != NULL) return 0;
return 1;
}
int main() {
srand(time(NULL));
int t, n,i; 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 (n <= 1000 && !check(head)) {
printf("incorrect insertAtBeginning");
return 0;
}
}
if (type == 1) {
int val = rng(1000);
head = insertAtEnd(head, val);
insertt(val, sz + 1);
if (n <= 1000 && !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 (n <= 1000 && !check(head)) {
printf("incorrect insertAtPosition");
return 0;
}
}
}
if (!check(head)) {
printf("incorrect");
return 0;
}
}
printf("correct");
}