Professional Documents
Culture Documents
• If we execute a statement:
• m[3] = 40;
• This statement will replace element at index 3
without considering that array is already fill.
List & Array- Comparison
• List is more organized than Array
• List ADT will ensure that
• No element is added if List is full
• No element is removed if List is empty
• No element is lost if new element is added
List ADT
• The List ADT contain functions/interfaces that allow
to Insert, Delete or Search elements
• We define List ADTs as follows:
• void insertAtZero(int m[])
• void insertAtEnd(int m[])
• void insertAtK(int m[])
• void deleteAtZero(int m[])
• void deleteAtEnd(int m[])
• void deleteAtK(int m[])
void insertAtZero(int m[])
Start
False True
Shift elements to right isFull() List is Full
}
void insertAtEnd(int m[])
Start
False True
User inputs new element isFull() List is Full
End
Pseudocode
}
void insertAtK(int m[])
Start
False True
User input position K isFull() List is Full
End
Pseudocode
void insertAtK(int m[]){
if(isFull()){
print “List is Full.”
}
else{
Input Position K
for i=size to K
m[i]= m[i-1]
Input new value
Store value at K
Increment Size of List
}
}
void deleteAtZero(int m[])
Start
True
isEmpty() List is Empty
False
End
Pseudocode
}
void deleteAtEnd(int m[])
Start
True
isEmpty() List is Empty
False
End
Pseudocode
void deleteAtEnd(int m[]){
if(isEmpty()){
print “List is Empty.”
}
else{
m[size]=0;
Decrement Size of List
}
}
void deleteAtK(int m[])
Start
True
isEmpty() List is Empty
False
End
Pseudocode
void deleteAtK(int m[]){
if(isEmpty()){
print “List is Empty.”
}
else{
User input position K
for i=K to size
m[i]=m[i+1];
Decrement Size of List
}
}
List – ADT for Searching
• Search is to find the presence or absence of any
value
• In unsorted list, Sequential Search method is used
• Sequential Search looks for a value in unsorted list
from first element to the last
• In simplest scenario, it just returns whether a value
is find or not
• Or the function can return the number of times a
value appears
Start
True
isEmpty() List is Empty
False
False
Compare V and ith element
of list
False
End of List[i] ==
List V
True True
End
void searchForV(int m[]){
if(isEmpty()){
print “List is Empty.”
}
else{
Input value V
for i=0 to size
if(List[i]==V)
Value V is found at ith Index
else
Value V is not in the list
}