Anda di halaman 1dari 38

LISTS

(USING ARRAYS)

SCHOOL OF COMPUTING MITCH M. ANDAYA


DEFINITION OF A LIST

• Mathematically, a list is a sequence of zero or more elements of a


given type (designated as ELEMENTTYPE).

• Lists are often represented as a comma-separated sequence of


elements:

a1, a2, . . . ,an

where n ≥ 0, and each ai is of type ELEMENTTYPE. The number n


of elements is said to be the length of the list.

Assuming n ≥ 1, it is said that a1 is the first element and an is the


last element. If n = 0, then the list is an empty list, one which has
no elements.

Lists (Using Arrays)


DEFINITION OF A LIST

• An important property of a list is that its elements can be


linearly ordered according to their position on the list.

a1, a2, . . . , ai-1, ai, ai+1, …, an

It is said that ai precedes ai+1 for i = 1, 2, . . . , n-1, and ai


follows ai-1 for i = 2, 3, . . . ,n. It can further be said that
the element ai is at position i.

Therefore, a list is a finite sequence of items with a


definite order.

Lists (Using Arrays)


THE typedef CONSTRUCT

• The typedef construct is a facility to have built-in or user-


defined data types.

• This facility allows a programmer to use a different name


for the fundamental and modified data types. The general
form of the typedef statement is:

typedef data_type type_name;

where data_type is any valid data type (int, char, float,


etc.) and type_name is the alternative name for this type.

Lists (Using Arrays)


THE typedef CONSTRUCT

Examples:

typedef int PESOS;


PESOS cost, profit;

This creates two variables, cost and profit of type


PESOS (which is actually an int variable).

typedef float GRADE;


GRADE gwa1, gwa2;

This creates two variables, gwa1 and gwa2 of type


GRADE (which are actually float variables).

Lists (Using Arrays)


OPERATIONS ON LISTS

• To form an abstract data type from the mathematical notion of a


list, a set of operations on lists must be defined.

• Some basic operations on lists:

1. Determining the length of a list


2. Locating if a particular element exists in a list (and possibly
determining its position within the list)
3. Retrieving an element at a particular location in a list
4. Inserting an element at a certain position in a list
5. Removing/deleting an element at a certain position from a list
6. Traversing a list (possibly making changes to the elements of
the list, printing the elements in the list, etc.)

Lists (Using Arrays)


ARRAY IMPLEMENTATION OF LISTS

• In the array implementation of lists, the elements of the list are


stored in the (contiguous) cells of an array.

• To implement a list using arrays, the array length must be


sufficient to hold the maximum length or size of the list that will
be encountered.

Example:
could be any type
const int MAX_SIZE = 1000; such as string,
structure, etc.
typedef int ELEMENTTYPE;

ELEMENTTYPE list[MAX_SIZE];

Lists (Using Arrays)


ARRAY IMPLEMENTATION OF LISTS

• Assume that the list has n


list[0] 1st Element
elements.
list[1] 2nd Element
The last element will be
list[2] 3rd Element
stored at cell n – 1.
. list
. There will be several
. empty cells.
list[n-1] Last Element

.
There must be an integer
.
variable to keep track of
empty
the index of the last
list[MAX_SIZE-1] element.

last = n - 1 last = n - 1

Lists (Using Arrays)


ARRAY IMPLEMENTATION OF LISTS

list[0] 1st Element


• Before adding an
element to the list, check
list[1] 2nd Element first if the list is full.
list[2] 3rd Element

. list The list is full if:


. last = MAX_SIZE – 1
.

list[n-1] Last Element • Before deleting an


element in the list, check
. first if the list is empty.
. empty
list[MAX_SIZE-1] The list is empty if:
last < 0 (last = -1)
last = n - 1

Lists (Using Arrays)


ARRAY IMPLEMENTATION OF LISTS

• Take note that the


list[0] 1st Element empty cells (cells
list[1] 2nd Element
whose indices are
greater than n-1) are
list[2] 3rd Element
not really empty.
. list
.
. • They actually contain
random or garbage
list[n-1] Last Element data.
.
. empty
• But as far as the
list[MAX_SIZE-1] program is concerned,
they are logically
last = n - 1 "empty."

Lists (Using Arrays)


OPERATIONS ON LISTS

• For the following programs for the operations on lists, assume the
following global declarations:

const int MAX_SIZE = 1000;


typedef int ELEMENTTYPE;

and the following local declarations in main():

ELEMENTTYPE list[MAX_SIZE];
int last;

• Since some functions for the operations on lists need to access


these local variables, they must be passed on to these functions
as arguments.

Lists (Using Arrays)


OPERATIONS ON LISTS (CHECK IF LIST IS FULL)

• Function to Check if List is Full (returns 1 if full and


0 if not full)

last )
int last)
int list_full (int
{
if (last
last == MAX_SIZE - 1)
1
return (1);
else
return (0);
}

Lists (Using Arrays)


OPERATIONS ON LISTS (CHECK IF LIST IS EMPTY)

• Function to Check if List is Empty (returns 1 if


empty and 0 if not empty)

int list_empty (int


int last)
last )
{
last < 0)
if (last 0
return (1);
else
return (0);
}

Lists (Using Arrays)


OPERATIONS ON LISTS (PRINT CONTENTS OF LIST)

• Function to Print the Contents of the List

void print_items(ELEMENTTYPE
ELEMENTTYPE list[],
list[], int last)
last )
{
int index;

if (list_empty(last)
list_empty(last) == 1)
1
cout << "\nThe List is Empty!";
else
for (index = 0; index <= last; ++index)
cout << "\nThe Value of Item " << index+1
<< " = " << list[index] <<".";
}

Lists (Using Arrays)


OPERATIONS ON LISTS (LOCATE ITEM IN LIST)

• Function to Locate an Item in List and Print its


Position (assuming items are unique)

void locate_item (ELEMENTTYPE


ELEMENTTYPE list[],
ELEMENTTYPE search_data last )
search_data,, int last)
{
int index;

if (list_empty(last)== 1)
cout << "\nThe List is Empty!";

Lists (Using Arrays)


OPERATIONS ON LISTS (LOCATE ITEM IN LIST)

else
{
index = 0;
index != last + 1 && list[index] != search_data
while (index search_data)
++index;

if (index != last + 1)
cout << "\nItem Requested is Item " << index + 1 << ".";
else
cout << "\nItem Does Not Exist.";
}
}

Lists (Using Arrays)


OPERATIONS ON LISTS (ADD/INSERT ITEM)
• To add or insert a new element at position or index pos (where 0 ≤ i ≤ n-1) in the list,
it is necessary to move down all elements starting at position pos.

0 000 0 000
1 111 1 111
2 222 2 222
3 333 3 333 Insert or
pos = 4 4 444 pos = 4 4 add new
element
5 555 5 444 here
6 666 6 555
7 777 7 666
last = 8 8 888 8 777
9 last = 9 9 888

• If the list is full, the item cannot be inserted and an error should be reported.

Lists (Using Arrays)


OPERATIONS ON LISTS (ADD/INSERT ITEM)

0 000 0 000
1 111 1 111
2 222 2 222
3 333 3 333 Insert or
pos = 4 4 444 pos = 4 4 add new
element
5 555 5 444
here
6 666 6 555
7 777 7 666
last = 8 8 888 8 777
9 last = 9 9 888

• To move all elements in the list down starting at position


pos (where 0 ≤ pos ≤ n-1 ):

index = last;
for (index last index >= pos
pos; --index)
--index
list[index+1] = list[index];

Lists (Using Arrays)


OPERATIONS ON LISTS (ADD/INSERT ITEM)

0 000 0 000
1 111 1 111
2 222 2 222
3 333 3 333 Insert or
pos = 4 4 444 pos = 4 4 add new
element
5 555 5 444
here
6 666 6 555
7 777 7 666
last = 8 8 888 8 777
9 last = 9 9 888

• Assume the variable new_data contains the value to be


inserted. To store new_data at position pos:

list[pos] = new_data;
++last;

Lists (Using Arrays)


OPERATIONS ON LISTS (ADD/INSERT ITEM)

• Let insert_item() be the name of the function that will add


an element to the list.

• This function will need three parameters:

1. the list itself (list[])


2. the value of the item to be added (new_data),
3. the index of the position where the item is to be
added (pos),
4. and the index of the last element of the list (last).

Lists (Using Arrays)


OPERATIONS ON LISTS (ADD/INSERT ITEM)

• The function will need to modify the values for the array list and
the variable last.

• The function must therefore use the call-by-reference (pointers)


method of passing these arguments for the variable list.

• Recall that this technique copies the address of an argument into


the parameter. Inside the function, the address is used to access
the actual argument used in the call.

• This will allow changes made to the parameter to affect the


variable used to call the function.

Lists (Using Arrays)


OPERATIONS ON LISTS (ADD/INSERT ITEM)

• For the array list, there is no need to use pointers


since C++ treats arrays differently as compared to
ordinary variables when they are passed to
functions.

• C++ passes arrays to functions as variable


parameters. In other words, C++ passes the array
itself to functions, not just a copy.

• Therefore, the insert_item() function can modify


the contents of the array list even if the program
did not declare that array as a global variable.
Lists (Using Arrays)
OPERATIONS ON LISTS (ADD/INSERT ITEM)

• But for the variable last, its address must be passed on to


the insert_item() function in order to allow the said
function to modify its contents.

• Consequently, the function must have a pointer parameter


to store this address.

void insert_item ( …., int *ptr_last)

• In main (), the insert_item() function can be invoked by:

insert_item ( …., &last);

Lists (Using Arrays)


OPERATIONS ON LISTS (ADD/INSERT ITEM)
• Function to Insert/Add an Element in the List (at position or index pos)

void insert_item (ELEMENTTYPE


ELEMENTTYPE list[],
list[] , ELEMENTTYPE new_data,
new_data ,int
intpos,
pos ,int
int*ptr_last)
*ptr_last)
{
int index;

(list_full(last) == 1) == 1)
if (list_full(*ptr_last)
cout << "\nThe List is Full!";
else
{
for (index = *ptr_last;
last; indexindex >=--index)
>= pos; pos; --index)
list[index+1] = list[index];

list[pos] = new_data;
++(*ptr_last);
++last;
cout << "\nItem Successfully Added!";
}
}

Lists (Using Arrays)


OPERATIONS ON LISTS (DELETE ITEM)
• To delete the element at position or index pos (where 0 ≤ pos ≤ n-1) in the list, it is
necessary to move up all elements in the list starting at position pos + 1.

0 000 0 000
1 111 1 111
2 222 2 222
3 333 3 333
pos = 4 4 444 pos = 4 4 555
5 555 5 666
6 666 6 777
7 777 last = 7 7 888
last = 8 8 888 8
9 9

• If the list is empty, the item cannot be deleted and an error should be reported.

Lists (Using Arrays)


OPERATIONS ON LISTS (DELETE ITEM)

0 000 0 000
1 111 1 111
2 222 2 222
3 333 3 333
pos = 4 4 444 pos = 4 4 555
5 555 5 666
6 666 6 777
7 777 7 888
last = 8 8 888 8
9 9

• To move all elements in the list up starting at position pos


(where 0 ≤ pos ≤ n-1 ):

index = pos + 11;index


for (index index<= <=last
last; ++index
++index)
list[index - 1] = list[index];

Lists (Using Arrays)


OPERATIONS ON LISTS (DELETE ITEM)

0 000 0 000
1 111 1 111
2 222 2 222
3 333 3 333
pos = 4 4 444 pos = 4 4 555
5 555 5 666
6 666 6 777
7 777 last = 7 7 888
last = 8 8 888 8
9 9

• After moving up all the necessary elements, the value for


last should be updated:

--last;

Lists (Using Arrays)


OPERATIONS ON LISTS (DELETE ITEM)

• Function to Delete an Element in the List (at position or index pos)

void delete_item (ELEMENTTYPE


ELEMENTTYPE list pos,, int *ptr_last)
list[][],, int pos
{
int index;

if (list_empty(*ptr_last)
*ptr_last == 1)
cout << "\nThe List is Empty!";
else
{
for (index = pos + 1; index <= *ptr_last; ++index)
list[index-1] = list[index];

--(*ptr_last);
cout << "\nItem Successfully Deleted!";
}
}

Lists (Using Arrays)


OPERATIONS ON LISTS (COUNT ITEMS IN LIST)

• Function to Count the Items in the List (and


return the count)

int last)
int count_list (int last
{
return (last+1);
}

Lists (Using Arrays)


ADVANTAGES AND DISADVATAGES OF USING ARRAYS

• Advantages of Using Arrays in Implementing Lists:

– Less memory space is needed. An array of


1,000 char elements requires only 1,000 bytes.
While a linked list (to be discussed in the next
chapter) of 1,000 char elements requires
16,000 bytes.

– Faster access to the array elements. As long as


the index is known, an element in an array can
be accessed directly and quickly.

Lists (Using Arrays)


ADVANTAGES AND DISADVATAGES OF USING ARRAYS

• Disadvantages of Using Arrays in Implementing Lists:

– Slow in inserting/deleting elements. Several elements


have to be moved every time a single element is
inserted or deleted in the array (particularly if there are
thousands of elements).

– Fixed size. The maximum number of elements in the


list must be known during coding. If the array declared
is too large, there will be several unused space. If the
array declared is too small, there is a limit on the
number of elements that can be stored in the list.

Lists (Using Arrays)


MACHINE PROBLEMS

1. Write a C++ program that will process a list of integers. The list can have a
maximum of 5 elements. The opening screen of the program should look like:

LIST (ARRAY) PROGRAM


-------------------------------
There are currently 0 items in the list.

Options:
1. Add Item To The List
2. Delete Item From The List
3. Locate Item In The List
4. Print Items In The List
5. Exit Program

Enter the number of your choice:

Lists (Using Arrays)


MACHINE PROBLEMS

If the user chooses option 1, the program should then prompt the
user to enter the position of the item to be added/inserted.

Make sure the program will check if the position entered by the
user is valid. For example, if the list is empty, the user can only
add/insert an item at position 1. If there are 3 items in the list,
the user can only add/insert an item at positions 1, 2, 3, or 4.

After entering the position, the program should then prompt the
user to enter the value of the item to be added. The program
then proceeds to add the item to the list and informs the user that
the item has been added successfully.

Lists (Using Arrays)


MACHINE PROBLEMS

If the user chooses option 2, the program should then


prompt the user to enter the position of the item to be
deleted.

Make sure the program will check if the position entered


by the user is valid. For example, If there are 3 items in
the list, the user can only delete an item at positions 1, 2,
or 3.

After entering the position, the program then proceeds to


delete the item from the list and informs the user that the
item has been deleted successfully.

Lists (Using Arrays)


MACHINE PROBLEMS

If the user chooses option 3, the program should then


prompt the user to enter the value of the item to be
searched. The program then performs the search
operation. If the item is found, the program prints the
location of the item in the list.

For example if the item to be searched is the item at


location 3 in the list, then the program will output

Item Requested is Item 3.

If the item is not found, the program prints that the item
does not exist in the list.

Lists (Using Arrays)


MACHINE PROBLEMS

If the user chooses option 4, the program should then


print the contents of the list.

For example, if the list contains 5, 8, and 4 (in that order),


then the output should be:

The value of item 1 = 5.


The value of item 2 = 8.
The value of item 3 = 4.

If the list is empty, the program simply informs the user


that the list is empty.

Lists (Using Arrays)


MACHINE PROBLEMS

Once a user makes a choice and the program


performs the required operation, the
program should then display the menu again
to allow the user to make another choice.
Make sure that the line displaying the
number of items in the list is always updated
whenever it is re-displayed again.

The program should allow the user to keep


on making choices until the user chooses
option 5 (exit).
Lists (Using Arrays)
MACHINE PROBLEMS

The program should have the following functions:

1. to check if the list full


2. to check if the list empty
3. to add an item to the list
4. to delete an item from the list
5. to locate an item in the list
6. to print the contents of the list
7. to prompt the user to enter a position in the
list (and check its validity)

Lists (Using Arrays)

Anda mungkin juga menyukai