cpppro

Archive for the ‘Pointers & Callbacks’ Category

Linked Lists

In C Tidbits, Data Structures in C/C++, Pointers & Callbacks, Tutorials on April 12, 2007 at 2:55 pm

Linked Lists Basics
[Download this tutorial as linked lists tutorial , for updated conetnt and comments, generate a new pdf with the option which is present below the post]

What are Linked Lists?
Linked lists store collections of data like arrays.  Linked lists are chain of records/nodes, one record/node points to the next. Record holds the data.

Why Linked Lists?
There are several  disadvantages with arrays, here are some…
1) The size of the array is fixed. Most often this size is specified at compile time however the size of the array can be deferred until the array is created at runtime (from heap), but after that it remains fixed. This causes to waste memory eventhough e may not use.
2) Inserting new elements at the front is potentially expensive because existing elements need to be shifted over to make room.
3)When array was full, to insert more data, it need to be resized, this operation is quite expensive, even may not be possible if in case memory got fragmented.
Linked lists performs well where arrays fail to do it.
Please refer http://www.refcode.net/2013/02/linked-lists.html

 

Sorting A Linked List with Bubble Sort

In C Tidbits, Data Structures in C/C++, Pointers & Callbacks on April 11, 2007 at 10:01 pm

The basic idea of this sorting algorithm is to
compare two neighboring objects, and to swap them if
they are in the wrong order.

here is an example, which shows sorted list in several passes..

5   10   20   13   7   17
5   10   13   7    17  20
5   10   7    13   17  20
5   7    10   13   17  20
5   7    10   13   17  20

Here’s a snippet of C code for bubble sort for sorting an array:

Please refer http://www.refcode.net/2013/02/sorting-linked-list-with-bubble-sort.html