Académique Documents
Professionnel Documents
Culture Documents
In computer science, a data structure is a particular way of storing and organizing data in
a computer so that it can be used efficiently when required. It can also be defined as a
data structure is a specialized format for organizing and storing data. General data
structure types include the array, the file, the record, the table, the tree, and so on. Any
data structure is designed to organize data to suit a specific purpose so that it can be
accessed and worked with in appropriate ways. In computer programming, a data
structure may be selected or designed to store data for the purpose of working on it with
various algorithms. Different kinds of data structures are suited to different kinds of
applications, and some are highly specialized to specific tasks. For example, B-trees are
particularly well-suited for implementation of databases, while compiler implementations
usually use hash tables to look up identifiers. Data structures are used in almost every
program or software system. Specific data structures are essential ingredients of many
efficient algorithms, and make possible the management of huge amounts of data, such as
large databases and internet indexing services. Some formal design methods and
programming languages emphasize data structures, rather than algorithms, as the key
organizing factor in software design.
Basic principles
Data structures are generally based on the ability of a computer to fetch and store data at
any place in its memory, specified by an address — a bit string that can be itself
stored in memory and manipulated by the program. Thus the record and array data
structures are based on computing the addresses of data items with arithmetic
operations; while the linked data structures are based on storing addresses of data
items within the structure itself. Many data structures use both principles,
sometimes combined in non-trivial ways . The implementation of a data structure
usually requires writing a set of procedures that create and manipulate instances of
that structure. The efficiency of a data structure cannot be analyzed separately from
those operations.
1. Primitive and Non- primitive : primitive data structures are basic data structure
and are directly operated upon machine instructions. Example Integer, character.
Non-primitive data structures are derived data structure from the primitive data
structures. Example Structure, union, array.
2. Homogeneous and heterogeneous: In homogeneous data structures all the
elements will be of same type. Example array. In heterogeneous data structure the
elements are of different types. Example structure.
3. Static and Dynamic data structures: In some data structures memory is allocated
at the time of compilation such data structures are known as static data structures .
If the allocation of memory is at run-time then such data structures are known as
Dynamic data structures. Functions such as malloc, calloc, etc.. are used for run-
time memory allocation.
4. Linear and Non-linear data structures: Linear data structure maintain a linear
relationship between it's elements. Example array. Non-linear data structures does
not maintain any linear relationship between the elements. Example tree.
Tree representations
Pointers:-
Types of Pointers
1. C pointers
int *money;
This declares money as a pointer to an integer. Since the contents of memory are
not guaranteed to be of any specific value in C, care must be taken to ensure that
the address that money points to is valid. This is why it is sometimes suggested to
initialize the pointer to NULL (however initialising pointers unnecessarily can
mask compiler analyses and hide bugs).
int *money = NULL;
If a NULL pointer is dereferenced then a runtime error will occur and execution
will stop, usually with a segmentation fault. Once a pointer has been declared, the
next logical step is for it to point at something:
int a = 5;
int *money = NULL;
money = &a;
This assigns the value of money to be the address of a. For example, if a is stored
at memory location of 0x8130 then the value of money will be 0x8130 after the
assignment. To dereference the pointer, an asterisk is used again:
*money = 8;
This means take the contents of money (which is 0x8130), "locate" that address in
memory and set its value to 8. If a is later accessed again, its new value will be 8.
This example may be more clear if memory is examined directly. Assume that a is
located at address 0x8130 in memory and money at 0x8134; also assume this is a
32-bit machine such that an int is 32-bits wide. The following is what would be in
memory after the following code snippet is executed
int a = 5;
int *money = NULL;
Address Contents
0x8130 0x00000005
0x8134 0x00000000
money = &a;
Address Contents
0x8130 0x00000005
0x8134 0x00008130
*money = 8;
Address Contents
0x8130 0x00000008
0x8134 0x00008130
In C and C++ programming, two null pointers are guaranteed to compare equal;
ANSI C guarantees that any null pointer will be equal to 0 in a comparison with
an integer type; furthermore the macro NULL is defined as a null pointer
constant, that is value 0 (either as an integer type or converted to a pointer to
void), so a null pointer will compare equal to NULL.
A null pointer should not be confused with an uninitialized pointer: a null pointer
is guaranteed to compare unequal to any valid pointer, whereas depending on the
language and implementation an uninitialized pointer might have either an
indeterminate (random or meaningless) value or might be initialised to an initial
constant (possibly but not necessarily NULL).
In most C programming environments malloc returns a null pointer if it is unable
to allocate the memory region requested, which notifies the caller that there is
insufficient memory available. However, some implementations of malloc allow
malloc (0) with the return of a null pointer and instead indicate failure by both
returning a null pointer and setting err no to an appropriate value.
3. Wild pointers
Wild pointers are pointers that have not been initialized (that is, a wild pointer
does not have any address assigned to it) and may make a program crash or
behave oddly. In the Pascal or C programming languages, pointers that are not
specifically initialized may point to unpredictable addresses in memory.
int func(void)
{
char *p1 = malloc(sizeof(char)); /* (undefined) value of some place on the
heap */
char *p2; /* wild (uninitialized) pointer */
*p1 = 'a'; /* This is OK, assuming malloc() has not returned NULL. */
*p2 = 'b'; /* This invokes undefined behavior */
}
int x = 4;
void * q = &x;
int* p = q; /* void* implicity converted to int*: valid C, but not C++ */
int i = *p;
int j = *(int*)q; /* when dereferencing inline, there is no implicit conversion */
int x = 4;
void* q = &x;
// int* p = q; This fails in C++: there is no implicit conversion from void*
int* a = (int*)q; // C-style cast
int* b = static_cast<int*>(q); // C++ cast