Professional Documents
Culture Documents
Lecture 5: List: Objectives
Lecture 5: List: Objectives
Objectives:
To define list and types of list.
2
Sample of Applications That Use List
3
Types of List
Linear Lists
General Restricted
4
List contains following characteristics
5
List execution can be done into two
methods:
1. Contiguous – use an array.
6
Example
Contiguous List
23 48 82 91
Linked List
23 48 82 91
7
Contiguous List
Use array
8
Contiguous List Operation
4 basic operations : insert, delete, retrieve and
traverse.
9
Contiguous List Advantages
Index – fast retrieval based on index
10
Contiguous List Weaknesses
Limited size – new item cannot insert when list
is full.
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
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
14
Linked List Weaknesses
Item cannot be retrieved directly through index.
15
Summary
Method List Size Retrieval Insert/Delete
Operation
16
Contiguous List Execution
List is being seen as abstract data.
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
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]
59
23 25 34 48 61 79 82 89 91 99
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
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]
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.
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]
23 25 34 48 61 79 82 89 91 99
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
28
Delete Operation Contiguous List
23 25 34 48 79 82 89 91 99
29
Algorithm
display()
If Size = 0, display message “empty list” and terminate
process.
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