1

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.

AstroCB
  • 12,337
  • 20
  • 57
  • 73
Ostap Hnatyuk
  • 1,116
  • 2
  • 14
  • 20
  • Why not just give your nodes references to an int[][]? Or create a struct for the data and give the nodes a reference to that? – Jordan Kaye Nov 05 '12 at 20:17
  • [They are side-effects in asking code for course work submission :)](http://stackoverflow.com/questions/4161475/counting-and-generating-perfect-squares) – P.P Nov 05 '12 at 20:18
  • I'm trying to understand the concept of a linked list though. That's exactly what I want to do, but I don't understand how to do it. What I'm confused mostly about is if you create a struct called nodeStruct, how does this line work struct nodeStruct *next;. – Ostap Hnatyuk Nov 05 '12 at 20:19
  • I'm not asking for a full solution, or anything close to that. Although that would be nice, I'm asking more along the lines of how do I even begin. – Ostap Hnatyuk Nov 05 '12 at 20:20
  • 1
    struct Node *next is a pointer (rather a container) which can be thought of as initially empty. It can be filled as soon as you insert new elements. By appending each new element – fkl Nov 05 '12 at 20:22
  • Okay I actually understand that, just a few more questions. – Ostap Hnatyuk Nov 05 '12 at 20:25
  • Woops pressed enter, what does -> mean exactly? And how would I access individual pieces of the linked list. That's mainly what I'm confused on. – Ostap Hnatyuk Nov 05 '12 at 20:26

4 Answers4

3

Hint : from your assignment, the stack is not supposed to fully represent the array, but to represent a path you dynamically build to find a way from the starting position of the maze to the target position of the maze.

2

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
};
fkl
  • 5,412
  • 4
  • 28
  • 68
1

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.

Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187
  • Okay this actually helps a lot. I will analyze this code more thoroughly when I'm not at work, but to implement this for a two dimensional array I would just do pushBack(l,arr[x][y]); Right? I'm guessing so. Thanks for the help! – Ostap Hnatyuk Nov 05 '12 at 20:36
  • You can handle it they way that you want: a linked list of x*y elements or an array of x lists, each of size y.Or (advanced, hard solution) a linked list of linked lists. – Ramy Al Zuhouri Nov 05 '12 at 21:05
1

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;
Wug
  • 12,956
  • 4
  • 34
  • 54
  • That helps me understand it a lot. I'm wondering though, would it be better to just have a next, and then if you run into a dead end pop all the top values off until you reach a square next to an open unexplored path? So then you could just have a linked list with a complete path, and no dead ends. And also yes I realize that converting the whole two dimensional array is rather pointless and stupid now, haha. – Ostap Hnatyuk Nov 05 '12 at 20:41
  • 1
    That's exactly what you're supposed to do. – Wug Nov 05 '12 at 20:42
  • Okay yep! I've just been stressed out completely today over this, but suddenly I realize that this isn't as difficult as I thought it was, except for the actual coding of it, that's still a bit foggy. Could you just explain what -> means exactly? For some reason google dislikes ->, and doesn't even seem to register it as anything. – Ostap Hnatyuk Nov 05 '12 at 20:45