Thread: How to add and delete items in a structure?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Quote Originally Posted by JorgeChemE View Post
    This is a hell!
    This is only a Hell if you are new in the technique of linked list.
    If you have programmed some weeks (or months) with it, you collect practice and linked lists will be a absolutly normal thing for you.
    Here is the logic of linked list (and also double linked).


    Linked List:
    You can allways start at 'head' and jump from struct to struct (with '*next') until you find what you searching for.
    'tail' is only a helper for append new struct, but you can not go backward.
    Code:
    .        –––––––     –––––––     –––––––     –––––––     –––––––
    *head-->| data  |   | data  |   | data  |   | data  |   | data  |<--*tail
            | …     |   | …     |   | …     |   | …     |   | …     |
            | *next |-->| *next |-->| *next |-->| *next |-->| *next |--> NULL
             –––––––     –––––––     –––––––     –––––––     –––––––

    Double Linked List:
    You can start at 'head' and jump forward (with '*next') or you can start at 'tail' and jump backward (with '*prev') until you find what you searching for.
    Code:
    .        –––––––     –––––––     –––––––     –––––––     –––––––
    *head-->| data  |   | data  |   | data  |   | data  |   | data  |<--*tail
            | …     |   | …     |   | …     |   | …     |   | …     |
            | *next |-->| *next |-->| *next |-->| *next |-->| *next |--> NULL
    NULL <--| *prev |<--| *prev |<--| *prev |<--| *prev |<--| *prev |
             –––––––     –––––––     –––––––     –––––––     –––––––

    It is very important that you never lose a pointer! If you lose a pointer you can never free the allocated memory!
    A lost pointer is automaticly a memory leak!
    Last edited by WoodSTokk; 05-29-2017 at 11:03 AM.
    Other have classes, we are class

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 08-25-2013, 05:09 AM
  2. using delete on a structure pointer
    By spongefreddie in forum C++ Programming
    Replies: 9
    Last Post: 07-06-2011, 12:12 PM
  3. Delete records in Structure
    By Myrren in forum C Programming
    Replies: 6
    Last Post: 11-19-2010, 02:15 AM
  4. Delete All Items In A Linked List
    By Khellendros in forum C++ Programming
    Replies: 6
    Last Post: 02-09-2009, 01:22 AM
  5. Delete duplicate items in an array
    By chaos in forum C Programming
    Replies: 4
    Last Post: 06-16-2005, 02:51 PM

Tags for this Thread