'Is there a way to make a node of Linkedlist point to a node that isnt next in the list

I am making a snakes and ladders game in C using a LinkedList, I have a square struct that represents squares on the board. I need to add snakes and ladder that when the player cursor lands on them the move the cursor forward or backward depending on if its a snake or a ladder. Is it possible to make a node in a linkedlist point to something other than the next node in the list

    #include<stdio.h>
#include <stdlib.h>
#include <time.h>

//structure for a square of the board 
struct Square{  //structure that respresents squares on the board
    int number;         //its number on the board
   
    struct Square *next;            //pointer to the next node

};
typedef struct Square square;

//this function will create a square
square *create_Square(int num) {
    square *sq  = malloc(sizeof(square));   //allocate memory
    sq->number=num;
    sq->next= NULL;         //points to null because the new one is added to the end of the linkedlist
    return sq;
}

//function to insert a square onto the board
square *insertSquare(square *newSquare, square *head){
    newSquare->next = head;            //pointing the new squares next to the head
    return newSquare;                   //returning the pointer to the new square which will become the head
}

int main(){
     square *head = NULL;   //pointer to the first node of the list
     square *tempSquare;        //will be used as temporary structures for creating the ladders

     //randomising the board
    srand(time(0));
    int upper = 64;      //setting the limits of the randomisation  
    int lower = 32;    
    int boardSize = (rand() % (upper-lower+1)) + lower;
    int ladderOne = (rand() % (10-1+1)) + 1;
    int ladderTwo =(rand() % (10-1+1)) + 1;

     
//creating the board
    for (int i = 0; i <= boardSize; i++)
    {
            if(i==rand){
                
            }
             tempSquare = create_Square(i);           //using a temporary square to create new squares
             head = insertSquare(tempSquare,head);     //the head will equal the pointer of the newest square
             
        
    }

    printBoard(head);

}


Solution 1:[1]

I'm responding to the question in the header. I'm not familiar with the Snakes And Ladders game, so I skipped the rest.

The answer is "No" and "Yes".

It's "No" because "next" means next: The one that follows in a sequence.

It's "Yes" because, in a linked list, there is no requirement whatever next points to has to be physically near the current node.

In short, "next" is a based on a logical relationship, not necessarily a physical relationship.

Consider that when you allocate memory for a new node and link an existing node to it, the two nodes might be in memory locations that are adjacent, or dozens, hundreds, or thousands of bytes apart.

Suppose you are working on an application for the Office of the Registrar at a college. The people there want to be able to quickly list students in alphabetical order or by student ID order. The college Honor Society wants to see students listed in GPA order. Instead of sorting, you decide to maintain the list in all 3 orders requested.

You can use a triply-linked list, represented by the table below:

Initial: No students in table

headName = 0, headSID = 0, headGPA = 0

R Name SID GPA nNam nSID nGPA

The first student is added:

headName = 1, headSID = 1, headGPA = 1

R Name SID GPA nNam nSID nGPA
1 Brown, Mary 24972 3.24 0 0 0

The 2nd student is added:

headName = 1, headSID = 1, headGPA = 1

R Name SID GPA nNam nSID nGPA
1 Brown, Mary 24972 3.24 2 2 2
2 Wilson, George 39572 3.00 0 0 0

George Wilson is after Mary Brown in all three orderings.

The 3rd student is added:

headName = 1, headSID = 1, headGPA = 3

R Name SID GPA nNam nSID nGPA
1 Brown, Mary 24972 3.24 3 2 2
2 Wilson, George 39572 3.00 0 3 0
3 Mitchell, Dennis 87523 3.51 2 0 1

Dennis Mitchell is between Mary Brown and George Wilson in name order, last in SID order, and first in GPA order

The 4th Student is added:

headName = 1, headSID = 1, headGPA = 4

R Name SID GPA nNam nSID nGPA
1 Brown, Mary 24972 3.24 3 2 2
2 Wilson, George 39572 3.00 0 4 0
3 Mitchell, Dennis 87523 3.51 4 0 1
4 Wade, Margaret 77920 4.00 2 3 3

The 5th Student is added:

headName = 5, headSID = 1, headGPA = 4

R Name SID GPA nNam nSID nGPA
1 Brown, Mary 24972 3.24 3 2 2
2 Wilson, George 39572 3.00 0 5 0
3 Mitchell, Dennis 87523 3.51 4 0 1
4 Wade, Margaret 77920 4.00 2 3 5
5 Aarrons, Adam 40001 3.60 1 4 3

The 6th Student is added:

headName = 5, headSID = 1, headGPA = 4

R Name SID GPA nNam nSID nGPA
1 Brown, Mary 24972 3.24 6 2 2
2 Wilson, George 39572 3.00 0 5 6
3 Mitchell, Dennis 87523 3.51 4 0 1
4 Wade, Margaret 77920 4.00 2 3 5
5 Aarrons, Adam 40001 3.60 1 6 3
6 Feeder, Bottom 64211 1.87 3 4 0

When a student is added, append the student data to the end of the table, or in your case, the list. For each linkage, follow the next pointers until you find the two old students the new student goes between. Set the next pointer for the new student to point to whatever the next pointer in the previous old student was pointing to, and set the next pointer in the "previous" old student to point to the new student.

In doing so, you make adjustments if the new student will be first or last in a chain.

Notes:

To fit the width allotted, I shortened "nextName", "nextSID", and "nextGPA" to "nNam", "nSID", "nGPA", respectively.

"Pointers" are to row numbers ("R") in the table. A value of zero means there is no next or header to point to; i.e., the last in the chain.

Name and Student ID are in ascending order. GPA is descending.

If it helps, print this out and draw arrows corresponding to the head and next pointers.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1