Beruflich Dokumente
Kultur Dokumente
STRUCTURES
Learning Outcomes:
A data structure is said to be linear And the data structure where there is
if its elements from a sequence no such sequence are called non
linear.
E.g: array or linked list. E.g: tree and graph
Types of data structures
STATIC DYNAMIC
The size of the structure is fixed The number of items to be stored is not known
before hand.
Static data structures are very good for In this case the programmer will consider using
storing a well-defined number of data items. a dynamic data structure. This means the data
structure is allowed to grow and shrink as the
demand for storage arises. The programmer
should also set a maximum size to help avoid
memory collision.
For example a programmer might be coding For example, programmer coding a print spooler
an ‘UNDO’ function where the last 10 user will have to maintain a data structure to store
actions are kept in case they want to undo print jobs, but cannot know before hand how
their actions. In this case the maximum many jobs there will be.
allowed is 10 steps and so he decides to form
a 10 item data structure.
Types of data structures
STATIC DYNAMIC
Memory is allocated to the data structure Memory is allocated at compile time. Fixed size
dynamically i.e as the program executes
Advantage: makes the most efficient use of Advantage: the memory allocation is fixed and so
memory as the data structure only uses as much there will be no problem with adding and removing
memory as it needs. data items.
Disadvantage: harder to program as the software Advantage: Easier to program as there is no need to
needs to keep track of its size and data item check on data structure size at any point.
locations at all times.
Disadvantage: because the memory allocation is Disadvantage: can be very inefficient as the memory
dynamic, it is possible for the structure to for the data structure has been set aside regardless of
‘overflow’ should it exceed its allowed limit. It can whether it is needed or not whilst the program is
also ‘underflow’ should it become empty. executing.
Data types
Array Representation
• Array can be declared in various ways in different languages. For illustration, let’s
take C array declaration.
Arrays
• As per above shown illustration, following are the important points to be considered.
- index start with 0.
- array length is 10 which means it can store 10 elements.
- each element can be accessed via its index. For example, we can fetch element
at index 6 as 27.
Disadvantages of Arrays
▪ We must know in advance that how many elements are to be stored
in array.
▪ Array is static structure. It means that array is of fixed size. The
memory which is allocated to array can not be increased or reduced.
▪ Since array is of fixed size, if we allocate more memory than
requirement then the memory space will be wasted. And if we
allocate less memory than requirement, then it will create problem.
▪ The elements of array are stored in consecutive memory locations.
So insertions and deletions are very difficult and time consuming.