02 Lists (Using Arrays)
02 Lists (Using Arrays)
(USING ARRAYS)
Examples:
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
• For the following programs for the operations on lists, assume the
following global declarations:
ELEMENTTYPE list[MAX_SIZE];
int last;
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] <<".";
}
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.";
}
}
• 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
list[pos] = new_data;
++last;
Lists (Using Arrays)
OPERATIONS ON LISTS
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!";
}
}
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) == 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!";
}
}
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.