0% found this document useful (0 votes)
14 views

02 Lists (Using Arrays)

The document discusses lists and their implementation using arrays. It defines a list as a sequence of zero or more elements of a given type that can be ordered by their position. Lists are often represented as comma-separated sequences. The document also discusses using arrays to store list elements, with an integer variable tracking the last element index. A number of basic list operations like checking for empty/full lists, retrieving elements, and inserting/deleting elements are presented.

Uploaded by

Dann Laurte
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

02 Lists (Using Arrays)

The document discusses lists and their implementation using arrays. It defines a list as a sequence of zero or more elements of a given type that can be ordered by their position. Lists are often represented as comma-separated sequences. The document also discusses using arrays to store list elements, with an integer variable tracking the last element index. A number of basic list operations like checking for empty/full lists, retrieving elements, and inserting/deleting elements are presented.

Uploaded by

Dann Laurte
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

LISTS

(USING ARRAYS)

SCHOOL OF COMPUTING BENNETT M. TANYAG


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)


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
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

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)


THE typedef CONSTRUCT

Examples:

typedef struct person_info


{
int height_cm;
float weight_kg;
} PERSON;

PERSON person1, person2;

This creates two variables, person1 and person2 that


are of type PERSON (which is actually a structure
variable of type person_info).

Lists (Using Arrays)


ARRAY IMPLEMENTATION OF LISTS

• In the array implementation of lists, the elements of the list 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

Lists (Using Arrays)


ARRAY IMPLEMENTATION OF LISTS

list[0] 1st Element • The list is full if last =


MAX_SIZE – 1.
list[1] 2nd Element
list[2] 3rd Element
• The list is empty if last < 0
. list (last = -1).
.
.
• Before adding an element
list[n-1] Last Element to the list, check first if
the list is full.
.
. empty
• Before deleting an
list[MAX_SIZE-1] element in the list, check
first if the list is empty.

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
"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

• Function to Check if List is • Function to Check if List is


Full (returns 1 if full and 0 if Empty (returns 1 if empty
not full) and 0 if not empty)

int list_full (int last) int list_empty (int last)


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

Lists (Using Arrays)


OPERATIONS ON LISTS

• Function to Print the Contents of the List

void print_items(ELEMENTTYPE list[], int last)


{
int index;

if (list_empty(last) == 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

• Function to Locate a Particular Element in a List

void locate_item (ELEMENTTYPE list[], ELEMENTTYPE search_data, int last)


{
int index;

if (list_empty(last)== 1)
cout << "\nThe List is Empty!";
else
{
index = 0;
while (index != last + 1 && list[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

• 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. If there is no room
in the array for an additional element (list if full), an error is reported.

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

Lists (Using Arrays)


OPERATIONS ON LISTS

• Program segment to add or insert an item at


position or index pos where 0 ≤ pos ≤ n-1
(assume the variable new_data contains the
value to be inserted):

for (index = last; index >= pos; --index)


list[index+1]=list[index];

list[pos] = new_data;
++last;
Lists (Using Arrays)
OPERATIONS ON LISTS

• The add_item() function needs to modify these local variables.


Specifically, the list itself and the variable last will have to be
modified.

• These functions must use the call-by-reference method of passing


arguments.

• 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

• For example, the add_item() function will have to modify


the value of last every time an item is added to the list by
executing the statement ++last.

• For the add_item() function to do this, the address of last


must be passed on to the function. Consequently, the
function must have a pointer to last to store this address.

void add_item ( …., int *ptr_last, …)

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

add_item ( …., &last, …);

Lists (Using Arrays)


OPERATIONS ON LISTS

• For the 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 add_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

• Function to Insert/Add an Element in the List (at position or index pos)

void add_item (ELEMENTTYPE list[], ELEMENTTYPE new_data, int pos, int *ptr_last)
{
int index;

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

Lists (Using Arrays)


OPERATIONS ON LISTS

• 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

Lists (Using Arrays)


OPERATIONS ON LISTS

• Program segment to delete an element at


position or index pos where 0 ≤ pos ≤ n-1:

for (index = pos; index <= last; ++index)


list[index]=list[index+1];

--last;

Lists (Using Arrays)


OPERATIONS ON LISTS

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

void delete_item (ELEMENTTYPE list [], int pos, int *ptr_last)


{
int index;

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

*ptr_last = *ptr_last - 1;
cout << "\nItem Successfully Deleted!";
}
}

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

Lists (Using Arrays)

You might also like