Hello,
I got Runtime Error on the problem 101. I read some posts related to the topics on this board and try to make the changes that they suggest and run the inputs i found there, it works fine on my computer but when uploaded on UVa, i got Runtime Error (8 times).
This is the email i receive
Hi,
This is an automated response from UVa Online Judge.
Your submission with number 8180799 for the problem 101 - The Blocks Problem has failed with verdict Runtime error.
This means that the execution of your program didn't finish properly. Remember to always terminate your code with the exit code 0.
Best regards,
The UVa Online Judge team
That's my code
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#define N 25
#define NIL -1
typedef struct node
{
int data;
struct node *next;
} list;
void init(list **l)
{
*l = NULL;
struct node *sentinel = malloc(sizeof(struct node));
sentinel->data = NIL;
sentinel->next = *l;
*l = sentinel;
}
void push(int data, struct node **n)
{
struct node *new = malloc(sizeof(struct node));
new->data = data;
new->next = *n;
*n = new;
}
void insert(int data, list **l)
{
struct node *temp = *l;
push(data, &(temp->next));
}
void print(list *l)
{
struct node *temp = l;
while((temp = temp->next) != NULL)
printf(" %d", temp->data);
printf("\n");
}
void free_list(list **l)
{
struct node *temp = *l, *next;
while(temp != NULL)
{
next = temp->next;
free(temp);
temp = next;
}
*l = NULL;
}
/* Returns the index in "array" of the list where "data" is stored and assign "ptr" to the node preceding "data" */
int search(struct node **ptr, int data, list *array[])
{
int i = 0;
int found = 0;
while(i < N && !found)
{
struct node *temp = array[i];
while(temp != NULL)
{
if(temp->data == data)
{
found = 1;
break;
}
*ptr = temp;
temp = temp->next;
}
i++;
}
return i - 1;
}
void move_onto(int a, int b, list *array[])
{
struct node *pa = NULL, *pb = NULL;
struct node *tempa = NULL, *tempb = NULL;
int i = search(&pa, a, array);
int j = search(&pb, b, array);
if(i != j) // If i == j then a and b are stored in the same list
{
struct node *temp = NULL;
tempa = pa->next;
tempb = pb->next;
temp = tempb->next;
pa->next = tempa->next;
tempb->next = tempa;
tempa->next = temp;
}
}
void move_over(int a, int b, list *array[])
{
struct node *pa = NULL, *pb = NULL;
struct node *tempa = NULL;
int i = search(&pa, a, array);
int j = search(&pb, b, array);
if(i != j)
{
struct node *temp = array[j];
tempa = pa->next;
pa->next = tempa->next;
while(temp->next != NULL)
temp = temp->next;
temp->next = tempa;
tempa->next = NULL;
}
}
void pile_onto(int a, int b, list *array[])
{
struct node *pa = NULL, *pb = NULL;
struct node *tempa = NULL, *tempb = NULL;
int i = search(&pa, a, array);
int j = search(&pb, b, array);
if(i != j)
{
struct node *temp = NULL;
tempa = pa->next;
tempb = pb->next;
temp = tempb->next;
pa->next = NULL;
tempb->next = tempa;
}
}
void pile_over(int a, int b, list *array[])
{
struct node *pa = NULL, *pb = NULL;
struct node *tempa = NULL;
int i = search(&pa, a, array);
int j = search(&pb, b, array);
if(i != j)
{
struct node *temp = array[j];
tempa = pa->next;
pa->next = NULL;
while(temp->next != NULL)
temp = temp->next;
temp->next = tempa;
}
}
int main()
{
list *array[N];
int i = 0;
int n = 0;
char cmd[4], param[4];
int a = 0, b = 0;
scanf("%d", &n);
for(i = 0; i < n; ++i)
{
init(&array[i]);
insert(i, &array[i]);
}
while(scanf("%s", cmd) && cmd[0] != 'q')
{
scanf("%d", &a);
scanf("%s", param);
scanf("%d", &b);
if(a < n && b < n)
{
if(cmd[0] == 'm') // move
{
if(param[1] == 'n') // onto
move_onto(a, b, array);
else if(param[1] == 'v') // over
move_over(a, b, array);
}
else if(cmd[0] == 'p') // pile
{
if(param[1] == 'n')
pile_onto(a, b, array);
else if(param[1] == 'v')
pile_over(a, b, array);
}
}
}
for(i = 0; i < n; ++i)
{
printf("%d:", i);
print(array[i]);
}
for(i = 0; i < n; ++i)
free_list(&array[i]);
return 0;
}
Two inputs on which i test my program and their outputs
Code: Select all
INPUT
24
pile 0 over 23
pile 1 over 23
pile 2 over 23
pile 3 over 23
pile 4 over 23
pile 5 over 23
pile 6 over 23
pile 7 over 23
pile 8 over 23
pile 9 over 23
pile 10 over 23
pile 11 over 23
pile 12 over 23
pile 13 over 23
pile 14 over 23
pile 15 over 23
pile 16 over 23
pile 17 over 23
pile 18 over 23
pile 19 over 23
pile 20 over 23
pile 21 over 23
pile 22 over 23
pile 23 over 23
pile 23 onto 0
quit
OUTPUT
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23: 23 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
INPUT
21
move 2 onto 1
move 3 onto 2
move 4 onto 3
move 5 over 1
pile 1 over 10
move 9 over 8
move 11 over 8
pile 3 over 8
pile 8 over 3
move 20 over 19
pile 19 over 18
pile 18 onto 15
move 15 over 3
pile 20 onto 19
pile 19 onto 18
pile 18 over 17
quit
OUTPUT
0: 0
1:
2:
3:
4:
5:
6: 6
7: 7
8: 8 9 11 3 4 5 15
9:
10: 10 1 2
11:
12: 12
13: 13
14: 14
15:
16: 16
17: 17 18 19 20
18:
19:
20:
Please, help me find the error in my code.
P. S. Sorry if my english is not very well, i'm french