Array-Based Lists |
|
|
|
Item Addition |
Creating a list consists of adding items to it. Items are usually added one at a time. The easiest way to do this is to add an item at the end of the existing list. This is the easiest of your operation. To add an item to the list, you first check whether the list is already full. A list is full if its count of items is equal or higher than the maximum number you had set. If the list is not empty, you can add an item at the end and increase the count by one. Here is how this can be done: |
Header File: listofnumbers.h |
#pragma once const int MaxItems = 20; //--------------------------------------------------------------------------- public __gc class CListOfNumbers { private: System::Double Item[]; int Size; public: CListOfNumbers(void); virtual ~CListOfNumbers(void); int Count(void); bool Add(double item); }; //--------------------------------------------------------------------------- |
Source Code: listofnumbers.cpp |
#include "StdAfx.h" #include ".\listofnumbers.h" #using <mscorlib.dll> //--------------------------------------------------------------------------- CListOfNumbers::CListOfNumbers(void) : Size(0) { Item = new System::Double[MaxItems]; } //--------------------------------------------------------------------------- CListOfNumbers::~CListOfNumbers(void) { } //--------------------------------------------------------------------------- // Provides the current number of items in the list int CListOfNumbers::Count(void) { return Size; } //--------------------------------------------------------------------------- // Adds a new item to the list if the list is not full // Increases the number of items in the list // Returns true if the item was added, otherwise returns false bool CListOfNumbers::Add(double item) { // Make sure the list is not yet full if( Size < MaxItems ) { // Since the list is not full, add the "item" at the end Item[Size] = item; // Increase the count and return the new position Size++; // Indicate that the item was successfully added return true; } // If the item was not added, return false; return false; } |
Once you have a means of adding items to the list, you can effectively create a list of items. Here is an example: |
Source Code: Exercise.cpp |
// This is the main project file for VC++ application project // generated using an Application Wizard. #include "stdafx.h" #include ".\listofnumbers.h" #using <mscorlib.dll> using namespace System; int _tmain() { // TODO: Please replace the sample code below with your own. CListOfNumbers *List = __gc new CListOfNumbers; List->Add(224.52); List->Add(60.48); List->Add(1250.64); List->Add(8.86); List->Add(1005.36); Console::WriteLine(S"Current Number of Items: {0}", __box(List->Count())); Console::WriteLine(S""); return 0; } |
This would produce: |
Current Number of Items: 5 Press any key to continue |
Item Retrieval |
After adding items to a list, you can retrieve them to do what you intended the list for. To retrieve an item, you can locate an item by its position, the same way you would do for an array. Having this index, you can check if the position specified is negative or higher than the current count of items. If it is, there is nothing much to do since the index would be wrong. If the index is in the right range, you can retrieve its corresponding item. The method to do this can be implemented as follows: |
Header File: listofnumbers.h |
#pragma once const int MaxItems = 20; //--------------------------------------------------------------------------- public __gc class CListOfNumbers { private: System::Double Item[]; int Size; public: CListOfNumbers(void); virtual ~CListOfNumbers(void); int Count(void); bool Add(double item); double Retrieve(int pos); }; //--------------------------------------------------------------------------- |
Source Code: listofnumbers.cpp |
//--------------------------------------------------------------------------- // Retrieves an item from the list based on the specified index double CListOfNumbers::Retrieve(int pos) { // Make sure the index is in the range if( pos >= 0 && pos <= Size ) return Item[pos]; // If the index was wrong, return 0 return 0; } //--------------------------------------------------------------------------- |
You can then call such a method to locate an item and use its value. Here is an example: |
Source Code: Exercise.cpp |
// This is the main project file for VC++ application project // generated using an Application Wizard. #include "stdafx.h" #include ".\listofnumbers.h" #using <mscorlib.dll> using namespace System; int _tmain() { // TODO: Please replace the sample code below with your own. CListOfNumbers *List = __gc new CListOfNumbers; List->Add(224.52); List->Add(60.48); List->Add(1250.64); List->Add(8.86); List->Add(1005.36); for(int i = 0; i < List->Count(); i++) Console::WriteLine(S"Item {0}: {1}", __box(i + 1), __box(List->Retrieve(i))); Console::WriteLine(S""); return 0; } |
This would produce: |
Item 1: 224.52 Item 2: 60.48 Item 3: 1250.64 Item 4: 8.86 Item 5: 1005.36 Press any key to continue |
Complex Operations on a List |
Item Insertion |
Inserting a new item in the list allows you to add one at a position of your choice. To insert a new item in the list, you must provide the new item and the desired position. Before performing this operation, you must first check two things. First, the list must not be empty. Second, the specified position must be in the allowed range. The method can be implemented as follows: |
Header File: listofnumbers.h |
#pragma once const int MaxItems = 20; //--------------------------------------------------------------------------- public __gc class CListOfNumbers { private: System::Double Item[]; public: CListOfNumbers(void); virtual ~CListOfNumbers(void); int Size; int Count(void); bool Add(double item); double Retrieve(int pos); bool Insert(double item, int pos); }; //--------------------------------------------------------------------------- |
Source Code: listofnumbers.cpp |
//--------------------------------------------------------------------------- // Before performing this operation, check that // 1. The list is not full // 2. The specified position is in an allowable range // Inserts a new item at a specified position in the list // After the new item is inserted, the count is increased bool CListOfNumbers::Insert(double item, int pos) { // Check that the item can be added to the list if( Size < MaxItems && pos >= 0 && pos <= Size ) { // Since there is room, // starting from the end of the list to the new position, // push each item to the next or up // to create room for the new item for(int i = Size; i > pos-1; i--) Item[i+1] = Item[i]; // Now that we have room, put the new item in the position created Item[pos] = item; // Since we have added a new item, increase the count Size++; // Indicate that the operation was successful return true; } // Since the item could not be added, return false return false; } //--------------------------------------------------------------------------- |
Here is a test of the new method: |
Source Code: Exercise.cpp |
// This is the main project file for VC++ application project // generated using an Application Wizard. #include "stdafx.h" #include ".\listofnumbers.h" #using <mscorlib.dll> using namespace System; int _tmain() { // TODO: Please replace the sample code below with your own. CListOfNumbers *List = __gc new CListOfNumbers; List->Add(224.52); List->Add(60.48); List->Add(1250.64); List->Add(8.86); List->Add(1005.36); for(int i = 0; i < List->Count(); i++) Console::WriteLine(S"Item {0}: {1}", __box(i + 1), __box(List->Retrieve(i))); List->Insert(555.25, 2); for(int i = 0; i < List->Count(); i++) Console::Write(S"\nItem {0}: {1}", __box(i + 1), __box(List->Retrieve(i))); Console::WriteLine(S"\n"); return 0; } |
This would produce: |
Item 1: 224.52 Item 2: 60.48 Item 3: 1250.64 Item 4: 8.86 Item 5: 1005.36 Item 1: 224.52 Item 2: 60.48 Item 3: 555.25 Item 4: 1250.64 Item 5: 8.86 Item 6: 1005.36 Press any key to continue |
Item Deletion |
Another operation you can perform on a list consists of deleting an item. This is also referred to as removing the item. To delete an item from the list, you can provide its position. Before performing the operation, you can first check that the specified position is valid. The method to perform this operation can be implemented as follows: |
Header File: listofnumbers.h |
#pragma once const int MaxItems = 20; //--------------------------------------------------------------------------- public __gc class CListOfNumbers { private: System::Double Item[]; public: CListOfNumbers(void); virtual ~CListOfNumbers(void); int Size; int Count(void); bool Add(double item); double Retrieve(int pos); bool Insert(double item, int pos); bool Delete(int pos); }; //--------------------------------------------------------------------------- |
Source Code: listofnumbers.cpp |
#include "StdAfx.h" #include ".\listofnumbers.h" #using <mscorlib.dll> //--------------------------------------------------------------------------- CListOfNumbers::CListOfNumbers(void) : Size(0) { Item = new System::Double[MaxItems]; } //--------------------------------------------------------------------------- CListOfNumbers::~CListOfNumbers(void) { } //--------------------------------------------------------------------------- // Provides the current number of items in the list int CListOfNumbers::Count(void) { return Size; } //--------------------------------------------------------------------------- // Adds a new item to the list if the list is not full // Increases the number of items in the list // Returns true if the item was added, otherwise returns false bool CListOfNumbers::Add(double item) { // Make sure the list is not yet full if( Size < MaxItems ) { // Since the list is not full, add the "item" at the end Item[Size] = item; // Increase the count and return the new position Size++; // Indicate that the item was successfully added return true; } // If the item was not added, return false; return false; } //--------------------------------------------------------------------------- // Retrieves an item from the list based on the specified index double CListOfNumbers::Retrieve(int pos) { // Make sure the index is in the range if( pos >= 0 && pos <= Size ) return Item[pos]; // If the index was wrong, return 0 return 0; } //--------------------------------------------------------------------------- // Before performing this operation, check that // 1. The list is not full // 2. The specified position is in an allowable range // Inserts a new item at a specified position in the list // After the new item is inserted, the count is increased bool CListOfNumbers::Insert(double item, int pos) { // Check that the item can be added to the list if( Size < MaxItems && pos >= 0 && pos <= Size ) { // Since there is room, // starting from the end of the list to the new position, // push each item to the next or up // to create room for the new item for(int i = Size; i > pos-1; i--) Item[i+1] = Item[i]; // Now that we have room, put the new item in the position created Item[pos] = item; // Since we have added a new item, increase the count Size++; // Indicate that the operation was successful return true; } // Since the item could not be added, return false return false; } //--------------------------------------------------------------------------- // Removes an item from the list // First check that the specified position is valid // Deletes the item at that position and decreases the count bool CListOfNumbers::Delete(int pos) { // Make sure the position specified is in the range if( pos >= 0 && pos <= Size ) { // Since there is room, starting at the specified position, // Replace each item by the next for(int i = pos; i < Size; i++) Item[i] = Item[i+1]; // Since an item has been removed, decrease the count Size--; // Indicate that the operation was successful return true; } // Since the position was out of range, return false return false; } //--------------------------------------------------------------------------- |
Here is a test of the new method: |
Source Code: Exercise.cpp |
// This is the main project file for VC++ application project // generated using an Application Wizard. #include "stdafx.h" #include ".\listofnumbers.h" #using <mscorlib.dll> using namespace System; int _tmain() { // TODO: Please replace the sample code below with your own. CListOfNumbers *List = __gc new CListOfNumbers; List->Add(224.52); List->Add(60.48); List->Add(1250.64); List->Add(8.86); List->Add(1005.36); for(int i = 0; i < List->Count(); i++) Console::WriteLine(S"Item {0}: {1}", __box(i + 1), __box(List->Retrieve(i))); List->Delete(2); for(int i = 0; i < List->Count(); i++) Console::Write(S"\nItem {0}: {1}", __box(i + 1), __box(List->Retrieve(i))); Console::WriteLine(S"\n"); return 0; } |
This would produce: |
Item 1: 224.52 Item 2: 60.48 Item 3: 1250.64 Item 4: 8.86 Item 5: 1005.36 Item 1: 224.52 Item 2: 60.48 Item 3: 8.86 Item 4: 1005.36 Press any key to continue |
|
||
Home | Copyright © 2004-2014 FunctionX. Inc. | |
|