Beruflich Dokumente
Kultur Dokumente
Example: Integer
Operations addition, subtraction, multiplication,
division and modulus
There is no rule telling us which operations must be
supported for each ADT.
This is a decision made for the given problem.
Example
Example - Abstract stack
Could be defined by three operations:
push, that inserts some data item onto the
structure
pop, that extracts an item from it (with the
constraint that each pop always returns the most
recently pushed item)
peek, that allows data on top of the structure to
be examined without removal.
Prof. Gayathri. P, SCOPE, VIT
Typical ADTs:
Lists
Stacks
Queues
Trees
Heaps
Graphs
Arrays
Array is a data structure, which provides the facility
to store a collection of data of same type
under single variable name.
Continuous storage structure
Elements are sequential
Array-Based Implementation
of Lists
An array is a viable choice for storing list elements
Element are sequential
Algorithm development is easy
Normally sequential orderings of list elements.
Array Implementation
Elements are stored in contiguous array
positions
Operations
printList: print the list
find: locate the position of an elements in a list
list: 34,12, 52, 16, 12
find(52) 3
Operations
Insert
Shift elements to
right of insertion point
Delete
Shift elements back
Also adjust
size up or
down
10
Bad things:
Slow deletion and insertion of elements
Size must be known when the array is created and
is fixed (static)
11
12
STACKS
- LIFO
Stack of plates
Stack of papers
Stack of folded towels
In particular the last item to be added to stack is the first item
to be removed
13
Operations On Stack
PUSH: is the term to insert an element into a stack
POP: is the term to delete an element from a stack
Example: Suppose the following 6 elements are pushed in order
AAA
onto an empty stack
BBB
AAA, BBB, CCC, DDD, EEE, FFF
CCC
FFF
This means:
DDD
EEE
DDD
FFF
TOP
CCC
BBB
AAA
14
PUSH (ITEM)
1. If TOP == MAX, then Print OVERFLOW and Return.
2. Else Set TOP = TOP + 1
3.
Set STACK [TOP] = ITEM.
4.
Return
POP ()
1. If TOP == 0, then Print UNDERFLOW and Return.
2. Else Set ITEM = STACK[TOP]
3.
Set TOP = TOP - 1
4.
Return
15
QUEUES - FIFO
Queue is a linear structure in which Deletions can take place
at one end only, called the Front and Insertions can take place
only at other end, called the Rear.
Three everyday examples of such a structure
BBB
CCC
DDD
AAA Front
DDD Rear
16
Representation of Queues
17
AAA
1
BBB CCC
2
3
DDD
4
.
5
.
6
BBB
2
CCC
3
DDD
4
.
5
.
6
BBB
2
CCC
3
DDD
4
EEE
5
FFF
6
CCC
3
DDD
4
EEE
5
FFF
6
18
ENQ (ITEM)
1.
2.
3.
4.
If REAR = N
Then Print OVERFLOW and return
If FRONT= 0 //queue is initially empty
Then Set FRONT=1 and REAR=1
Else Set REAR=REAR+1
Set QUEUE[REAR]=ITEM
Return
19
DEQ ()
1. If FRONT=0, then print UNDERFLOW and Return
2. Set ITEM=QUEUE[FRONT]
3. If FRONT=REAR, then //Queue has only one element]
Set FRONT=0 and REAR=0
Else Set FRONT=FRONT+1
4. Return
20
Stack applications
Expression evaluation
Balancing parenthesis
21
Arithmetic Expressions
Precedence Level
Highest
Next Highest
Lowest
Exponentiation ( )
Multiplication (*) and Division ( / )
Addition (+) and subtraction (-)
Infix Notation
A+B
Prefix Notation
+ AB
Postfix or Suffix Notation (Reverse Polish Notation)
AB +
22
Evaluation of Expression
23
Q: A + ( B * C - ( D / E
F ) * G ) * H
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Symbol
(1) A
(2) +
(3) (
(4) B
(5) *
(6) C
(7) (8) (
(9) D
(10) /
(11) E
STACK
+
+(
+(
+(*
+(*
+(+(- (
+(- (
+(- (/
+(- (/
Expression P
A
A
A
AB
AB
ABC
ABC *
ABC *
ABC *D
ABC *D
ABC *DE
24
Q: A + ( B * C - ( D / E
F ) * G ) * H
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Symbol
(12)
(13) F
(14) )
(15) *
(16) G
(17) )
(18) *
(19) H
(20)
P:
STACK
+(- (/
+(- (/
+(+(-*
+(-*
+
+*
+*
Expression P
ABC *DE
ABC *DEF
ABC *DEF
ABC *DEF
ABC *DEF
ABC *DEF
ABC *DEF
ABC *DEF
ABC *DEF
/
/
/G
/G*/G*/G*-H
/G*-H*+
25
Algorithm:
1.
2.
3.
4.
5.
6.
7.
POLISH (Q, P)
Evaluation
This Algorithm Finds the VALUE of an Arithmetic Expression
P Written in Postfix Notation.
1. Scan P from left to right and repeat step 3 and 4 for each
element of P until end of expression is encountered.
2.
If an operand is encountered, put it in STACK.
3.
If an operator X is encountered then:
a) Remove the two top elements of STACK
b) Evaluate T2 X T1
c) Place the result of (b) back on STACK.
4. [End of IF Structure]
5. [End of STEP1 loop]
6. Set VALUE equal to the top element on STACK.
7. Exit
Prof. Gayathri. P, SCOPE, VIT
27
1.
2.
3.
4.
5.
6.
7.
8.
9.
Q:
5 * ( 6 + 2 ) 12 / 4
P:
5 , 6 , 2 , + , * , 12 , 4 , / , Symbol Scanned
5
6
2
+
*
12
4
/
Prof. Gayathri. P, SCOPE, VIT
STACK
5
5,6
5,6,2
5,8
40
40,12
40, 12, 4
40, 3
37
28
Queue applications
Printer
Device connected to a network
Client-server communication
29