Chapter 4 Data Structures
Chapter 4 Data Structures
Introduction:
Data representation:
➢ Logical and mathematical description of structure.
Types of arrays
One-dimensional array
Two-dimensional array
Multi-dimensional array
Chapter 4 Data Structures
One-dimensional array:
Array can be denoted as,
Data_type array_name[size];
Features:
➢ Array size should be positive number only.
➢ String array always terminates with null
character (‘\0’).
➢ Array elements are counted from 0 to n-1.
➢ Useful for multiple reading of elements.
Chapter 4 Data Structures
One-dimensional array
Calculating the length of array:
L=UB-LB+1
Example: if an array A has values
10,20,30,40,50,60 stored in
locations 0,1,2,3,4,5 then UB=5
and LB=0.
Size of the array L=5-0+1=6
Chapter 4 Data Structures
Representation of one-dimensional arrays in memory:
LOC(A[P])=Base(A)+W(P-LB)
Example:
Address content location
1000 A s[0]
1001 B s[1]
1002 C s[2]
1003 D s[3]
1004 E s[4]
Address(s[3])=base(s)+w(p-LB)
= 1000+1(3-0)
=1003
Chapter 4 Data Structures
Basic operations on one-dimensional arrays:
1.Traversing
2.Searching
3.Sorting
4.Insertion
5.Deletion
6.merging
Chapter 4 Data Structures
Traversing a linear array
Algorithm:
1.for LOC=LB to UB
PROCESS A[LOC]
end of for
2. exit
Chapter 4 Data Structures
Searching an element
Search element =3 1≠3 1 A[0]
Linear Search:
Algorithm: 2≠3 2 A[1]
3 is at position 2
Chapter 4 Data Structures
B=0
E=4
M = 0+4/2=2
4≠5
0 1 2 3 4
1 2 3 4 5
Chapter 4 Data Structures
Search Element = 5
if(ele>a[M])
B=M+1
5=5
0 1 2 3 4
1 2 3 4 5
5 is at position 4
Chapter 4 Data Structures
Search Element = 1
if(ele<a[M])
E=M-1
3≠1
0 1 2 3 4
1 2 3 4 5
Chapter 4 Data Structures
Search Element = 1
if(ele<a[M])
E=M-1
2≠1
0 1 2 3 4
1 2 3 4 5
Chapter 4 Data Structures
Search Element = 1
if(ele<a[M])
E=M-1
1=1
0 1 2 3 4
1 2 3 4 5
1 is at position 0
Algorithm:
Chapter 4 Data Structures
Step 1: set B=LB, E=UB
Step 2: while(B<=E)
M=int(B+E)/2
if(ELE=A[M])
loc=M
goto 3
else
if(ELE<A[M])
E=M-1
else
B=M+1
end of while
Step 3: if(LOC>=0)
PRINT LOC
else
PRINT “Search is unsuccessful”
Step 4: exit
Chapter 4 Data Structures
Insertion of an element:
Chapter 4 Data Structures
Insertion of an element:
Chapter 4 Data Structures
Chapter 4 Data Structures
Chapter 4 Data Structures
70 60 50 10 80 60 70 50 10 80
Correct Key
position
70 60 50 10 80 60 50 70 10 80
60 70 50 10 80 50 60 70 10 80
Key Key
Chapter 4 Data Structures
50 60 70 10 80 50 10 60 70 80
50 60 10 70 80 10 50 60 70 80
Key
10 50 60 70 80
Chapter 4 Data Structures
Chapter 4 Data Structures
Two-dimensional arrays
TOP=MAXSTK ->
STACK IS FULL
TOP=NULL ->
STACK IS EMPTY
Chapter 4 Data Structures
Stacks:
Linked list representation of a Stack
Chapter 4 Data Structures
Stacks:
Linked list representation of a Stack
Insertion
Chapter 4 Data Structures
Stacks:
Linked list representation of a Stack
Deletion
Chapter 4 Data Structures
Stacks:
Operation On Stack
➢ stack() creates a new stack that is empty. It needs no parameters
and returns an empty stack.
➢ push(item) adds a new item to the top of the stack. It needs the
item and returns nothing.
➢ pop() removes the top item from the stack. It needs no
parameters and returns the item. The stack is modified.
➢ peek() returns the top item from the stack but does not remove it.
It needs no parameters. The stack is not modified.
➢ isEmpty() tests whether the stack is empty. It needs no
parameters and returns a Boolean value.
➢ size() returns the number of items on the stack. It needs no
parameters and returns an integer.
Chapter 4 Data Structures
Stacks:
Algorithm for PUSH Operation:
Chapter 4 Data Structures
Stacks:
Algorithm for POP Operation:
Chapter 4 Data Structures
Applications of Stacks
➢ Stack is used to reverse a word
➢ It is used in “undo” mechanism in text
editors
➢ Conversion of decimal number into binary
➢ To solve Tower of Hanoi
➢ Expression evaluation and syntax parsing
➢ Conversion of infix expression into prefix
and postfix
➢ Quick sort
➢ Run time memory management
Chapter 4 Data Structures
Arithmetic expression:
Infix expression:
Postfix expression
Prefix expression
Chapter 4 Data Structures
Infix expression: if an operator is in
between two operands it is called infix
expression
Example 2: (a+b)*(c-d)*e
Chapter 4 Data Structures
Postfix expression: If an operator
follows the two operands it is called
post fix expression.
Example 1: ab +
Example 1: +ab