hey guys, im a new guy here in the forum and im also an IT student who gets into C++ troubles most of the time, but enough about that... i just wanna ask if anyone can help me in my linked list problem, i think its about pointers and address. like a->link, well thats what it looked like. Problem is i dont understand a thing and my prof wont even give me an example, i dont even get the slightest idea of what's it all about and i dont know where to start, SO CAN ANYONE PLS SHOW ME AN EXAMPLE,even just the basic ones THANKS!!!
^_^

Dani AI

Generated

A few focused notes that clarify and extend the replies from and , and that point out a couple of beginner traps in the example posted.

The main mental model: a list node holds data and a pointer to the next node; a single pointer called the head (or anchor) points to the first node. Two common mistakes to watch for: (1) if a function takes the head pointer by value, assigning a new head inside that function does not change the caller’s head — either return the new head, pass the head by reference (pointer reference) or use a pointer-to-pointer; (2) when appending to the tail, make sure to loop until the last node (curr->next == NULL) rather than moving just one step and stopping. Both of those are subtle but explain many “it works sometimes” bugs.

Deletion and memory safety deserve special emphasis. Remove nodes by first fixing the surrounding links, then delete the node; never use a deleted pointer and set dangling pointers to nullptr. For leak hunting and undefined-behavior checks, use AddressSanitizer (compile with -fsanitize=address,undefined) or run valgrind; build with warnings enabled (-Wall -Wextra) and add small unit tests that exercise empty-list, single-node, and multi-node cases.

Modern C++ options greatly reduce manual errors: use std::forward_list or std::list for production code, and consider std::unique_ptr for the next pointer if implementing your own list to get automatic destruction. For learning, implement these features in order: push_front/pop_front, size(), find(), erase(value), push_back (traversal), and reverse. Draw pointer diagrams while stepping through with a debugger — that visualization turns pointer math into predictable steps.

Recommended Answers

All 3 Replies

Member Avatar for Member #46692

try google + "linked lists"

Linked list are typically used to work with data structures.
Access to items in the list is controlled by pointers.

This is a very simple example to construct a linked list. You will need to do a lot more reading to use them correctly.

One thing to remember:
You must delete all the list items when exiting the program otherwise you will create a memory leak.

struct dataRec {
  type     data;
  dataRec  *next;
}

dataRec *prList = NULL;  // the list anchor

function addRecord(dataRec *prList) {
  dataRec *temp = new dataRec;  // create a new record to add to list
  temp->data = whatever;
  temp->next = NULL;

  // Now add this record to the end of the list
  dataRec *curr = prList;	
  if (curr == NULL) {  // the list is empty
    prList = temp;  // prList, the anchor, now points to the first record
  }
  else {
    if (curr->next == NULL) { // last record in list
      curr->next = temp;  // add temp to end of list
    }
    else {  // move up the list until the last entry
      curr = curr->next;
    }
  }
}

thanks, i really appreciate it! i think i'll do some head crackinh for awhile to master this ^^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.