I have the following doubly linked list struct:
struct coords
{
int x;
int y;
struct coords* previous;
struct coords* next;
};
I have a linked list with the following values, shown here as (x, y):
head tail
(-1, -1) <--> (0, 1) <--> (2, 1) <--> (1, 0) <--> (0, 2) <--> (-1, -1)
Under my implementation, head and tail with always be (-1, -1). I also have newCoords, an size 4 array of coords* with the following elements:
[(0, 2), (2, 2), (1, 3), no value]
newCoords can have anywhere between zero and four assigned elements. I also keep track of the number of nodes in an int called newCoords (which currently has the value of 3). I want to add these nodes to my linked list, between tail and the last non-tail node. For this, I have the following code (print statements removed for clarity):
void insert (struct coords* position, struct coords* newCoord)
{
newCoord->next = position->next;
newCoord->previous = position;
position->next = newCoord;
}
... //here I create the initial linked list
struct coords* newCoords[4]; //4 is the maximum number of new coords that can be added
int numberOfNewCoords = 0;
... //here I fill newCoords, and as I do I increment numberOfNewCoords by 1
if (numberOfNewCoords > 0) //numberOfNewCoords stores the number of coords in newCoords
{
struct coords* temp = tail->previous;
/* add new possible locations to list */
for (int i = 0; i < numberOfNewCoords; i++)
{
insert(temp, newCoords[i]);
temp = temp->next;
}
}
The first two values in newCoords are added just as I expect them to be. However, the last value is not inserted into the linked list. What is inserted where it should be is a node with numbers that change every time I run the program. The list should be
head tail
(-1, -1) <--> (0, 1) <--> (2, 1) <--> (1, 0) <--> (0, 2) <--> (0, 2) <--> (2, 2) <--> (1, 3) <--> (-1, -1)
but instead it's
head tail
(-1, -1) <--> (0, 1) <--> (2, 1) <--> (1, 0) <--> (0, 2) <--> (0, 2) <--> (2, 2) <--> (9765060, 9770824) <--> (-1, -1)