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

Data Structures and Algorithms

1) The document discusses various data structures like arrays, stacks, queues, and their implementation and operations. 2) It explains concepts like records, arrays, stacks, queues, their representation using arrays or linked lists, and basic operations on each. 3) It also covers evaluation of expressions and conversion between infix, prefix and postfix notations using stacks.

Uploaded by

Maria Canete
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
132 views

Data Structures and Algorithms

1) The document discusses various data structures like arrays, stacks, queues, and their implementation and operations. 2) It explains concepts like records, arrays, stacks, queues, their representation using arrays or linked lists, and basic operations on each. 3) It also covers evaluation of expressions and conversion between infix, prefix and postfix notations using stacks.

Uploaded by

Maria Canete
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 46

Data Structures and

Algorithms

Contents:
Records, Arrays, Stacks,
Queues, Priority Queues
(Heaps), Deque and
Evaluation of expressions
Terminology
Data Types
Refer to the different kinds of data that
a variable may hold in a programming
language.
Examples: integer, real, float, char,
string
Data Objects

Refer to the set of elements


Example: data object integer refers
to D={0, 1, 2, 3,..}
Data Structures
 Describe the set of objects and how
they are related.
 Describe the set of operations that can
be applied on the elements of a data
object.
 Typically this describe the more complex
data types such as arrays, stacks, queues,
trees, graphs and operations of how to
manipulate these.
Records
A structure that can have a number
of heterogeneous elements
Declaration
typedef struct
{ <data type1> field1; RecordType
<data type2> field2; field1
field2
<data type3> field3;
field3
:
:
<data typeN> fieldN;
fieldN
} RecordType;
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:
cout<< A.field1;
cout<< A.field2 ;
cout<< A.field3;
Arrays

consecutive set of memory locations


is a set of pairs - index and a value
finite, ordered set of homogeneous
elements 1
forms 2
 one-dimensional array 3
4
 n-dimensional array
5
6
•2 data types
base type or component type
index type
•Declaration
int A[10]; char B[45];
•2 basic operations
extraction and storing
If an array is declared to be A[n], then:
n = number of elements

If an array is declared to be A[n][m], then:


n*m = number of elements

If given n-dimensional array declaration,


A[b][c][d][e]..[n] = b,c,..n
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

2-dimensional arrays:
A[i][j] =  + [(i) * (N2) + (j)] * esize
where: N2 = number of columns
3-dimensional arrays:
A[i][j][k] =  + [(i) * (N2) *(N3) + (j)*(N3)
+ (k)] * esize
where: N2 = num of elems in 2nd dimension;
N3 = num of elems in 3rd dimension
esize = element size in bytes

Typical esize: integer - 2 bytes


char - 1 byte
float - 4 bytes
Examples
1. Given A[10][3][3][6],  = 2000, esize=4 bytes:
a. find the formula to represent an element in a 4-
dimensional array.
b. find the total number of elements
c. find the address of A[2][2][0][4]

2. Given X[8][3][6][2][3], = 3000, esize = 3 bytes


a. find the total number of elements
b. find the address of X[0][2][5][1][2]
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 that the base address is 2000.
B. Assume that we do not 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
2000.
Stacks
An ordered list in which all insertions and
deletions are made at one end called the
TOP.
LIFO (last in first out) G
A D F
top
A D F G
top
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
Representation of stacks

•one-dimensional array
•singly linked-list

a b a c d e a
top

a b c e
top
Declaration
#define n <constant value>;
typedef <data type> elementtype;
typedef elementtype Stack[n];
Example:
 Processing of procedure calls and their

terminations
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;}
}
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
}
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);
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);
}
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: ==, >=, <=, >,<
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.

Prefix: <Operator> <Operand1>


<Operand2>
Postfix: <Operand1> <Operand2> <Operator>
Infix: <Operand1> <Operator> <Operand2>
Expression: A+B
Prefix: +AB
Postfix: AB+

Expression: (A + B * C) / D
Prefix: /+A*BC D
Postfix: ABC*+ D/
Conversion from infix to postfix
using stacks
IN-Stack Priority (ISP) - the priority of the
operator as an element of the stack
IN-Coming Priority (ICP) - the priority of
the operator as current token.
SYMBOL ISP ICP
) ----- -----
^ 3 4
*, / 2 2
+, - 1 1
( 0 4
RULE:

Operators are taken out of the stack (POP)


as long as their in-stack priority (ISP) is
greater than or equal to the in-coming
priority (ICP) of the new operator.

Note: ISP and ICP of # sign = -1


Algorithm:
void POSTFIX (expression E)
token x, y;

stack[0] = ‘#’; top = 0;


x = nexttoken(E); //takes the first token and
remove it from the original
expression
while x !=‘#’
{ if x is an operand printf(x);
else if x == ‘)’ {//unstack until ‘(‘
while stack [top] != ‘(‘
{ pop(y); printf(y);}
pop (y); //delete ‘(‘
}
else { while isp[stack[top]] >= icp[x]
{ pop(y); printf(y); }
push (x);
}
x = nexttoken(E);
}
if !(empty(stack))
{ while stack[top] != ‘#’
{ pop(y); printf(y);}
}
Queues

•An ordered list in 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.
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 it to variable Item.
 Qfront(Queue, Front, Rear) - returns the

front element of the queue.


 Qempty (Front, Rear) - determines if the

queue is empty or not.


Returns 1 if true
otherwise return 0
Front = Rear means queue is empty.
Representation of queues

one-dimensional array
singly linked-list
a b a c d e a
front rear

a b c e
rear front
Declaration

#define n <constant value>;


typedef <data type> elementtype;
typedef elementtype Queue[n];
typedef int FR
FR front, rear;
Queue Q;
Example:
 Processing of customers’ transactions (i.e.
cashiers, bank-related transactions)

Procedures for Queue Operations:

void createq(FR *front, FR *rear)


{*front = *rear = -1}

void insert(queue Q, FR *rear, elementype item)


{if (rear == n-1) queuefull;
else { (*rear) ++;
Q[*rear] = item;}
}
void delete(queue Q, FR *front, FR *rear,
elementype *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
}

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.
Circular Queues
.. n-1

.. 0

4 1
3 2

void createq(FR *front, FR *rear)


{*front = *rear = -1}
void insert(queue *Q, FR front, FR *rear,
elementype 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];}
}
elementtype cqfront(queue Q, FR front, FR rear)
{ if front==rear errorroutine
else return(Q[front+1] mod N)
}

Int cquempty()
{ if front== rear return 1
else return 0
}
Priority Queues (Heaps)
A special kind of queue in which deletion
of items from any position of the queue is
based on some conditions.
For instance, given a line printer, jobs are
sent to the printer queue. If a particular
job has a higher priority, then it is
reasonably to execute this job first than
other jobs in the queue.
If there are several one-page jobs and one
hundred-page job, it might be reasonable to
make the long job go last, even it is not the
last job submitted.

Deque
Double-ended queue is a linear list in which insertions and
deletions are made to or from either end of the structure.

deletion insertion
insertion deletion
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)
Prefix to infix

1. - + - ^ A B C * D ^ E F G
2. ^ + - A B C + D - E F
3. || || && A B C ! <= E F

Postfix to Infix
1. A B + C D E - F + * G - /
2. A B - C + D E F - + ^
3. A B && C || E F <= ! ||
Infix to Postfix use of Stack

1. B+C^(E+F*G^H)/(K-L/M)+N
2. B+C/D^(E+F*G^H)+Z
3. A+B-C*D*(E+F-G*H^I^J) + L/M+
(N*O/P+(Q^R^S^T))+U

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy