Vous êtes sur la page 1sur 4

#include <stdio.

h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
/* Implementation by TutorialsPoint.com */

void printList();
void saveCoordinates(int x, int y);
void reverseList();
struct node* findLast_RefNode();
void shortenPath();
void followRoute();

struct node {
int x;
int y;
struct node *next;
};

struct node *head = NULL;


struct node *current = NULL;

//display the list


void printList() {
struct node *ptr = head;
printf("[ ");

//start from the beginning


while(ptr != NULL) {
printf("(%d,%d) ",ptr->x,ptr->y);
ptr = ptr->next;
}

printf(" ]\n");
}

//insert link at the first location


void saveCoordinates(int x, int y) {
//create a link
struct node *link = (struct node*) malloc(sizeof(struct node));

link->x = x;
link->y = y;

//point it to old first node


link->next = head;

//point first to new first node


head = link;
}

void reverseLinkedList(struct node** head_ref) {


struct node* prev = NULL;
struct node* current = *head_ref;
struct node* next;

while (current != NULL) {


next = current->next;
current->next = prev;
prev = current;
current = next;
}

*head_ref = prev;
}

void reverseList() {
reverseLinkedList(&head);
}

//find last (1, 1) node


struct node* findLast_RefNode() {

//start from the first link


struct node* current = head;
struct node* lastItem = NULL;
//if list is empty
if(head == NULL) {
return NULL;
}

//navigate through list


while(1) {

if(current->x != 1 && current->y != 1) {


//if it is last node
if(current->next == NULL) {
return NULL;
} else {
//go to next link
current = current->next;
}
}
else if (current->x == 1 && current->y == 1){
lastItem = current;
}
}

//if data found, return the current Link


return lastItem;
}

void shortenPath() {
struct node* current = head->next;
while(current->next != NULL)
{
if (current->x == 1 && current->y ==1)
{
head->next = current->next;
current = current->next;
}
else
{
current = current->next;
}
}
}
void followRoute()
{
struct node* current = head;
struct node* destination = current->next;
int direction = 2;
while (current->next != NULL)
{
if (current->y != destination->y)
{
if (current->y < destination->y && direction == 2)
{
moveOneSquare();
current = destination;
destination = current->next;
continue;

}
else if (current->y < destination->y && direction == 3)
{
turnLeft();
moveOneSquare();
current = destination;
destination = current->next;
direction -= 1;
continue;
}

else if (current->y < destination->y && direction == 1){


turnRight();
moveOneSquare();
current = destination;
destination = current->next;
direction += 1;
continue;
}
}
else if (current->x != destination->x)
{
if (current->x < destination->x && direction != 3)
{
turnRight();
moveOneSquare();
current = destination;
destination = current->next;
direction += 1;
continue;

}
else if (current->x < destination->x && direction == 3){
moveOneSquare();
current = destination;
destination = current->next;
continue;
}
else{
turnLeft();
moveOneSquare();
current = destination;
destination = current->next;
direction = direction - 1;
continue;

}
}
}
}