VOGONS


First post, by vgagame

User metadata
Rank Newbie
Rank
Newbie

Can someone show me how I could access the link pointer of any node in a linked list? My linked list is implemented with a struct that contains a pointer to the next structure or next node in the linked list. If I have several nodes, each having a link to the structure that comes next in the list, how could I access the pointer to the next structure for any node in the linked list? I am interested in the process of inserting a node into the list. The insert function first creates a new node and a character input by the user is saved in the node. If it is the first node in the list the pointer the next node is NULL. For each succesive node the pointer contains an address to the next node. If for example I had 4 nodes linked, each having the address of the next node after it, how could I access or otherwise use a debugger or printf to "see" the pointer of say the second or the first node if I just created the last node? What is happening in my insert function malloc is used to create a new node and there is a pointer to NULL for the first node and the address of the next node for the nodes following the first. Once I several nodes have been created I would like a way to findout what the contents of the pointer is of an earlier node stored in memory. I can use a debugger to see pointers of nodes that are currently being established. But not nodes that were already created.

// self-referential structure                       
struct listNode {
char data; // each listNode contains a character
struct listNode *nextPtr; // pointer to next node
};

typedef struct listNode ListNode; // synonym for struct listNode
typedef ListNode *ListNodePtr; // synonym for ListNode*

...

// insert a new value into the list in sorted order
void insert(ListNodePtr *sPtr, char value) {
ListNodePtr newPtr = malloc(sizeof(ListNode)); // create node

if (newPtr != NULL) { // is space available?
newPtr->data = value; // place value in node
newPtr->nextPtr = NULL; // node does not link to another node

ListNodePtr previousPtr = NULL;
ListNodePtr currentPtr = *sPtr;

// loop to find the correct location in the list
while (currentPtr != NULL && value > currentPtr->data) {
previousPtr = currentPtr; // walk to ...
currentPtr = currentPtr->nextPtr; // ... next node
}

// insert new node at beginning of list
if (previousPtr == NULL) {
newPtr->nextPtr = *sPtr;
*sPtr = newPtr;
}
else { // insert new node between previousPtr and currentPtr
previousPtr->nextPtr = newPtr;
newPtr->nextPtr = currentPtr;
}
}
else {
printf("%c not inserted. No memory available.\n", value);
}
}

Reply 1 of 16, by megatron-uk

User metadata
Rank l33t
Rank
l33t

If you want to go up and down the list you will probably want to add a *prev_pointer to your node struct.

That way you can go back and forth, regardless of where you are in the list.

If you are talking about directly accessing any node in the list, well, that's direct access, which a linked list isn't.

You could have another data structure which is just an array of addresses of each node, but you would have to implement sorting/reordering for that as well as your linked list to maintain consistency between the two when you add/remove nodes to the linked list.

My collection database and technical wiki:
https://www.target-earth.net

Reply 2 of 16, by pshipkov

User metadata
Rank l33t
Rank
l33t

You can do something like this which works with what you got already.
 

ListNodePtr* getLinkedPointer(ListNodePtr *ptr, int pos)
{
if (pos < 0 || *ptr == NULL) return NULL;
ListNodePtr currPtr = *ptr;
int currPos = 0;
while (currPtr != NULL && currPos < pos) {currPtr = currPtr->nextPtr; currPos++;}
currPtr == NULL ? return NULL: return &(currPtr->nextPtr);
}

retro bits and bytes

Reply 3 of 16, by cyclone3d

User metadata
Rank l33t++
Rank
l33t++

That is not directly accessing it though. That is serial access until the correct node is found, which is the absolute slowest way.

A better "search" would be a binary search which is the fastest way to find something in a list other than using an array and being able to directly access it.

Yamaha modified setupds and drivers
Yamaha XG repository
YMF7x4 Guide
Aopen AW744L II SB-LINK

Reply 4 of 16, by jakethompson1

User metadata
Rank Oldbie
Rank
Oldbie

Especially for debugging, you probably want a helper routine to walk the entire list and print out the current contents in-order.

As you are finding out, linked lists make inserting a node between node k and node k+1 cheap, but make writing a "find kth" operation expensive (linear time).

Reply 5 of 16, by pshipkov

User metadata
Rank l33t
Rank
l33t

@cyclone3d
There are better ways to improve perf if necessary, but the case is trivial and does not require premature optimizations. My reasoning:
Problem description implies small dataset, so iterating is probably fine, especially if original code stays unchanged, which i assume will be preferred.
There is a second requirement about readable logging which can be done by repurposing the snippet i typed - make it void and printf inside the loop, or something like that.
The implementation handles pairs only and cannot form a hierarchy with multiple descendants (branches/leaves) linked "under" a parent (kd or some other tree structure) - so no recursions expected which can quickly make the getLinkedPointer routine expensive if invoked from within the graph traversal.

retro bits and bytes

Reply 6 of 16, by BloodyCactus

User metadata
Rank Oldbie
Rank
Oldbie

use a double linked list instead of a single linked list

each node points forward and backward and the list has a head and tail. that way you can walk the list in any direction from any pointer.

--/\-[ Stu : Bloody Cactus :: [ https://bloodycactus.com :: http://kråketær.com ]-/\--

Reply 7 of 16, by vgagame

User metadata
Rank Newbie
Rank
Newbie

Thanks for the replies. It will take me some time to understand pshipkov's code and I have also been looking at doubly linked lists as BloodyCactus suggested.

Reply 8 of 16, by megatron-uk

User metadata
Rank l33t
Rank
l33t
vgagame wrote on 2025-03-31, 16:14:

Thanks for the replies. It will take me some time to understand pshipkov's code and I have also been looking at doubly linked lists as BloodyCactus suggested.

That's precisely what I said in my first reply 😀

My collection database and technical wiki:
https://www.target-earth.net

Reply 9 of 16, by BloodyCactus

User metadata
Rank Oldbie
Rank
Oldbie

if you look at my code in my underdark game it has my dlist code in there

https://github.com/stu/UnderDark/blob/main/dlist.h

https://github.com/stu/UnderDark/blob/main/dlist.c

--/\-[ Stu : Bloody Cactus :: [ https://bloodycactus.com :: http://kråketær.com ]-/\--

Reply 10 of 16, by vgagame

User metadata
Rank Newbie
Rank
Newbie

What platform is underdark BloodyCactus?

Today I tried using pshipcov's code in my linked list program and I am not sure how to best make use of it. The thing is that I have managed to witness , pointers, pointers to pointers, pointers to sturctures, and function poiinters. I still can't figure it out. So I moved on and eventually I found that I have read most of the book I am studying "C How to Program" by Havey Deitel. Basically the perspective of this book tops out at showing one things about the language that they could not have learned about unless they came across it from somewhere and this book is one of those "somewheres". It shows some good examples. But figuring out those examples doesn't arise from the subject matter of that book alone. Tonight I started looking at a book called "Understanding and Using C Pointers" by Richard Reese. I started with a section about the single linked list. But his linked list is much more robust (yet hard to fathom) than the one in my introductory textbook.

I am trying to learn to program in C - that is getting better at understanding programs - being able to see someone's concept and using it in my own programs.

There are two ideas I have for getting to where I want to be.

Learn SQL, learn databases, and then study SQLite. Thus I would use C to tackle database scenerios (hopefully deeper than just CRUD).

Or I could try using Allegro 3.x. If I can find tutorials then that version of the game development library might teach me to program in C through concepts I am more familiar with such as Mode 13h. MCGA is also simpler than what is used by Allegro 5.x, the display mode to which I have tried to study but was not sucessful.

Or does someone else here have some other resource I am not seeing the value of?

Reply 11 of 16, by BloodyCactus

User metadata
Rank Oldbie
Rank
Oldbie
vgagame wrote on 2025-04-01, 14:38:

What platform is underdark BloodyCactus?

underdark runs on
- linux + sdl (clang/gcc + sdl2)
- win32/win64 + sdl (mingw gcc + sdl2, its cross built on linux)
- dos (watcom v2)

vgagame wrote on 2025-04-01, 14:38:

Today I tried using pshipcov's code in my linked list program and I am not sure how to best make use of it. The thing is that I have managed to witness , pointers, pointers to pointers, pointers to sturctures, and function poiinters. I still can't figure it out.

yeah my double linked list uses all those features.

i have a github repository of dos stuff, which are usually simple single file stuff, some C programs, some written in asm. none very complex I think.

https://github.com/stu/dos_code

--/\-[ Stu : Bloody Cactus :: [ https://bloodycactus.com :: http://kråketær.com ]-/\--

Reply 12 of 16, by st31276a

User metadata
Rank Member
Rank
Member

Perhaps build a map structure (either tree or unordered) that indexes the list nodes' values (or at least the part thereof you are trying to look up) as keys, mapping them to node pointers of existing nodes in the list.

There is no other direct way that is fast, unless with "direct access" you just mean to cache the address of a specific node somewhere else for later referral.

Reply 13 of 16, by jakethompson1

User metadata
Rank Oldbie
Rank
Oldbie

If you give more info on what else you need to with these typed-in characters we could advise on whether there is another kind of data structure closer to what you want.

Reply 14 of 16, by vgagame

User metadata
Rank Newbie
Rank
Newbie
jakethompson1 wrote on 2025-04-02, 00:17:

If you give more info on what else you need to with these typed-in characters we could advise on whether there is another kind of data structure closer to what you want.

I feel like I can not get any further with understanding a singly linked list in C. I have actually found different ways of keeping track of the pointers and pointers to pointers. I tried writing down the address of a known pointer to discern the address of a pointer assigned the same address. But I had not been able to track when a node is added to the end of the linked list nor the case when it is inserted in the middle of the list. That is what my goal was to understand a linked list. This list just stores a char in each of its nodes and prints which character was stored in the list of nodes. One issue is two pointers are needed to store the address of two of the nodes so that if a node is inserted in the middle of the list an update can be made for pointers used to link the nodes. It is difficult for me to see why the pointer for the current node, stored as a link to the previous node's next pointer is not assigned as a pointer for one of the pointers that keeps track of the current node. The tracking pointer, called current pointer, changes depending on if I am inserting a node in the middle or if I'm adding to the end of the list.

Reply 15 of 16, by jakethompson1

User metadata
Rank Oldbie
Rank
Oldbie

I don't really understand, but if you use the dummy node technique to store your list head rather than pointers to pointers, it will cut down on the number of special cases to handle while learning.

Reply 16 of 16, by megatron-uk

User metadata
Rank l33t
Rank
l33t

I feel like the OP could benefit from reading a general CS book on data structures, rather than asking specific implementation questions. Having a solid foundation of when and why to use a certain type of data structure is probably of more value than asking for implementation help.

Once you understand the fundamentals, you should be able to make your own decisions on the best paradigm to use, and how to extend it to meet your specific needs.

My collection database and technical wiki:
https://www.target-earth.net