Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Chapter II:
Data Structures
FAP:UC-BCF 1
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 2
Terminologies
Data Objects:
Refer to the set of elements
FAP:UC-BCF 3
Terminologies
Data Structures:
FAP:UC-BCF 4
Records
A structure that can have a number of heterogeneous
elements.
Declaration: RecordType
typedef struct field1
{
field2
<data type1> field1;
<data type2> field2; field3
<data type3> field3; .
<data typeN> fieldN;
.
}RecordType;
fieldN
FAP:UC-BCF 5
Records
Field1, field2, field3 … fieldN can be of any data type (i.e. integer,
float, real, record)
Forms:
- one-dimensional array
- n-dimensional array
FAP:UC-BCF 7
Arrays
2 data types
Base type of component type
Index type
Declaration
int A[10]; char B[45];
2 basic operations
Extraction
Storing
FAP:UC-BCF 8
Arrays
If an array is declared to be A[n], then:
n = number of elements
FAP:UC-BCF 9
Arrays
To determine the ith element of a Single-dimension
array:
A[i] = α + (i) * esize
where:
α - base or starting address
i – element
esize – element size in bytes
FAP:UC-BCF 10
Arrays
To determine the ith element of a Two-
dimension array:
A[i][j] = α + [(i)*(UB2)+(j)] * esize
where:
UB2 – upper bound of the 2nd dimension
α - base or starting address
esize – element size in bytes
FAP:UC-BCF 11
Arrays
To determine the ith element of a Three-
dimension 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
α - base or starting address
esize – element size in bytes
FAP:UC-BCF 12
Arrays
Exercises:
FAP:UC-BCF 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;
a. compute the address of element A1[120][3][3] given the
base address at 2000.
b. Assume that we do not know the size of RECTYPE in
number of bytes but we know that the address of
A1[20][2][3] is 2160. Give the size of RECTYPE in
bytes. Assume the base address is at 2000.
FAP:UC-BCF 14
Stacks
An ordered list in which all insertions and deletions are
made at one end called the TOP.
A B C D E F
TOP
A B C D E F G
TOP
FAP:UC-BCF 15
Stacks
Operations:
Create(top) – Create an empty stack
Push(Stack, top, item) – Inserts an element item into the stack
Pop(Stack, top, item) – Removes the top element of the stack
and stores the value in item
S_top(Stack, top) – Returns the top element of the stack
Empty(top) – Determines whether the stack is empty or not.
Stack full: Top = n-1
Stack empty: Top = -1
FAP:UC-BCF 16
Stacks
Representation:
One-dimensional array
A B C D E F
TOP
Singly linked-list
A B C D
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;}
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.
FAP:UC-BCF 23
Evaluation of Expressions
Our concern: how the expressions are evaluated
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
Conversion from Infix to Postfix
using Stacks
IN-Stack Priority (ISP) – The priority of the operator as
an element of the stack.
FAP:UC-BCF 27
Conversion from Infix to Postfix
using Stacks
Algorithm:
void POSTFIX(expression E)
token x,y;
FAP:UC-BCF 29
Conversion from Infix to Postfix
using Stacks
Algorithm:
Do steps 1-3 while x is not #, else do step 4
1. Get the token from E and save it as X
2. If X is an operand, immediately print in the output
else check if X == ‘)’ pop operators in the stack and print in the output.
delete ‘)’
else while ISP of top of stack (Y) >= ICP of X,
Pop and print in the output.
Push X
3. Process the next token
4. If stack is not empty, pop and print until not #.
FAP:UC-BCF 30
Queues
An ordered list which all insertions take place at one
end, called the REAR, while all deletions take place at
the other end, called the FRONT.
FIFO (First-In-First-Out)
Elements are processed in the same order as they were
received. The first element inserted in the queue will be
the first one to be removed.
FAP:UC-BCF 31
Queues
Conventions for FRONT and REAR:
Front is always 1 less than the actual front of the queue.
Rear always points to the last element in the queue.
Initial value: Front = Rear = -1
Operations:
Createq(Front, Rear) – creates an empty queue
Insert(Queue, Rear, Item) – inserts the element item to the rear
of the queue.
Delete(Queue, Front, Rear, Item) – removes the front element
from the queue and assigns is to variable item.
Qfront(Queue, Front, Rear) – returns the front element of the
queue
FAP:UC-BCF 32
Queues
Qempty(Front, Rear) – determines if the queue is empty or not
Returns 1 if true
Otherwise return 0
FAP:UC-BCF 33
Queues
Representation
One-dimensional Array
A B C D E F G
Front Rear
Singly linked-list
A B C D
Rear Front
FAP:UC-BCF 34
Queues
Declaration:
FAP:UC-BCF 35
Queues
Example:
Processing of customers’ transactions (i.e. cashiers, bank-related)
FAP:UC-BCF 38
Circular Queues
.. n-1
.. 0
4 1
3 2
FAP:UC-BCF 39
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;}
}
FAP:UC-BCF 40
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 41
Exercises
Convert the following infix expressions to postfix and
prefix:
1. A+B*C/D
2. A/B^C+D*E-A*C
3. (A+B)*D+E/(F+A*D)+C
4. A+B*D^E^F^G/H^J*K-L
5. !(A&&!(B<C)||(C>D))||(C<E)
FAP:UC-BCF 42
Exercises
Convert the following prefix expressions to infix:
6. -+-^ABC*D^EFG
7. ^+-ABC+D-EF
8. || || &&ABC!<=EF
FAP:UC-BCF 43
Exercises
Convert the following infix expressions to postfix
using stack:
12. B+C^(E+F*G^H)/(K-L/M)+N
13. B+C/D^(E+F*G^H)+Z
14. A+B-C*D*(E+F-G*H^I^J)+L/M+(N*O/P+
(Q^R^S^T))+U
FAP:UC-BCF 44