You are on page 1of 20

COMP4113

Data Structures and Analysis of Algorithms

Lecture # 02 Commonly Used Data Structures- List

Dr. Aftab Akram (PhD CS)


Instructor: Assistant Professor
Department of Information Sciences,
University of Education, Lahore
List Data Structure
• List is a non-primitive data structure
• A List is actually a collection of items which can be
stored and accessed randomly.
• A List is somewhat similar to an Array
• But, List is more sophisticated and organized than
an Array.
• List can be implemented using Array or Linked List.
List & Array- Comparison
• Items can be added/removed to a List and Array in
any order
• However, Array does not ensure old items are
conserved as new items are added to it.
• For example, assume following Array
m 5 10 13 7 22 2

• 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

User inputs new element

Store element at zero

Increment size of List End


Pseudocode
void insertAtZero(int m[]){
if(isFull()){
print “List is Full.”
}
else{
for size to 0
m[i]=m[i-1];
Input new value
Store at 0
Increment Size of List
}

}
void insertAtEnd(int m[])
Start

False True
User inputs new element isFull() List is Full

Store element at end

Increment size of List

End
Pseudocode

void insertAtEnd(int m[]){


if(isFull()){
print “List is Full.”
}
else{
Input new value
Store at End
Increment Size of List
}

}
void insertAtK(int m[])
Start

False True
User input position K isFull() List is Full

Shift elements to right

Input & Store element at K

Increment size of List

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

Shift elements to left

Decrement size of List

End
Pseudocode

void deleteAtZero(int m[]){


if(isEmpty()){
print “List is Empty.”
}
else{
for 0 to size
m[i]= m[i+1]
Decrement Size of List
}

}
void deleteAtEnd(int m[])
Start

True
isEmpty() List is Empty

False

Delete element at End

Decrement size of List

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

User input position K

Shift elements to left

Decrement size of List

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

Input value V to search

False
Compare V and ith element
of list

False
End of List[i] ==
List V

True True

Value V is not in List Value V found at Kth index

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
}

You might also like