Delete


Program Listing

Delete.cpp

/************************************************************************
*                                                                       *
*   Program:    Delete                                                  *
*                                                                       *
*   Purpose:    This program will read a list of numbers that are in    *
*               increasing order, then delete a number from the list    *
*                                                                       *
************************************************************************/

//  Header files

#include <iostream>
#include <string>

using namespace std;

//  Function prototypes

bool Delete(int array[], int& size, int Item);
void ReadList(int array[], int& size, int maxSize);

/************************************************************************
*                                                                       *
*   Function:   main                                                    *
*                                                                       *
*   Purpose:    To read a list of numbers that are in increasing        *
*               order, then delete a number from the list               *
*                                                                       *
************************************************************************/

int main() {

//  Establish the array

    const int maxSize = 100;
    int array[maxSize];
    int size;

//  Read numbers into the array

    ReadList(array, size, maxSize);

//  Insert a new number into the array

    cout << "Enter a value to be deleted:  " << flush;
    int Item;
    cin >> Item;

    bool deleted = Delete(array, size, Item);

//  Report the results

    if (deleted)
        cout << Item << " was deleted" << endl;
    else
        cout << Item << " was not deleted" << endl;
    
//  Display the list

    for (int i = 0; i < size; i++)
        cout << array[i] << ' ';
    cout << endl;
    
    return 0;
}

/************************************************************************
*                                                                       *
*   Function:   Delete                                                  *
*                                                                       *
*   Purpose:    To delete a number from a list of numbers               *
*                                                                       *
************************************************************************/

bool Delete(int array[], int& size, int Item) {

//  If list is empty, return false

    if (size == 0)                      // List is empty
        return false;

//  If list is not empty, search for and delete the number

    else {
    
    //  Search for the number
    
        int i;
        for (i = 0; i < size && array[i] != Item; i++)
            continue;
    
    //  See if number was found
    
        if (i == size)                  // Item was not found
            return false;
    
    //  Shift the remaining members of the list to the left
    
        for (int j = i + 1; j < size; j++)
            array[j - 1] = array[j];
    
        size--;
        
        return true;
    }
}

/************************************************************************
*                                                                       *
*   Function:   ReadList                                                *
*                                                                       *
*   Purpose:    To read a list of numbers                               *
*                                                                       *
*   Note:       The input is controlled by a sentinel value             *
*                                                                       *
************************************************************************/

void ReadList(int array[], int& size, int maxSize) {
    const int SENTINEL = -1;

//  Read the first value

    cout << "Enter a list of integers (" << SENTINEL
         << " to quit)." << endl;
    int i = 0;
    int value;
    cin >> value;

//  Read remaining values, until the sentinel is read

    while (i < maxSize && value != SENTINEL) {
        array[i++] = value;
        cin >> value;
    }
    
//  Store the size of the list

    size = i;
    return;
}

Sample Run #1

Enter a list of integers (-1 to quit).
30 50 80 35 55 25 70 -1
Enter a value to be deleted:  35
35 was deleted
30 50 80 55 25 70 

Sample Run #2

Enter a list of integers (-1 to quit).
30 50 80 35 55 25 70 -1
Enter a value to be deleted:  45
45 was not deleted
30 50 80 35 55 25 70 



Return to Lectures page


e-mail me at robbk@hsc.edu

This page was last modified on Tue Dec 7 16:04:56 1999 .

URL: http://people.hsc.edu/faculty-staff/robbk/Coms261/Examples/Delete.html

Return to Coms 261 home page