You are on page 1of 40

LECTURE 5: LIST

Objectives:
To define list and types of list.

To list down basic operations of list.

Toexplain the algorithm for each basic


operation of list.

To execute contiguous list in the program 1


Lists
List is a group of sequential data item.
Example List:
 - List of products to be purchase at supermarket.
 - List of assignments to be done.
 - List of students in the class.

2
Sample of Applications That Use List

1. Information retrieval system


2. Programming language translation.
3. Simulation.
4. Text editor.
5. Storage management technique.

3
Types of List
Linear Lists

General Restricted

Unordered Ordered FIFO LIFO


(queue) (stack)

4
List contains following characteristics

No. of elements in list is finite. Means, there


are first element and last element.

Elements in the list is homogeneous (same


types)

Elements in the list are sequential ordered.

5
List execution can be done into two
methods:
1. Contiguous – use an array.

2. Linked – use node which contains data and


pointer.

6
Example
Contiguous List

[0] [1] [2] [3]

23 48 82 91

Linked List
23 48 82 91

7
Contiguous List
Use array

2 categories – global and limited

Data in global list can be inserted or deleted


from its location and no limit to make an
operation to the list.

2 types of global list –


 random list (unordered data)
 ordered list (based on key)

8
Contiguous List Operation
4 basic operations : insert, delete, retrieve and
traverse.

Insert, delete and retrieve can be executed by


all lists.

Traverse cannot be executed by limited list.

9
Contiguous List Advantages
Index – fast retrieval based on index

Faster – in general, the retrieval using array is


faster than retrieval using linked list.

10
Contiguous List Weaknesses
Limited size – new item cannot insert when list
is full.

If the inserted still need to be done, all items


need to copy into larger array in order make
more space and then that new item is allow to
be inserted.

Insert/delete complex data -


inserted/deleted in the middle of array
involved of moving operation to make more
space for new item or to remove item to be
deleted.
11
Insert Operation Contiguous List

23 25 34 48 61 79 82 89 91 99
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] Before

23 25 34 48 59 61 79 82 89 91 99
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
After

Operation of insert number 59 at index 4.


12
Delete Operation Contiguous List
Before
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

23 25 34 48 61 79 82 89 91 99

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
23 25 34 48 79 82 89 91 99
After

Operation of delete number 61 at


index 4.
13
Linked List Advantages
No limited size – no maximum limit but
depends on computer memory.

More easier to insert data – insert item in any


location in the list is more easier due to use of
pointer.

14
Linked List Weaknesses
Item cannot be retrieved directly through index.

In general, retrieval process is slow compared


when using array.

15
Summary
Method List Size Retrieval Insert/Delete
Operation

Contiguous Limited Fast Complex

Linked No limited Slow Easy

16
Contiguous List Execution
List is being seen as abstract data.

List is defined as class.

List contains data and method.

17
Contiguous List Data Execution
MaximumSize : integer constant refer to number
of maximum item that can be insert into list.
Size : integer variable refer to number of items in
the list.
Array : array variable to keep the data in the list.

18
Contiguous List Method/Operation Execution

numberItem() : to get number of items in the


list.
insert() : insert item in the list.
delete() : delete item from the list.
display() : display each item in the list.

19
Algorithm
constructor()
 List Size is zero.
 Index position is at -1

numberItem()
 Return List Size

20
Algorithm
insert()
• If Size = MaximumSize, display message “list
is full” and terminate process.
• If insert position is less than 0 or more than
Size, display message “invalid position” and
terminate process.
• Start from last item until insert item position,
do movement for 1 position to the higher
position.
• Enter new item to the insert position.
• Increase List Size with 1.

21
Insert Operation Contiguous List

23 25 34 48 61 79 82 89 91 99
59
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

Operation of insert number 59 at index 4.

Precondition : Size value 10.

Postcondition : number 59 insert into index 4.


Size increase become 11.
22
Insert Operation Contiguous List

59
23 25 34 48 61 79 82 89 91 99

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

All items from index 9 until index 4


are move to 1 higher position.

23
Insert Operation Contiguous List

59

23 25 34 48 61 61 79 82 89 91 99
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

Insert 59 to index 4

24
Insert Operation Contiguous List

23 25 34 48 59 61 79 82 89 91 99

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

Size increase with 1 become 11

25
Algorithm
delete()
If Size = 0, display message “empty list” and
terminate process.
If position less than 0 or, equal or more than Size,
display message “invalid position” and terminate
process.

Start from deleted item position + 1 until last item,


do movement to 1 lower position.

Decrease List Size with 1.

26
Delete Operation Contiguous List

23 25 34 48 61 79 82 89 91 99

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

Operation of delete number 61 at index 4.

Precondition: Size value 10

Postcondition: Delete number 61


Size value 9
27
Delete Operation Contiguous List

23 25 34 48 61 79 82 89 91 99

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

All items from index 5 until index 9 are move


to 1 lower position.

28
Delete Operation Contiguous List

23 25 34 48 79 82 89 91 99

[0] [1] [2] [3] [4] [5] [6] [7] [8]

Size decrease with 1 become 9

29
Algorithm

display()
If Size = 0, display message “empty list” and terminate
process.

Start from first item until last item, display item.

30
Define List Class (List.h)
#include <iostream>
const int MaximumSize = 100;
class List
{
public:
/******* Method ********/
List();
~ List();
int numberItem();
void insert(int item, int position);
void delete(int position);
void display();

private :
/******* Data Member ***********/
int Size;
int Array[MaximumSize];
};
31
Definition List Methods (List.cpp)
#include <iostream>
using namespace std;
#include “List.h”

List:: List()
{Size = 0 ; }

List::~ List()
{}

int List::numberItem()
{ return Size; }
32
Definition List Methods (List.cpp) cont.
void List::insert (int item, int position)
{
if (Size == MaximumSize)
{
cout<<"***List is full*****\n";
return ;
}
if (position < 0 || position > Size)
{
cout<<"***Invalid position******\n";
return;
}
for (int i = Size-1 ; i >= position - 1 ; i--)
Array[i+1] = Array [i];

Array[position - 1] = item;
Size++;
} 33
Definition List Methods (List.cpp) cont.
void List ::delete(int position)
{
if (Size == 0)
{
cout<<"***Empty list ***\n";
return;
}
if (position < 0 || position >= Size)
{
cout<<"***Invalid position ****\n";
return;
}
for(int i = position+1 ; i< Size; i++)
Array[i-1] = Array[i];
Size--;
} 34
Definition List Methods (List.cpp) cont.

void List::display()
{
for(int i = 0; i < Size; i++)
cout<<Array[i]<<“ “;
cout<<" \n";
}

35
Contiguous List Application in
main program
#include <iostream>
using namespace std;
#include “List.h"
int main()
{
int item,position;
List intList;
//Test either list is empty or not.
if ( intList.numberItem() == 0 )
cout<<“Tiada elemen dalam senarai ini\n";
//Insert item
for (int i=0 ; i<10 ; i++)
intList.insert(i*2,i);
36
Contiguous List Application in main program
(cont.)
cout<<“Senarai yang diwujudkan selepas operasi
tambah\n”;
intList.display();
cout<<“Masukkan nombor untuk ditambah ke dalam senarai : ";
cin>>item;
cout<<“Masukkan indeks nombor tersebut : ";
cin>>position;
intList.insert(item,position);
cout<<“Senarai Baru : \n";
intList.display();
return 0;
}

37
Running Output 1

38
Running Output 2

39
Running Output 3

40

You might also like