Professional Documents
Culture Documents
Lecture 1,2,3
• Quizzes : (Unannounced)
• Assignments : 1 Every 3 weeks
FURC
Why Data Structures?
• All types of tasks require some kind of data to be manipulated. By
creating new ways to manage (access and change) data, we can
make programs more efficient, and thus obtain more reliable and
faster results. Different types of programs require different ways of
handling data, however, standards exists between various
programs. This course gives you a peek into those standards, into
the world of data structures.
FURC
Data Structures: Definition
• Data structures are some objects that we generate to
store data in them , these data can be
– Usually more than one piece of data
– Should provide legal operations on the data
– The data might be joined together (e.g. in an array): a
collection
FURC
Data Structures: Definitions
• A collection of data elements whose organization is
characterized by accessing operations that are used
to store and retrieve the individual data elements;
• The logical arrangement of data as used by a system
for data management; a representation of a data
model in computer form
FURC
Abstract Data Type ADT
• A data type whose properties (domain and
operations) are specified independently of any
particular implementation.
FURC
Abstract Data Types (ADTs)
FURC
Introduction to Data Structures
FURC
Data Structures
• A data structure is a user-defined abstract data type
• Examples:
– Complex numbers: with operations +, -, /, *, magnitude,
angle, etc.
– Stack: with operations push, pop, peek, isempty
– Queue: enqueue, dequeue, isempty …
– Binary Search Tree: insert, delete, search.
– Heap: insert, min, delete-min.
FURC
Data Structure Design
• Specification
– A set of data
– Specifications for a number of operations to be performed
on the data
• Design
– A lay-out organization of the data
– Algorithms for the operations
• Goals of Design: fast operations
FURC
Implementation of a Data
Structure
• Representation of the data using built-in data types
of the programming language (such as int, double,
char, strings, arrays, structs, classes, pointers, etc.)
FURC
OOP and Data Structures
• When implementing a data structure in non-OOP languages
such as C, the data representation and the operations are
separate
FURC
Data Structures: More specifically
– A data structure is a way of grouping fundamental types
(like integers, floating point numbers, and arrays) into a
bundle that represents some identifiable thing.
• For example, a matrix may be thought of as the bundle of the
number of rows and columns, and the array of values of the
elements of the matrix. This information must be known in order
to manipulate the matrix.
FURC
Data Structures: More specifically
• Data structure: An arrangement of data in a
computer’s memory
– Arrays, linked lists, stacks, trees, hash tables.
FURC
ADT and Data Structures
• ADT is implementation independent. For example, it only
describes what a data type List consists (data) of and what are
the operations it can perform, but it has no information about
how the List is actually implemented.
• Whereas data structure is implementation dependent, as in the
same example, it is about how the List is implemented i.e.,
using array or pointer. Ultimately, data structure is how we
implement the data in an abstract data type.
FURC
Why different data structures
• Each data structure has different advantages and
disadvantages, and will be useful for different types
of applications.
– Example: arrays
– Fast access: if we know the index of the item we are
looking for
– Deletion is slow, size is fixed.
FURC
A Real Life Example • Lisa
Electronic Phone Book • Michele
• John
Contains different DATA: • 110
- names • 622-9823
- phone number • 112-4433
• 75
- addresses • Bronson
Need to perform certain OPERATIONS: • Paola
- add
- delete
- look for a phone number
- look for an address
FURC
The first Data Structure
• An Array!
The simplest form of an Array is a one dimensional array
that may be defined as a finite ordered set of homogenous
elements
• For Example
int a[100];
FURC
Basic Operations
• Extraction
– A function that accepts an array “a” and an index “i”, and
returns an element of the array.
– Example
a[i]
• Storing
– It accepts array “a” an index “i” and an element x.
– Example
– a[i]=x
FURC
One Dimensional Array
• range = upper - lower+1 Lower
Bound [0]
Upper
Bound
FURC
Initializing an Array
What is the output of the following array?
• int values[5]={1,2,3};
• double junk[5]={0};
• double junk[5]={ };
FURC
Implementation of 1Dimenional
Array
• int b[100];
– Reserves 100 successive locations, each large enough to contain a single
integer.
• The address of the first of these locations is called the Base
Address: base(b)
• Reference to element b[0] is to the element at location base(b)
• Reference to b[1] is to the element at
base(b) + 1
Hence
b gives you the starting memory address of the array b
FURC
Memory view of an array
int a[5] 2 3 4 7 8
FURC
Array of Characters
a e I o u
• Char vowels[5]={‘a’, ’e’ ,’I’, ’o’, ’u’,}
• Char vowels[ ]={‘a’, ’e’ ,’I’, ’o’, ’u’,}
void main(void)
{
char len[25];
cin >> len;
cout << len_str(len) << endl << len_str_while(len);
}
//function with for loop
int len_str(char s[25])
{
for (int i = 0; s[i] != '\0'; i++);
return i;
}
• struct name
{
type field_name1;
type field_name2; ...
};
where
– name is the name of the record type established by the struct,
– each type field_name defines a field in the record,
– the field definitions are separated by semicolons, and
– the struct has a trailing semicolon.
• emp.age = 20
• Cin >> emp.age
• Cout << emp.age
2 3 7 8
How to add 4
2 3 4 7 8