分享

重写了个arrayList,深刻体会到细节的重要性!

 liema2000 2009-03-26

权哥写了string,我参考重写了个arrayList,深刻体会到细节的重要性!
/******************************************************
*GCC is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation;
* <arrayListType>  arrayBasedList.h
* lilin@tofree,Beijing,China,Jan 17
*this program is partly reference from D.S.Malik
*[C++ Programming program design including data structrues]
********************************************************/
#include <iostream>
using namespace std;

class arrayListType
{
public:
        arrayListType(int size = 100);
                //constructor,default size 100
        arrayListType(const arrayListType& otherList);
                //copy constructor
        ~arrayListType();
                //destructor
        bool isEmpty();
                //if the list is empty, reutrn ture
                //otherwise, return false
        bool isFull();
                //Like above
        int listSize();
                //return the current size of the list
        int maxListSize();
                //return the maximum size of the list
                //&the maximun size number can be stored
                //in the list
        void print()    const;
                //outputs the elements
        bool isItemAtEqual(int location, int item);
                //if the item is the same as the list element
                //at which specified by the location,return true
                //otherwise return false.
        void insertAt(int location, int insertItem);
                //insert an item in the list by parametre location
                //&if the list is full or out of range,return a
                //proper message
        void insertEnd(int insertItem);
                //insert an item in the end of the list
        void insert(int insertItem);
                //insert an item in the end of the list
                //differ from insertEnd(),it will firstly search
                //the list to see if there is already a same item
                //if there is, output a proper message
        void removeAt(int location);
                //remove the item specified by the parametre location
        void remove(int removeItem);
                //remove the item specified by removeItem
        void retrieveAt(int location, int& retItem);
                //retrieve the element from the list specified by location
                //&the item is returned via the parametre retItem
        void replaceAt(int location, int repItem);
                //replace the item specified by location & repItem]
                //if the list is full, a proper message will be displayed
        void clearList();
                //remove all the list elements, &list size be zero
        int seqSearch(int item);
                //search the given item in the list,if found,return it's
                //location,otherwise return -1
protected:
        //protected menbers is for some special list
        int* list; //to hold the list elements
        int length; //store the length of the list
        int maxSize; //store the maximum size of the list

};

arrayListType::arrayListType(int size)
{
        if (size < 0)
        {
                cout << "The array size must be positive!"
                        << "Creat a size of 100." << endl;

                maxSize = 100;
        }
        else
                maxSize = size;

        length = 0;
        list = new int[maxSize];
}

arrayListType::arrayListType(const arrayListType& otherList)
{
        int j;

        maxSize = otherList.maxSize;
        length  = otherList.length;
        list    = new int[maxSize]; //creat the array

        if (length != 0)
        {
                for (j=0; j<length; j++)
                        list[j] = otherList.list[j];
        }
}

arrayListType::~arrayListType()
{
        delete[] list;
}

bool arrayListType::isEmpty()
{
        return (length == 0);
}

bool arrayListType::isFull()
{
        return (length == maxSize);
}

int arrayListType::listSize()
{
        return length;
}

int arrayListType::maxListSize()
{
        return maxSize;
}

void arrayListType::print() const
{
        int i;

        for (i=0; i<length; i++)
        {
                cout << list << ", ";
        }

        cout << endl;
}

bool arrayListType::isItemAtEqual(int location, int item)
{
        return (list[location] == item);
}

void arrayListType::insertAt(int location, int insertItem)
{
        int i;

        if (location < 0 || location >= maxSize)
        {
                cout << "The position is out of range! " << endl;
        }
        else
        {
                if (length >= maxSize)
                {
                        cout << "List is full! Cannot insert." << endl;
                }
                else
                {
                        for (i = length; i > location; i--)
                        {
                                list = list[i-1];
                        }

                        list[location] = insertItem;//insert complete

                        length++;

                }
        }
}

void arrayListType::insertEnd(int insertItem)
{
        if (length >= maxSize)
        {
                cout << "Cannot insert item into a full list!"
                        << endl;
        }
        else
        {
                list[length] = insertItem;
                length++;
        }
}

void arrayListType::insert(int insertItem)
{
        int loc;

        if (length == maxSize)
        {
                cout << "Cannot insert into a full list."
                        << endl;
        }
        else
        {
                loc = seqSearch(insertItem);
                if (loc == -1)
                {
                        list[length++] = insertItem;
                }
                else
                {
                        cout << "The item is already being there !"
                                << endl;
                }
        }
}

void arrayListType::removeAt(int location)
{
        int i;

        if (location < 0 || location >= length)
        {
                cout << "The location of the item is out of range"
                        << endl;
        }
        else
        {
                for (i = location; i < length -1; i++)
                {
                        list = list[i+1];
                }

                length--;
        }
}

void arrayListType::remove(int removeItem)
{
        int loc;

        if (length == 0)
                {
                        cout << "Cannot delete from an empty list"
                                << endl;
                }
        else
        {
                loc = seqSearch(removeItem);

                if (loc != -1)
                {
                        removeAt(loc);
                }
                else
                {
                        cout << "The item isn't in the list."
                                << endl;
                }
        }

}

void arrayListType::retrieveAt(int location, int& retItem)
{
        if (location < 0 || location >= length)
        {
                cout << "The location of the item is out of range."
                        << endl;
        }
        else
        {
                retItem = list[location];
        }
}

void arrayListType::replaceAt(int location, int repItem)
{
        if (location < 0 || location >= length)
        {
                cout << "The location is out of range."
                        << endl;
        }
        else
        {
                list[location] = repItem;
        }
}

void arrayListType::clearList()
{
        length = 0;
}

int arrayListType::seqSearch(int item)
{
        int loc;
        bool found;
        found = false;

        for (loc = 0; loc < length; loc++)
        {
                if (list[loc] == item)
                {
                        found = true;
                        break;
                }
        }

        if (found)
        {
                return loc;
        }
        else
        {
                return -1;
        }

}
 
 
 
*****
 
 
/******************************************************
*GCC is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation;
* <arrayListType>  testCopyConstructor.cc
* lilin@tofree,Beijing,China,Jan 17
*this program is partly reference from D.S.Malik
*[C++ Programming program design including data structrues]
********************************************************/


#include <iostream>
#include "arrayBasedList.h"

using namespace std;

void testCopyConstructor(arrayListType testList);

int main ()
{
        arrayListType list;
        int num;

        cout << "Enter your numbers ending with -1000"
                << endl;
        cin >> num;

        while (num != -1000)
        {
                list.insert(num);
                cin >> num;
        }

        cout << "The list you created is: "
                << endl;
        list.print();

        cout << "The list size is: "
                << list.listSize() << endl;
        cout << "Enter the item to be deleted: "
                << endl;
        cin >> num;
        cout << endl;
        list.remove(num);

        cout << "After removing " << num
                << ", the list size is:"
                << list.listSize() << endl;

        testCopyConstructor(list);

        cout << "The list after the copy constructor:"
                << endl;
        list.print();

        cout << "The list size is "
                << list.listSize() << endl;

        return 0;
}

void testCopyConstructor(arrayListType testList)
{
        cout << "Call the function testCopyConstructor."
                << endl;

        testList.print();

        cout << "The list size is: "
                << testList.listSize() << endl;

}

********************
Test output
********************
lilin@tofree:~/workplace/arrayList$ a.out
Enter your numbers ending with -1000
12
233
54
54
The item is already being there !
53
456
65
54
The item is already being there !
7
6
5
77
66
55
44
444
32
545
-1000
The list you created is:
12, 233, 54, 53, 456, 65, 7, 6, 5, 77, 66, 55, 44, 444, 32, 545,
The list size is: 16
Enter the item to be deleted:
7

After removing 7, the list size is:15
Call the function testCopyConstructor.
12, 233, 54, 53, 456, 65, 6, 5, 77, 66, 55, 44, 444, 32, 545,
The list size is: 15
The list after the copy constructor:
12, 233, 54, 53, 456, 65, 6, 5, 77, 66, 55, 44, 444, 32, 545,
The list size is 15
 
 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约