0% found this document useful (0 votes)
36 views

1 Data Structures Introduction - 1

This document provides an introduction to data structures. It discusses that data structures are important for organizing data in a way that considers relationships between data items. Common data structure types include arrays, linked lists, stacks, queues, trees, and graphs. The document emphasizes that choosing an appropriate data structure impacts the efficiency of algorithms and programs. It also introduces some basic concepts like abstract data types, linear and tree structures, and the importance of algorithms for manipulating different data structures.

Uploaded by

njugunatracy10
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

1 Data Structures Introduction - 1

This document provides an introduction to data structures. It discusses that data structures are important for organizing data in a way that considers relationships between data items. Common data structure types include arrays, linked lists, stacks, queues, trees, and graphs. The document emphasizes that choosing an appropriate data structure impacts the efficiency of algorithms and programs. It also introduces some basic concepts like abstract data types, linear and tree structures, and the importance of algorithms for manipulating different data structures.

Uploaded by

njugunatracy10
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Data Structures; an Introduction

™The computer can be used for among many things:

™Scientific Calculations

™ Information Management System for an enterprise

™ Real-time control system for an assembly line

1
Introduction

‰In performing these operations, to use the computer


effectively one needs to:

a)Obtain the needed data and their relations

b)Store the data into the computer according to their


relations

c)Design algorithms to solve the problem

2
Introduction

‰ The tasks of programming and data processing


require efficient algorithms for accessing the
data both in main memory and on secondary
storage devices.
‰ This efficiency is directly linked to the structure
of the data being processed.
‰ A data structure is a way of organizing data that
considers not only the items stored but also their
relationship to each other.

3
Introduction

‰ The way data is accessed is a function of how it is


organized, thus a general understanding of data structures
is essential to developing efficient algorithms.

‰ A clear notion of the relative advantages and


disadvantages of each technique is obviously crucial to
those designing systems.

4
Introduction

‰ The ability to make correct decisions regarding which data


structures to use typically involve the following general issues:
a) The efficiency of the program with respect to its run time. Does
it perform the optimal number of operations to achieve its goal?
b) The efficiency of a program with respect to its utilization of
main memory and secondary storage devices. Does it
consume such resources in a fashion that makes its use
impractical?
c) The developmental costs of a program or system. Could a
different approach to the problem significantly reduce the total
person-hours invested in it? 5
Introduction
‰ Thorough knowledge of a programming language is not
a sufficient base upon which to make these decisions.
‰ The study of data structures will expose you to a vast
collection of tried and proven methods used in designing
efficient programs.
‰ In many applications, different types of data structures
could be used to achieve the same end. In such
situations, the designer plays a game of tradeoffs.
‰ One data structure sacrifices memory compactness for
speed; another utilizes memory efficiently but results in a
slow run time.
‰ For each positive there is seemingly a corresponding
negative. 6
Algorithms for Data structures

‰ Once a data structure has been chosen for a particular


application, it is given life by the logical instructions that
manipulate the related data items stored in it i.e. the
algorithm.
Desirable traits of an algorithm are (among others):
i. completely free of ambiguity.
ii. It must be efficient. It should not unnecessarily use
memory locations nor should it require an excessive
number of logical operations.
iii. Should be concise to facilitate verification of their
7
correctness.
Algorithms for Data structures
‰ Verification involves observing the performance of the
algorithm with a carefully selected set of test cases.
‰ These test cases should attempt to cover all of the
exceptional circumstances likely to be encountered by the
algorithm.
‰ These exceptional circumstances will depend upon the
data structure being manipulated.
‰ However, certain generic circumstances must be verified
for all algorithms manipulating data structures:
a)Does the algorithm work when the data structure is empty?
b)Does the algorithm work when the data structure is full?
c)Does the algorithm work for all the possibilities that can occur
between an empty structure and a full structure?
8
Algorithms for Data structures

‰The need for conciseness in algorithms becomes obvious


when you consider the problem of verifying their
correctness.
‰Hence, an algorithm should concern itself with one specific
problem.
‰Each algorithm is a logical module designed to handle a
specific problem relative to a particular data structure.
‰Such modules could be tied together by higher level
algorithms, which focus upon the connections between
modules rather than the underlying data structures. 9
Basic definitions

i. Data structures – This is a way of organizing or storing data usually in


computer memory.

ii. An abstract data type (ADT) - is a specification of a data structure and


the set of operations that can be performed on it. E.g a queue

iii. Abstraction - This is the separation between what a data structure


represents and what an algorithm accomplishes, from the
implementation details of how things are actually carried out. i.e., hiding
the unnecessary details

iv. Data Abstraction - Hiding of the representation details

v. Data Types - a data type, or type is a classification of data. It indicates


a set of values that have the same general meaning or intended
purpose, e.g primitive types(integers,strings),records,classes

10
Basic Data Types

‰The basic data types are also referred to as atomic types.


‰They consist of simple values
‰Other type of data are made by combining these values in
some way
‰Examples include
–Boolean,
–Integer
–Float / real
–character

11
Structured Data Types

‰A structured data type has a definition that imposes


structure upon its values.
‰In many structured data types, there is an internal structural
relationship, or organization, that holds between the
components.
‰For example, if we think of an array as a structured type,
with each position in the array being a component, then
there is a structural relationship of `followed by': we say that
component i is followed by component i+1.
‰Many structured data types do have an internal structural
relationship, and these can be classified according to the
12
properties of this relationship.
Structured Data types: Linear Structure

‰The most common organization for components is a linear


structure. A structure is linear if it has these 2 properties:

¾Property P1: Each element is ‘followed by’ at most one


other element.
¾Property P2: No two elements are ‘followed by’ the same
element.
‰An array is an example of a linearly structured data type.
We generally write a linearly structured data type like this:
A->B->C->D (this is one value with 4 parts).

‰Example 1 (violates P1): A points to B and C B<-A->C


‰Example 2 (violates P2): A and B both point to C
A->C<-B
13
Structured Data types: Classes: Tree Structure

‰If we drop the first constraint (P1)and keep


the second (P2) we get a tree structure or
hierarchy: no two elements are followed by the
same element. This is a very common structure
too, and extremely useful.

‰Example 1 in the slide before is a tree, but


Example 2 is not.
‰A is followed by B C D, B by E F, C by G.

14
Structured Data types: Classes: Graph Structure

Dropping both P1 and P2


‰If we drop both constraints, we get a graph.

15
Structured Data types: Classes: Cyclic Structures:

• All the examples we've seen are acyclic.


• This means that there is no sequence of arrows
that leads back to where it started.
• Linear structures are usually acyclic, but cyclic
ones are not uncommon.
• Example: Add an edge from G to D, and from E to
A.

16
Structured Data Types : Arrays
‰ An Arrays is a physically sequential, fixed size
collection of homogeneous objects.
‰ In addition, objects with the structure have the random
access property.
I. The array is physically sequential in that the data objects
are stored in consecutive memory locations.
II. The array has a fixed size in that its size can neither be
increased nor reduced though the number of items it
contains can vary
III. The array is homogeneous in that they are made up of
objects that are all the same type.
IV. The random access property means that the time it takes
to access one object in the structure does not depend on
what object in the structure had been accessed
17
previously.
Arrays

‰ An array type is appropriate for representing an abstract


data type when the following conditions are satisfied:
1)The data objects in the abstract data type are
composed of homogeneous objects
2)The solution requires the representation of a fixed,
predetermined number of objects

18
Example 1: String Values in an array and Getting the greatest value in
the array
#include <stdio.h>
int main()
{
const int SIZE=5;
int numbers[SIZE]={3,12,5,2,10};
int i;
int greatest;
greatest=numbers[1];
for(i=0;i<SIZE;i++)
{
if(numbers[i]>greatest)
{
greatest=numbers[i];
}
}
printf("\nThe greatest number in the array is
%d\n",greatest);
} 19
Example 2: Getting Array Values from the user

#include <stdio.h>
int main()
{
const int SIZE=5;
int numbers[SIZE];
int j;
int num, sum;
printf("\nProvide Five Integers to be stored in the
array");
printf("\n___________________________________________
_______");
for(j=0;j<SIZE;j++)
{
printf("\nEnter Value %d:::",j+1);
scanf("%d",&num);
numbers[j]=num;
20
}
Example 2: Getting Array Values from the user

//program continued
//get the sum of the numbers
sum=0;
for(j=0;j<SIZE;j++)
{
sum=sum+numbers[j];
}

//output the sum


printf("\nThe sum of the numbers you have provided is
%d\n",sum);

21
Multidimensional arrays

‰Multidimensional arrays can be described as arrays of arrays. For


example, a two-dimensional array consists of a certain number of rows and
columns:
‰A one-dimensional array is usually processed via a ‘for’ loop. Similarly, a
22
two-dimensional array may be processed with a nested for loop:
Adding The elements of a multi-dimensional Array
#include <stdio.h>
int main()
{
const int ROWS=3, COLS=3;
int matrix[ROWS][COLS]={{4,3,6},{5,6,3},{7,1,9} };
int i,j;
printf("\nOutput the elements of a
multidimensional array");
printf("\n___________________________________");
for(i=0;i<ROWS;i++)
{
printf("\n");
for (j=0;j<COLS;j++)
printf(" %d",matrix[i][j]);
}
printf("\n");
}
23
What would be the print out of the following initialization?
int Array1[2][3] = { {1, 2, 3} , {4, 5, 6} };
int Array2[2][3] = { 1, 2, 3, 4, 5 };
int Array3[2][3] = { {1, 2} , {4 } };

for (int row = 0; row < 2; row++)


{
for (int col = 0; col < 3; col++)
{
printf(“%d”,Array1[row][col]);
}
printf(“\n\n”);
}
24
Arrays:Disadvantages

1) The size of the array is fixed —


Most often this size is specified at compile time with a simple
declaration such as in the example above. With a little extra effort,
the size of the array can be deferred until the array is created at
runtime, but after that it remains fixed.
2) Because of (1), the most convenient thing for programmers to do
is to allocate arrays which seem "large enough" (e.g. the 100
items). Although convenient, this strategy has two disadvantages:
- most of the time there are just 20 or 30 elements in the array and
70% the space in the array really is wasted.
- If the program ever needs process more than 100 scores, the
code breaks.
3) Inserting new elements at the front or somewhere at the middle is
potentially expensive because existing elements need to be 25
shifted over to make room.
Exercise 1

1) A matrix can be represented using a two


dimensional array. Create a C program that does
the following
i. creates a three by three matrix.
ii. Allows a user to enter values into the elements of
the matrix
iii. Outputs the elements of the matrix
iv. Sums all the elements of the matrix and outputs
the result

26
Assignment 1 (Due on or before 25th
July)
1) Write a program that does the following
i. Stores the following values in a 3 by 3 matrix
9, 3, 18, 5, 8 ,4, 17, 9, 17
ii. Outputs the sum of the values in each of the
rows of the matrix
Note: Assessment is by presentation of the
running program in a computer

27

You might also like