Question:
When using Linked Lists in C Programming?
We have just started
2013-12-09 08:25:01 UTC
So I'm beginning to understand the concept of nodes and linked lists but am confused about something. Say I have a table 3 by 3 and I want to go through each cell to check what the value is. From what I understand, to create a node, i would have to do the following:

struct NODE{
int data; //stores data of node
NODE* next; //points to the next node
}

I'm not sure if that's completely right. My question is, do I need a seperate node for each element (so would that be 9 different nodes?). If so, I can't imagine I would have to write the above 9 times over so how would i create this linked list (in simple terms). Thanks!
Three answers:
Jonathan
2013-12-09 12:12:49 UTC
Your code is a DECLARATION, not a DEFINITION. It declares how something would look, if you created one. It doesn't actually create anything. All it does is provide some information for the compiler, should it need it later. You only need to tell the compiler once. It remembers after that.



And linked lists are NOT tables. Tables are consecutive pieces of information, stored next to each other in memory. A linked list is a simple, linear list of disconnected items which are usually NOT anywhere near each other. Which is WHY they have to have a pointer of some kind included with each "value." That "next" pointer is used to find a distant, assumed not adjacent, next item.



You usually use linked lists if you don't create new items all at once. So if you already know you need a 3x3 of something, there really is no need for a linked list to make that. You just create it either by a definition saying so, or by using an appropriate malloc() function call and then appropriate indexing into that block of memory of adjacent items.



Technically, using two pointers in each node, I suppose you _could_ create a 3x3 matrix once element at a time, if you set up the pair of linked list pointers (or two pairs, if doubly linked) to point to the right elements in the matrix. But I can't recall ever seeing that done, in practice. It's "insane" to imagine doing things that way... well, unless you wanted to support grossly sparse matrices (which a 3x3 would be silly bothering with.)



You need to spend some time trying to understand WHY linked lists are useful, at times, and actually WHAT your structure MEANS. It's not just some STAMPED OUT template that you learn to recognize like ornate bookends. You need to understand WHY these bookends are used, at all (bookends are used to keep the books upright, for example, and that is desirable for WHAT reason exactly?)



I suspect you don't really understand them at all, yet. You have ONLY learned how to spot the idiom. But you don't have any idea why anyone bothers with it. You need to understand that part.
Andrew
2013-12-09 11:29:41 UTC
Generally you don't make a grid of nodes, you normally make a simple list.

If you need more than one list you either have an array of Node pointers, one entry for each row in the grid or if you need to be able to change the number of rows a master linked list where the data is a Node* rather than an int.



And you only define the structure once but as a typedef.



e.g.



typedef struct Node {

int data;

Node *next;

} Node;



Node *ListHead = null;





addNewNodeToEnd (int value) { // creates a new Node and adds it to the end of the list.



Node *nodeToAdd = (node *)malloc (sizeOf (Node));

nodeToAdd->data = value;

nodeToAdd->next = null;



if (ListHead == null) {

ListHead = nodeToAdd;

} else {

Node *lastNode = ListHead;

while (lastNode->next != null) {

lastNode = lastNode->next;

}

lastNode->next = nodeToAdd;

}

}



addNewNodeToStart (int value) { // creates a new Node and adds it to the start of the list.



Node *nodeToAdd = (node *)malloc (sizeOf (Node));

nodeToAdd->data = value;

nodeToAdd->next = ListHead;

ListHead = nodeToAdd;

}



deleteList() { // frees memory allocated to the list. Call at the end of the program or when you want to delete the list



Node *thisNode = ListHead;

Node *nextNode;

while (thisNode != null) {

nextNode = thisNode->next;

free(thisNode);

thisNode = nextNode;

}

}





Adding to the middle or deleting single entries is much the same idea, you just have to be careful to make a copy of any pointers before you change or free them them so that you can re-attach the rest of the list. Until you get the hang of it drawing diagrams of where each thing is pointing at each stage is normally a good idea.
Xperiaz X
2013-12-09 09:01:00 UTC
No no

the above code is written only once.

You create an array of sttructures.

Write a loop to raed data of each.

Google linnked list. There are many solved examples


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...