Basic Data Structure
Basic Data Structure
Data
Structure
What is Data Type?
•
Negative numbers in binary
• Converting from decimal to binary
– Apply Two’s complement:
1. Write number in binary
2. Invert the digits
3. Add 1
1000.112 = 1.000112 x 23
= (-1)0 x (1 + 0.000112) x 23
S=0
F = 00011
E-bias = 3
E-127 = 3
E=13010 =100000102
IEEE-754 Single Precision Floating-Point Numbers of decimal value 8.75 is
0 10000010 00011000000000000000000
Conversion from binary to decimal
Example 2. Determine the decimal value of
0 10000000 110000000000000000000002
based on IEEE-754
Based on this format:
0 10000000 110000000000000000000002
S E F
• Access an item
• Insert a new item
• Delete an item
One dimensional Array in C++
Address of Array Elements
Row Major and Column Major
Address Calculation
•In computing, row-major order and column-
major order are methods for storing
multidimensional arrays in linear storage such
as random access memory
•Different programming languages may use
different data layout when passing arrays
Row versus Column-wise based on
Programming Language
Calculating address of array
element (Row-wise)
•
Calculating address of array
element (Column-wise)
•
Two dimensional Array in C++
Three dimensional Array in C++
Two dimensional Array in SciLab
Two dimensional Array in MatLab
Example
Question:
Using column-wise method, calculate the address of element
indexed as [5, 4, 7, 5] in an integer array of dimensions
6x7x8x9, where the base address, B = 173.
Answer
Question:
Using column-wise method, calculate the address of element indexed as
[5, 4, 7, 5] in an integer array of dimensions 6x7x8x9, where the base
address, B = 173.
Example
Question:
Using row-wise method, calculate the address of element
indexed as [5, 4, 7, 5] in an integer array of dimensions
6x7x8x9, where the base address, B = 173.
Can you find the answer?
Question:
Using row-wise method, calculate the address of element
indexed as [5, 4, 7, 5] in an integer array of dimensions
6x7x8x9, where the base address, B = 173.
The answer is…
Question: Using row-wise method, calculate the address of element indexed as [5, 4, 7, 5] in
an integer array of dimensions 6x7x8x9, where the base address, B = 173.
Pointers
• Stores addresses of the next data
• Self referential structure
• Dynamic Memory allocation
– location not fixed
– memory location not contiguous
15 10 \
Operation with Pointers
• Declare pointers
• Define variables of pointer
• Access values at address
• Insert a pointer
• Delete a pointer
Class
• Implemented in Object-Oriented Programming
• Class describes attributes and methods of
objects that belong to it
• Attributes: variables (data)
• Methods: functions
• Objects: instance of a class
Example of Class
● Menu is a Class
● menu1 and obj are objects of class Menu
Implementation of Menu class
From Data Types
DataTypes to
TypesAbstract Data
• Data type
– a set of objects + a set of operations
– Example: integer
• set of whole numbers
• operations: +, -, x, /
• Can this be generalized?
– e.g. procedures generalize the notion of an operator
– Yes!
Abstract Data Types (ADT)
Examples of ADT…
• Examples
– the set ADT
• A set of elements
• Operations: union, intersection, size and complement
– the queue ADT
• A set of sequences of elements
• Operations: create empty queue, insert, examine, delete, and
destroy queue
• Two ADT’s are different if they have the same underlying model but
different operations
– E.g. a different set ADT with only the union and find
operations
– The appropriateness of an implementation depends
very much on the operations to be performed
Pros and Cons