简体   繁体   中英

C Two Dimensional Array into Linked List

So I'm still trying to wrap my head around linked lists in C. They are.. mind-boggling to me right now because I have yet to fully understand pointers, let alone pointers to pointers, and dynamic memory allocation that linked lists require.

I'm trying to create a two dimensional array with independent height, and width values. At most they would be 30x30. I have a two dimensional array let's call it arr[x][y]. arr[x][y] is filled with values of integers ranging from -2 to 1, how would I transfer this two dimensional array into a linked list? How would I then access values from this linked list on whim? I'm very confused, and any help would be appreciated. I'm looking through tutorials as we speak.

Additionally this is supposed to be a sort of stack linked list where I could call functions such as push(pushes a new value to the top of the linked list), pop(pops a value from the top of the linked list), top(returns the value most recently pushed onto the stack), isEmpty(checks if the stack is empty).

I don't need any full code, but code would be helpful here. I just need an understanding though of Linked Lists, and how to implement these sort of functions.

Additionally here is the assignment that this is related to: Assignment

It's a maze solver, I've already done code for analyzing a ascii picture into integer values for the two dimensional array. And as stated above that is what I need help with.

提示:从您的分配来看,堆栈不应该完全表示数组,而是表示您动态构建的路径,以找到从迷宫的起始位置到迷宫的目标位置的方法。

Basically you need to create a link list, whose each node is the head of another list contained as a member (which conceptually grows downwards), along with a usual next pointer in the list.

For accessing an element like 2D array such as arr[3][4], you need to walk the first list while keeping a count of yand then move downward counting x Or you could do vice versa.

This is a common data structure assignment which goes by the name "multi stack or multi queue" which if implemented by lists gives what you are looking for.

struct Node
{
    int data;
    struct Node *next;
    struct Node *head; // This head can be null initially as well as for the last node in a direction
};

First of all you need to define the proper structure.The first times it will be easier for you to create a list that terminates when the pointer to the next node is NULL.Afterwards you will discover lists with sentinel, bidirectional lists and things that now may seem too complicated.
For example that's a structure:

typedef struct __node
{
    int info;
    struct __node* next;
}node;

typedef node* list;

This time let's assume that list and node are the same thing, you will find more precise to separate the concept of list than the concept of node, and for example you may store in the list it's length (avoiding to count everytime all the nodes), but for now let's do it that way.
You initialize the list:

list l=NULL;

So the list contains zero nodes, to test if it's empty you just see if the pointer is NULL.
Add a new element:

if(NULL==l)
{
    l=(node*)malloc(sizeof(node));
    l->next=NULL;
    l->info=0;
}

Now the list contains zero nodes, create a function to add a new node:

void pushBack(list* listPointer, int info)
{
    if(NULL==*listPointer)
    {
        *listPointer=(node*)malloc(sizeof(node));
        (*listPointer)->info=info;
    }
    else
    {
        node* ptr=l;
        while(ptr->next!=NULL)
            ptr=ptr->next;
        ptr->next=(node*)malloc(sizeof(node));
        ptr->info=info;
    }
}

You could also gain efficiency adding the elements in front.Or optimize the code by returning the added element, so that you don't have to find the last element everytime.I leave this to you.Now let's call the pushBack function for every element of the array:

for(int i=0; i<N; i++)
{
    pushBack(l,arr[i]);
}

That's all, learn your way to implement linked lists.

You're not supposed to convert the whole array into a linked list, you're only supposed to convert the best path into a linked list. You'd do this by brute force, trying directions and backtracking when you ran into dead ends.

Your path, the linked list, would need to look something like this:

struct PathNode
{
    int coordX, coordY;
    PathNode * next, * prev;
}

If I remember later, I'll draw a picture or something of this structure and add it to the post. comment on this post in a few hours to attract my attention.

The list would always contain a starting point, which would be the first node in the list. As you moved to other positions, one after the other, you'd push them onto the end of the list. This way, you could follow your path from your current position to the beginning of the maze by simply popping elements off of the list, one by one, in order.

This particular linked list is special in that it's two way: it has a pointer to both the next element and the previous one. Lists with only one of the two are called singly linked lists, this one with both is called a doubly linked list. Singly linked lists are one way only, and can only be traversed in one direction.

Think of your linked list as giant pile of strings, each with a starting end and a finishing end. As you walk through the maze, you tie a string at every node you visit and bring an end with you to the next square. If you have to backtrack, you bring the string back with you so it no longer points to the wrong square. Once you find your way to the end of the maze, you will be able to trace your steps by following the string.

Could you just explain what -> means exactly?

-> is an all-in-one pointer dereference and member access operator. Say we have:

PathNode * p = malloc(sizeof(*p));
PathNode q;

We can access p's and q's members in any of the following ways:

(*p).coordX;
q.coordX;
p->coordX;
(&q)->coordX;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM