You are on page 1of 10

Introduction to Data Structures Using C

A data structure is an arrangement of data in a computer's memory or even disk storage.


An example of several common data structures are arrays, linked lists, queues, stacks,
binary trees, and hash tables.
Algorithms, on the other hand, are used to manipulate the data contained in these data
structures as in searching and sorting
Name Position
Aaron Manager
Charles V
!eorge "mployee
#ack "mployee
#anet V
#ohn resident
$im Manager
%arry Manager
Martha "mployee
atricia "mployee
&ick 'ecretary
'arah V
'usan Manager
(homas "mployee
)ack "mployee
*ut only one vie+ of the company is sho+n by the above list. ,ou also +ant the database
to represent the relationships bet+een employees and management at A*C. -t does not
tell you +hich managers are responsible for +hich +orkers and so on, although your list
contains both name and position. After thinking about the problem for a +hile. ,ou
decide that the tree diagram is much better structure for sho+ing the +ork relationships at
the A*C company.
(hese t+o above diagrams are examples of the different data structures. ,our data is
organi.ed into a list, in one of the data structures +hich is above. (he employees name
can be stored in alphabetical order so that +e can locate the employee's record very
quickly. /or sho+ing the relationships bet+een employees these structure is not very
useful. (he tree structure is a much better suited for this purpose.
(he data structures are an important +ay of organi.ing information in computers. (here
are many different data structures that programmers use to organi.e data in computers,
0ust like the above illustrated diagrams. 'ome of the data structures are similar to the tree
diagram because they are good for representing the relationships bet+een different data.
1ther structures are good for ordering the data in a particular +ay like the list of
employees +hich is sho+n above. "ach data structure has their o+n unique properties
that make it +ell suited to give a certain vie+ of the data.
*y the end of this tutorial, you should be able to do the follo+ing2
'ho+ ho+ data structures are represented in the computer,
-dentify linear and nonlinear data structures,
Manipulate data structures +ith basic operations, and
Compare different implementations of the same data structure.
Data Structures Using C Interview Questions
3hat is the data structures used to perform recursion4
%ist out the areas in +hich data structures are applied extensively4
3hat is data structure4
5ifference bet+een calloc and malloc in data structures4
3hat exactly does this procedure *6**%"7'1&( 8$, 9: does4
5efine addressing and linear addressing functions4
5efine -ndex area and its subdivisions4
5efine 'imulation4
3hat is the distant relationship bet+een a list structure and a digraph4
9ame t+o desirable properties of hashing functions.
Pointers and Indirection
(he problem +ith representing 5ata 'tructures 6sing C that are not linear. 3e need some
+ay to map these data structures to the computer's linear memory. 1ne solution to this, is
to use pointers.
ointers are the memory locations that are stored in the memory cells. *y using a pointer,
by holding a memory address rather than the data one memory cell can point to another
memory cell.
(he memory cell at address ;<<= contains a pointer the address of another cell, +hich can
be seen in the above figure. >ere the pointer is pointing to the memory cell ;<<? +hich
contains the letter C. (his means that, +e have t+o +ays of accessing the letter C. 3e can
refer to the memory cell +hich contains the value C directly or +e can use the pointer to
refer to it indirectly. (he process of accessing the data through pointers is kno+n as
indirection. 6sing pointers, +e can also create multiple levels of indirection.
ointers can become very complex and difficult to use by having many levels of
indirection. 3hen used pointers incorrectly, it can make data structures very difficult to
understand. (he tradeoff bet+een complexity and flexibility should be consider +henever
you use pointers in constructing data structures.
(he idea of pointers and indirection is not exclusive to the computer memory. ointers
appear in many different aspects of computer's usage. >yperlinks in +eb pages is a good
example pointers. (his links are really the pointers to the other +eb page. erhaps you
have even experienced the double indirection +hen you +ent to visit the familiar +eb site
and found the site had moved. ,ou sa+ a notice that the +eb pages had been moved and a
link to the ne+ site, instead of the page you expected.
Linear Data Structures
3e sa+ that it is very simple to create data structures that are organi.ed similar to the
+ay the computer's memory is organi.ed. /or example, the list of employee's from the
A*C company is a linear data structure.
A D V E R T I S E M E N T
'ince the computer's memory is also linear, it is very easy to see ho+ +e can represent
this list +ith the computer's memory. Any data structure +hich organi.es the data
elements one after the other is kno+n as linear data structure. 'o far +e have seen t+o
examples of linear data structures2 the string data structure and the A*C company list.
,ou may have noticed that these t+o examples of linear data structures resemble to each
other. (his is because they both are really different kinds of lists. -n general, all linear
data structures look like the list. >o+ever, this does not mean that all the linear data
structures are exactly the same. 'uppose - +ant to design a list to store the names of the
A*C employees in the computer. 1ne possible design is to organi.e the names similar to
the example picture above. Another possible design is to use the pointers +e learned
about in the last lesson. 3hile these t+o designs provide the same functionality the +ay
they are implemented in the computer is much different. (his means that there is an
abstract vie+ of a list +hich is distinct from any particular computer implementation
,ou may have also noticed that the picture of the A*C employees is not as exactly the
same as the original list. 3hen +e make a list of names, +e tend to organi.e this list into
a column rather than a ro+. -n this case, the conceptual or logical representation of a list
is the column of names. >o+ever, the physical representation of the list in the computer's
memory is the ro+ of strings. /or most data structures, the +ay that +e think about them
is far different from the +ay they are implemented in the computer. -n other +ords, the
physical representation is much different from that of the logical representation,
especially in data structures that use pointers.
Ordered List: The Abstract View
(he most common linear data structure used is the list. *y no+ you are already pretty
familiar +ith the idea of a list and at least one +ay of representing a list in the computer.
9o+ +e are going to look at a particular kind of list2 an ordered list. 1rdered lists are
very similar to the alphabetical list of employee names for the A*C company. (hese lists
keep items in a specific order such as alphabetical or numerical order. 3henever an item
is added to the list, it is placed in the correct sorted position so that the entire list is
al+ays sorted.
*efore +e consider ho+ to implement such a list, +e need to consider the abstract vie+
of an ordered list. 'ince the idea of an abstract vie+ of a list may be a little confusing,
let's think about a more familiar example. Consider the abstract vie+ of a television.
&egardless of +ho makes a television, +e all expect certain basic things like the ability to
change channels and ad0ust the volume. As long as these operations are available and the
(V displays the sho+s +e +ant to vie+, +e really don't care about +ho made the (V or
ho+ they chose to construct it. (he circuitry inside the (V set may be very different from
one brand to the next, but the functionality remains the same. 'imilarly, +hen +e
consider the abstract vie+ of an ordered list, +e don't +orry about the details of
implementation. 3e are only concerned +ith +hat the list does, not ho+ it does it.
'uppose +e +ant a list that can hold the follo+ing group of sorted numbers2 @; A B CD.
3hat are some things that +e might +ant to do +ith our list4 3ell, since our list is in
order, +e +ill need some +ay of adding numbers to the list in the proper place, and +e
+ill need some +ay of deleting numbers +e don't +ant from the list. (o represent these
operations, +e +ill use the follo+ing notation2
Add%ist-tem8%ist, -tem:
&emove%ist-tem8%ist, -tem:
"ach operation has the name and the list of parameters the operation needs. (he
parameter list for the Add%ist-tem operation includes a list and an item. (he
&emove%ist-tem operation is very similar except this time +e +ill specify the item +e
+ant to remove. (hese operations are part of the abstract vie+ of an ordered list. (hey are
+hat +e expect from any ordered list regardless of ho+ it is implemented in the
computer's memory.
Array Implementation
1ne approach to creating a list is simply to reserve a block of ad0acent memory cells to
large enough to hold the entire list. 'uch a block of memory is called as array. 1f course,
since +e +ant to add items to our list, +e need to reserve more than 0ust four memory
cells. /or no+, +e +ill make our array large enough to hold as much as six numbers.
Pointer Implementation
A second approach to creating a list is to link groups of memory cells together using the
pointers. "ach group of memory cells is called as a node. 3ith this implementation every
node contains the data item and the pointer to the next item in the list. ,ou can picture
this structure as a chain of nodes linked together by the pointers. As long as +e kno+
+here the chain begins, +e can follo+ the links to reach any item in the list. 1ften this
structure is called as a linked list.
9otice that the last memory cell in our chain contains the symbol called E9ullE. (his
symbol is a special value that tells us that +e have reached the end of our list. ,ou can
think that this symbol as a pointer that points to nothing. 'ince +e are using the pointers
to implement our list, the list operations Add%ist-tem and the &emove%ist-tem +ill +ork
differently than they did for sequential lists.
Trees: The Abstract View
Another common nonlinear data structure is the tree.
-n this diagram above, +e can see that the starting point, or the root node, is circled in
blue colour. A node is a simple structure that holds data and links to other nodes.
A D V E R T I S E M E N T
-n this case, our root node contains the data string E#ohnE and three links to the other
nodes. 9otice that the group of nodes circled in red do not have any links8childs:. (hese
nodes are at the end of the branches and they are appropriately called as leaves or leaf
nodes. -n our diagram, the nodes are interconnected +ith solid black lines called arcs or
edges. (hese edges sho+ the relationships bet+een the nodes in the tree.
1ne important relationship in the binary tree is the parentFchild relationship. arent nodes
have at least one edge to the node lo+er in the tree. (his lo+er node is called the child
node. 9odes can have more than one child, but the children can only have a single parent.
9otice that the root node has no parent, and the leaf nodes has no children. /inal feature
to note in our diagram is the subtree. At each level of the tree, +e can see that the tree
structure is repeated. /or example, the t+o nodes representing ECharlesE and E&ickE
compose a very simple tree +ith ECharlesE as the root node and and E&ickE as a single
leaf node.
(rees are implemented in the computer's memory. 3e +ill begin by introducing the
simple tree structure called the binary tree. *inary trees have the restriction that nodes
can't have more than t+o children. 3ith this restriction, +e can easily determine ho+ to
represent a single binary node in the memory. 1ur node +ill need to reserve memory for
the data and t+o pointers8for pointing t+o childs of that node:.
Structure of a binary node:
6sing our binary nodes, +e can construct a binary tree. -n the data cell of each node, +e
+ill can store a letter. (he physical representation of our tree might look something like
the figure belo+2
Introduction to Graphs
(he last data structure that +e +ill study in this tutorial is the graphs. !raphs are similar
to trees except that, they do not have as many restrictions.
A D V E R T I S E M E N T
-n the previous tutorial, +e sa+ that every tree has a root node, and all the other nodes in
the tree are children of a perticular node. 3e also sa+ that the nodes can have many
children but only one parent. 3hen +e relax these restrictions, +e get the graph data
structure. (he logical representation of a typical graph might look something like the
figure sho+n belo+2
-t is not hard to imagine ho+ the graph data structure could be useful for representing the
data. erhaps each of the nodes above could represent a city and the edges connecting the
nodes could represent the roads. 1r +e could use a graph to represent a computer
net+ork +here the nodes are +orkstations and the edges are the net+ork connections.
!raphs have so many applications in the computer science and mathematics that several
algorithms have been +ritten to perform the standard graph operations such as searching
the graph and finding the shortest path bet+een nodes of a graph.
9otice that our graph does not have any root node like the tree data structure. -nstead, any
node can be connected +ith any other node in the graph. 9odes do not have any clear
parentFchild relationship like +e sa+ in the tree. -nstead nodes are called as neighbors if
they are connected by an edge. /or example, node A above has three neighbors2 *, C, and
5.
9o+ that you have a basic idea of the logical representation of the graphs, let's take a
look at one +ay that graphs are commonly represented in computers. (he representation
is called an ad0acency matrix, and it uses the t+oFdimensional array to store the
information about the graph nodes. (he ad0acency matrix for our graph is given belo+.
A B C D E F
A
B
C
D
E
F
FF G G G FF FF
G FF G FF G FF
G G FF FF FF FF
G FF FF FF G G
FF G FF G FF FF
FF FF FF G FF FF
9otice that the matrix sho+n above has six ro+s and six columns labeled +ith the nodes
from the graph. 3e mark a 'G' in a cell if there exists an edge from t+o nodes that index
that cell. /or example, since +e have a edge bet+een A and *, +e mark a 'G' in the cells
indexed by A and *. (hese cells are marked +ith a dark gray background in the
ad0acency matrix. 3ith our ad0acency matrix, +e can represent every possible edge that
our graph can have.

You might also like