codingdir logo sitemap sitemap |

Adding a pointer to a sensor to a linked list

By : , Category : c++

A plain linked list consists of one anchor and one end pointer, which points to the last element of the linked list.

So you'll need two variables. Here's a possible setup:

SensorBlock *Anchor = NULL, *EndPtr = Anchor;
if(!Anchor) EndPtr = Anchor = new SensorBlock(s);  //Assuming that you have a constructor, which takes this parameter.
else EndPtr = EndPtr->LLelement = new SensorBlock(s);

This code first checks, whether the anchor is already set or not. If it isn't, the anchor is instantiated and the EndPtr is set to Anchor. Otherwise, it is 'appended' to the list by creating a new SensorBlock element, which is then assigned to 'EndPtr->LLelement' and finally to EndPtr itself.

Be careful to release the dynamically allocated SensorBlock elements once you're done with them and if you delete any element, be sure to update ALL references, which point to it. (Anchor, EndPtr, LLelement of previous element). Otherwise you'll break your chain and destroy the list.

ReLated :

If it was another type of pointer you can't do


You make traverse point to the next item in list, which in turn should have a struct member "next", and so on.

Using pointers this way is the easiest way to use linked lists. Using ints or any other ID's would mean that the linked list will be implemented via some kind of indexed array, and therefore needs more logic to handle the index and cope with expanding or decreasing array size. The fun part of linked-lists is that you can remove parts of the link or add one somewhere in the middle, without having to re-arrange the whole list.

You increment size and then call isEmpty() (which checks whether size is zero) to decide how to add the item:

    size++ ;
    if (isEmpty( )) {

I suspect you were lucky with your linked list test. Try inserting something at the head of the list.

To expand on that...

main() has a pointer to the head of the list which it passes by value into your version of sortedInsert(). If sortedInsert() inserts into the middle or end of the list then no problem, the head is not changed and when it returns to main() the head is the same. However, if your version of sortedInsert() has to insert a new head, fine it can do that, but how does it return the information about the new head back to main()? It can't, when it returns to main() main will still be pointing at the old head.

Passing a pointer to main()'s copy of the head pointer allows sortedInsert() to change its value if it has to.

A pointer is nil by default and needs to be initialized.


// Init initializes or clears list l.
func (l *List) Init() *List { = l.root
  l.root.prev = l.root
  l.len = 0
  return l

should become this:

// Init initializes or clears list l.
func (l *List) Init() *List {
  l.root = new(Element) // necessary to avoid dereferencing a nil pointer = l.root
  l.root.prev = l.root
  l.len = 0
  return l

Demo at

In the case of the standard library, it is not necessary to have root be a pointer, however, for prev and next it is necessary, otherwise the struct definition would be recursive, which is not allowed, because it would in theory cause a struct of infinite size...


Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © All Rights Reserved .