Data Structures IT
Data Structures IT
Chapter II:
Data Structures
FAP:UC-BCF
Terminologies
Data Types:
Refer to the different kinds of data that a variable may hold in a
programming languages
Examples:
integer
real
float
char
string
FAP:UC-BCF
Terminologies
Data Objects:
Refer to the set of elements
Example: data object integer refers to
D = {0, 1, 2, 3, }
FAP:UC-BCF
Terminologies
Data Structures:
FAP:UC-BCF
Records
A structure that can have a number of heterogeneous
elements.
Declaration:
typedef struct
{
<data type1>
<data type2>
<data type3>
<data typeN>
}RecordType;
RecordType
field1
field1;
field2;
field3;
fieldN;
field2
field3
.
.
fieldN
FAP:UC-BCF
Records
Field1, field2, field3 fieldN can be of any data type (i.e. integer,
float, real, record)
To define a variable for the record:
RecordType A;
Assigning a value to a record field:
A.field1 = <value>;
A.field2 = <value>;
To retrieve a value from a record field:
printf(%d, A.field1);
printf(%c, A.field2);
printf(%d:4:2, A.field3);
FAP:UC-BCF
Arrays
Consecutive set of memory locations is a set of pairs
index and a value finite, ordered set of homogeneous
elements.
Forms:
- one-dimensional array
- n-dimensional array
FAP:UC-BCF
Arrays
2 data types
Declaration
2 basic operations
Extraction
Storing
FAP:UC-BCF
Arrays
If an array is declared to be A[n], then:
n = number of elements
FAP:UC-BCF
Arrays
To determine the ith element of a Single-dimension
array:
A[i] = + (i) * esize
where:
10
Arrays
To determine the ith element of a Twodimension array:
A[i][j] = + [(i)*(UB2)+(j)] * esize
where:
UB2 upper bound of the 2nd dimension
FAP:UC-BCF
11
Arrays
To determine the ith element of a Threedimension array:
A[i][j][k]= +[(i)*(UB2)*(UB3)+(j)*(UB3)+(k)]*esize
where:
UB3 upper bound of the 3rd dimension
UB2 upper bound of the 2nd dimension
FAP:UC-BCF
12
Arrays
Exercises:
1. Given A[10][3][3][6], =2000, esize=4 bytes:
a.find the formula to represent an element in a 4dimensional array.
b.find the total number of elements
c. find the address of A[2][2][0][4]
13
Arrays
3. Consider the following declaration:
typedef struct{
int A;
char B[10];
float C;
char D;
}rectype;
typedef rectype matrix[121][4][5];
matrix A1;
14
Stacks
An ordered list in which all insertions and deletions are
made at one end called the TOP.
LIFO (Last In First Out)
A
TOP
TOP
FAP:UC-BCF
15
Stacks
Operations:
FAP:UC-BCF
16
Stacks
Representation:
One-dimensional array
A
TOP
Singly linked-list
A
TOP
FAP:UC-BCF
17
Stacks
Declaration
#define n <constant value>
typedef <data type> elementtype;
typedef elementtype Stack[n];
Example:
Processing of procedure calls and their terminations
FAP:UC-BCF
18
Stacks
Procedures for Stack Operations:
void create(int *top)
{*top = -1;}
void push(stack S; int *top;elementtype item)
{
if(top==n-1)
stackfull;
else {
*top++;
S[*top] = item;
}
}
FAP:UC-BCF
19
Stacks
void pop(stack S; int *top; elementtype *item)
{
if(top==1) stackempty;
else{ *item = S[top];
*top--;}
}
elementtype s_top(stack S; int top)
{
if(top==-1) error_routine;
else return S[top];
}
int empty(int top)
{
if(top == 1) return 1;
else return 0;
}
FAP:UC-BCF
20
Stacks
void main()
{ create(&top1); create(&top2);
s_empty = empty(top1);
printf(%d, s_empty);
push(s1, &top1, 16);
s_empty = empty(top1);
printf(%d, s_empty);
push(s1, &top1, 10); push(s1, &top1, 9);
push(s1, &top1, 8); push(s1, &top1, 7);
j=s_top(s1, top1);
printf(Top is %d, j);
printf(%d\n, top1);
FAP:UC-BCF
21
Stacks
s_empty = empty(top2);
printf(%d, s_empty);
push(s2, &top2, 10); push(s2, &top2, 9);
push(s2, &top2, 8); push(s2, &top2, 7);
push(s2, &top2, 12); push(s2, &top2, 4);
pop(s1, &top1, &item);
pop(s2, &top2, &item);
j=s_top(s2, top2); printf(Top is %d, j);
printf(%d\n, top2);
}
FAP:UC-BCF
22
Evaluation of Expressions
Expression is made up of operands, operators and
delimiters.
Operands can be any legal variable names or constants
in programming languages.
Operations are described by operators:
Basic arithmetic operators: + - * /
Unary operators: - +
Relational operators: ==, >, <, >=, <=
FAP:UC-BCF
23
Evaluation of Expressions
Our concern: how the expressions are evaluated
The compiler accepts expressions and produce correct result by
reworking the expressions into postfix form. Other forms include
infix and prefix.
FAP:UC-BCF
24
Evaluation of Expressions
Expression: A + B
Prefix : +AB
Postfix: AB+
Expression: (A+B*C)/D
Prefix : /+A*BCD
Postfix: ABC*+D/
FAP:UC-BCF
25
ISP
ICP
-3
2
1
0
-4
2
1
4
26
FAP:UC-BCF
27
}
x = nexttoken(E);
}
if(!empty(stack))
{ while stack[top] != #
{ pop(y);
printf(y);
}
}
FAP:UC-BCF
29
Queues
FAP:UC-BCF
30
Queues
Conventions for FRONT and REAR:
Operations:
FAP:UC-BCF
31
Queues
FAP:UC-BCF
32
Queues
Representation
One-dimensional Array
A
Front
G
Rear
Singly linked-list
A
Rear
FAP:UC-BCF
D
Front
33
Queues
Declaration:
#define n <constant value>
typedef <data type> elementtype;
typedef elementtype Queue[n];
typedef int FR
FR front, rear;
Queue Q;
FAP:UC-BCF
34
Queues
Example:
Processing of customers transactions (i.e. cashiers, bank-related)
35
Queues
void delete(queue Q, FR *front, FR *rear, elementtype
*item)
{ if(*front == rear) queueempty;
else { (*front)++;
item = Q[front];}
}
elementtype qfront(queue Q, FR front, FR rear)
{ if(front == rear) errorroutine;
else return(Q[front + 1])
}
int quempty()
{ if(front == rear) return 1;
else return 0;
}
FAP:UC-BCF
36
Queues
Notes:
QUEUEFULL signal does not necessary imply that
there are N elements in the queue.
To solve this, move the entire queue to the left so that
the first element is again at Q[0] and front = -1.
FAP:UC-BCF
37
Circular Queues
..
..
3
FAP:UC-BCF
n-1
2
38
Circular Queues
void insert(queue *Q, FR front, FR *rear, elementtype
item)
{ if(front == (rear+1)%n) queuefull;
else { *rear = (*rear+1)%n;
Q[*rear] = item;}
}
void delete(queue Q, FR *front, FR rear, elementtype
*item)
{ if(front == rear) then cqueueempty;
else { *front = (*front+1)%n;
*item = Q[front]; }
}
FAP:UC-BCF
39
Circular Queues
elementtype cqfront(queue Q, FR front, FR rear)
{
if(front == rear) errorrouting;
else return(Q[front+1]%n);
}
int cquempty()
{
if(front == rear) return 1;
else return 0;
}
FAP:UC-BCF
40
Exercises
FAP:UC-BCF
A+B*C/D
A/B^C+D*E-A*C
(A+B)*D+E/(F+A*D)+C
A+B*D^E^F^G/H^J*K-L
!(A&&!(B<C)||(C>D))||(C<E)
41
Exercises
FAP:UC-BCF
42
Exercises
FAP:UC-BCF
43