02 Lists (Using Arrays)
02 Lists (Using Arrays)
(USING ARRAYS)
Examples:
Example:
could be any type
const int MAX_SIZE = 1000; such as string,
structure, etc.
typedef int ELEMENTTYPE;
ELEMENTTYPE list[MAX_SIZE];
.
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
• For the following programs for the operations on lists, assume the
following global declarations:
ELEMENTTYPE list[MAX_SIZE];
int last;
last )
int last)
int list_full (int
{
if (last
last == MAX_SIZE - 1)
1
return (1);
else
return (0);
}
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] <<".";
}
if (list_empty(last)== 1)
cout << "\nThe List is Empty!";
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.";
}
}
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.
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
index = last;
for (index last index >= pos
pos; --index)
--index
list[index+1] = list[index];
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
list[pos] = new_data;
++last;
• The function will need to modify the values for the array list and
the variable last.
(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!";
}
}
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.
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
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
--last;
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!";
}
}
int last)
int count_list (int last
{
return (last+1);
}
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:
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
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.
If the item is not found, the program prints that the item
does not exist in the list.