Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

1.

3 Introduction to Data Structure


Digital Computer can manipulate only primitive data, i.e. data in term of O's and J 's.
Mani'pulation of primitive data is inherent within the computer and need not require any extra
effort from the user's side. But in our real life applications, various kinds of data other than
the primitive data are involved. Manipulation of real Iife data ( can also be termed as user '
defined data) requires the following essential tasks :

. 1) Storage representation of user data: User data should be stored in such a


way that computer can understand it.
1.16 *Data Structures Using
·
C
d in a computer should be retrie
2) Retrieval of stored data: Data st ore ._ Ved 1~
nd
such a way that the user can underS ta it. .
. erations which require to be perfi0
3) Transformation of user data: Vanous op fi e form to other 11neq
on user data so that it can be transformed rom on ·
I who design and develope co
Knowledge of data structures is required for peop . e1· n software. As we know dlllputet
programs of any kind: system software or. app I
• ,ca IO 's data area or record penn ' ata are
represented by data values held temporary w1th111 program
d tO each other. ,..•o ena bl e progranenth,J
on a file. Often the different data values are re Iate . allls to
· · d I t be in an organized form. The orga .
ma ke use
. of tI1ese relat1onsh1ps
. ' these ata va ues mus ~
have to follow certain rull~Zee!
~llect1on of data 1s called data structure. The programs ru es to
access anJ process ~the structurelctata. "'
Data Structure= Organized data+ Allowed operation
Data may be organized in many 'different ways; the logical or mathematic~! model of
e:irticular organization of data is called a data st;ucture. The choice of a partiClil'ar;r'~
model depe;;'ason'two considerati-;-s. Fi;; , it must be ricl; enough in structure.to mirror the
actual relationship of the data in the real world. On the other hand, the structure should be
simple enough that one can effectively process the data when necessary.

1:4 Classification of Data Structure


Data structures may be classified as ! ~d f~es. These are derived from the primitive
data types (int, char, float, double). These aata structures emphasize on structuring of a
_group of homogeneous (same type) or heterogeneous (different type) data items which are
shown in Figure 1.8.
Data Structure

Lists Files

Linear Lists Non-Linear Lists

Stacks Queues Trees Graphs

Figure 1.8 Classification of data Structure


.
We may classify data structu .
*
Introduction to Algorithms 1.17
. re in many ways. They are:
Linear ·
In linear data structures the dat .t Non-Linear
. . a1emsare In non-linear data structures the data items
arranged 111 a lmear sequence 1,·ke ·
111 a array. are not in sequence .
Example: Array, Linked List.
Example : Tree and Graph
Homogeneous Non-Homogeneous
In homogeneous data structure all the In non-homogeneous dat~ structure the
elements are same type. elements may or may not be of the same type.
Example: Array Example: Records .
Static Dynamic

Static structures are ones whose sizes and Dynamic structures are ones which expand
or shrink as required during.program execution
structures associated memory locations are
and their associated memory locations change.
fixed at compile time.
Example: Linked list created using pointers.
Example: Array

· 1.5 Description of various Data Structure


1.5.1 Arrays
An array is defined as a set of finite number of homogeneous elements or data items. It
means an array can contain one type of data only, either all integers, an floating-point numbers, ./
or all characters. Declaration of arrays is as follows:

int a[1 OJ;


Some common operations perfonned on arrays are:
1. Creation of an array.
2. Traver_sing an array (accessing array ele1nents).
3. Insertion of new elements.
4. Deletion of required element.
5. Modification of an element.
6. Merging-of arrays.
1.5.2. Lists
A list (Linear linked list) can be defined as a collection of variable nu1111;>er of data items.
Lists are the most com_monly used data structures. An e_le111ent of list must contain at least
two fields, one for storing data or information and other for storing address of next element.
Introduction to Algorithms *1·19
As we know for storing address we have a special data structure in C called pointers. hence
the second field of the list must be pointer type. Technically each such element is referred to
as a node, therefore a list can be defined as a collection of nodes as shown in Figure 1.9.
start

4 44 I+[ I3-, 13-, I I, Ix r


33 II 66 55

Figure 1.9 Linked list


t.5.3. Stack

A stack is also an ordered collection of elements like I


.,
arrays, but it has a special feature that deletion and

--- .,-,:=--..::,
=-
___,
-~
_...:;:

insertion of elements can be done only from one end, ' -=--
_..
=-
called the top ofstack (TOP). Due to this property it is
also cal led as last in first out .type of data structure
..
(LIFO). It could be thought ~fjust like a stack of plates
placed on table in a party, a guest always takes off~ Figure 1.10 Stack
fresh plate from the top, and the new plates are placed
onto the stack at the top,

When an element is inserted into a stack or removed from the stack, its base remains fixed
whereas the top of stack changes. Insertion of element into stack is called Pusli and Deletion
of element from stack is cal led Pop. The figure I; 10 shows how the operations take place on
a stack.

The stacks can be impiemented in two ways:


i) Using arrays (static implementation) ,
ii) Using pointers (dynamic implementation).
'
1.5.4. Queues

Queues are first in first out type of data structure (i.e. FIFO). In a queue, new elements are
added to the queue from one end called rear end, and the elements are always removed
from other end called the front end. The people standing in a railway reservation row are an
example of queue. Each new person comes and stands at the end of row (rear end of
queue), and persons getting their reservations confirmed, get out from the front erid. The
representation of queue is shown in Figure 1.11.
12 C ~~------------
.:·::o~•~D~a~ta~S~tr~uc~tu~r,::es~U~s~in~g~----=---
Queues can also be implemented in two ways:
i) Using arrays.
ii) Using pointer.
7 16
72 85
50 65 rear
front
Figure 1.11 Queue

1.5.5. Trees - . ) Tree is non-linear type of data


A tree can be defined as finite set of data items (no_des · rt d sequence.' Trees represent
structure in which data items are arranged.or stored 111 a so e · ,
the hierarchical relationship between various elements. In trees: t f ti t
_ f h' 11y called the roo o 1e ree.
I. There is a special data item at the top o . 1erarc b · of mutually exclusive . (1.e
.
2. The remaining data items are partitioned mto num ~r . 11 d th subtree. ·,
disjoint) subsets, each of which is itself, a tree, which is ca e e .
· · d ta structures unhke natural
3. · The tree always grow in length towards bottom m a '
trees which grows upwards.
1 12
The tre~ structure organizes the data into branches, which· is sh?w.n in Figur~ - · In
general, this type of structure is very useful.

Figure 1.12 Binary Tree .

1.5.6. Graph
Graph is a mathematical non-linear data structure capable of representing many kinds of
physical structures. It has found applications in diverse field like Geography, Chemistry and
Engineering Sciences.
Defining Graph
A graph G( V, E) is a set of vertices v and a set of edges e. An edge connects a pair of
vertices and many have weight such as length, cost or ~nother measurin.g instruments for
recording the graph Verti lntroduc:1/on lo Algorithrm • 1.2 I
· ces on the Ourapt1
drawn as arcs or line scomc 11 t Tl ate s11own as point or circles and edges are
· His on edg
0

are pair of vertices. The v rt' . b


c can c represented as ~=(v, w) where v and w
e ices ., and .., I' " .
edges, arcs, line segmc,1t as roads. •eon e, vertices may be considered as cities and
Types of Graph

The graphs are classified in the fi . .


0 11
owing categories:
i) Directed Graph
ii) Non-directed Graph
iii) Connected Graph
iv) Non-connected Graph
v) Simple Graph
vi) Multi-Graph

1.6 Data Structure Operations


Large number of operations can be performed on data structures. Some of the important
operations are SJ.1elled out below.
I. Creating
A data structure say, queue is created from the
data.
2. Inserting
New items are added to the data structure.
3. Modifying
The values of the structures are modified by
replacing old values of the fields with new values.
4. Traversing
Each field/node in the structure is- visited for
processmg purposes.
5. Searching Certain record with key is searcheEI in the structure,
record n:iay or may not exist in the data structure_.
.6. Deleting Deleting is a process of removing a record from
the data structure.
~.
7. Sorting.· Records are sorted an ascending or descending
key in alphabetic or numeric order.
8. ~erging .Reccrds ·in Jll~re than one sorted file are merged
together fo produce a-single sorted file.
9. Copying Records in one file am copied to another file.
1.22 *Data Structures Using C Records of a file ' are appended at th~ end of
I0. Concatenating: another file.
in a vet"\/ big file are split into smaller fil
11 . Splitting Records •; 1 es

for pro.cessing.

1.7 Abstract Data Types (ADT)


ADT is a useful tool for specifying the logical properties of a data typ~- A data type is,
· . I 5 The collection and operati
co1Iect10n of values
. and a set of operations on
. those va tued · sing a particular hardwar ons
1
tionn a mathematical construct that may be imp emen e u e or
software data structure. In defining an abstract data type as a mathematical concept, we are
not concerned with space or time efficiency. Those are implementation issues. The definition
of ADT is not concerned with implementation details at all. ·

The specification of an abstract data type includes:

i) The domain of values.


ii) The operations on the 'domain.
Objects such as lists, sets, strings and graphs, along \,'Vith their operations·, can be viewed as
abstract data types.The elements of a set are considered the domain of va!ues and union,
intersection, size and comple~ent are considered as the operati~ns on th~ domain.Similarly,
when we -implement string as an ADT, the operations are I •

• string con~atenation
• string length
· • string compare
. . -
The basic idea is that the implementation of these operations is written once in the program,
and any other part of the program that needs to perform an operation on the ADT can do so
by_calling the appropriate· function. If for some reason ·implementation details need to be
changed, it should be easy to do so by merely changing the routines that perfo,rm the ADT
9peration. This change would be completely transparent to the rest of the program.
. .
There is no rule telling us which operations must be supported for eac~ ADT; this is a design
decision. Some data structures that we will study in this book are primarily examples of
ADT. We will see how each can be implemented, but if they are done correctly, the programs
that use them will not need to know which implementation was used.

You might also like